diff options
5 files changed, 88 insertions, 23 deletions
diff --git a/main/src/cgeo/geocaching/EditWaypointActivity.java b/main/src/cgeo/geocaching/EditWaypointActivity.java index e886dea..7f011fc 100644 --- a/main/src/cgeo/geocaching/EditWaypointActivity.java +++ b/main/src/cgeo/geocaching/EditWaypointActivity.java @@ -488,11 +488,18 @@ public class EditWaypointActivity extends AbstractActivity { waypoint.setId(id); Geocache cache = cgData.loadCache(geocode, LoadFlags.LOAD_WAYPOINTS); - if (null != cache && cache.addOrChangeWaypoint(waypoint, true)) { + if (cache == null) { + finishHandler.sendEmptyMessage(SAVE_ERROR); + return null; + } + Waypoint oldWaypoint = cache.getWaypointById(id); + if (cache.addOrChangeWaypoint(waypoint, true)) { cgData.saveCache(cache, EnumSet.of(SaveFlag.SAVE_DB)); - StaticMapsProvider.removeWpStaticMaps(id, geocode); - if (Settings.isStoreOfflineWpMaps()) { - StaticMapsProvider.storeWaypointStaticMap(cache, waypoint, false); + if (!StaticMapsProvider.hasAllStaticMapsForWaypoint(geocode, waypoint)) { + StaticMapsProvider.removeWpStaticMaps(oldWaypoint, geocode); + if (Settings.isStoreOfflineWpMaps()) { + StaticMapsProvider.storeWaypointStaticMap(cache, waypoint, false); + } } final RadioButton modifyLocal = (RadioButton) findViewById(R.id.modify_cache_coordinates_local); final RadioButton modifyBoth = (RadioButton) findViewById(R.id.modify_cache_coordinates_local_and_remote); diff --git a/main/src/cgeo/geocaching/StaticMapsActivity.java b/main/src/cgeo/geocaching/StaticMapsActivity.java index d7cef65..005ee9e 100644 --- a/main/src/cgeo/geocaching/StaticMapsActivity.java +++ b/main/src/cgeo/geocaching/StaticMapsActivity.java @@ -134,7 +134,8 @@ public class StaticMapsActivity extends AbstractActivity { for (int level = 1; level <= 5; level++) { try { if (waypoint_id != null) { - final Bitmap image = StaticMapsProvider.getWaypointMap(geocode, waypoint_id, level); + final Geocache cache = cgData.loadCache(geocode, LoadFlags.LOAD_CACHE_OR_DB); + final Bitmap image = StaticMapsProvider.getWaypointMap(geocode, cache.getWaypointById(waypoint_id), level); if (image != null) { maps.add(image); } @@ -186,8 +187,10 @@ public class StaticMapsActivity extends AbstractActivity { final Waypoint waypoint = cache.getWaypointById(waypoint_id); if (waypoint != null) { showToast(res.getString(R.string.info_storing_static_maps)); + // refresh always removes old waypoint files + StaticMapsProvider.removeWpStaticMaps(waypoint, geocode); StaticMapsProvider.storeWaypointStaticMap(cache, waypoint, true); - return StaticMapsProvider.hasStaticMapForWaypoint(geocode, waypoint_id); + return StaticMapsProvider.hasStaticMapForWaypoint(geocode, waypoint); } showToast(res.getString(R.string.err_detail_not_load_map_static)); return false; diff --git a/main/src/cgeo/geocaching/StaticMapsProvider.java b/main/src/cgeo/geocaching/StaticMapsProvider.java index 6feacc2..8e853d6 100644 --- a/main/src/cgeo/geocaching/StaticMapsProvider.java +++ b/main/src/cgeo/geocaching/StaticMapsProvider.java @@ -95,13 +95,29 @@ public class StaticMapsProvider { 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 (Waypoint waypoint : cache.getWaypoints()) { - storeWaypointStaticMap(cache.getGeocode(), edge, waypoint, false); + 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); } } @@ -124,8 +140,10 @@ 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(Geocache cache, final boolean waitForResult) { @@ -210,13 +228,15 @@ public class StaticMapsProvider { 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); } @@ -250,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; } @@ -263,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) { diff --git a/main/src/cgeo/geocaching/Waypoint.java b/main/src/cgeo/geocaching/Waypoint.java index 4b014a6..53160e5 100644 --- a/main/src/cgeo/geocaching/Waypoint.java +++ b/main/src/cgeo/geocaching/Waypoint.java @@ -269,4 +269,13 @@ public class Waypoint implements IWaypoint, Comparable<Waypoint> { public boolean isVisited() { return visited; } + + public int calculateStaticMapsHashcode() { + long hash = 0; + if (coords != null) { + hash = coords.hashCode(); + } + hash = hash ^ waypointType.markerId; + return (int) hash; + } } diff --git a/main/src/cgeo/geocaching/apps/cache/navi/AbstractStaticMapsApp.java b/main/src/cgeo/geocaching/apps/cache/navi/AbstractStaticMapsApp.java index d089e82..d898d7e 100644 --- a/main/src/cgeo/geocaching/apps/cache/navi/AbstractStaticMapsApp.java +++ b/main/src/cgeo/geocaching/apps/cache/navi/AbstractStaticMapsApp.java @@ -1,11 +1,11 @@ package cgeo.geocaching.apps.cache.navi; +import cgeo.geocaching.Geocache; import cgeo.geocaching.ILogable; import cgeo.geocaching.R; import cgeo.geocaching.StaticMapsActivity; import cgeo.geocaching.StaticMapsProvider; import cgeo.geocaching.Waypoint; -import cgeo.geocaching.Geocache; import cgeo.geocaching.cgData; import cgeo.geocaching.activity.ActivityMixin; import cgeo.geocaching.apps.AbstractApp; @@ -34,9 +34,8 @@ abstract class AbstractStaticMapsApp extends AbstractApp implements CacheNavigat return false; } String geocode = waypoint.getGeocode(); - int id = waypoint.getId(); if (StringUtils.isNotEmpty(geocode) && cgData.isOffline(geocode, null)) { - return StaticMapsProvider.hasStaticMapForWaypoint(geocode, id); + return StaticMapsProvider.hasStaticMapForWaypoint(geocode, waypoint); } return false; } |
