if (canvas.quickReject(pt.x, pt.y, pt.x + px_size, pt.y + px_size, Canvas.EdgeType.AA))
return;
- AltosImage altos_image = cache.get(this, store, px_size, px_size);
+ AltosImage altos_image = this.get_image();
MapImage map_image = (MapImage) altos_image;
if (t.has_location()) {
String message = null;
switch (status) {
- case AltosMapTile.loading:
- message = "Loading...";
+ case AltosMapTile.fetching:
+ message = "Fetching...";
break;
case AltosMapTile.bad_request:
message = "Internal error";
}
}
- public MapTile(AltosMapTileListener listener, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size) {
- super(listener, upper_left, center, zoom, maptype, px_size, 2);
+ 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 AltosMapTile new_tile(AltosMapTileListener listener, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size) {
- return new MapTile(listener, 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) {
+ return new MapTile(cache, upper_left, center, zoom, maptype, px_size);
}
public AltosMapPath new_path() {
}
AltosMap map;
- AltosMapLoader loader;
class PreloadMapImage implements AltosImage {
public void flush() {
public void paint(AltosMapTransform t) {
}
- public PreloadMapTile(AltosMapTileListener listener, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size) {
- super(listener, upper_left, center, zoom, maptype, px_size, 2);
+ 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(AltosMapTileListener listener, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size) {
- return new PreloadMapTile(listener, 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) {
+ return new PreloadMapTile(cache, upper_left, center, zoom, maptype, px_size);
}
public int width() {
AltosDebug.debug("PreloadMap load %f %f %d %d %f %d\n",
lat, lon, min, max, r, t);
- loader.load(lat, lon, min, max, r, t);
+ new AltosMapLoader(map, this, lat, lon, min, max, r, t);
} catch (ParseException e) {
AltosDebug.debug("PreloadMap load raised exception %s", e.toString());
}
map = new AltosMap(this);
- loader = new AltosMapLoader(map, this);
-
// Listen for GPS and Network position updates
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
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(this, ul, center, zoom, maptype, px_size);
+ AltosMapTile tile = map_interface.new_tile(cache, ul, center, zoom, maptype, px_size);
+ tile.add_listener(this);
tiles.put(point, tile);
}
}
centre(lat, lon);
tiles.clear();
make_tiles();
- for (AltosMapTile tile : tiles.values()) {
- tile.add_store_listener(this);
- if (tile.store_status() != AltosMapTile.loading)
- listener.notify_tile(tile, tile.store_status());
- }
repaint();
}
public class AltosMapCache implements AltosMapCacheListener {
/* An entry in the MapCache */
- class MapCacheElement implements AltosMapStoreListener {
+ class MapCacheElement implements AltosMapTileListener {
AltosMapTile tile; /* Notify when image has been loaded */
AltosImage image;
- AltosMapStore store;
long used;
class loader implements Runnable {
public void run() {
- if (image != null)
- tile.notify_image(image);
- try {
- image = map_interface.load_image(store.file);
- } catch (Exception ex) {
+ if (image == null) {
+ try {
+ image = map_interface.load_image(tile.store.file);
+ } catch (Exception ex) {
+ }
}
- if (image == null)
- tile.set_status(AltosMapTile.failed);
- else
- tile.set_status(AltosMapTile.success);
tile.notify_image(image);
}
}
}
public boolean has_map() {
- return store.status() == AltosMapTile.success;
+ return tile.status == AltosMapTile.loaded;
}
- public synchronized void notify_store(AltosMapStore store, int status) {
- switch (status) {
- case AltosMapTile.loading:
- break;
- case AltosMapTile.success:
+ public synchronized void notify_tile(AltosMapTile tile, int status) {
+ if (status == AltosMapTile.fetched) {
+ System.out.printf("tile fetched, loading image\n");
load();
- break;
- default:
- tile.set_status(status);
- tile.notify_image(null);
}
}
- public MapCacheElement(AltosMapTile tile, AltosMapStore store) throws IOException {
+ public MapCacheElement(AltosMapTile tile) {
this.tile = tile;
this.image = null;
- this.store = store;
this.used = 0;
-
- int status = store.status();
- switch (status) {
- case AltosMapTile.loading:
- store.add_listener(this);
- break;
- case AltosMapTile.success:
- load();
- break;
- default:
- tile.set_status(status);
- tile.notify_image(null);
- break;
- }
+ tile.add_listener(this);
}
}
}
}
- public AltosImage get(AltosMapTile tile, AltosMapStore store, int width, int height) {
+ public AltosImage get(AltosMapTile tile) {
int oldest = -1;
long age = used;
oldest = i;
break;
}
- if (store.equals(element.store)) {
+ if (tile.store.equals(element.tile.store)) {
element.used = used++;
return element.image;
}
}
}
- try {
- element = new MapCacheElement(tile, store);
- element.used = used++;
- if (elements[oldest] != null)
- elements[oldest].flush();
+ element = new MapCacheElement(tile);
+ element.used = used++;
+ if (elements[oldest] != null)
+ elements[oldest].flush();
- elements[oldest] = element;
-
- if (element.image == null)
- tile.set_status(AltosMapTile.loading);
- else
- tile.set_status(AltosMapTile.success);
-
- return element.image;
- } catch (IOException e) {
- tile.set_status(AltosMapTile.failed);
- return null;
- }
+ elements[oldest] = element;
+ System.out.printf("AltosMapCache.get image ? %s\n",
+ element.image == null ? "false" : "true");
+ return element.image;
}
}
public abstract AltosMapMark new_mark(double lat, double lon, int state);
- public abstract AltosMapTile new_tile(AltosMapTileListener listener, 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);
public abstract int width();
import java.net.URL;
import java.net.URLConnection;
-public class AltosMapLoader implements AltosMapTileListener, AltosMapStoreListener {
+public class AltosMapLoader extends Thread implements AltosMapTileListener {
AltosMapLoaderListener listener;
double latitude, longitude;
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 = map.map_interface.new_tile(this, ul, center, zoom, maptype, AltosMap.px_size);
- tile.add_store_listener(this);
- if (tile.store_status() != AltosMapTile.loading)
- notify_tile(tile, tile.store_status());
+ AltosMapTile tile = map.map_interface.new_tile(null, ul, center, zoom, maptype, AltosMap.px_size);
+ tile.add_listener(this);
}
}
}
do_load();
}
- private void start_load() {
+ public void run() {
cur_z = min_z;
int ntype = 0;
cur_type = next_type(0);
+ tiles_total = 0;
for (int z = min_z; z <= max_z; z++)
- tiles_total += tiles_per_layer(z);
+ tiles_total += tiles_per_layer(z) * ntype;
layers_total = (max_z - min_z + 1) * ntype;
layers_loaded = 0;
tiles_loaded_total = 0;
- listener.debug("total tiles %d\n", tiles_total);
+ listener.debug("total tiles %d layers %d\n", tiles_total, layers_total);
listener.loader_start(tiles_total);
do_load();
}
- public void load(double latitude, double longitude, int min_z, int max_z, double radius, int 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.latitude = latitude;
- this.longitude = longitude;
- this.min_z = min_z;
- this.max_z = max_z;
- this.radius = radius;
- this.all_types = all_types;
- start_load();
- }
-
- public synchronized void notify_store(AltosMapStore store, int status) {
+ public synchronized void notify_tile(AltosMapTile tile, int status) {
boolean do_next = false;
- if (status == AltosMapTile.loading)
+ if (status == AltosMapTile.fetching)
return;
+ tile.remove_listener(this);
+
if (layers_loaded >= layers_total)
return;
++tiles_loaded_total;
++tiles_loaded_layer;
+ listener.debug("AltosMapLoader.notify_tile 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) {
++layers_loaded;
listener.debug("%d layers loaded\n", layers_loaded);
- if (layers_loaded == layers_total) {
- listener.loader_done(tiles_total);
- return;
- } else {
- do_next = true;
- }
+ do_next = true;
}
- listener.loader_notify(tiles_loaded_total,
- tiles_total, store.file.toString());
- if (do_next)
- next_load();
- }
- public synchronized void notify_tile(AltosMapTile tile, int status) {
- notify_store(tile.store, status);
+ if (tiles_loaded_total == tiles_total)
+ listener.loader_done(tiles_total);
+ else {
+ listener.loader_notify(tiles_loaded_total,
+ tiles_total, tile.store.file.toString());
+ if (do_next)
+ next_load();
+ }
}
- public AltosMapCache cache() { return map.cache(); }
-
- public AltosMapLoader(AltosMap map, AltosMapLoaderListener listener) {
+ public AltosMapLoader(AltosMap map, AltosMapLoaderListener listener,
+ double latitude, double longitude, int min_z, int max_z, double radius, int 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.min_z = min_z;
+ this.max_z = max_z;
+ this.radius = radius;
+ this.all_types = all_types;
+ start();
}
}
public synchronized void add_listener(AltosMapStoreListener listener) {
if (!listeners.contains(listener))
listeners.add(listener);
+ listener.notify_store(this, status);
}
public synchronized void remove_listener(AltosMapStoreListener listener) {
file.delete();
return AltosMapTile.bad_request;
}
- return AltosMapTile.success;
+ return AltosMapTile.fetched;
}
static Object fetch_lock = new Object();
static final long forbidden_interval = 60l * 1000l * 1000l * 1000l;
static final long google_maps_ratelimit_ms = 1200;
- static Object loader_lock = new Object();
+ static Object fetcher_lock = new Object();
static LinkedList<AltosMapStore> waiting = new LinkedList<AltosMapStore>();
static LinkedList<AltosMapStore> running = new LinkedList<AltosMapStore>();
- static final int concurrent_loaders = 128;
+ static final int concurrent_fetchers = 128;
- static void start_loaders() {
- while (!waiting.isEmpty() && running.size() < concurrent_loaders) {
+ static void start_fetchers() {
+ while (!waiting.isEmpty() && running.size() < concurrent_fetchers) {
AltosMapStore s = waiting.remove();
running.add(s);
- Thread lt = s.make_loader_thread();
+ Thread lt = s.make_fetcher_thread();
lt.start();
}
}
- void finish_loader() {
- synchronized(loader_lock) {
+ void finish_fetcher() {
+ synchronized(fetcher_lock) {
running.remove(this);
- start_loaders();
+ start_fetchers();
}
}
- void add_loader() {
- synchronized(loader_lock) {
+ void add_fetcher() {
+ synchronized(fetcher_lock) {
waiting.add(this);
- start_loaders();
+ start_fetchers();
}
}
- class loader implements Runnable {
+ class fetcher implements Runnable {
public void run() {
try {
if (file.exists()) {
- notify_listeners(AltosMapTile.success);
+ notify_listeners(AltosMapTile.fetched);
return;
}
synchronized (fetch_lock) {
long startTime = System.nanoTime();
new_status = fetch_url();
- if (new_status == AltosMapTile.success) {
+ if (new_status == AltosMapTile.fetched) {
long duration_ms = (System.nanoTime() - startTime) / 1000000;
if (duration_ms < google_maps_ratelimit_ms) {
try {
}
notify_listeners(new_status);
} finally {
- finish_loader();
+ finish_fetcher();
}
}
}
- private Thread make_loader_thread() {
- return new Thread(new loader());
+ private Thread make_fetcher_thread() {
+ return new Thread(new fetcher());
}
- private void load() {
- add_loader();
+ private void fetch() {
+ add_fetcher();
}
private AltosMapStore (String url, File file) {
this.file = file;
if (file.exists())
- status = AltosMapTile.success;
+ status = AltosMapTile.fetched;
else {
- status = AltosMapTile.loading;
- load();
+ status = AltosMapTile.fetching;
+ fetch();
}
}
import java.io.*;
import java.util.*;
-public abstract class AltosMapTile implements AltosFontListener {
- AltosMapTileListener listener;
+public abstract class AltosMapTile implements AltosFontListener, AltosMapStoreListener {
+ LinkedList<AltosMapTileListener> listeners = new LinkedList<AltosMapTileListener>();
public AltosLatLon upper_left, center;
public int px_size;
int zoom;
int maptype;
int scale;
+ private AltosMapCache cache;
public AltosMapStore store;
- public AltosMapCache cache;
public int status;
- static public final int success = 0;
- static public final int loading = 1;
- static public final int failed = 2;
- static public final int bad_request = 3;
- static public final int forbidden = 4;
+ static public final int loaded = 0; /* loaded from file */
+ static public final int fetched = 1; /* downloaded to file */
+ static public final int fetching = 2; /* downloading from net */
+ static public final int failed = 3; /* loading from file failed */
+ static public final int bad_request = 4;/* downloading failed */
+ static public final int forbidden = 5; /* downloading failed */
private File map_file() {
double lat = center.lat;
public void font_size_changed(int font_size) {
}
- public void set_status(int status) {
+ private synchronized void notify_listeners(int status) {
this.status = status;
- listener.notify_tile(this, status);
+ for (AltosMapTileListener listener : listeners)
+ listener.notify_tile(this, status);
}
- public void notify_image(AltosImage image) {
- listener.notify_tile(this, status);
+ public void notify_store(AltosMapStore store, int status) {
+// System.out.printf("AltosMapTile.notify_store %d\n", status);
+ notify_listeners(status);
}
- public int store_status() {
- return store.status();
+ public void notify_image(AltosImage image) {
+ if (image == null)
+ status = failed;
+ else
+ status = loaded;
+ notify_listeners(status);
}
- public void add_store_listener(AltosMapStoreListener listener) {
- store.add_listener(listener);
+ public abstract void paint(AltosMapTransform t);
+
+ public AltosImage get_image() {
+ if (cache == null)
+ return null;
+ return cache.get(this);
}
- public void remove_store_listener(AltosMapStoreListener listener) {
- store.remove_listener(listener);
+ public synchronized void add_listener(AltosMapTileListener listener) {
+ if (!listeners.contains(listener))
+ listeners.add(listener);
+ listener.notify_tile(this, status);
}
- public abstract void paint(AltosMapTransform t);
+ public synchronized void remove_listener(AltosMapTileListener listener) {
+ listeners.remove(listener);
+ }
- public AltosMapTile(AltosMapTileListener listener, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size, int scale) {
- this.listener = listener;
+ public AltosMapTile(AltosMapCache cache, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size, int scale) {
+ this.cache = cache;
this.upper_left = upper_left;
- this.cache = listener.cache();
while (center.lon < -180.0)
center.lon += 360.0;
this.px_size = px_size;
this.scale = scale;
- status = AltosMapTile.loading;
store = AltosMapStore.get(map_url(), map_file());
+ store.add_listener(this);
}
- public AltosMapTile(AltosMapTileListener listener, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size) {
- this(listener, upper_left, center, zoom, maptype, px_size, 1);
+ 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);
}
}
public interface AltosMapTileListener {
abstract public void notify_tile(AltosMapTile tile, int status);
-
- abstract public AltosMapCache cache();
}
}
class MapTile extends AltosMapTile {
- public MapTile(AltosMapTileListener listener, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size) {
- super(listener, upper_left, center, zoom, maptype, px_size);
+ 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 void paint(AltosMapTransform t) {
if (!g.hitClip(point.x, point.y, px_size, px_size))
return;
- AltosImage altos_image = cache.get(this, store, px_size, px_size);
-
+ AltosImage altos_image = get_image();
AltosUIImage ui_image = (AltosUIImage) altos_image;
-
- Image image = null;
+ Image image = null;
if (ui_image != null)
image = ui_image.image;
if (t.has_location()) {
String message = null;
switch (status) {
- case AltosMapTile.loading:
- message = "Loading...";
+ case AltosMapTile.fetching:
+ message = "Fetching...";
break;
case AltosMapTile.bad_request:
message = "Internal error";
return new MapMark(lat, lon, state);
}
- public AltosMapTile new_tile(AltosMapTileListener listener, AltosLatLon upper_left, AltosLatLon center, int zoom, int maptype, int px_size) {
- return new MapTile(listener, 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) {
+ return new MapTile(cache, upper_left, center, zoom, maptype, px_size);
}
public int width() {
JProgressBar pbar;
- AltosMapLoader loader;
-
JLabel site_list_label;
JComboBox<AltosLaunchSite> site_list;
r = r * 1000;
loading = true;
- loader.load(latitude, longitude, min_z, max_z, r, all_types());
+ new AltosMapLoader(map.map, this,
+ latitude, longitude,
+ min_z, max_z, r, all_types());
+
} catch (ParseException pe) {
load_button.setSelected(false);
}
map = new AltosUIMapNew();
- loader = new AltosMapLoader(map.map, this);
-
c.fill = GridBagConstraints.BOTH;
c.anchor = GridBagConstraints.CENTER;
c.insets = i;