diff options
author | Portree-Kid <keith.paterson@gmx.de> | 2012-04-12 18:37:54 +0200 |
---|---|---|
committer | Portree-Kid <keith.paterson@gmx.de> | 2012-04-12 18:37:54 +0200 |
commit | 5b5fb8ea1d949067bead8d6bbfd8a836a635d9fa (patch) | |
tree | 0c344ac532476fd7dff6805a518c94ccedbcb86f /main/src/cgeo/geocaching/CacheCache.java | |
parent | 2a11bc0780b7b77c8871478e201ff11a8cff38cd (diff) | |
parent | 925f3f6177d81de33cb231ac2afb91138df04cbb (diff) | |
download | cgeo-5b5fb8ea1d949067bead8d6bbfd8a836a635d9fa.zip cgeo-5b5fb8ea1d949067bead8d6bbfd8a836a635d9fa.tar.gz cgeo-5b5fb8ea1d949067bead8d6bbfd8a836a635d9fa.tar.bz2 |
Merge remote-tracking branch 'upstream/master' into master-new
Conflicts:
main/src/cgeo/geocaching/cgData.java
Diffstat (limited to 'main/src/cgeo/geocaching/CacheCache.java')
-rw-r--r-- | main/src/cgeo/geocaching/CacheCache.java | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/main/src/cgeo/geocaching/CacheCache.java b/main/src/cgeo/geocaching/CacheCache.java index a84f1bd..1ebb47f 100644 --- a/main/src/cgeo/geocaching/CacheCache.java +++ b/main/src/cgeo/geocaching/CacheCache.java @@ -1,10 +1,18 @@ 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 cgeo.geocaching.utils.Log; 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 +23,14 @@ public class CacheCache { private static final int MAX_CACHED_CACHES = 1000; final private LeastRecentlyUsedMap<String, cgCache> cachesCache; + final private RemoveHandler<cgCache> removeHandler; private static CacheCache instance = null; private CacheCache() { cachesCache = new LeastRecentlyUsedMap.LruCache<String, cgCache>(MAX_CACHED_CACHES); + removeHandler = new CacheRemoveHandler(); + cachesCache.setRemoveHandler(removeHandler); } public static CacheCache getInstance() { @@ -74,9 +85,34 @@ public class CacheCache { return cachesCache.get(geocode); } + public Set<String> getInViewport(final Long centerLat, final Long centerLon, final Long spanLat, final Long spanLon, final CacheType cacheType) { + final Set<String> geocodes = new HashSet<String>(); + 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; + } + @Override public String toString() { return StringUtils.join(cachesCache.keySet(), ' '); } + private class CacheRemoveHandler implements RemoveHandler<cgCache> { + + @Override + public void onRemove(cgCache removed) { + GCBase.removeFromTileCache(removed.getCoords()); + } + } + } |