altosdroid: Only speak when GUI is running
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / AltosDroid.java
index 10e735c8cbaa5f5b42748ffc8025579d03d5f93c..41045f036cf00d61fcca9df035b6d3f6f27b7541 100644 (file)
@@ -352,7 +352,7 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
                for (AltosDroidTab mTab : mTabs)
                        mTab.update_ui(state, from_receiver, location, mTab == mTabsAdapter.currentItem());
 
-               if (state != null)
+               if (state != null && mAltosVoice != null)
                        mAltosVoice.tell(state, from_receiver);
 
                saved_state = state;
@@ -465,8 +465,6 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
                mStateLayout   = (RelativeLayout) findViewById(R.id.state_container);
                mStateView     = (TextView) findViewById(R.id.state_value);
                mAgeView       = (TextView) findViewById(R.id.age_value);
-
-               mAltosVoice = new AltosVoice(this);
        }
 
        @Override
@@ -484,6 +482,8 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
 
                doBindService();
 
+               if (mAltosVoice == null)
+                       mAltosVoice = new AltosVoice(this);
        }
 
        @Override
@@ -504,6 +504,10 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
                if(D) Log.e(TAG, "-- ON STOP --");
 
                doUnbindService();
+               if (mAltosVoice != null) {
+                       mAltosVoice.stop();
+                       mAltosVoice = null;
+               }
        }
 
        @Override
@@ -553,6 +557,13 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
                }
        }
 
+       private void disconnectDevice() {
+               try {
+                       mService.send(Message.obtain(null, TelemetryService.MSG_DISCONNECT, null));
+               } catch (RemoteException e) {
+               }
+       }
+
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                MenuInflater inflater = getMenuInflater();
@@ -610,9 +621,14 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
                        serverIntent = new Intent(this, DeviceListActivity.class);
                        startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
                        return true;
+               case R.id.disconnect:
+                       /* Disconnect the bluetooth device
+                        */
+                       disconnectDevice();
+                       return true;
                case R.id.quit:
                        Log.d(TAG, "R.id.quit");
-                       stopService(new Intent(AltosDroid.this, TelemetryService.class));
+                       disconnectDevice();
                        finish();
                        return true;
                case R.id.select_freq: