package com.icecondor.nest;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

public class Start extends Activity implements ServiceConnection {
	static final String appTag = "Start";
	public static final String PREFS_NAME = "IceNestPrefs";
	PigeonService pigeon;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	Log.i(appTag, "onCreate");
        super.onCreate(savedInstanceState);
        startPigeon();
    }

    private void restorePreferences() {
		Log.i(appTag, "restorePreferences()");
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        boolean startPigeon = settings.getBoolean("startPigeon", true);
        if (startPigeon) {
            try {
				pigeon.startTransmitting();
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
	}

	private void startPigeon() {
		// Start the pigeon service
    	Intent pigeon_service = new Intent(this, Pigeon.class);
        startService(pigeon_service);
        bindService(new Intent(this, Pigeon.class), this, 0); // 0 = do not auto-start
	}
	
	private void stopPigeon() {
		Log.i(appTag, "stopPigeon");
		unbindService(this);
		stopService(new Intent(this, Pigeon.class));
	}
	
	public void onServiceConnected(ComponentName className, IBinder service) {
		Log.i(appTag, "onServiceConnected "+service);
		pigeon = PigeonService.Stub.asInterface(service);
        restorePreferences();
        // handoff to the Radar
        startActivity(new Intent(this, Radar.class));
        // dont come back here
        finish();
	}

	public void onServiceDisconnected(ComponentName className) {
		Log.i(appTag, "onServiceDisconnected "+className);
		
	}

}

@@ -22,16 +22,40 @@
 .nest;%0A%0A
+import java.util.UUID;%0A%0A
 import a
@@ -75,16 +75,16 @@
 tivity;%0A
-
 import a
@@ -228,16 +228,65 @@
 rences;%0A
+import android.content.SharedPreferences.Editor;%0A
 import a
@@ -902,16 +902,395 @@
 ME, 0);%0A
+%09%09Editor editor = settings.edit();%0A%0A        // Set the unique ID%0A%09%09String uuid;%0A%09%09if(settings.contains(%22uuid%22)) %7B%0A%09%09%09uuid = settings.getString(%22uuid%22, null);%0A%09%09%09Log.i(appTag, %22retrieved UUID of %22+uuid);%0A%09%09%7D else %7B%0A%09%09%09uuid = UUID.randomUUID().toString();%0A%09%09%09editor.putString(%22uuid%22, uuid);%0A%09%09%09Log.i(appTag, %22no UUID in preferences. generated %22+uuid);%0A%09%09%7D%0A%09%09%0A%09%09// Start the Pigeon%0A
         
@@ -1532,16 +1532,16 @@
 );%0A%09%09%09%7D%0A
-
         
@@ -1542,16 +1542,25 @@
       %7D%0A
+        %0A
 %09%7D%0A%0A%09pri

create a default identity if one does not exist.