altoslib: Get rid of AltosMap from AltosMapLoader
authorKeith Packard <keithp@keithp.com>
Wed, 11 May 2016 19:48:10 +0000 (12:48 -0700)
committerKeith Packard <keithp@keithp.com>
Thu, 12 May 2016 06:22:15 +0000 (23:22 -0700)
Cleans up the loader API and eliminates a AltosMapTile for every chunk
of map data.

Signed-off-by: Keith Packard <keithp@keithp.com>
altosdroid/src/org/altusmetrum/AltosDroid/AltosMapOffline.java
altosdroid/src/org/altusmetrum/AltosDroid/PreloadMapActivity.java
altoslib/AltosMap.java
altoslib/AltosMapInterface.java
altoslib/AltosMapLoader.java
altoslib/AltosMapTile.java
altosuilib/AltosUIMapNew.java
altosuilib/AltosUIMapPreloadNew.java

index ab142b175c43f4ba4a4e6d2cb23d46778bac835c..e7edf3c043d0e1f7c33cc11ed2bb8717e0f5d0f0 100644 (file)
@@ -85,6 +85,8 @@ public class AltosMapOffline extends View implements ScaleGestureDetector.OnScal
        AltosMap                map;
        AltosDroid              altos_droid;
 
        AltosMap                map;
        AltosDroid              altos_droid;
 
+       static int scale = 2;
+
        AltosLatLon     here;
        AltosLatLon     there;
        AltosLatLon     pad;
        AltosLatLon     here;
        AltosLatLon     there;
        AltosLatLon     pad;
@@ -181,14 +183,14 @@ public class AltosMapOffline extends View implements ScaleGestureDetector.OnScal
                        }
                }
 
                        }
                }
 
-               public MapTile(AltosMapCache cache, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size) {
-                       super(cache, upper_left, center, zoom, maptype, px_size, 2);
+               public MapTile(AltosMapCache cache, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size, int scale) {
+                       super(cache, upper_left, center, zoom, maptype, px_size, scale);
                }
 
        }
 
                }
 
        }
 
-       public AltosMapTile new_tile(AltosMapCache cache, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size) {
-               return new MapTile(cache, upper_left, center, zoom, maptype, px_size);
+       public AltosMapTile new_tile(AltosMapCache cache, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size, int scale) {
+               return new MapTile(cache, upper_left, center, zoom, maptype, px_size, scale);
        }
 
        public AltosMapPath new_path() {
        }
 
        public AltosMapPath new_path() {
@@ -492,7 +494,7 @@ public class AltosMapOffline extends View implements ScaleGestureDetector.OnScal
 
        public void onCreateView(AltosDroid altos_droid) {
                this.altos_droid = altos_droid;
 
        public void onCreateView(AltosDroid altos_droid) {
                this.altos_droid = altos_droid;
-               map = new AltosMap(this);
+               map = new AltosMap(this, scale);
                AltosPreferences.register_map_type_listener(this);
                map.set_maptype(AltosPreferences.map_type());
 
                AltosPreferences.register_map_type_listener(this);
                map.set_maptype(AltosPreferences.map_type());
 
index 11d712806570ad4b7e0a74dc0f13f701735469f8..9dfc727ed2ee190369b0f628e20851c1edab9131 100644 (file)
@@ -49,7 +49,7 @@ import org.altusmetrum.altoslib_10.*;
  * by the user, the MAC address of the device is sent back to the parent
  * Activity in the result Intent.
  */
  * by the user, the MAC address of the device is sent back to the parent
  * Activity in the result Intent.
  */
-public class PreloadMapActivity extends Activity implements AltosLaunchSiteListener, AltosMapInterface, AltosMapLoaderListener, LocationListener {
+public class PreloadMapActivity extends Activity implements AltosLaunchSiteListener, AltosMapLoaderListener, LocationListener {
 
        private ArrayAdapter<AltosLaunchSite> known_sites_adapter;
 
 
        private ArrayAdapter<AltosLaunchSite> known_sites_adapter;
 
@@ -110,7 +110,12 @@ public class PreloadMapActivity extends Activity implements AltosLaunchSiteListe
                        });
        }
 
                        });
        }
 
+       public void debug(String format, Object ... arguments) {
+               AltosDebug.debug(format, arguments);
+       }
+
        /* AltosLaunchSiteListener interface */
        /* AltosLaunchSiteListener interface */
+
        public void notify_launch_sites(final List<AltosLaunchSite> sites) {
                this.runOnUiThread(new Runnable() {
                                public void run() {
        public void notify_launch_sites(final List<AltosLaunchSite> sites) {
                this.runOnUiThread(new Runnable() {
                                public void run() {
@@ -120,70 +125,6 @@ public class PreloadMapActivity extends Activity implements AltosLaunchSiteListe
                        });
        }
 
                        });
        }
 
-       AltosMap        map;
-
-       class PreloadMapImage implements AltosImage {
-               public void flush() {
-               }
-
-               public PreloadMapImage(File file) {
-               }
-       }
-
-       public AltosMapPath new_path() {
-               return null;
-       }
-
-       public AltosMapLine new_line() {
-               return null;
-       }
-
-       public AltosImage load_image(File file) throws Exception {
-               return new PreloadMapImage(file);
-       }
-
-       public AltosMapMark new_mark(double lat, double lon, int state) {
-               return null;
-       }
-
-       class PreloadMapTile extends AltosMapTile {
-               public void paint(AltosMapTransform t) {
-               }
-
-               public PreloadMapTile(AltosMapCache cache, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size) {
-                       super(cache, upper_left, center, zoom, maptype, px_size, 2);
-               }
-
-       }
-
-       public AltosMapTile new_tile(AltosMapCache cache, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size) {
-               return new PreloadMapTile(cache, upper_left, center, zoom, maptype, px_size);
-       }
-
-       public int width() {
-               return AltosMap.px_size;
-       }
-
-       public int height() {
-               return AltosMap.px_size;
-       }
-
-       public void repaint() {
-       }
-
-       public void repaint(AltosRectangle damage) {
-       }
-
-       public void set_zoom_label(String label) {
-       }
-
-       public void select_object(AltosLatLon latlon) {
-       }
-
-       public void debug(String format, Object ... arguments) {
-               AltosDebug.debug(format, arguments);
-       }
-
        /* LocationProvider interface */
 
        AltosLaunchSite current_location_site;
        /* LocationProvider interface */
 
        AltosLaunchSite current_location_site;
@@ -281,7 +222,7 @@ public class PreloadMapActivity extends Activity implements AltosLaunchSiteListe
 
                        AltosDebug.debug("PreloadMap load %f %f %d %d %f %d\n",
                                         lat, lon, min, max, r, t);
 
                        AltosDebug.debug("PreloadMap load %f %f %d %d %f %d\n",
                                         lat, lon, min, max, r, t);
-                       loader = new AltosMapLoader(map, this, lat, lon, min, max, r, t);
+                       loader = new AltosMapLoader(this, lat, lon, min, max, r, t, AltosMapOffline.scale);
                } catch (ParseException e) {
                        AltosDebug.debug("PreloadMap load raised exception %s", e.toString());
                }
                } catch (ParseException e) {
                        AltosDebug.debug("PreloadMap load raised exception %s", e.toString());
                }
@@ -412,8 +353,6 @@ public class PreloadMapActivity extends Activity implements AltosLaunchSiteListe
                known_sites_spinner.setAdapter(known_sites_adapter);
                known_sites_spinner.setOnItemSelectedListener(new SiteListListener());
 
                known_sites_spinner.setAdapter(known_sites_adapter);
                known_sites_spinner.setOnItemSelectedListener(new SiteListListener());
 
-               map = new AltosMap(this);
-
                // Listen for GPS and Network position updates
                LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
 
                // Listen for GPS and Network position updates
                LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
 
index 1841277f8bd61889390f0d5802dff09a7584b603..08ac5f3c9bc6c859491c9447964af115196f4bbc 100644 (file)
@@ -51,6 +51,7 @@ public class AltosMap implements AltosMapTileListener, AltosMapStoreListener {
        };
 
        AltosMapInterface       map_interface;
        };
 
        AltosMapInterface       map_interface;
+       int                     scale;
 
        AltosMapCache           cache;
 
 
        AltosMapCache           cache;
 
@@ -328,7 +329,8 @@ public class AltosMap implements AltosMapTileListener, AltosMapStoreListener {
                                if (!tiles.containsKey(point)) {
                                        AltosLatLon     ul = transform.lat_lon(point);
                                        AltosLatLon     center = transform.lat_lon(new AltosPointDouble(x + AltosMap.px_size/2, y + AltosMap.px_size/2));
                                if (!tiles.containsKey(point)) {
                                        AltosLatLon     ul = transform.lat_lon(point);
                                        AltosLatLon     center = transform.lat_lon(new AltosPointDouble(x + AltosMap.px_size/2, y + AltosMap.px_size/2));
-                                       AltosMapTile tile = map_interface.new_tile(cache, ul, center, zoom, maptype, px_size);
+                                       AltosMapTile tile = map_interface.new_tile(cache, ul, center, zoom, maptype, px_size, scale);
+                                       debug("show state %s url %s\n", AltosMapTile.status_name(tile.store.status()), tile.store.url);
                                        tile.add_listener(this);
                                        tiles.put(point, tile);
                                }
                                        tile.add_listener(this);
                                        tiles.put(point, tile);
                                }
@@ -475,11 +477,16 @@ public class AltosMap implements AltosMapTileListener, AltosMapStoreListener {
                        drag_stop(x, y);
        }
 
                        drag_stop(x, y);
        }
 
-       public AltosMap(AltosMapInterface map_interface) {
+       public AltosMap(AltosMapInterface map_interface, int scale) {
                this.map_interface = map_interface;
                this.map_interface = map_interface;
+               this.scale = scale;
                cache = new AltosMapCache(map_interface);
                line = map_interface.new_line();
                path = map_interface.new_path();
                set_zoom_label();
        }
                cache = new AltosMapCache(map_interface);
                line = map_interface.new_line();
                path = map_interface.new_path();
                set_zoom_label();
        }
+
+       public AltosMap(AltosMapInterface map_interface) {
+               this(map_interface, 1);
+       }
 }
 }
index 756a78f20e88c437d9fef8c2bd9767c2b47d104e..df09224ab4261d6f71c90f6c6a6a62dbc008d2b1 100644 (file)
@@ -29,7 +29,7 @@ public interface AltosMapInterface {
 
        public abstract AltosMapMark new_mark(double lat, double lon, int state);
 
 
        public abstract AltosMapMark new_mark(double lat, double lon, int state);
 
-       public abstract AltosMapTile new_tile(AltosMapCache cache, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size);
+       public abstract AltosMapTile new_tile(AltosMapCache cache, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size, int scale);
 
        public abstract int width();
 
 
        public abstract int width();
 
index 8b856e13debbb5cbbc8b5df832cc38fff342ae04..015c0ad315f8599f5b4e47c294afa0cf7824c9d1 100644 (file)
@@ -25,7 +25,7 @@ import java.lang.Math;
 import java.net.URL;
 import java.net.URLConnection;
 
 import java.net.URL;
 import java.net.URLConnection;
 
-public class AltosMapLoader extends Thread implements AltosMapTileListener {
+public class AltosMapLoader extends Thread implements AltosMapStoreListener {
        AltosMapLoaderListener  listener;
 
        double  latitude, longitude;
        AltosMapLoaderListener  listener;
 
        double  latitude, longitude;
@@ -35,6 +35,7 @@ public class AltosMapLoader extends Thread implements AltosMapTileListener {
        int     all_types;
        int     cur_type;
        double  radius;
        int     all_types;
        int     cur_type;
        double  radius;
+       int     scale;
 
        int     tiles_loaded_layer;
        int     tiles_loaded_total;
 
        int     tiles_loaded_layer;
        int     tiles_loaded_total;
@@ -49,8 +50,6 @@ public class AltosMapLoader extends Thread implements AltosMapTileListener {
 
        boolean abort;
 
 
        boolean abort;
 
-       AltosMap        map;
-
        int tile_radius(int zoom) {
                double  delta_lon = AltosMapTransform.lon_from_distance(latitude, radius);
 
        int tile_radius(int zoom) {
                double  delta_lon = AltosMapTransform.lon_from_distance(latitude, radius);
 
@@ -80,8 +79,6 @@ public class AltosMapLoader extends Thread implements AltosMapTileListener {
                AltosLatLon load_centre = new AltosLatLon(latitude, longitude);
                AltosMapTransform transform = new AltosMapTransform(256, 256, zoom, load_centre);
 
                AltosLatLon load_centre = new AltosLatLon(latitude, longitude);
                AltosMapTransform transform = new AltosMapTransform(256, 256, zoom, load_centre);
 
-               map.centre(load_centre);
-
                AltosPointInt   upper_left;
                AltosPointInt   lower_right;
 
                AltosPointInt   upper_left;
                AltosPointInt   lower_right;
 
@@ -103,8 +100,9 @@ public class AltosMapLoader extends Thread implements AltosMapTileListener {
                                AltosPointInt   point = new AltosPointInt(x, y);
                                AltosLatLon     ul = transform.lat_lon(point);
                                AltosLatLon     center = transform.lat_lon(new AltosPointDouble(x + AltosMap.px_size/2, y + AltosMap.px_size/2));
                                AltosPointInt   point = new AltosPointInt(x, y);
                                AltosLatLon     ul = transform.lat_lon(point);
                                AltosLatLon     center = transform.lat_lon(new AltosPointDouble(x + AltosMap.px_size/2, y + AltosMap.px_size/2));
-                               AltosMapTile    tile = new AltosMapTile(null, ul, center, zoom, maptype, AltosMap.px_size);
-                               tile.add_listener(this);
+                               AltosMapStore   store = AltosMapStore.get(center, zoom, maptype, AltosMap.px_size, scale);
+                               listener.debug("load state %s url %s\n", AltosMapTile.status_name(store.status()), store.url);
+                               store.add_listener(this);
                                if (abort)
                                        return false;
                        }
                                if (abort)
                                        return false;
                        }
@@ -170,14 +168,14 @@ public class AltosMapLoader extends Thread implements AltosMapTileListener {
                        listener.loader_done(tiles_total);
        }
 
                        listener.loader_done(tiles_total);
        }
 
-       public synchronized void notify_tile(AltosMapTile tile, int status) {
+       public synchronized void notify_store(AltosMapStore store, int status) {
                boolean do_next = false;
                if (status == AltosMapTile.fetching)
                        return;
 
                loading.release();
 
                boolean do_next = false;
                if (status == AltosMapTile.fetching)
                        return;
 
                loading.release();
 
-               tile.remove_listener(this);
+               store.remove_listener(this);
 
                if (layers_loaded >= layers_total)
                        return;
 
                if (layers_loaded >= layers_total)
                        return;
@@ -185,7 +183,7 @@ public class AltosMapLoader extends Thread implements AltosMapTileListener {
                ++tiles_loaded_total;
                ++tiles_loaded_layer;
 
                ++tiles_loaded_total;
                ++tiles_loaded_layer;
 
-               listener.debug("AltosMapLoader.notify_tile status %d total %d of %d layer %d of %d\n",
+               listener.debug("AltosMapLoader.notify_store status %d total %d of %d layer %d of %d\n",
                               status, tiles_loaded_total, tiles_total, tiles_loaded_layer, tiles_this_layer);
 
                if (tiles_loaded_layer == tiles_this_layer) {
                               status, tiles_loaded_total, tiles_total, tiles_loaded_layer, tiles_this_layer);
 
                if (tiles_loaded_layer == tiles_this_layer) {
@@ -198,18 +196,17 @@ public class AltosMapLoader extends Thread implements AltosMapTileListener {
                        listener.loader_done(tiles_total);
                else
                        listener.loader_notify(tiles_loaded_total,
                        listener.loader_done(tiles_total);
                else
                        listener.loader_notify(tiles_loaded_total,
-                                              tiles_total, tile.store.file.toString());
+                                              tiles_total, store.file.toString());
        }
 
        public void abort() {
                this.abort = true;
        }
 
        }
 
        public void abort() {
                this.abort = true;
        }
 
-       public AltosMapLoader(AltosMap map, AltosMapLoaderListener listener,
-                             double latitude, double longitude, int min_z, int max_z, double radius, int all_types) {
+       public AltosMapLoader(AltosMapLoaderListener listener,
+                             double latitude, double longitude, int min_z, int max_z, double radius, int all_types, int scale) {
                listener.debug("lat %f lon %f min_z %d max_z %d radius %f all_types %d\n",
                               latitude, longitude, min_z, max_z, radius, all_types);
                listener.debug("lat %f lon %f min_z %d max_z %d radius %f all_types %d\n",
                               latitude, longitude, min_z, max_z, radius, all_types);
-               this.map = map;
                this.listener = listener;
                this.latitude = latitude;
                this.longitude = longitude;
                this.listener = listener;
                this.latitude = latitude;
                this.longitude = longitude;
@@ -217,6 +214,7 @@ public class AltosMapLoader extends Thread implements AltosMapTileListener {
                this.max_z = max_z;
                this.radius = radius;
                this.all_types = all_types;
                this.max_z = max_z;
                this.radius = radius;
                this.all_types = all_types;
+               this.scale = scale;
                this.abort = false;
                start();
        }
                this.abort = false;
                start();
        }
index 19332de191385254591155f755f0c85fabebdaea..b0bac963074387f2367efd78f9db02684ba5d257 100644 (file)
@@ -38,6 +38,25 @@ public class AltosMapTile implements AltosFontListener, AltosMapStoreListener {
        static public final int bad_request = 4;/* downloading failed */
        static public final int forbidden = 5;  /* downloading failed */
 
        static public final int bad_request = 4;/* downloading failed */
        static public final int forbidden = 5;  /* downloading failed */
 
+       static public String status_name(int status) {
+               switch (status) {
+               case loaded:
+                       return "loaded";
+               case fetched:
+                       return "fetched";
+               case fetching:
+                       return "fetching";
+               case failed:
+                       return "failed";
+               case bad_request:
+                       return "bad_request";
+               case forbidden:
+                       return "forbidden";
+               default:
+                       return "unknown";
+               }
+       }
+
        public void font_size_changed(int font_size) {
        }
 
        public void font_size_changed(int font_size) {
        }
 
@@ -96,8 +115,4 @@ public class AltosMapTile implements AltosFontListener, AltosMapStoreListener {
                store = AltosMapStore.get(center, zoom, maptype, px_size, scale);
                store.add_listener(this);
        }
                store = AltosMapStore.get(center, zoom, maptype, px_size, scale);
                store.add_listener(this);
        }
-
-       public AltosMapTile(AltosMapCache cache, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size) {
-               this(cache, upper_left, center, zoom, maptype, px_size, 1);
-       }
 }
 }
index a90e8c91ae76f9c9387db39db1a47a2b646956c7..768fd058b9b7b204ff31ed0fb4eeaa02016ec171 100644 (file)
@@ -241,8 +241,8 @@ public class AltosUIMapNew extends JComponent implements AltosFlightDisplay, Alt
        }
 
        class MapTile extends AltosMapTile {
        }
 
        class MapTile extends AltosMapTile {
-               public MapTile(AltosMapCache cache, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size) {
-                       super(cache, upper_left, center, zoom, maptype, px_size);
+               public MapTile(AltosMapCache cache, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size, int scale) {
+                       super(cache, upper_left, center, zoom, maptype, px_size, scale);
                }
 
                public void paint(AltosMapTransform t) {
                }
 
                public void paint(AltosMapTransform t) {
@@ -332,8 +332,8 @@ public class AltosUIMapNew extends JComponent implements AltosFlightDisplay, Alt
                return new MapMark(lat, lon, state);
        }
 
                return new MapMark(lat, lon, state);
        }
 
-       public AltosMapTile new_tile(AltosMapCache cache, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size) {
-               return new MapTile(cache, upper_left, center, zoom, maptype, px_size);
+       public AltosMapTile new_tile(AltosMapCache cache, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size, int scale) {
+               return new MapTile(cache, upper_left, center, zoom, maptype, px_size, scale);
        }
 
        public int width() {
        }
 
        public int width() {
index 3e4a604fa80cd6356bb25a0b37cb6a25831aebaf..7cdba3824d6d3ec6889612f8ba95a0a6b947b6e4 100644 (file)
@@ -138,6 +138,7 @@ public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener
        JComboBox<Integer>      min_zoom;
        JComboBox<Integer>      max_zoom;
        JComboBox<Double>       radius;
        JComboBox<Integer>      min_zoom;
        JComboBox<Integer>      max_zoom;
        JComboBox<Double>       radius;
+       int scale = 1;
 
        Integer[]               zooms = { -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6 };
 
 
        Integer[]               zooms = { -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6 };
 
@@ -249,9 +250,12 @@ public class AltosUIMapPreloadNew extends AltosUIFrame implements ActionListener
                                        else
                                                r = r * 1000;
 
                                        else
                                                r = r * 1000;
 
-                                       loader = new AltosMapLoader(map.map, this,
+                                       map.map.centre(new AltosLatLon(latitude, longitude));
+
+                                       loader = new AltosMapLoader(this,
                                                                    latitude, longitude,
                                                                    latitude, longitude,
-                                                                   min_z, max_z, r, all_types());
+                                                                   min_z, max_z, r,
+                                                                   all_types(), scale);
 
                                } catch (ParseException pe) {
                                        load_button.setSelected(false);
 
                                } catch (ParseException pe) {
                                        load_button.setSelected(false);