Skip to content

Commit

Permalink
Merge pull request cgeo#2550 from marco-jacob/wpImagesWithHash
Browse files Browse the repository at this point in the history
fixes cgeo#2507 - Waypoints only stored if not existing
  • Loading branch information
marco-dev committed Mar 8, 2013
2 parents b48ad9b + a47a2ab commit 3b28333
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 23 deletions.
15 changes: 11 additions & 4 deletions main/src/cgeo/geocaching/EditWaypointActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -488,11 +488,18 @@ protected Void doInBackground(Void... params) {
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);
Expand Down
7 changes: 5 additions & 2 deletions main/src/cgeo/geocaching/StaticMapsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ public void run() {
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);
}
Expand Down Expand Up @@ -186,8 +187,10 @@ private boolean downloadStaticMaps() {
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;
Expand Down
75 changes: 61 additions & 14 deletions main/src/cgeo/geocaching/StaticMapsProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,29 @@ public static void downloadMaps(Geocache 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 (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);
}
}

Expand All @@ -124,8 +140,10 @@ private static void storeWaypointStaticMap(final String geocode, int edge, Waypo
}
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) {
Expand Down Expand Up @@ -210,13 +228,15 @@ private static String getWpMarkerUrl(final Waypoint waypoint) {
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);
}
Expand Down Expand Up @@ -250,25 +270,52 @@ public static boolean hasStaticMap(final Geocache cache) {
* 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;
}
}
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) {
Expand Down
9 changes: 9 additions & 0 deletions main/src/cgeo/geocaching/Waypoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,13 @@ public void setVisited(boolean visited) {
public boolean isVisited() {
return visited;
}

public int calculateStaticMapsHashcode() {
long hash = 0;
if (coords != null) {
hash = coords.hashCode();
}
hash = hash ^ waypointType.markerId;
return (int) hash;
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -34,9 +34,8 @@ protected static boolean hasStaticMap(Waypoint waypoint) {
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;
}
Expand Down

0 comments on commit 3b28333

Please sign in to comment.