altosdroid: Synchronize access to the 'rockets' list for online maps
authorKeith Packard <keithp@keithp.com>
Wed, 19 May 2021 17:48:58 +0000 (10:48 -0700)
committerKeith Packard <keithp@keithp.com>
Wed, 19 May 2021 17:53:45 +0000 (10:53 -0700)
Online maps gets rockets added by the telem code and the same data are
used to create the maps UI. Synchronise access to that object to prevent
simutaneous operations.

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

index c35bbb4d308c32ae608583ac5fe813ad69a2e3ef..76cd990be7f5b474f38fbddea13f3c8653db1341 100644 (file)
@@ -174,10 +174,12 @@ public class AltosMapOnline implements AltosDroidMapInterface, GoogleMap.OnMarke
        }
 
        private RocketOnline[] sorted_rockets() {
-               RocketOnline[]  rocket_array = rockets.values().toArray(new RocketOnline[0]);
+               synchronized(rockets) {
+                       RocketOnline[]  rocket_array = rockets.values().toArray(new RocketOnline[0]);
 
-               Arrays.sort(rocket_array);
-               return rocket_array;
+                       Arrays.sort(rocket_array);
+                       return rocket_array;
+               }
        }
 
        public void onMapClick(LatLng lat_lng) {
@@ -260,22 +262,26 @@ public class AltosMapOnline implements AltosDroidMapInterface, GoogleMap.OnMarke
                if (mMap == null)
                        return;
 
-               if (rockets.containsKey(serial)) {
-                       rocket = rockets.get(serial);
-                       rocket.set_position(new AltosLatLon(state.gps.lat, state.gps.lon), state.received_time);
-               } else {
-                       rocket = new RocketOnline(context,
-                                                 serial,
-                                                 mMap, state.gps.lat, state.gps.lon,
-                                                 state.received_time);
-                       rockets.put(serial, rocket);
+               synchronized(rockets) {
+                       if (rockets.containsKey(serial)) {
+                               rocket = rockets.get(serial);
+                               rocket.set_position(new AltosLatLon(state.gps.lat, state.gps.lon), state.received_time);
+                       } else {
+                               rocket = new RocketOnline(context,
+                                                         serial,
+                                                         mMap, state.gps.lat, state.gps.lon,
+                                                         state.received_time);
+                               rockets.put(serial, rocket);
+                       }
                }
        }
 
        private void remove_rocket(int serial) {
-               RocketOnline rocket = rockets.get(serial);
-               rocket.remove();
-               rockets.remove(serial);
+               synchronized(rockets) {
+                       RocketOnline rocket = rockets.get(serial);
+                       rocket.remove();
+                       rockets.remove(serial);
+               }
        }
 
        public void set_visible(boolean visible) {
@@ -286,13 +292,15 @@ public class AltosMapOnline implements AltosDroidMapInterface, GoogleMap.OnMarke
        public void show(TelemetryState telem_state, AltosState state, AltosGreatCircle from_receiver, Location receiver) {
 
                if (telem_state != null) {
-                       for (int serial : rockets.keySet()) {
-                               if (!telem_state.containsKey(serial))
-                                       remove_rocket(serial);
-                       }
+                       synchronized(rockets) {
+                               for (int serial : rockets.keySet()) {
+                                       if (!telem_state.containsKey(serial))
+                                               remove_rocket(serial);
+                               }
 
-                       for (int serial : telem_state.keySet()) {
-                               set_rocket(serial, telem_state.get(serial));
+                               for (int serial : telem_state.keySet()) {
+                                       set_rocket(serial, telem_state.get(serial));
+                               }
                        }
                }