diff options
| author | Bananeweizen <bananeweizen@gmx.de> | 2012-05-30 07:30:36 +0200 |
|---|---|---|
| committer | Bananeweizen <bananeweizen@gmx.de> | 2012-05-30 07:30:36 +0200 |
| commit | 1cc60d0d381939573b719a3091637c2cef5a6df8 (patch) | |
| tree | 8f74fa85cbccdbf08fe224a66ae7eeb46426370f /main/src | |
| parent | e8b6712d85938c49a82554ca27946456c8410c5d (diff) | |
| download | cgeo-1cc60d0d381939573b719a3091637c2cef5a6df8.zip cgeo-1cc60d0d381939573b719a3091637c2cef5a6df8.tar.gz cgeo-1cc60d0d381939573b719a3091637c2cef5a6df8.tar.bz2 | |
refactoring: clean up leaked interface of static maps provider
Diffstat (limited to 'main/src')
4 files changed, 48 insertions, 45 deletions
diff --git a/main/src/cgeo/geocaching/CacheDetailActivity.java b/main/src/cgeo/geocaching/CacheDetailActivity.java index f5e2f52..35f0e9a 100644 --- a/main/src/cgeo/geocaching/CacheDetailActivity.java +++ b/main/src/cgeo/geocaching/CacheDetailActivity.java @@ -46,7 +46,6 @@ import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.graphics.Bitmap; -import android.graphics.BitmapFactory; import android.graphics.Typeface; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; @@ -1830,7 +1829,7 @@ public class CacheDetailActivity extends AbstractActivity { } private Bitmap decode(final cgCache cache) { - return BitmapFactory.decodeFile(StaticMapsProvider.getMapFile(cache.getGeocode(), "preview", false).getPath()); + return StaticMapsProvider.getPreviewMap(cache.getGeocode()); } @Override diff --git a/main/src/cgeo/geocaching/StaticMapsActivity.java b/main/src/cgeo/geocaching/StaticMapsActivity.java index a95f4d2..7ddc4e0 100644 --- a/main/src/cgeo/geocaching/StaticMapsActivity.java +++ b/main/src/cgeo/geocaching/StaticMapsActivity.java @@ -135,42 +135,28 @@ public class StaticMapsActivity extends AbstractActivity { factory = new BitmapFactory(); } - for (int level = 1; level <= 5; level++) { - try { - if (waypoint_id != null) { - final Bitmap image = BitmapFactory.decodeFile(StaticMapsProvider.getMapFile(geocode, "wp" + waypoint_id + "_" + level, false).getPath()); - if (image != null) { - maps.add(image); - } - } else { - final Bitmap image = BitmapFactory.decodeFile(StaticMapsProvider.getMapFile(geocode, "" + level, false).getPath()); - if (image != null) { - maps.add(image); - } - } - } catch (Exception e) { - Log.e("StaticMapsActivity.LoadMapsThread.run.1: " + e.toString()); - } - } - - if (maps.isEmpty()) { + // try downloading 2 times + for (int trials = 0; trials < 2; trials++) { for (int level = 1; level <= 5; level++) { try { if (waypoint_id != null) { - final Bitmap image = BitmapFactory.decodeFile(StaticMapsProvider.getMapFile(geocode, "wp" + waypoint_id + "_" + level, false).getPath()); + final Bitmap image = StaticMapsProvider.getWaypointMap(geocode, waypoint_id, level); if (image != null) { maps.add(image); } } else { - final Bitmap image = BitmapFactory.decodeFile(StaticMapsProvider.getMapFile(geocode, "" + level, false).getPath()); + final Bitmap image = StaticMapsProvider.getCacheMap(geocode, level); if (image != null) { maps.add(image); } } } catch (Exception e) { - Log.e("StaticMapsActivity.LoadMapsThread.run.2: " + e.toString()); + Log.e("StaticMapsActivity.LoadMapsThread.run: " + e.toString()); } } + if (!maps.isEmpty()) { + break; + } } loadMapsHandler.sendMessage(Message.obtain()); @@ -201,13 +187,13 @@ public class StaticMapsActivity extends AbstractActivity { if (waypoint_id == null) { showToast(res.getString(R.string.info_storing_static_maps)); StaticMapsProvider.storeCacheStaticMap(cache, this, true); - return StaticMapsProvider.doesExistStaticMapForCache(geocode); + return StaticMapsProvider.hasStaticMapForCache(geocode); } final cgWaypoint waypoint = cache.getWaypointById(waypoint_id); if (waypoint != null) { showToast(res.getString(R.string.info_storing_static_maps)); StaticMapsProvider.storeWaypointStaticMap(cache, this, waypoint, true); - return StaticMapsProvider.doesExistStaticMapForWaypoint(geocode, waypoint_id); + return StaticMapsProvider.hasStaticMapForWaypoint(geocode, waypoint_id); } 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 1e013c5..cd17120 100644 --- a/main/src/cgeo/geocaching/StaticMapsProvider.java +++ b/main/src/cgeo/geocaching/StaticMapsProvider.java @@ -8,11 +8,14 @@ import cgeo.geocaching.network.Parameters; import cgeo.geocaching.utils.Log; 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.util.DisplayMetrics; import android.view.Display; import android.view.WindowManager; @@ -21,6 +24,7 @@ import java.io.File; import java.util.concurrent.TimeUnit; public class StaticMapsProvider { + private static final String PREFIX_PREVIEW = "preview"; private static final String GOOGLE_STATICMAP_URL = "http://maps.google.com/maps/api/staticmap"; private static final String SATELLITE = "satellite"; private static final String ROADMAP = "roadmap"; @@ -32,7 +36,7 @@ public class StaticMapsProvider { /** ThreadPool restricting this to 1 Thread. **/ private static final BlockingThreadPool pool = new BlockingThreadPool(1, Thread.MIN_PRIORITY); - public static File getMapFile(final String geocode, String prefix, final boolean createDirs) { + private static File getMapFile(final String geocode, String prefix, final boolean createDirs) { return LocalStorage.getStorageFile(geocode, MAP_FILENAME_PREFIX + prefix, false, createDirs); } @@ -48,7 +52,7 @@ public class StaticMapsProvider { final Parameters params = new Parameters( "center", latlonMap, "zoom", String.valueOf(zoom), - "size", width + "x" + height, + "size", String.valueOf(width) + 'x' + String.valueOf(height), "maptype", mapType, "markers", "icon:" + markerUrl + '|' + shadow + latlonMap, "sensor", "false"); @@ -80,7 +84,7 @@ public class StaticMapsProvider { downloadMaps(cache, display); } - public static void downloadMaps(cgCache cache, Display display) { + private static void downloadMaps(cgCache cache, Display display) { if ((!Settings.isStoreOfflineMaps() && !Settings.isStoreOfflineWpMaps()) || StringUtils.isBlank(cache.getGeocode())) { return; } @@ -114,7 +118,7 @@ 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); + downloadMaps(geocode, wpMarkerUrl, WAYPOINT_PREFIX + waypoint.getId() + '_', wpLatlonMap, edge, null, waitForResult); } public static void storeCacheStaticMap(cgCache cache, Activity activity, final boolean waitForResult) { @@ -130,7 +134,7 @@ public class StaticMapsProvider { continue; } final String wpMarkerUrl = getWpMarkerUrl(waypoint); - waypoints.put("markers", "icon:" + wpMarkerUrl + "|" + waypoint.getCoords().format(Format.LAT_LON_DECDEGREE_COMMA)); + waypoints.put("markers", "icon:" + wpMarkerUrl + '|' + waypoint.getCoords().format(Format.LAT_LON_DECDEGREE_COMMA)); } // download map images in separate background thread for higher performance final String cacheMarkerUrl = getCacheMarkerUrl(cache); @@ -145,7 +149,7 @@ public class StaticMapsProvider { display.getMetrics(metrics); final int width = metrics.widthPixels; final int height = (int) (110 * metrics.density); - downloadMap(cache.getGeocode(), 15, ROADMAP, markerUrl, "preview", "shadow:false|", latlonMap, width, height, null); + downloadMap(cache.getGeocode(), 15, ROADMAP, markerUrl, PREFIX_PREVIEW, "shadow:false|", latlonMap, width, height, null); } private static int guessMaxDisplaySide(Display display) { @@ -182,14 +186,15 @@ public class StaticMapsProvider { } private static String getCacheMarkerUrl(final cgCache cache) { - String type = cache.getType().id; + StringBuilder url = new StringBuilder(MARKERS_URL); + url.append("marker_cache_").append(cache.getType().id); if (cache.isFound()) { - type += "_found"; + url.append("_found"); } else if (cache.isDisabled()) { - type += "_disabled"; + url.append("_disabled"); } - - return MARKERS_URL + "marker_cache_" + type + ".png"; + url.append(".png"); + return url.toString(); } private static String getWpMarkerUrl(final cgWaypoint waypoint) { @@ -198,11 +203,12 @@ public class StaticMapsProvider { } public static void removeWpStaticMaps(int wp_id, final String geocode) { + if (wp_id <= 0) { + return; + } for (int level = 1; level <= 5; level++) { try { - if (wp_id > 0) { - StaticMapsProvider.getMapFile(geocode, WAYPOINT_PREFIX + wp_id + "_" + level, false).delete(); - } + StaticMapsProvider.getMapFile(geocode, WAYPOINT_PREFIX + wp_id + '_' + level, false).delete(); } catch (Exception e) { Log.e("StaticMapsProvider.removeWpStaticMaps: " + e.toString()); } @@ -215,9 +221,9 @@ public class StaticMapsProvider { * @param geocode * @return <code>true</code> if at least one mapfile exists; <code>false</code> otherwise */ - public static boolean doesExistStaticMapForCache(String geocode) { + public static boolean hasStaticMapForCache(String geocode) { for (int level = 1; level <= 5; level++) { - File mapFile = StaticMapsProvider.getMapFile(geocode, "" + level, false); + File mapFile = StaticMapsProvider.getMapFile(geocode, String.valueOf(level), false); if (mapFile != null && mapFile.exists()) { return true; } @@ -232,7 +238,7 @@ public class StaticMapsProvider { * @param waypointId * @return <code>true</code> if at least one mapfile exists; <code>false</code> otherwise */ - public static boolean doesExistStaticMapForWaypoint(String geocode, int waypointId) { + public static boolean hasStaticMapForWaypoint(String geocode, int waypointId) { for (int level = 1; level <= 5; level++) { File mapFile = StaticMapsProvider.getMapFile(geocode, WAYPOINT_PREFIX + waypointId + "_" + level, false); if (mapFile != null && mapFile.exists()) { @@ -241,4 +247,16 @@ public class StaticMapsProvider { } return false; } + + public static Bitmap getPreviewMap(final String geocode) { + return BitmapFactory.decodeFile(StaticMapsProvider.getMapFile(geocode, PREFIX_PREVIEW, false).getPath()); + } + + public static Bitmap getWaypointMap(final String geocode, int waypoint_id, int level) { + return BitmapFactory.decodeFile(StaticMapsProvider.getMapFile(geocode, WAYPOINT_PREFIX + waypoint_id + "_" + level, false).getPath()); + } + + public static Bitmap getCacheMap(final String geocode, int level) { + return BitmapFactory.decodeFile(StaticMapsProvider.getMapFile(geocode, String.valueOf(level), false).getPath()); + } } diff --git a/main/src/cgeo/geocaching/apps/cache/navi/AbstractStaticMapsApp.java b/main/src/cgeo/geocaching/apps/cache/navi/AbstractStaticMapsApp.java index 8e5d7d1..213e806 100644 --- a/main/src/cgeo/geocaching/apps/cache/navi/AbstractStaticMapsApp.java +++ b/main/src/cgeo/geocaching/apps/cache/navi/AbstractStaticMapsApp.java @@ -27,7 +27,7 @@ abstract class AbstractStaticMapsApp extends AbstractApp implements CacheNavigat protected static boolean hasStaticMap(cgCache cache) { String geocode = cache.getGeocode(); if (StringUtils.isNotEmpty(geocode) && cgeoapplication.getInstance().isOffline(geocode, null)) { - return StaticMapsProvider.doesExistStaticMapForCache(geocode); + return StaticMapsProvider.hasStaticMapForCache(geocode); } return false; } @@ -36,7 +36,7 @@ abstract class AbstractStaticMapsApp extends AbstractApp implements CacheNavigat String geocode = waypoint.getGeocode(); int id = waypoint.getId(); if (StringUtils.isNotEmpty(geocode) && cgeoapplication.getInstance().isOffline(geocode, null)) { - return StaticMapsProvider.doesExistStaticMapForWaypoint(geocode, id); + return StaticMapsProvider.hasStaticMapForWaypoint(geocode, id); } return false; } |
