X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=blobdiff_plain;f=altosdroid%2Fsrc%2Forg%2Faltusmetrum%2FAltosDroid%2FAltosDroid.java;h=db7815f3a7ab5ac4fcd069bd6f35bff95785b305;hp=942ebbd5f06d1b4b50441b0677fb948ad23ec54c;hb=bfa6cd8934b993bd4a67cfc7a4eeecf9b11915ef;hpb=c46c2c5767c6e909fa58587e6c864a4fbaa9fa20 diff --git a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java index 942ebbd5..db7815f3 100644 --- a/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java +++ b/altosdroid/src/org/altusmetrum/AltosDroid/AltosDroid.java @@ -18,10 +18,9 @@ package org.altusmetrum.AltosDroid; import java.lang.ref.WeakReference; -import java.util.ArrayList; -import java.util.Timer; -import java.util.TimerTask; import java.text.*; +import java.util.*; +import java.io.*; import android.app.Activity; import android.app.PendingIntent; @@ -88,6 +87,11 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener { private int mAgeNewColor; private int mAgeOldColor; + public static final String tab_pad_name = "pad"; + public static final String tab_flight_name = "flight"; + public static final String tab_recover_name = "recover"; + public static final String tab_map_name = "map"; + // field to display the version at the bottom of the screen private TextView mVersion; @@ -248,15 +252,25 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener { } } - boolean registered_units_listener; - + int selected_serial = 0; int current_serial; + long switch_time; + + void set_switch_time() { + switch_time = System.currentTimeMillis(); + selected_serial = 0; + } + + boolean registered_units_listener; void update_state(TelemetryState new_telemetry_state) { if (new_telemetry_state != null) telemetry_state = new_telemetry_state; + if (selected_serial != 0) + current_serial = selected_serial; + if (current_serial == 0) current_serial = telemetry_state.latest_serial; @@ -266,14 +280,41 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener { } serials = telemetry_state.states.keySet().toArray(new Integer[0]); + Arrays.sort(serials); update_title(telemetry_state); - AltosDebug.debug("update state current serial %d\n", current_serial); - AltosState state = null; - if (telemetry_state.states.containsKey(current_serial)) + boolean aged = true; + + if (telemetry_state.states.containsKey(current_serial)) { state = telemetry_state.states.get(current_serial); + int age = state_age(state); + if (age < 20) + aged = false; + if (current_serial == selected_serial) + aged = false; + else if (switch_time != 0 && (switch_time - state.received_time) > 0) + aged = true; + } + + if (aged) { + AltosState newest_state = null; + int newest_age = 0; + + for (int serial : telemetry_state.states.keySet()) { + AltosState existing = telemetry_state.states.get(serial); + int existing_age = state_age(existing); + + if (newest_state == null || existing_age < newest_age) { + newest_state = existing; + newest_age = existing_age; + } + } + + if (newest_state != null) + state = newest_state; + } update_ui(telemetry_state, state, telemetry_state.location); @@ -363,9 +404,9 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener { if (prev_locked != locked) { String currentTab = mTabHost.getCurrentTabTag(); if (locked) { - if (currentTab.equals("pad")) mTabHost.setCurrentTabByTag("descent"); + if (currentTab.equals(tab_pad_name)) mTabHost.setCurrentTabByTag(tab_flight_name); } else { - if (currentTab.equals("descent")) mTabHost.setCurrentTabByTag("pad"); + if (currentTab.equals(tab_flight_name)) mTabHost.setCurrentTabByTag(tab_pad_name); } } } else { @@ -373,16 +414,13 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener { String currentTab = mTabHost.getCurrentTabTag(); switch (state.state) { case AltosLib.ao_flight_boost: - if (currentTab.equals("pad")) mTabHost.setCurrentTabByTag("ascent"); - break; - case AltosLib.ao_flight_drogue: - if (currentTab.equals("ascent")) mTabHost.setCurrentTabByTag("descent"); + if (currentTab.equals(tab_pad_name)) mTabHost.setCurrentTabByTag(tab_flight_name); break; case AltosLib.ao_flight_landed: - if (currentTab.equals("descent")) mTabHost.setCurrentTabByTag("landed"); + if (currentTab.equals(tab_flight_name)) mTabHost.setCurrentTabByTag(tab_recover_name); break; case AltosLib.ao_flight_stateless: - if (currentTab.equals("pad")) mTabHost.setCurrentTabByTag("descent"); + if (currentTab.equals(tab_pad_name)) mTabHost.setCurrentTabByTag(tab_flight_name); break; } } @@ -428,8 +466,8 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener { for (AltosDroidTab mTab : mTabs) mTab.update_ui(telem_state, state, from_receiver, location, mTab == mTabsAdapter.currentItem()); - if (state != null && mAltosVoice != null) - mAltosVoice.tell(state, from_receiver); + if (mAltosVoice != null) + mAltosVoice.tell(telem_state, state, from_receiver, location, (AltosDroidTab) mTabsAdapter.currentItem()); saved_state = state; } @@ -474,12 +512,20 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener { return tab_view; } + public void set_map_source(int source) { + for (AltosDroidTab mTab : mTabs) + mTab.set_map_source(source); + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); AltosDebug.init(this); AltosDebug.debug("+++ ON CREATE +++"); + // Initialise preferences + AltosDroidPreferences.init(this); + fm = getSupportFragmentManager(); // Set up the window layout @@ -494,12 +540,10 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener { mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager); - mTabsAdapter.addTab(mTabHost.newTabSpec("pad").setIndicator(create_tab_view("Pad")), TabPad.class, null); - mTabsAdapter.addTab(mTabHost.newTabSpec("ascent").setIndicator(create_tab_view("Ascent")), TabAscent.class, null); - mTabsAdapter.addTab(mTabHost.newTabSpec("descent").setIndicator(create_tab_view("Descent")), TabDescent.class, null); - mTabsAdapter.addTab(mTabHost.newTabSpec("landed").setIndicator(create_tab_view("Landed")), TabLanded.class, null); - mTabsAdapter.addTab(mTabHost.newTabSpec("map").setIndicator(create_tab_view("Map")), TabMap.class, null); - mTabsAdapter.addTab(mTabHost.newTabSpec("offmap").setIndicator(create_tab_view("OffMap")), TabMapOffline.class, null); + mTabsAdapter.addTab(mTabHost.newTabSpec(tab_pad_name).setIndicator(create_tab_view("Pad")), TabPad.class, null); + mTabsAdapter.addTab(mTabHost.newTabSpec(tab_flight_name).setIndicator(create_tab_view("Flight")), TabFlight.class, null); + mTabsAdapter.addTab(mTabHost.newTabSpec(tab_recover_name).setIndicator(create_tab_view("Recover")), TabRecover.class, null); + mTabsAdapter.addTab(mTabHost.newTabSpec(tab_map_name).setIndicator(create_tab_view("Map")), TabMap.class, null); // Display the Version mVersion = (TextView) findViewById(R.id.version); @@ -750,6 +794,7 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener { void setFrequency(double freq) { try { mService.send(Message.obtain(null, TelemetryService.MSG_SETFREQUENCY, freq)); + set_switch_time(); } catch (RemoteException e) { } } @@ -764,6 +809,7 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener { void setBaud(int baud) { try { mService.send(Message.obtain(null, TelemetryService.MSG_SETBAUD, baud)); + set_switch_time(); } catch (RemoteException e) { } } @@ -790,14 +836,26 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener { void select_tracker(int serial) { int i; - for (i = 0; i < serials.length; i++) - if (serials[i] == serial) - break; - if (i == serials.length) + + AltosDebug.debug("select tracker %d\n", serial); + + if (serial == selected_serial) { + AltosDebug.debug("%d already selected\n", serial); return; + } + + if (serial != 0) { + for (i = 0; i < serials.length; i++) + if (serials[i] == serial) + break; + + if (i == serials.length) { + AltosDebug.debug("attempt to select unknown tracker %d\n", serial); + return; + } + } - AltosDebug.debug("Switching to serial %d\n", serial); - current_serial = serial; + current_serial = selected_serial = serial; update_state(null); } @@ -888,17 +946,27 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener { serverIntent = new Intent(this, MapTypeActivity.class); startActivityForResult(serverIntent, REQUEST_MAP_TYPE); return true; + case R.id.map_source: + int source = AltosDroidPreferences.map_source(); + int new_source = source == AltosDroidPreferences.MAP_SOURCE_ONLINE ? AltosDroidPreferences.MAP_SOURCE_OFFLINE : AltosDroidPreferences.MAP_SOURCE_ONLINE; + AltosDroidPreferences.set_map_source(new_source); + set_map_source(new_source); + return true; case R.id.select_tracker: if (serials != null) { - String[] trackers = new String[serials.length]; + String[] trackers = new String[serials.length+1]; + trackers[0] = "Auto"; for (int i = 0; i < serials.length; i++) - trackers[i] = String.format("%d", serials[i]); + trackers[i+1] = String.format("%d", serials[i]); AlertDialog.Builder builder_serial = new AlertDialog.Builder(this); builder_serial.setTitle("Select a tracker"); builder_serial.setItems(trackers, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { - select_tracker(serials[item]); + if (item == 0) + select_tracker(0); + else + select_tracker(serials[item-1]); } }); AlertDialog alert_serial = builder_serial.create(); @@ -927,4 +995,29 @@ public class AltosDroid extends FragmentActivity implements AltosUnitsListener { } return false; } + + static String direction(AltosGreatCircle from_receiver, + Location receiver) { + if (!receiver.hasBearing()) + return null; + + float bearing = receiver.getBearing(); + float heading = (float) from_receiver.bearing - bearing; + + while (heading <= -180.0f) + heading += 360.0f; + while (heading > 180.0f) + heading -= 360.0f; + + int iheading = (int) (heading + 0.5f); + + if (-1 < iheading && iheading < 1) + return "ahead"; + else if (iheading < -179 || 179 < iheading) + return "backwards"; + else if (iheading < 0) + return String.format("left %d", -iheading); + else + return String.format("right %d", iheading); + } }