package com.lukekorth.httpebble.receivers;

import static com.getpebble.android.kit.Constants.APP_UUID;
import static com.getpebble.android.kit.Constants.MSG_DATA;
import static com.getpebble.android.kit.Constants.TRANSACTION_ID;

import java.util.UUID;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.getpebble.android.kit.PebbleKit;
import com.lukekorth.httpebble.Constants;

public class PebbleDataReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		String uuid = ((UUID) intent.getSerializableExtra(APP_UUID)).toString().toUpperCase();

		Log.d(Constants.HTTPEBBLE, "Pebble Request UUID: " + uuid);

		if (uuid.startsWith(Constants.HTTPEBBLE_UUID_PREFIX)) {
			int transactionId = intent.getIntExtra(TRANSACTION_ID, -1);
			String data = intent.getStringExtra(MSG_DATA);

			Log.d(Constants.HTTPEBBLE, "Pebble transaction id: " + transactionId);
			Log.d(Constants.HTTPEBBLE, "Request data: " + data);

			if (transactionId >= 0 && transactionId <= 255 && data != null && data.length() != 0) {
				PebbleKit.sendAckToPebble(context, transactionId);

				Intent serviceIntent = new Intent(context, PebbleProxyIntentService.class);
				serviceIntent.putExtras(intent.getExtras());

				context.startService(serviceIntent);
			}
		}
	}

}

@@ -220,24 +220,50 @@
 ACTION_ID;%0A%0A
+import java.lang.Integer;%0A
 import java.
@@ -269,24 +269,24 @@
 .util.UUID;%0A
-
 %0Aimport andr
@@ -894,10 +894,25 @@
 ID, 
--1
+Integer.MIN_VALUE
 );%0A%09
@@ -1092,16 +1092,159 @@
 data);%0A%0A
+%09%09%09if (transactionId == Integer.MIN_VALUE) %7B%0A%09%09%09%09Log.d(Constants.HTTPEBBLE, %22No transaction id%22);%0A%09%09%09%09return;%0A%09%09%09%7D%0A%0A%09%09%09transactionId &= 0xff;%0A%0A
 %09%09%09if (t
@@ -1508,24 +1508,24 @@
 Extras());%0A%0A
-
 %09%09%09%09context.
@@ -1558,16 +1558,88 @@
 );%0A%09%09%09%7D%0A
+%09%09%09else%0A%09%09%09%7B%0A%09%09%09%09Log.d(%22Transaction Id out of range, or no data%22);%0A%09%09%09%7D%0A
 %09%09%7D%0A%09%7D%0A%0A

Handle wrapping transaction ids