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.java156
1 files changed, 98 insertions, 58 deletions
diff --git a/main/src/cgeo/geocaching/StaticMapsProvider.java b/main/src/cgeo/geocaching/StaticMapsProvider.java
index 87a04fa..8e853d6 100644
--- a/main/src/cgeo/geocaching/StaticMapsProvider.java
+++ b/main/src/cgeo/geocaching/StaticMapsProvider.java
@@ -1,5 +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;
@@ -12,10 +13,10 @@ import ch.boye.httpclientandroidlib.HttpResponse;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
-import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
+import android.graphics.Point;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;
@@ -61,30 +62,25 @@ public class StaticMapsProvider {
}
final HttpResponse httpResponse = Network.getRequest(GOOGLE_STATICMAP_URL, params);
- if (httpResponse != null) {
- if (httpResponse.getStatusLine().getStatusCode() == 200) {
- 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) {
- file.delete();
- }
- }
- } else {
- Log.d("StaticMapsProvider.downloadMap: httpResponseCode = " + httpResponse.getStatusLine().getStatusCode());
- }
- } else {
+ 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) {
+ file.delete();
+ }
}
}
- public static void downloadMaps(cgCache cache) {
- final Display display = ((WindowManager) cgeoapplication.getInstance().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
- downloadMaps(cache, display);
- }
-
- private static void downloadMaps(cgCache cache, Display display) {
+ public static void downloadMaps(Geocache cache) {
if (cache == null) {
Log.e("downloadMaps - missing input parameter cache");
return;
@@ -92,29 +88,45 @@ public class StaticMapsProvider {
if ((!Settings.isStoreOfflineMaps() && !Settings.isStoreOfflineWpMaps()) || StringUtils.isBlank(cache.getGeocode())) {
return;
}
- int edge = guessMaxDisplaySide(display);
+ int edge = guessMaxDisplaySide();
if (Settings.isStoreOfflineMaps() && cache.getCoords() != null) {
storeCachePreviewMap(cache);
storeCacheStaticMap(cache, edge, false);
}
- // download static map for current waypoints
+ // clean old and download static maps for waypoints if one is missing
if (Settings.isStoreOfflineWpMaps() && CollectionUtils.isNotEmpty(cache.getWaypoints())) {
- // remove all waypoint static map files due to origin cache waypoint id changed on saveCache
- LocalStorage.deleteFilesWithPrefix(cache.getGeocode(), MAP_FILENAME_PREFIX + WAYPOINT_PREFIX);
- for (cgWaypoint waypoint : cache.getWaypoints()) {
- storeWaypointStaticMap(cache.getGeocode(), edge, waypoint, false);
+ for (Waypoint waypoint : cache.getWaypoints()) {
+ if (!hasAllStaticMapsForWaypoint(cache.getGeocode(), waypoint)) {
+ refreshAllWpStaticMaps(cache, edge);
+ }
}
+
+ }
+ }
+
+ /**
+ * Deletes and download all Waypoints static maps.
+ *
+ * @param cache
+ * The cache instance
+ * @param edge
+ * The boundings
+ */
+ private static void refreshAllWpStaticMaps(Geocache cache, int edge) {
+ LocalStorage.deleteFilesWithPrefix(cache.getGeocode(), MAP_FILENAME_PREFIX + WAYPOINT_PREFIX);
+ for (Waypoint waypoint : cache.getWaypoints()) {
+ storeWaypointStaticMap(cache.getGeocode(), edge, waypoint, false);
}
}
- public static void storeWaypointStaticMap(cgCache cache, Activity activity, cgWaypoint waypoint, boolean waitForResult) {
- int edge = StaticMapsProvider.guessMaxDisplaySide(activity);
+ public static void storeWaypointStaticMap(Geocache cache, Waypoint waypoint, boolean waitForResult) {
+ int edge = StaticMapsProvider.guessMaxDisplaySide();
storeWaypointStaticMap(cache.getGeocode(), edge, waypoint, waitForResult);
}
- private static void storeWaypointStaticMap(final String geocode, int edge, cgWaypoint waypoint, final boolean waitForResult) {
+ private static void storeWaypointStaticMap(final String geocode, int edge, Waypoint waypoint, final boolean waitForResult) {
if (geocode == null) {
Log.e("storeWaypointStaticMap - missing input parameter geocode");
return;
@@ -128,19 +140,21 @@ public class StaticMapsProvider {
}
String wpLatlonMap = waypoint.getCoords().format(Format.LAT_LON_DECDEGREE_COMMA);
String wpMarkerUrl = getWpMarkerUrl(waypoint);
- // download map images in separate background thread for higher performance
- downloadMaps(geocode, wpMarkerUrl, WAYPOINT_PREFIX + waypoint.getId() + '_', wpLatlonMap, edge, null, waitForResult);
+ if (!hasAllStaticMapsForWaypoint(geocode, waypoint)) {
+ // download map images in separate background thread for higher performance
+ downloadMaps(geocode, wpMarkerUrl, WAYPOINT_PREFIX + waypoint.getId() + '_' + waypoint.calculateStaticMapsHashcode() + "_", wpLatlonMap, edge, null, waitForResult);
+ }
}
- public static void storeCacheStaticMap(cgCache cache, Activity activity, final boolean waitForResult) {
- int edge = guessMaxDisplaySide(activity);
+ public static void storeCacheStaticMap(Geocache cache, final boolean waitForResult) {
+ int edge = guessMaxDisplaySide();
storeCacheStaticMap(cache, edge, waitForResult);
}
- private static void storeCacheStaticMap(final cgCache cache, final int edge, final boolean waitForResult) {
+ private static void storeCacheStaticMap(final Geocache cache, final int edge, final boolean waitForResult) {
final String latlonMap = cache.getCoords().format(Format.LAT_LON_DECDEGREE_COMMA);
final Parameters waypoints = new Parameters();
- for (final cgWaypoint waypoint : cache.getWaypoints()) {
+ for (final Waypoint waypoint : cache.getWaypoints()) {
if (waypoint.getCoords() == null) {
continue;
}
@@ -152,34 +166,31 @@ public class StaticMapsProvider {
downloadMaps(cache.getGeocode(), cacheMarkerUrl, "", latlonMap, edge, waypoints, waitForResult);
}
- public static void storeCachePreviewMap(final cgCache cache) {
+ public static void storeCachePreviewMap(final Geocache cache) {
if (cache == null) {
Log.e("storeCachePreviewMap - missing input parameter cache");
return;
}
final String latlonMap = cache.getCoords().format(Format.LAT_LON_DECDEGREE_COMMA);
- final String markerUrl = MARKERS_URL + "my_location_mdpi.png";
final Display display = ((WindowManager) cgeoapplication.getInstance().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
final int width = metrics.widthPixels;
final int height = (int) (110 * metrics.density);
+ final String markerUrl = MARKERS_URL + "my_location_mdpi.png";
downloadMap(cache.getGeocode(), 15, ROADMAP, markerUrl, PREFIX_PREVIEW, "shadow:false|", latlonMap, width, height, null);
}
- private static int guessMaxDisplaySide(Display display) {
- final int maxWidth = display.getWidth() - 25;
- final int maxHeight = display.getHeight() - 25;
+ private static int guessMaxDisplaySide() {
+ Point displaySize = Compatibility.getDisplaySize();
+ final int maxWidth = displaySize.x - 25;
+ final int maxHeight = displaySize.y - 25;
if (maxWidth > maxHeight) {
return maxWidth;
}
return maxHeight;
}
- private static int guessMaxDisplaySide(Activity activity) {
- return guessMaxDisplaySide(((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay());
- }
-
private static void downloadMaps(final String geocode, final String markerUrl, final String prefix, final String latlonMap, final int edge,
final Parameters waypoints, boolean waitForResult) {
if (waitForResult) {
@@ -195,12 +206,12 @@ public class StaticMapsProvider {
try {
pool.add(currentTask, 20, TimeUnit.SECONDS);
} catch (InterruptedException e) {
- Log.e("StaticMapsProvider.downloadMaps error adding task: " + e.toString());
+ Log.e("StaticMapsProvider.downloadMaps error adding task", e);
}
}
}
- private static String getCacheMarkerUrl(final cgCache cache) {
+ private static String getCacheMarkerUrl(final Geocache cache) {
StringBuilder url = new StringBuilder(MARKERS_URL);
url.append("marker_cache_").append(cache.getType().id);
if (cache.isFound()) {
@@ -212,20 +223,22 @@ public class StaticMapsProvider {
return url.toString();
}
- private static String getWpMarkerUrl(final cgWaypoint waypoint) {
+ private static String getWpMarkerUrl(final Waypoint waypoint) {
String type = waypoint.getWaypointType() != null ? waypoint.getWaypointType().id : null;
return MARKERS_URL + "marker_waypoint_" + type + ".png";
}
- public static void removeWpStaticMaps(int wp_id, final String geocode) {
- if (wp_id <= 0) {
+ public static void removeWpStaticMaps(Waypoint waypoint, final String geocode) {
+ if (waypoint == null) {
return;
}
+ int waypointId = waypoint.getId();
+ int waypointMapHash = waypoint.calculateStaticMapsHashcode();
for (int level = 1; level <= 5; level++) {
try {
- StaticMapsProvider.getMapFile(geocode, WAYPOINT_PREFIX + wp_id + '_' + level, false).delete();
+ StaticMapsProvider.getMapFile(geocode, WAYPOINT_PREFIX + waypointId + "_" + waypointMapHash + '_' + level, false).delete();
} catch (Exception e) {
- Log.e("StaticMapsProvider.removeWpStaticMaps: " + e.toString());
+ Log.e("StaticMapsProvider.removeWpStaticMaps", e);
}
}
}
@@ -236,7 +249,7 @@ public class StaticMapsProvider {
* @param cache
* @return <code>true</code> if at least one mapfile exists; <code>false</code> otherwise
*/
- public static boolean hasStaticMap(final cgCache cache) {
+ public static boolean hasStaticMap(final Geocache cache) {
if (cache == null) {
return false;
}
@@ -257,12 +270,14 @@ public class StaticMapsProvider {
* Checks if at least one map file exists for the given geocode and waypoint ID.
*
* @param geocode
- * @param waypointId
+ * @param waypoint
* @return <code>true</code> if at least one mapfile exists; <code>false</code> otherwise
*/
- public static boolean hasStaticMapForWaypoint(String geocode, int waypointId) {
+ public static boolean hasStaticMapForWaypoint(String geocode, Waypoint waypoint) {
+ int waypointId = waypoint.getId();
+ int waypointMapHash = waypoint.calculateStaticMapsHashcode();
for (int level = 1; level <= 5; level++) {
- File mapFile = StaticMapsProvider.getMapFile(geocode, WAYPOINT_PREFIX + waypointId + "_" + level, false);
+ File mapFile = StaticMapsProvider.getMapFile(geocode, WAYPOINT_PREFIX + waypointId + "_" + waypointMapHash + "_" + level, false);
if (mapFile != null && mapFile.exists()) {
return true;
}
@@ -270,12 +285,37 @@ public class StaticMapsProvider {
return false;
}
+ /**
+ * Checks if at least one map file exists for the given geocode and waypoint ID.
+ *
+ * @param geocode
+ * @param waypoint
+ * @return <code>true</code> if at least one mapfile exists; <code>false</code> otherwise
+ */
+ public static boolean hasAllStaticMapsForWaypoint(String geocode, Waypoint waypoint) {
+ int waypointId = waypoint.getId();
+ int waypointMapHash = waypoint.calculateStaticMapsHashcode();
+ for (int level = 1; level <= 5; level++) {
+ File mapFile = StaticMapsProvider.getMapFile(geocode, WAYPOINT_PREFIX + waypointId + "_" + waypointMapHash + "_" + level, false);
+ if (mapFile == null) {
+ return false;
+ }
+ boolean mapExists = mapFile.exists();
+ if (!mapExists) {
+ return false;
+ }
+ }
+ return true;
+ }
+
public static Bitmap getPreviewMap(final String geocode) {
return decodeFile(StaticMapsProvider.getMapFile(geocode, PREFIX_PREVIEW, false));
}
- public static Bitmap getWaypointMap(final String geocode, int waypoint_id, int level) {
- return decodeFile(StaticMapsProvider.getMapFile(geocode, WAYPOINT_PREFIX + waypoint_id + "_" + level, false));
+ public static Bitmap getWaypointMap(final String geocode, Waypoint waypoint, int level) {
+ int waypointId = waypoint.getId();
+ int waypointMapHash = waypoint.calculateStaticMapsHashcode();
+ return decodeFile(StaticMapsProvider.getMapFile(geocode, WAYPOINT_PREFIX + waypointId + "_" + waypointMapHash + "_" + level, false));
}
public static Bitmap getCacheMap(final String geocode, int level) {