diff options
Diffstat (limited to 'main/src/cgeo/geocaching/StaticMapsProvider.java')
-rw-r--r-- | main/src/cgeo/geocaching/StaticMapsProvider.java | 216 |
1 files changed, 111 insertions, 105 deletions
diff --git a/main/src/cgeo/geocaching/StaticMapsProvider.java b/main/src/cgeo/geocaching/StaticMapsProvider.java index eaab159..030c379 100644 --- a/main/src/cgeo/geocaching/StaticMapsProvider.java +++ b/main/src/cgeo/geocaching/StaticMapsProvider.java @@ -1,7 +1,6 @@ package cgeo.geocaching; import cgeo.geocaching.compatibility.Compatibility; -import cgeo.geocaching.concurrent.BlockingThreadPool; import cgeo.geocaching.files.LocalStorage; import cgeo.geocaching.geopoint.GeopointFormatter.Format; import cgeo.geocaching.network.Network; @@ -9,22 +8,30 @@ import cgeo.geocaching.network.Parameters; import cgeo.geocaching.settings.Settings; import cgeo.geocaching.utils.FileUtils; import cgeo.geocaching.utils.Log; +import cgeo.geocaching.utils.RxUtils; import ch.boye.httpclientandroidlib.HttpResponse; + import org.apache.commons.lang3.StringUtils; import org.eclipse.jdt.annotation.NonNull; +import rx.Observable; +import rx.functions.Action0; +import rx.util.async.Async; + import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Point; import java.io.File; -import java.util.concurrent.TimeUnit; +import java.util.LinkedList; +import java.util.List; public final class StaticMapsProvider { static final int MAPS_LEVEL_MAX = 5; private static final String PREFIX_PREVIEW = "preview"; private static final String GOOGLE_STATICMAP_URL = "http://maps.google.com/maps/api/staticmap"; + private static final int GOOGLE_MAX_ZOOM = 20; private static final String SATELLITE = "satellite"; private static final String ROADMAP = "roadmap"; private static final String WAYPOINT_PREFIX = "wp"; @@ -34,9 +41,6 @@ public final class StaticMapsProvider { /** We assume there is no real usable image with less than 1k. */ private static final int MIN_MAP_IMAGE_BYTES = 1000; - /** ThreadPool restricting this to 1 Thread. **/ - private static final BlockingThreadPool POOL = new BlockingThreadPool(1, Thread.MIN_PRIORITY); - /** * max size in free API version: https://developers.google.com/maps/documentation/staticmaps/#Imagesizes */ @@ -50,69 +54,85 @@ public final class StaticMapsProvider { return LocalStorage.getStorageFile(geocode, MAP_FILENAME_PREFIX + prefix, false, createDirs); } - private static void downloadDifferentZooms(final String geocode, final String markerUrl, final String prefix, final String latlonMap, final int edge, final Parameters waypoints) { - downloadMap(geocode, 20, SATELLITE, markerUrl, prefix + '1', "", latlonMap, edge, edge, waypoints); - downloadMap(geocode, 18, SATELLITE, markerUrl, prefix + '2', "", latlonMap, edge, edge, waypoints); - downloadMap(geocode, 16, ROADMAP, markerUrl, prefix + '3', "", latlonMap, edge, edge, waypoints); - downloadMap(geocode, 14, ROADMAP, markerUrl, prefix + '4', "", latlonMap, edge, edge, waypoints); - downloadMap(geocode, 11, ROADMAP, markerUrl, prefix + '5', "", latlonMap, edge, edge, waypoints); + private static Observable<String> downloadDifferentZooms(final String geocode, final String markerUrl, final String prefix, final String latlonMap, final int width, final int height, final Parameters waypoints) { + return Observable.merge(downloadMap(geocode, 20, SATELLITE, markerUrl, prefix + '1', "", latlonMap, width, height, waypoints), + downloadMap(geocode, 18, SATELLITE, markerUrl, prefix + '2', "", latlonMap, width, height, waypoints), + downloadMap(geocode, 16, ROADMAP, markerUrl, prefix + '3', "", latlonMap, width, height, waypoints), + downloadMap(geocode, 14, ROADMAP, markerUrl, prefix + '4', "", latlonMap, width, height, waypoints), + downloadMap(geocode, 11, ROADMAP, markerUrl, prefix + '5', "", latlonMap, width, height, waypoints)); } - private static void downloadMap(final String geocode, final int zoom, final String mapType, final String markerUrl, final String prefix, final String shadow, final String latlonMap, final int width, final int height, final Parameters waypoints) { - final Parameters params = new Parameters( - "center", latlonMap, - "zoom", String.valueOf(zoom), - "size", String.valueOf(limitSize(width)) + 'x' + String.valueOf(limitSize(height)), - "maptype", mapType, - "markers", "icon:" + markerUrl + '|' + shadow + latlonMap, - "sensor", "false"); - if (waypoints != null) { - params.addAll(waypoints); + private static Observable<String> downloadMap(final String geocode, final int zoom, final String mapType, final String markerUrl, final String prefix, final String shadow, final String latlonMap, final int width, final int height, final Parameters waypoints) { + int scale = 1; + if (width > GOOGLE_MAPS_MAX_SIZE) { + scale = 2; } - final HttpResponse httpResponse = Network.getRequest(GOOGLE_STATICMAP_URL, params); + final float aspectRatio = width / (float) height; + final int requestWidth = Math.min(width / scale, GOOGLE_MAPS_MAX_SIZE); + final int requestHeight = (aspectRatio > 1) ? Math.round(requestWidth / aspectRatio) : requestWidth; + final int requestScale = scale; + final int requestZoom = Math.min((scale == 2) ? zoom + 1 : zoom, GOOGLE_MAX_ZOOM); + return Async.fromAction(new Action0() { + @Override + public void call() { + final Parameters params = new Parameters( + "center", latlonMap, + "zoom", String.valueOf(requestZoom), + "size", String.valueOf(requestWidth) + 'x' + String.valueOf(requestHeight), + "scale", String.valueOf(requestScale), + "maptype", mapType, + "markers", "icon:" + markerUrl + '|' + shadow + latlonMap, + "sensor", "false"); + if (waypoints != null) { + params.addAll(waypoints); + } + final HttpResponse httpResponse = Network.getRequest(GOOGLE_STATICMAP_URL, params); - if (httpResponse == null) { - Log.e("StaticMapsProvider.downloadMap: httpResponse is null"); - return; - } - if (httpResponse.getStatusLine().getStatusCode() != 200) { - Log.d("StaticMapsProvider.downloadMap: httpResponseCode = " + httpResponse.getStatusLine().getStatusCode()); - return; - } - final File file = getMapFile(geocode, prefix, true); - if (LocalStorage.saveEntityToFile(httpResponse, file)) { - // Delete image if it has no contents - final long fileSize = file.length(); - if (fileSize < MIN_MAP_IMAGE_BYTES) { - FileUtils.deleteIgnoringFailure(file); + if (httpResponse == null) { + Log.e("StaticMapsProvider.downloadMap: httpResponse is null"); + return; + } + if (httpResponse.getStatusLine().getStatusCode() != 200) { + Log.d("StaticMapsProvider.downloadMap: httpResponseCode = " + httpResponse.getStatusLine().getStatusCode()); + return; + } + final File file = getMapFile(geocode, prefix, true); + if (LocalStorage.saveEntityToFile(httpResponse, file)) { + // Delete image if it has no contents + final long fileSize = file.length(); + if (fileSize < MIN_MAP_IMAGE_BYTES) { + FileUtils.deleteIgnoringFailure(file); + } + } } - } + }, prefix, RxUtils.networkScheduler); } - private static int limitSize(final int imageSize) { - return Math.min(imageSize, GOOGLE_MAPS_MAX_SIZE); - } - - public static void downloadMaps(final Geocache cache) { + public static Observable<String> downloadMaps(final Geocache cache) { if ((!Settings.isStoreOfflineMaps() && !Settings.isStoreOfflineWpMaps()) || StringUtils.isBlank(cache.getGeocode())) { - return; + return Observable.empty(); } - int edge = guessMaxDisplaySide(); + // TODO Check if this is also OK, was width -25 + final Point displaySize = Compatibility.getDisplaySize(); + + final List<Observable<String>> downloaders = new LinkedList<>(); if (Settings.isStoreOfflineMaps() && cache.getCoords() != null) { - storeCachePreviewMap(cache); - storeCacheStaticMap(cache, edge, false); + downloaders.add(storeCachePreviewMap(cache)); + downloaders.add(storeCacheStaticMap(cache, displaySize.x, displaySize.y)); } // clean old and download static maps for waypoints if one is missing if (Settings.isStoreOfflineWpMaps()) { for (final Waypoint waypoint : cache.getWaypoints()) { if (!hasAllStaticMapsForWaypoint(cache.getGeocode(), waypoint)) { - refreshAllWpStaticMaps(cache, edge); + downloaders.add(refreshAllWpStaticMaps(cache, displaySize.x, displaySize.y)); } } } + + return Observable.merge(downloaders); } /** @@ -123,44 +143,49 @@ public final class StaticMapsProvider { * @param edge * The boundings */ - private static void refreshAllWpStaticMaps(final Geocache cache, final int edge) { + private static Observable<String> refreshAllWpStaticMaps(final Geocache cache, final int width, final int height) { LocalStorage.deleteFilesWithPrefix(cache.getGeocode(), MAP_FILENAME_PREFIX + WAYPOINT_PREFIX); - for (Waypoint waypoint : cache.getWaypoints()) { - storeWaypointStaticMap(cache.getGeocode(), edge, waypoint, false); + final List<Observable<String>> downloaders = new LinkedList<>(); + for (final Waypoint waypoint : cache.getWaypoints()) { + downloaders.add(storeWaypointStaticMap(cache.getGeocode(), width, height, waypoint)); } + return Observable.merge(downloaders); } - public static void storeWaypointStaticMap(final Geocache cache, final Waypoint waypoint, final boolean waitForResult) { - int edge = StaticMapsProvider.guessMaxDisplaySide(); - storeWaypointStaticMap(cache.getGeocode(), edge, waypoint, waitForResult); + public static Observable<String> storeWaypointStaticMap(final Geocache cache, final Waypoint waypoint) { + // TODO Check if this is also OK, was width -25 + final Point displaySize = Compatibility.getDisplaySize(); + return storeWaypointStaticMap(cache.getGeocode(), displaySize.x, displaySize.y, waypoint); } - private static void storeWaypointStaticMap(final String geocode, final int edge, final Waypoint waypoint, final boolean waitForResult) { + private static Observable<String> storeWaypointStaticMap(final String geocode, final int width, final int height, final Waypoint waypoint) { if (geocode == null) { Log.e("storeWaypointStaticMap - missing input parameter geocode"); - return; + return Observable.empty(); } if (waypoint == null) { Log.e("storeWaypointStaticMap - missing input parameter waypoint"); - return; + return Observable.empty(); } if (waypoint.getCoords() == null) { - return; + return Observable.empty(); } - String wpLatlonMap = waypoint.getCoords().format(Format.LAT_LON_DECDEGREE_COMMA); - String wpMarkerUrl = getWpMarkerUrl(waypoint); + final String wpLatlonMap = waypoint.getCoords().format(Format.LAT_LON_DECDEGREE_COMMA); + final String wpMarkerUrl = getWpMarkerUrl(waypoint); if (!hasAllStaticMapsForWaypoint(geocode, waypoint)) { // download map images in separate background thread for higher performance - downloadMaps(geocode, wpMarkerUrl, WAYPOINT_PREFIX + waypoint.getId() + '_' + waypoint.getStaticMapsHashcode() + "_", wpLatlonMap, edge, null, waitForResult); + return downloadMaps(geocode, wpMarkerUrl, WAYPOINT_PREFIX + waypoint.getId() + '_' + waypoint.getStaticMapsHashcode() + "_", wpLatlonMap, width, height, null); } + return Observable.empty(); } - public static void storeCacheStaticMap(final Geocache cache, final boolean waitForResult) { - int edge = guessMaxDisplaySide(); - storeCacheStaticMap(cache, edge, waitForResult); + public static Observable<String> storeCacheStaticMap(final Geocache cache) { + // TODO Check if this is also OK, was width -25 + final Point displaySize = Compatibility.getDisplaySize(); + return storeCacheStaticMap(cache, displaySize.x, displaySize.y); } - private static void storeCacheStaticMap(final Geocache cache, final int edge, final boolean waitForResult) { + private static Observable<String> storeCacheStaticMap(final Geocache cache, final int width, final int height) { final String latlonMap = cache.getCoords().format(Format.LAT_LON_DECDEGREE_COMMA); final Parameters waypoints = new Parameters(); for (final Waypoint waypoint : cache.getWaypoints()) { @@ -172,48 +197,29 @@ public final class StaticMapsProvider { } // download map images in separate background thread for higher performance final String cacheMarkerUrl = getCacheMarkerUrl(cache); - downloadMaps(cache.getGeocode(), cacheMarkerUrl, "", latlonMap, edge, waypoints, waitForResult); + return downloadMaps(cache.getGeocode(), cacheMarkerUrl, "", latlonMap, width, height, waypoints); } - public static void storeCachePreviewMap(final Geocache cache) { + public static Observable<String> storeCachePreviewMap(final Geocache cache) { final String latlonMap = cache.getCoords().format(Format.LAT_LON_DECDEGREE_COMMA); final Point displaySize = Compatibility.getDisplaySize(); final int minSize = Math.min(displaySize.x, displaySize.y); final String markerUrl = MARKERS_URL + "my_location_mdpi.png"; - downloadMap(cache.getGeocode(), 15, ROADMAP, markerUrl, PREFIX_PREVIEW, "shadow:false|", latlonMap, minSize, minSize, null); - } - - private static int guessMaxDisplaySide() { - Point displaySize = Compatibility.getDisplaySize(); - return Math.max(displaySize.x, displaySize.y) - 25; + return downloadMap(cache.getGeocode(), 15, ROADMAP, markerUrl, PREFIX_PREVIEW, "shadow:false|", latlonMap, minSize, minSize, null); } - private static void downloadMaps(final String geocode, final String markerUrl, final String prefix, final String latlonMap, final int edge, - final Parameters waypoints, final boolean waitForResult) { - if (waitForResult) { - downloadDifferentZooms(geocode, markerUrl, prefix, latlonMap, edge, waypoints); - } - else { - final Runnable currentTask = new Runnable() { - @Override - public void run() { - downloadDifferentZooms(geocode, markerUrl, prefix, latlonMap, edge, waypoints); - } - }; - try { - POOL.add(currentTask, 20, TimeUnit.SECONDS); - } catch (InterruptedException e) { - Log.e("StaticMapsProvider.downloadMaps error adding task", e); - } - } + private static Observable<String> downloadMaps(final String geocode, final String markerUrl, final String prefix, + final String latlonMap, final int width, final int height, + final Parameters waypoints) { + return downloadDifferentZooms(geocode, markerUrl, prefix, latlonMap, width, height, waypoints); } private static String getCacheMarkerUrl(final Geocache cache) { - StringBuilder url = new StringBuilder(MARKERS_URL); + final StringBuilder url = new StringBuilder(MARKERS_URL); url.append("marker_cache_").append(cache.getType().id); if (cache.isFound()) { url.append("_found"); - } else if (cache.isDisabled()) { + } else if (cache.isDisabled() || cache.isArchived()) { url.append("_disabled"); } url.append(".png"); @@ -221,7 +227,7 @@ public final class StaticMapsProvider { } private static String getWpMarkerUrl(final Waypoint waypoint) { - String type = waypoint.getWaypointType() != null ? waypoint.getWaypointType().id : null; + final String type = waypoint.getWaypointType() != null ? waypoint.getWaypointType().id : null; return MARKERS_URL + "marker_waypoint_" + type + ".png"; } @@ -229,8 +235,8 @@ public final class StaticMapsProvider { if (waypoint == null) { return; } - int waypointId = waypoint.getId(); - int waypointMapHash = waypoint.getStaticMapsHashcode(); + final int waypointId = waypoint.getId(); + final int waypointMapHash = waypoint.getStaticMapsHashcode(); for (int level = 1; level <= MAPS_LEVEL_MAX; level++) { final File mapFile = StaticMapsProvider.getMapFile(geocode, WAYPOINT_PREFIX + waypointId + "_" + waypointMapHash + '_' + level, false); if (!FileUtils.delete(mapFile)) { @@ -251,7 +257,7 @@ public final class StaticMapsProvider { return false; } for (int level = 1; level <= MAPS_LEVEL_MAX; level++) { - File mapFile = StaticMapsProvider.getMapFile(geocode, String.valueOf(level), false); + final File mapFile = StaticMapsProvider.getMapFile(geocode, String.valueOf(level), false); if (mapFile.exists()) { return true; } @@ -267,10 +273,10 @@ public final class StaticMapsProvider { * @return <code>true</code> if at least one map file exists; <code>false</code> otherwise */ public static boolean hasStaticMapForWaypoint(final String geocode, final Waypoint waypoint) { - int waypointId = waypoint.getId(); - int waypointMapHash = waypoint.getStaticMapsHashcode(); + final int waypointId = waypoint.getId(); + final int waypointMapHash = waypoint.getStaticMapsHashcode(); for (int level = 1; level <= MAPS_LEVEL_MAX; level++) { - File mapFile = StaticMapsProvider.getMapFile(geocode, WAYPOINT_PREFIX + waypointId + "_" + waypointMapHash + "_" + level, false); + final File mapFile = StaticMapsProvider.getMapFile(geocode, WAYPOINT_PREFIX + waypointId + "_" + waypointMapHash + "_" + level, false); if (mapFile.exists()) { return true; } @@ -286,11 +292,11 @@ public final class StaticMapsProvider { * @return <code>true</code> if all map files exist; <code>false</code> otherwise */ public static boolean hasAllStaticMapsForWaypoint(final String geocode, final Waypoint waypoint) { - int waypointId = waypoint.getId(); - int waypointMapHash = waypoint.getStaticMapsHashcode(); + final int waypointId = waypoint.getId(); + final int waypointMapHash = waypoint.getStaticMapsHashcode(); for (int level = 1; level <= MAPS_LEVEL_MAX; level++) { - File mapFile = StaticMapsProvider.getMapFile(geocode, WAYPOINT_PREFIX + waypointId + "_" + waypointMapHash + "_" + level, false); - boolean mapExists = mapFile.exists(); + final File mapFile = StaticMapsProvider.getMapFile(geocode, WAYPOINT_PREFIX + waypointId + "_" + waypointMapHash + "_" + level, false); + final boolean mapExists = mapFile.exists(); if (!mapExists) { return false; } @@ -303,8 +309,8 @@ public final class StaticMapsProvider { } public static Bitmap getWaypointMap(final String geocode, final Waypoint waypoint, final int level) { - int waypointId = waypoint.getId(); - int waypointMapHash = waypoint.getStaticMapsHashcode(); + final int waypointId = waypoint.getId(); + final int waypointMapHash = waypoint.getStaticMapsHashcode(); return decodeFile(StaticMapsProvider.getMapFile(geocode, WAYPOINT_PREFIX + waypointId + "_" + waypointMapHash + "_" + level, false)); } |