altosdroid: Deal with bluetooth connection failures better
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / AltosDroid.java
index d276798eb929e0665e3ea416d4b3abfda0380a31..4e7bdd6b331dc7b2d3bd10274d6f75e00f81aef0 100644 (file)
@@ -52,9 +52,9 @@ import android.widget.Toast;
 import android.app.AlertDialog;
 import android.location.Location;
 
-import org.altusmetrum.altoslib_5.*;
+import org.altusmetrum.altoslib_6.*;
 
-public class AltosDroid extends FragmentActivity {
+public class AltosDroid extends FragmentActivity implements AltosUnitsListener {
        // Debugging
        static final String TAG = "AltosDroid";
        static final boolean D = true;
@@ -187,6 +187,11 @@ public class AltosDroid extends FragmentActivity {
                mTabs.remove(mTab);
        }
 
+       public void units_changed(boolean imperial_units) {
+               for (AltosDroidTab mTab : mTabs)
+                       mTab.units_changed(imperial_units);
+       }
+
        void update_title(TelemetryState telemetry_state) {
                switch (telemetry_state.connect) {
                case TelemetryState.CONNECT_CONNECTED:
@@ -202,9 +207,12 @@ public class AltosDroid extends FragmentActivity {
                        }
                        break;
                case TelemetryState.CONNECT_CONNECTING:
-                       mTitle.setText(R.string.title_connecting);
+                       if (telemetry_state.address != null)
+                               mTitle.setText(String.format("Connecting to %s...", telemetry_state.address.name));
+                       else
+                               mTitle.setText("Connecting to something...");
                        break;
-               case TelemetryState.CONNECT_READY:
+               case TelemetryState.CONNECT_DISCONNECTED:
                case TelemetryState.CONNECT_NONE:
                        mTitle.setText(R.string.title_not_connected);
                        break;
@@ -226,13 +234,19 @@ public class AltosDroid extends FragmentActivity {
                }
        }
 
+       boolean registered_units_listener;
+
        void update_state(TelemetryState telemetry_state) {
+
+               if (!registered_units_listener) {
+                       registered_units_listener = true;
+                       AltosPreferences.register_units_listener(this);
+               }
+
                update_title(telemetry_state);
                update_ui(telemetry_state.state, telemetry_state.location);
                if (telemetry_state.connect == TelemetryState.CONNECT_CONNECTED)
                        start_timer();
-               else
-                       stop_timer();
        }
 
        boolean same_string(String a, String b) {
@@ -254,8 +268,6 @@ public class AltosDroid extends FragmentActivity {
 
        void update_ui(AltosState state, Location location) {
 
-               Log.d(TAG, "update_ui");
-
                int prev_state = AltosLib.ao_flight_invalid;
 
                AltosGreatCircle from_receiver = null;
@@ -264,7 +276,6 @@ public class AltosDroid extends FragmentActivity {
                        prev_state = saved_state.state;
 
                if (state != null) {
-                       Log.d(TAG, String.format("prev state %d new state  %d\n", prev_state, state.state));
                        if (state.state == AltosLib.ao_flight_stateless) {
                                boolean prev_locked = false;
                                boolean locked = false;
@@ -284,7 +295,6 @@ public class AltosDroid extends FragmentActivity {
                        } else {
                                if (prev_state != state.state) {
                                        String currentTab = mTabHost.getCurrentTabTag();
-                                       Log.d(TAG, "switch state");
                                        switch (state.state) {
                                        case AltosLib.ao_flight_boost:
                                                if (currentTab.equals("pad")) mTabHost.setCurrentTabByTag("ascent");
@@ -315,19 +325,18 @@ public class AltosDroid extends FragmentActivity {
                        }
 
                        if (saved_state == null || !same_string(saved_state.callsign, state.callsign)) {
-                               Log.d(TAG, "update callsign");
                                mCallsignView.setText(state.callsign);
                        }
                        if (saved_state == null || state.serial != saved_state.serial) {
-                               Log.d(TAG, "update serial");
                                mSerialView.setText(String.format("%d", state.serial));
                        }
                        if (saved_state == null || state.flight != saved_state.flight) {
-                               Log.d(TAG, "update flight");
-                               mFlightView.setText(String.format("%d", state.flight));
+                               if (state.flight == AltosLib.MISSING)
+                                       mFlightView.setText("");
+                               else
+                                       mFlightView.setText(String.format("%d", state.flight));
                        }
                        if (saved_state == null || state.state != saved_state.state) {
-                               Log.d(TAG, "update state");
                                if (state.state == AltosLib.ao_flight_stateless) {
                                        mStateLayout.setVisibility(View.GONE);
                                } else {
@@ -336,7 +345,6 @@ public class AltosDroid extends FragmentActivity {
                                }
                        }
                        if (saved_state == null || state.rssi != saved_state.rssi) {
-                               Log.d(TAG, "update rssi");
                                mRSSIView.setText(String.format("%d", state.rssi));
                        }
                }
@@ -344,7 +352,7 @@ public class AltosDroid extends FragmentActivity {
                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;
@@ -457,8 +465,6 @@ public class AltosDroid extends FragmentActivity {
                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
@@ -476,16 +482,18 @@ public class AltosDroid extends FragmentActivity {
 
                doBindService();
 
+               if (mAltosVoice == null)
+                       mAltosVoice = new AltosVoice(this);
        }
 
        @Override
-       public synchronized void onResume() {
+       public void onResume() {
                super.onResume();
                if(D) Log.e(TAG, "+ ON RESUME +");
        }
 
        @Override
-       public synchronized void onPause() {
+       public void onPause() {
                super.onPause();
                if(D) Log.e(TAG, "- ON PAUSE -");
        }
@@ -496,6 +504,10 @@ public class AltosDroid extends FragmentActivity {
                if(D) Log.e(TAG, "-- ON STOP --");
 
                doUnbindService();
+               if (mAltosVoice != null) {
+                       mAltosVoice.stop();
+                       mAltosVoice = null;
+               }
        }
 
        @Override
@@ -532,19 +544,26 @@ public class AltosDroid extends FragmentActivity {
                }
        }
 
-       private void connectDevice(String address) {
+       private void connectDevice(Intent data) {
                // Attempt to connect to the device
                try {
-                       if (D) Log.d(TAG, "Connecting to " + address);
-                       mService.send(Message.obtain(null, TelemetryService.MSG_CONNECT, address));
+                       String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
+                       String name = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_NAME);
+
+                       if (D) Log.d(TAG, "Connecting to " + address + " " + name);
+                       DeviceAddress   a = new DeviceAddress(address, name);
+                       mService.send(Message.obtain(null, TelemetryService.MSG_CONNECT, a));
+                       if (D) Log.d(TAG, "Sent connecting message");
                } catch (RemoteException e) {
+                       if (D) Log.e(TAG, "connect device message failed");
                }
        }
 
-       private void connectDevice(Intent data) {
-               // Get the device MAC address
-               String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
-               connectDevice(address);
+       private void disconnectDevice() {
+               try {
+                       mService.send(Message.obtain(null, TelemetryService.MSG_DISCONNECT, null));
+               } catch (RemoteException e) {
+               }
        }
 
        @Override
@@ -604,9 +623,14 @@ public class AltosDroid extends FragmentActivity {
                        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:
@@ -656,6 +680,10 @@ public class AltosDroid extends FragmentActivity {
                        AlertDialog alert_rate = builder_rate.create();
                        alert_rate.show();
                        return true;
+               case R.id.change_units:
+                       boolean imperial = AltosPreferences.imperial_units();
+                       AltosPreferences.set_imperial_units(!imperial);
+                       return true;
                }
                return false;
        }