package com.lukekorth.httpebble;

import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import com.commonsware.cwac.wakeful.WakefulIntentService;
import com.github.kevinsawicki.http.HttpRequest;
import com.github.kevinsawicki.http.HttpRequest.HttpRequestException;
import com.google.android.gms.gcm.GoogleCloudMessaging;

public class RegisterIntentService extends WakefulIntentService {

	public RegisterIntentService() {
		super("RegisterIntentService");
	}

	@Override
	protected void doWakefulWork(Intent intent) {
		if (getSharedPreferences(Constants.HTTPEBBLE, MODE_PRIVATE).getString("gcmId", "").equals("")) {
			try {
				GoogleCloudMessaging.getInstance(this).register(Constants.GCM_ID);
			} catch (IOException e) {
			}
		} else {
			RegisterIntentService.register(this);
		}
	}

	public static void register(Context context) {
		SharedPreferences prefs = context.getSharedPreferences(Constants.HTTPEBBLE, Context.MODE_PRIVATE);

		JSONObject data = new JSONObject();
		try {
			data.put("userId", prefs.getString("userId", ""));
			data.put("userToken", prefs.getString("userToken", ""));
			data.put("gcmId", prefs.getString("gcmId", ""));
		} catch (JSONException e1) {
			data = new JSONObject();
		}

		int code;
		try {
			HttpRequest response = HttpRequest.post(Constants.URL + "register").send(data.toString());
			code = response.code();
		} catch (HttpRequestException e) {
			code = 400;
		}

		if (code == 200)
			prefs.edit().putBoolean("needToRegister", false).commit();
		else
			prefs.edit().putBoolean("needToRegister", true).commit();
	}
}

@@ -220,16 +220,41 @@
 erences;
+%0Aimport android.util.Log;
 %0A%0Aimport
@@ -1473,16 +1473,26 @@
 %22).send(
+%22data=%22 + 
 data.toS
@@ -1585,16 +1585,186 @@
 0;%0A%09%09%7D%0A%0A
+%09%09Log.d(Constants.HTTPEBBLE, %22Cloud Access registration request: %22 + data.toString());%0A%09%09Log.d(Constants.HTTPEBBLE, %22Cloud Access registration response code: %22 + code);%0A%0A
 %09%09if (co

Add registration logging and send data correctly