altosdroid: begin adding state support
authorMike Beattie <mike@ethernal.org>
Sun, 26 Aug 2012 11:19:06 +0000 (23:19 +1200)
committerMike Beattie <mike@ethernal.org>
Sun, 26 Aug 2012 11:19:06 +0000 (23:19 +1200)
Signed-off-by: Mike Beattie <mike@ethernal.org>
altosdroid/src/org/altusmetrum/AltosDroid/TelemetryService.java

index cf7ae6da452a312dd095d7810ca052b2877db7a8..6a23dca32a6ab264ad922f69432dfa963775c0cc 100644 (file)
@@ -66,7 +66,7 @@ public class TelemetryService extends Service {
        // Name of the connected device
        private String mConnectedDeviceName = null;
        private AltosBluetooth mAltosBluetooth = null;
-
+       private int state = STATE_NONE;
        LinkedBlockingQueue<AltosLine> telem;
 
        // Handler of incoming messages from clients.
@@ -92,6 +92,7 @@ public class TelemetryService extends Service {
                                break;
                        case MSG_CONNECTED:
                                if (D) Log.d(TAG, "Connected to device");
+                               s.setState(STATE_CONNECTED);
                                break;
                        default:
                                super.handleMessage(msg);
@@ -100,6 +101,7 @@ public class TelemetryService extends Service {
        }
 
        private void stopAltosBluetooth() {
+               setState(STATE_READY);
                if (mAltosBluetooth != null) {
                        mAltosBluetooth.close();
                        mAltosBluetooth = null;
@@ -110,6 +112,14 @@ public class TelemetryService extends Service {
        private void startAltosBluetooth(BluetoothDevice d) {
                mAltosBluetooth = new AltosBluetooth(d);
                mAltosBluetooth.add_monitor(telem);
+                       setState(STATE_CONNECTING);
+       }
+
+       private synchronized void setState(int s) {
+               if (D) Log.d(TAG, "setState() " + state + " -> " + s);
+               state = s;
+
+               sendMessageToClients(Message.obtain(null, AltosDroid.MSG_STATE_CHANGE, state, -1));
        }
 
        @Override
@@ -118,6 +128,7 @@ public class TelemetryService extends Service {
                //mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
 
                telem = new LinkedBlockingQueue<AltosLine>();
+               setState(STATE_READY);
        }
 
        @Override