aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/StaticMapsProvider.java
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/cgeo/geocaching/StaticMapsProvider.java')
-rw-r--r--main/src/cgeo/geocaching/StaticMapsProvider.java154
1 files changed, 77 insertions, 77 deletions
diff --git a/main/src/cgeo/geocaching/StaticMapsProvider.java b/main/src/cgeo/geocaching/StaticMapsProvider.java
index ae61112..0551fc0 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;
@@ -15,12 +14,18 @@ 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.schedulers.Schedulers;
+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;
@@ -35,9 +40,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
*/
@@ -51,69 +53,78 @@ 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 edge, final Parameters waypoints) {
+ return Observable.merge(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 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);
- }
- final HttpResponse httpResponse = Network.getRequest(GOOGLE_STATICMAP_URL, params);
+ 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) {
+ return Async.fromAction(new Action0() {
+ @Override
+ public void call() {
+ 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);
+ }
+ 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, Schedulers.io());
}
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();
+ final List<Observable<String>> downloaders = new LinkedList<Observable<String>>();
+
if (Settings.isStoreOfflineMaps() && cache.getCoords() != null) {
- storeCachePreviewMap(cache);
- storeCacheStaticMap(cache, edge, false);
+ downloaders.add(storeCachePreviewMap(cache));
+ downloaders.add(storeCacheStaticMap(cache, edge));
}
// 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, edge));
}
}
}
+
+ return Observable.merge(downloaders);
}
/**
@@ -124,44 +135,47 @@ 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 edge) {
LocalStorage.deleteFilesWithPrefix(cache.getGeocode(), MAP_FILENAME_PREFIX + WAYPOINT_PREFIX);
+ final List<Observable<String>> downloaders = new LinkedList<Observable<String>>();
for (Waypoint waypoint : cache.getWaypoints()) {
- storeWaypointStaticMap(cache.getGeocode(), edge, waypoint, false);
+ downloaders.add(storeWaypointStaticMap(cache.getGeocode(), edge, 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) {
+ final int edge = StaticMapsProvider.guessMaxDisplaySide();
+ return storeWaypointStaticMap(cache.getGeocode(), edge, 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 edge, 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);
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, edge, null);
}
+ return Observable.empty();
}
- public static void storeCacheStaticMap(final Geocache cache, final boolean waitForResult) {
+ public static Observable<String> storeCacheStaticMap(final Geocache cache) {
int edge = guessMaxDisplaySide();
- storeCacheStaticMap(cache, edge, waitForResult);
+ return storeCacheStaticMap(cache, edge);
}
- private static void storeCacheStaticMap(final Geocache cache, final int edge, final boolean waitForResult) {
+ private static Observable<String> storeCacheStaticMap(final Geocache cache, final int edge) {
final String latlonMap = cache.getCoords().format(Format.LAT_LON_DECDEGREE_COMMA);
final Parameters waypoints = new Parameters();
for (final Waypoint waypoint : cache.getWaypoints()) {
@@ -173,15 +187,15 @@ 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, edge, 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);
+ return downloadMap(cache.getGeocode(), 15, ROADMAP, markerUrl, PREFIX_PREVIEW, "shadow:false|", latlonMap, minSize, minSize, null);
}
private static int guessMaxDisplaySide() {
@@ -189,24 +203,10 @@ public final class StaticMapsProvider {
return Math.max(displaySize.x, displaySize.y) - 25;
}
- 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 edge,
+ final Parameters waypoints) {
+ return downloadDifferentZooms(geocode, markerUrl, prefix, latlonMap, edge, waypoints);
}
private static String getCacheMarkerUrl(final Geocache cache) {