altosdroid: When the user switches frequency, look for something there
authorKeith Packard <keithp@keithp.com>
Sun, 9 Feb 2020 07:55:58 +0000 (23:55 -0800)
committerKeith Packard <keithp@keithp.com>
Sun, 9 Feb 2020 07:55:58 +0000 (23:55 -0800)
Instead of sticking with the original tracker serial, when the user
selects a new frequency, check to see if some data appears there and
switch to that tracker.

Only do this once, and stop looking if the user selects a tracker.

Signed-off-by: Keith Packard <keithp@keithp.com>
altosdroid/app/src/main/java/org/altusmetrum/AltosDroid/AltosDroid.java

index 0b354f637f7f37c45c4deb7d06819a8d55495129..bead186362fd57595efa1f7dca4a9a3ed79f2ade 100644 (file)
@@ -311,9 +311,23 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener,
                if (new_telemetry_state != null)
                        telemetry_state = new_telemetry_state;
 
-               if (selected_serial == 0 || telemetry_state.get(selected_serial) == null) {
-                       AltosDebug.debug("selected serial set to %d", selected_serial);
+               if (selected_frequency != AltosLib.MISSING) {
+                       AltosState selected_state = telemetry_state.get(selected_serial);
+                       AltosState latest_state = telemetry_state.get(telemetry_state.latest_serial);
+
+                       if (selected_state != null && selected_state.frequency == selected_frequency) {
+                               selected_frequency = AltosLib.MISSING;
+                       } else if ((selected_state == null || selected_state.frequency != selected_frequency) &&
+                                  (latest_state != null && latest_state.frequency == selected_frequency))
+                       {
+                               selected_frequency = AltosLib.MISSING;
+                               selected_serial = telemetry_state.latest_serial;
+                       }
+               }
+
+               if (!telemetry_state.containsKey(selected_serial)) {
                        selected_serial = telemetry_state.latest_serial;
+                       AltosDebug.debug("selected serial set to %d", selected_serial);
                }
 
                int shown_serial = selected_serial;
@@ -989,9 +1003,11 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener,
        }
 
        double telem_frequency = 434.550;
+       double selected_frequency = AltosLib.MISSING;
 
        void setFrequency(double freq) {
                telem_frequency = freq;
+               selected_frequency = AltosLib.MISSING;
                try {
                        mService.send(Message.obtain(null, TelemetryService.MSG_SETFREQUENCY, freq));
                        set_switch_time();
@@ -1134,6 +1150,7 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener,
                                         new DialogInterface.OnClickListener() {
                                                 public void onClick(DialogInterface dialog, int item) {
                                                         setFrequency(frequencies[item]);
+                                                        selected_frequency = frequencies[item].frequency;
                                                 }
                                         });
                        AlertDialog alert_freq = builder_freq.create();