package com.education.flashmath.network;

import android.content.Context;
import android.provider.Settings.Secure;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class FlashMathClient {
	private static FlashMathClient flashMathClient;
	private RequestParams auth;
	private AsyncHttpClient client;
	
	private FlashMathClient(Context context) {  // Private constructor
		auth = new RequestParams();
		// TODO Replace this "global" with the device token
		String android_id = Secure.getString(context.getContentResolver(),
                Secure.ANDROID_ID); 
		auth.put("token", android_id);
		client = new AsyncHttpClient();
	}
	
	public static FlashMathClient getClient(Context context) {
		if (flashMathClient == null) {
			flashMathClient = new FlashMathClient(context);
		}
		return flashMathClient;
	}
	
	public void getQuestions(String subject, AsyncHttpResponseHandler handler) {
		String url = "http://flashmathapi.herokuapp.com/quizzes/" + subject + "/";
    	client.get(url, auth, handler);
	}
	
	public void getScores(String subject, AsyncHttpResponseHandler handler) {
		String url = "http://flashmathapi.herokuapp.com/scores/" + subject + "/";
    	client.get(url, auth, handler);
	}

	public void clearScores(String subject, AsyncHttpResponseHandler handler) {
		String url = "http://flashmathapi.herokuapp.com/scores/" + subject + "/clear";
    	client.get(url, auth, handler);
	}
	
	public void putScore(String subject, String score, AsyncHttpResponseHandler handler) {
		String url = "http://flashmathapi.herokuapp.com/scores/" + subject + "/" + score + "/";
    	client.get(url, auth, handler);
	}
}

@@ -35,16 +35,173 @@
 twork;%0A%0A
+import java.io.UnsupportedEncodingException;%0Aimport java.security.MessageDigest;%0Aimport java.security.NoSuchAlgorithmException;%0Aimport java.util.Formatter;%0A%0A
 import a
@@ -224,16 +224,16 @@
 ontext;%0A
-
 import a
@@ -837,16 +837,32 @@
 token%22, 
+encryptPassword(
 android_
@@ -864,16 +864,17 @@
 roid_id)
+)
 ;%0A%09%09clie
@@ -1844,32 +1844,32 @@
  + score + %22/%22;%0A
-
     %09client.get(
@@ -1892,10 +1892,760 @@
 er);%0A%09%7D%0A
+%09%0A%09private static String encryptPassword(String password)%0A%09%7B%0A%09    String sha1 = %22%22;%0A%09    try%0A%09    %7B%0A%09        MessageDigest crypt = MessageDigest.getInstance(%22SHA-1%22);%0A%09        crypt.reset();%0A%09        crypt.update(password.getBytes(%22UTF-8%22));%0A%09        sha1 = byteToHex(crypt.digest());%0A%09    %7D%0A%09    catch(NoSuchAlgorithmException e)%0A%09    %7B%0A%09        e.printStackTrace();%0A%09    %7D%0A%09    catch(UnsupportedEncodingException e)%0A%09    %7B%0A%09        e.printStackTrace();%0A%09    %7D%0A%09    return sha1;%0A%09%7D%0A%0A%09private static String byteToHex(final byte%5B%5D hash)%0A%09%7B%0A%09    Formatter formatter = new Formatter();%0A%09    for (byte b : hash)%0A%09    %7B%0A%09        formatter.format(%22%2502x%22, b);%0A%09    %7D%0A%09    String result = formatter.toString();%0A%09    formatter.close();%0A%09    return result;%0A%09%7D%0A
 %7D%0A

Hash the device code using SHA-1