From ad85facd78ae215cc7b6324440df2c0d1f74fdac Mon Sep 17 00:00:00 2001 From: rsudev Date: Thu, 12 Apr 2012 11:44:05 +0200 Subject: Implements cache for live-map tiles --- main/src/cgeo/geocaching/CacheCache.java | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'main/src/cgeo/geocaching/CacheCache.java') diff --git a/main/src/cgeo/geocaching/CacheCache.java b/main/src/cgeo/geocaching/CacheCache.java index a84f1bd..daee9ed 100644 --- a/main/src/cgeo/geocaching/CacheCache.java +++ b/main/src/cgeo/geocaching/CacheCache.java @@ -1,10 +1,17 @@ package cgeo.geocaching; import cgeo.geocaching.cgData.StorageLocation; +import cgeo.geocaching.connector.gc.GCBase; +import cgeo.geocaching.enumerations.CacheType; +import cgeo.geocaching.geopoint.Viewport; import cgeo.geocaching.utils.LeastRecentlyUsedMap; +import cgeo.geocaching.utils.LeastRecentlyUsedMap.RemoveHandler; import org.apache.commons.lang3.StringUtils; +import java.util.HashSet; +import java.util.Set; + /** * Cache for Caches. Every cache is stored in memory while c:geo is active to * speed up the app and to minimize network request - which are slow. @@ -15,11 +22,14 @@ public class CacheCache { private static final int MAX_CACHED_CACHES = 1000; final private LeastRecentlyUsedMap cachesCache; + final private RemoveHandler removeHandler; private static CacheCache instance = null; private CacheCache() { cachesCache = new LeastRecentlyUsedMap.LruCache(MAX_CACHED_CACHES); + removeHandler = new CacheRemoveHandler(); + cachesCache.setRemoveHandler(removeHandler); } public static CacheCache getInstance() { @@ -74,9 +84,29 @@ public class CacheCache { return cachesCache.get(geocode); } + public Set getInViewport(final Long centerLat, final Long centerLon, final Long spanLat, final Long spanLon, final CacheType cacheType) { + Set geocodes = new HashSet(); + for (cgCache cache : cachesCache.values()) { + if (Viewport.isCacheInViewPort(centerLat.intValue(), centerLon.intValue(), spanLat.intValue(), spanLon.intValue(), cache.getCoords())) { + if (CacheType.ALL == cacheType || cache.getType() == cacheType) { + geocodes.add(cache.getGeocode()); + } + } + } + return geocodes; + } + @Override public String toString() { return StringUtils.join(cachesCache.keySet(), ' '); } + private class CacheRemoveHandler implements RemoveHandler { + + @Override + public void onRemove(cgCache removed) { + GCBase.removeFromTileCache(removed.getCoords()); + } + } + } -- cgit v1.1 From 925f3f6177d81de33cb231ac2afb91138df04cbb Mon Sep 17 00:00:00 2001 From: Samuel Tardieu Date: Thu, 12 Apr 2012 18:18:16 +0200 Subject: Partial fix: do not crash if a cache has no coordinates It looks like from time to time some caches are sent with null coordinates, at least when browsing the live map. This change prevents a NullPointerException from being thrown and logs this as an error in the log. This issue still needs to be addressed, as the root cause of the problem is yet unknown. --- main/src/cgeo/geocaching/CacheCache.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'main/src/cgeo/geocaching/CacheCache.java') diff --git a/main/src/cgeo/geocaching/CacheCache.java b/main/src/cgeo/geocaching/CacheCache.java index daee9ed..1ebb47f 100644 --- a/main/src/cgeo/geocaching/CacheCache.java +++ b/main/src/cgeo/geocaching/CacheCache.java @@ -6,6 +6,7 @@ import cgeo.geocaching.enumerations.CacheType; import cgeo.geocaching.geopoint.Viewport; import cgeo.geocaching.utils.LeastRecentlyUsedMap; import cgeo.geocaching.utils.LeastRecentlyUsedMap.RemoveHandler; +import cgeo.geocaching.utils.Log; import org.apache.commons.lang3.StringUtils; @@ -85,12 +86,17 @@ public class CacheCache { } public Set getInViewport(final Long centerLat, final Long centerLon, final Long spanLat, final Long spanLon, final CacheType cacheType) { - Set geocodes = new HashSet(); - for (cgCache cache : cachesCache.values()) { - if (Viewport.isCacheInViewPort(centerLat.intValue(), centerLon.intValue(), spanLat.intValue(), spanLon.intValue(), cache.getCoords())) { - if (CacheType.ALL == cacheType || cache.getType() == cacheType) { - geocodes.add(cache.getGeocode()); - } + final Set geocodes = new HashSet(); + for (final cgCache cache : cachesCache.values()) { + if (cache.getCoords() == null) { + // FIXME: this kludge must be removed, it is only present to help us debug the cases where + // caches contain null coordinates. + Log.e(Settings.tag, "CacheCache.getInViewport: got cache with null coordinates: " + cache.getGeocode()); + continue; + } + if ((CacheType.ALL == cacheType || cache.getType() == cacheType) && + Viewport.isCacheInViewPort(centerLat.intValue(), centerLon.intValue(), spanLat.intValue(), spanLon.intValue(), cache.getCoords())) { + geocodes.add(cache.getGeocode()); } } return geocodes; -- cgit v1.1