altosdroid: Support for sorting rockets by age
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / TabMapOffline.java
index 90b4115634ea4ef26b3116e683b252045240308f..296e21225bd55a94127b71c872e8608cb21f40b0 100644 (file)
@@ -32,9 +32,10 @@ import android.widget.*;
 import android.location.Location;
 import android.content.*;
 
-class Rocket {
+class Rocket implements Comparable {
        AltosLatLon     position;
        String          name;
+       long            last_packet;
        TabMapOffline   tab;
 
        void paint() {
@@ -42,20 +43,31 @@ class Rocket {
                tab.draw_text(position, name, 0, 3*tab.rocket_bitmap.getHeight()/4);
        }
 
-       void set_position(AltosLatLon position) {
+       void set_position(AltosLatLon position, long last_packet) {
                this.position = position;
+               this.last_packet = last_packet;
        }
 
        Rocket(String name, TabMapOffline tab) {
                this.name = name;
                this.tab = tab;
        }
+
+       public int compareTo(Object o) {
+               Rocket other = (Rocket) o;
+
+               long    diff = last_packet - other.last_packet;
+
+               if (diff > 0)
+                       return 1;
+               if (diff < 0)
+                       return -1;
+               return 0;
+       }
 }
 
 public class TabMapOffline extends AltosDroidTab implements AltosMapInterface {
 
-       AltosDroid mAltosDroid;
-
        AltosMap map;
 
        AltosLatLon     here;
@@ -139,59 +151,10 @@ public class TabMapOffline extends AltosDroidTab implements AltosMapInterface {
                CYAN,   // stateless
        };
 
-       class MapPath extends AltosMapPath {
-
-               boolean line_in(AltosPointDouble a, AltosPointDouble b) {
-                       final Rect bounds = canvas.getClipBounds();
-                       int left = (int) Math.floor (Math.min((float) a.x, (float) b.x) - stroke_width / 2.0f);
-                       int right = (int) Math.ceil(Math.max((float) a.x, (float) b.x) + stroke_width / 2.0f);
-                       int top = (int) Math.floor(Math.min((float) a.y, (float) b.y) - stroke_width / 2.0f);
-                       int bottom = (int) Math.ceil(Math.max((float) a.y, (float) b.y) + stroke_width / 2.0f);
-
-                       return left < bounds.right && bounds.left < right &&
-                               top < bounds.bottom && bounds.top < bottom;
-               }
-
-               public void paint(AltosMapTransform t) {
-                       AltosPointDouble        prev = null;
-                       int                     cur_color = paint.getColor();
-
-                       for (AltosMapPathPoint point : points) {
-                               AltosPointDouble        cur = t.screen(point.lat_lon);
-
-                               if (prev != null && line_in(prev, cur)) {
-                                       int color;
-                                       if (0 <= point.state && point.state < stateColors.length)
-                                               color = stateColors[point.state];
-                                       else
-                                               color = stateColors[AltosLib.ao_flight_invalid];
-                                       if (color != cur_color) {
-                                               paint.setColor(color);
-                                               cur_color = color;
-                                       }
-                                       canvas.drawLine((float) prev.x, (float) prev.y, (float) cur.x, (float) cur.y, paint);
-                               }
-                               prev = cur;
-                       }
-               }
-
-               public MapPath() {
-                       stroke_width = TabMapOffline.this.stroke_width;
-               }
-       }
-
        public AltosMapPath new_path() {
                return null;
        }
 
-       class MapLine extends AltosMapLine {
-               public void paint(AltosMapTransform t) {
-               }
-
-               public MapLine() {
-               }
-       }
-
        public AltosMapLine new_line() {
                return null;
        }
@@ -325,11 +288,9 @@ public class TabMapOffline extends AltosDroidTab implements AltosMapInterface {
        @Override
        public void onAttach(Activity activity) {
                super.onAttach(activity);
-               mAltosDroid = (AltosDroid) activity;
-               mAltosDroid.registerTab(this);
 
                map = new AltosMap(this);
-               map.set_maptype(mAltosDroid.map_type);
+               map.set_maptype(altos_droid.map_type);
 
                pad_bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pad);
                /* arrow at the bottom of the launchpad image */
@@ -347,11 +308,6 @@ public class TabMapOffline extends AltosDroidTab implements AltosMapInterface {
                here_off_y = here_bitmap.getHeight() / 2;
        }
 
-       @Override
-       public void onDetach() {
-               mAltosDroid = null;
-       }
-
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
@@ -381,7 +337,6 @@ public class TabMapOffline extends AltosDroidTab implements AltosMapInterface {
        public void onDestroyView() {
                super.onDestroyView();
 
-               mAltosDroid.unregisterTab(this);
        }
 
        private void center(double lat, double lon, double accuracy) {
@@ -394,7 +349,7 @@ public class TabMapOffline extends AltosDroidTab implements AltosMapInterface {
 
        public String tab_name() { return "offmap"; }
 
-       public void show(AltosState state, AltosGreatCircle from_receiver, Location receiver) {
+       public void show(TelemetryState telem_state, AltosState state, AltosGreatCircle from_receiver, Location receiver) {
                if (from_receiver != null) {
                        mBearingView.setText(String.format("%3.0f°", from_receiver.bearing));
                        set_value(mDistanceView, AltosConvert.distance, 6, from_receiver.distance);
@@ -410,20 +365,31 @@ public class TabMapOffline extends AltosDroidTab implements AltosMapInterface {
                        }
                        if (state.pad_lat != AltosLib.MISSING && pad == null)
                                pad = new AltosLatLon(state.pad_lat, state.pad_lon);
+               }
+
+               if (telem_state != null) {
+                       Integer[] old_serial = rockets.keySet().toArray(new Integer[0]);
+                       Integer[] new_serial = telem_state.states.keySet().toArray(new Integer[0]);
 
-                       int serial = state.serial;
-                       if (serial == AltosLib.MISSING)
-                               serial = 0;
+                       /* remove deleted keys */
+                       for (int serial : old_serial) {
+                               if (!telem_state.states.containsKey(serial))
+                                       rockets.remove(serial);
+                       }
 
-                       Rocket  rocket = null;
+                       /* set remaining keys */
 
-                       if (state.gps != null && state.gps.locked) {
-                               if (!rockets.containsKey(serial)) {
+                       for (int serial : new_serial) {
+                               Rocket          rocket;
+                               AltosState      t_state = telem_state.states.get(serial);
+                               if (rockets.containsKey(serial))
+                                       rocket = rockets.get(serial);
+                               else {
                                        rocket = new Rocket(String.format("%d", serial), this);
                                        rockets.put(serial, rocket);
-                               } else
-                                       rocket = rockets.get(serial);
-                               rocket.set_position(new AltosLatLon(state.gps.lat, state.gps.lon));
+                               }
+                               if (t_state.gps != null)
+                                       rocket.set_position(new AltosLatLon(t_state.gps.lat, t_state.gps.lon), t_state.received_time);
                        }
                }