altosdroid: start restoring from log data on startup
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / AltosDroid.java
index ebbe9330867ae515aa8b91022f6ebd945da284bb..c94f36fda0d82a233f599d1212b2950e13fa8e55 100644 (file)
@@ -37,6 +37,7 @@ import android.os.Message;
 import android.os.Messenger;
 import android.os.RemoteException;
 import android.support.v4.app.FragmentActivity;
+import android.support.v4.app.FragmentManager;
 import android.util.DisplayMetrics;
 import android.util.Log;
 import android.view.Menu;
@@ -49,12 +50,12 @@ import android.widget.Toast;
 import android.app.AlertDialog;
 import android.location.Location;
 
-import org.altusmetrum.altoslib_1.*;
+import org.altusmetrum.altoslib_5.*;
 
 public class AltosDroid extends FragmentActivity {
        // Debugging
-       private static final String TAG = "AltosDroid";
-       private static final boolean D = true;
+       static final String TAG = "AltosDroid";
+       static final boolean D = true;
 
        // Message types received by our Handler
        public static final int MSG_STATE_CHANGE    = 1;
@@ -67,6 +68,8 @@ public class AltosDroid extends FragmentActivity {
        private static final int REQUEST_CONNECT_DEVICE = 1;
        private static final int REQUEST_ENABLE_BT      = 2;
 
+       public static FragmentManager   fm;
+
        // Layout Views
        private TextView mTitle;
 
@@ -98,9 +101,6 @@ public class AltosDroid extends FragmentActivity {
        private Messenger mService = null;
        final Messenger mMessenger = new Messenger(new IncomingHandler(this));
 
-       // Preferences
-       private AltosDroidPreferences prefs = null;
-
        // TeleBT Config data
        private AltosConfigData mConfigData = null;
        // Local Bluetooth adapter
@@ -145,9 +145,10 @@ public class AltosDroid extends FragmentActivity {
                                ad.set_location((Location) msg.obj);
                                break;
                        case MSG_CRC_ERROR:
+                               break;
                        case MSG_UPDATE_AGE:
                                if (ad.saved_state != null) {
-                                       ad.mAgeView.setText(String.format("%d", (System.currentTimeMillis() - ad.saved_state.report_time + 500) / 1000));
+                                       ad.mAgeView.setText(String.format("%d", (System.currentTimeMillis() - ad.saved_state.received_time + 500) / 1000));
                                }
                                break;
                        }
@@ -206,13 +207,29 @@ public class AltosDroid extends FragmentActivity {
 
        void set_location(Location location) {
                saved_location = location;
+               Log.d(TAG, "set_location");
                update_ui(saved_state);
        }
 
+       boolean same_string(String a, String b) {
+               if (a == null) {
+                       if (b == null)
+                               return true;
+                       return false;
+               } else {
+                       if (b == null)
+                               return false;
+                       return a.equals(b);
+               }
+       }
+
        void update_ui(AltosState state) {
+
+               Log.d(TAG, "update_ui");
                if (state != null && saved_state != null) {
                        if (saved_state.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");
@@ -226,7 +243,6 @@ public class AltosDroid extends FragmentActivity {
                                }
                        }
                }
-               saved_state = state;
 
                AltosGreatCircle from_receiver = null;
 
@@ -243,18 +259,35 @@ public class AltosDroid extends FragmentActivity {
                }
 
                if (state != null) {
-                       mCallsignView.setText(state.data.callsign);
-                       mSerialView.setText(String.format("%d", state.data.serial));
-                       mFlightView.setText(String.format("%d", state.data.flight));
-                       mStateView.setText(state.data.state());
-                       mRSSIView.setText(String.format("%d", state.data.rssi));
+                       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 (saved_state == null || state.state != saved_state.state) {
+                               Log.d(TAG, "update state");
+                               mStateView.setText(state.state_name());
+                       }
+                       if (saved_state == null || state.rssi != saved_state.rssi) {
+                               Log.d(TAG, "update rssi");
+                               mRSSIView.setText(String.format("%d", state.rssi));
+                       }
                }
 
                for (AltosDroidTab mTab : mTabs)
-                       mTab.update_ui(state, from_receiver, saved_location);
+                       mTab.update_ui(state, from_receiver, saved_location, mTab == mTabsAdapter.currentItem());
 
                if (state != null)
                        mAltosVoice.tell(state);
+
+               saved_state = state;
        }
 
        private void onTimerTick() {
@@ -266,7 +299,7 @@ public class AltosDroid extends FragmentActivity {
 
        static String pos(double p, String pos, String neg) {
                String  h = pos;
-               if (p == AltosRecord.MISSING)
+               if (p == AltosLib.MISSING)
                        return "";
                if (p < 0) {
                        h = neg;
@@ -278,13 +311,13 @@ public class AltosDroid extends FragmentActivity {
        }
 
        static String number(String format, double value) {
-               if (value == AltosRecord.MISSING)
+               if (value == AltosLib.MISSING)
                        return "";
                return String.format(format, value);
        }
 
        static String integer(String format, int value) {
-               if (value == AltosRecord.MISSING)
+               if (value == AltosLib.MISSING)
                        return "";
                return String.format(format, value);
        }
@@ -294,6 +327,8 @@ public class AltosDroid extends FragmentActivity {
                super.onCreate(savedInstanceState);
                if(D) Log.e(TAG, "+++ ON CREATE +++");
 
+               fm = getSupportFragmentManager();
+
                // Get local Bluetooth adapter
                mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
 
@@ -305,8 +340,7 @@ public class AltosDroid extends FragmentActivity {
                }
 
                // Initialise preferences
-               prefs = new AltosDroidPreferences(this);
-               AltosPreferences.init(prefs);
+               AltosDroidPreferences.init(this);
 
                // Set up the window layout
                requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
@@ -414,7 +448,7 @@ public class AltosDroid extends FragmentActivity {
                super.onDestroy();
                if(D) Log.e(TAG, "--- ON DESTROY ---");
 
-               mAltosVoice.stop();
+               if (mAltosVoice != null) mAltosVoice.stop();
        }
 
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
@@ -442,9 +476,7 @@ public class AltosDroid extends FragmentActivity {
                }
        }
 
-       private void connectDevice(Intent data) {
-               // Get the device MAC address
-               String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
+       private void connectDevice(String address) {
                // Get the BLuetoothDevice object
                BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
                // Attempt to connect to the device
@@ -455,6 +487,12 @@ public class AltosDroid extends FragmentActivity {
                }
        }
 
+       private void connectDevice(Intent data) {
+               // Get the device MAC address
+               String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
+               connectDevice(address);
+       }
+
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                MenuInflater inflater = getMenuInflater();
@@ -476,6 +514,33 @@ public class AltosDroid extends FragmentActivity {
                }
        }
 
+       void setBaud(int baud) {
+               try {
+                       mService.send(Message.obtain(null, TelemetryService.MSG_SETBAUD, baud));
+               } catch (RemoteException e) {
+               }
+       }
+
+       void setBaud(String baud) {
+               try {
+                       int     value = Integer.parseInt(baud);
+                       int     rate = AltosLib.ao_telemetry_rate_38400;
+                       switch (value) {
+                       case 2400:
+                               rate = AltosLib.ao_telemetry_rate_2400;
+                               break;
+                       case 9600:
+                               rate = AltosLib.ao_telemetry_rate_9600;
+                               break;
+                       case 38400:
+                               rate = AltosLib.ao_telemetry_rate_38400;
+                               break;
+                       }
+                       setBaud(rate);
+               } catch (NumberFormatException e) {
+               }
+       }
+
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
                Intent serverIntent = null;
@@ -501,16 +566,36 @@ public class AltosDroid extends FragmentActivity {
                                "Channel 9 (435.450MHz)"
                        };
 
-                       AlertDialog.Builder builder = new AlertDialog.Builder(this);
-                       builder.setTitle("Pick a frequency");
-                       builder.setItems(frequencies,
+                       AlertDialog.Builder builder_freq = new AlertDialog.Builder(this);
+                       builder_freq.setTitle("Pick a frequency");
+                       builder_freq.setItems(frequencies,
                                         new DialogInterface.OnClickListener() {
                                                 public void onClick(DialogInterface dialog, int item) {
                                                         setFrequency(frequencies[item]);
                                                 }
                                         });
-                       AlertDialog alert = builder.create();
-                       alert.show();
+                       AlertDialog alert_freq = builder_freq.create();
+                       alert_freq.show();
+                       return true;
+               case R.id.select_rate:
+                       // Set the TBT baud rate
+
+                       final String[] rates = {
+                               "38400",
+                               "9600",
+                               "2400",
+                       };
+
+                       AlertDialog.Builder builder_rate = new AlertDialog.Builder(this);
+                       builder_rate.setTitle("Pick a baud rate");
+                       builder_rate.setItems(rates,
+                                        new DialogInterface.OnClickListener() {
+                                                public void onClick(DialogInterface dialog, int item) {
+                                                        setBaud(rates[item]);
+                                                }
+                                        });
+                       AlertDialog alert_rate = builder_rate.create();
+                       alert_rate.show();
                        return true;
                }
                return false;