From 867f3024ce13da0963306fd86242cd915a7858f8 Mon Sep 17 00:00:00 2001 From: Bananeweizen Date: Sun, 8 Jan 2012 12:52:41 +0100 Subject: refactoring: clean up code around static maps --- main/AndroidManifest.xml | 2 +- main/src/cgeo/geocaching/CacheCache.java | 4 +- main/src/cgeo/geocaching/CacheDetailActivity.java | 8 +- main/src/cgeo/geocaching/StaticMapsActivity.java | 147 +++++++++++++++++++++ main/src/cgeo/geocaching/StaticMapsProvider.java | 2 +- main/src/cgeo/geocaching/VisitCacheActivity.java | 4 +- .../geocaching/apps/cache/navi/StaticMapApp.java | 4 +- main/src/cgeo/geocaching/cgBase.java | 4 +- main/src/cgeo/geocaching/cgCache.java | 10 +- main/src/cgeo/geocaching/cgData.java | 7 +- main/src/cgeo/geocaching/cgeoapplication.java | 6 +- main/src/cgeo/geocaching/cgeosmaps.java | 147 --------------------- main/src/cgeo/geocaching/cgeowaypoint.java | 2 +- main/src/cgeo/geocaching/files/GPXImporter.java | 2 +- tests/src/cgeo/geocaching/cgeoApplicationTest.java | 2 +- 15 files changed, 174 insertions(+), 177 deletions(-) create mode 100644 main/src/cgeo/geocaching/StaticMapsActivity.java delete mode 100644 main/src/cgeo/geocaching/cgeosmaps.java diff --git a/main/AndroidManifest.xml b/main/AndroidManifest.xml index 23852a3..b94a4d7 100644 --- a/main/AndroidManifest.xml +++ b/main/AndroidManifest.xml @@ -173,7 +173,7 @@ diff --git a/main/src/cgeo/geocaching/CacheCache.java b/main/src/cgeo/geocaching/CacheCache.java index 3b27186..ea8d749 100644 --- a/main/src/cgeo/geocaching/CacheCache.java +++ b/main/src/cgeo/geocaching/CacheCache.java @@ -1,6 +1,6 @@ package cgeo.geocaching; -import cgeo.geocaching.cgData.StorageLocations; +import cgeo.geocaching.cgData.StorageLocation; import java.util.ArrayList; import java.util.HashMap; @@ -57,7 +57,7 @@ public class CacheCache { cachesCache.remove(cache.getGeocode()); } - cache.addStorageLocation(StorageLocations.CACHE); + cache.addStorageLocation(StorageLocation.CACHE); cachesCache.put(cache.getGeocode(), cache); } diff --git a/main/src/cgeo/geocaching/CacheDetailActivity.java b/main/src/cgeo/geocaching/CacheDetailActivity.java index 339219e..3b55bc9 100644 --- a/main/src/cgeo/geocaching/CacheDetailActivity.java +++ b/main/src/cgeo/geocaching/CacheDetailActivity.java @@ -1,6 +1,6 @@ package cgeo.geocaching; -import cgeo.geocaching.cgData.StorageLocations; +import cgeo.geocaching.cgData.StorageLocation; import cgeo.geocaching.activity.AbstractActivity; import cgeo.geocaching.activity.Progress; import cgeo.geocaching.apps.cache.GeneralAppsFactory; @@ -1657,7 +1657,7 @@ public class CacheDetailActivity extends AbstractActivity { @Override public void run() { - app.removeCacheFromCache(cache.getGeocode()); + cgeoapplication.removeCacheFromCache(cache.getGeocode()); search = cgBase.searchByGeocode(cache.getGeocode(), null, 0, true, handler); handler.sendEmptyMessage(0); @@ -1850,10 +1850,10 @@ public class CacheDetailActivity extends AbstractActivity { final TextView internalsText = (TextView) view.findViewById(R.id.debug_text); String sl = "Storage location: "; - if (cache.getStorageLocation().contains(StorageLocations.CACHE)) { + if (cache.getStorageLocation().contains(StorageLocation.CACHE)) { sl += "Cache "; } - if (cache.getStorageLocation().contains(StorageLocations.DATABASE)) { + if (cache.getStorageLocation().contains(StorageLocation.DATABASE)) { sl += "Database (" + cache.getListId() + ")"; } diff --git a/main/src/cgeo/geocaching/StaticMapsActivity.java b/main/src/cgeo/geocaching/StaticMapsActivity.java new file mode 100644 index 0000000..b33d17d --- /dev/null +++ b/main/src/cgeo/geocaching/StaticMapsActivity.java @@ -0,0 +1,147 @@ +package cgeo.geocaching; + +import cgeo.geocaching.activity.AbstractActivity; + +import org.apache.commons.collections.CollectionUtils; + +import android.app.ProgressDialog; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.os.Bundle; +import android.os.Handler; +import android.os.Message; +import android.util.Log; +import android.view.LayoutInflater; +import android.widget.ImageView; +import android.widget.LinearLayout; + +import java.util.ArrayList; +import java.util.List; + +public class StaticMapsActivity extends AbstractActivity { + + private final List maps = new ArrayList(); + private String geocode = null; + private LayoutInflater inflater = null; + private ProgressDialog waitDialog = null; + private LinearLayout smapsView = null; + private BitmapFactory factory = null; + private final Handler loadMapsHandler = new Handler() { + + @Override + public void handleMessage(Message msg) { + try { + if (CollectionUtils.isEmpty(maps)) { + if (waitDialog != null) { + waitDialog.dismiss(); + } + + showToast(res.getString(R.string.err_detail_not_load_map_static)); + + finish(); + return; + } else { + if (waitDialog != null) { + waitDialog.dismiss(); + } + + if (inflater == null) { + inflater = getLayoutInflater(); + } + + if (smapsView == null) { + smapsView = (LinearLayout) findViewById(R.id.maps_list); + } + smapsView.removeAllViews(); + + for (Bitmap image : maps) { + if (image != null) { + final ImageView map = (ImageView) inflater.inflate(R.layout.map_static_item, null); + map.setImageBitmap(image); + smapsView.addView(map); + } + } + } + } catch (Exception e) { + if (waitDialog != null) { + waitDialog.dismiss(); + } + Log.e(Settings.tag, "StaticMapsActivity.loadMapsHandler: " + e.toString()); + } + } + }; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setTheme(); + setContentView(R.layout.map_static); + setTitle(res.getString(R.string.map_static_title)); + + // get parameters + final Bundle extras = getIntent().getExtras(); + + // try to get data from extras + if (extras != null) { + geocode = extras.getString("geocode"); + } + + if (geocode == null) { + showToast("Sorry, c:geo forgot for what cache you want to load static maps."); + finish(); + return; + } + + waitDialog = ProgressDialog.show(this, null, res.getString(R.string.map_static_loading), true); + waitDialog.setCancelable(true); + + (new LoadMapsThread()).start(); + } + + @Override + public void onResume() { + super.onResume(); + + } + + private class LoadMapsThread extends Thread { + + @Override + public void run() { + try { + if (factory == null) { + factory = new BitmapFactory(); + } + + for (int level = 1; level <= 5; level++) { + try { + final Bitmap image = BitmapFactory.decodeFile(StaticMapsProvider.getMapFile(geocode, level, false).getPath()); + if (image != null) { + maps.add(image); + } + } catch (Exception e) { + Log.e(Settings.tag, "StaticMapsActivity.LoadMapsThread.run.1: " + e.toString()); + } + } + + if (maps.isEmpty()) { + for (int level = 1; level <= 5; level++) { + try { + final Bitmap image = BitmapFactory.decodeFile(StaticMapsProvider.getMapFile(geocode, level, false).getPath()); + if (image != null) { + maps.add(image); + } + } catch (Exception e) { + Log.e(Settings.tag, "StaticMapsActivity.LoadMapsThread.run.2: " + e.toString()); + } + } + } + + loadMapsHandler.sendMessage(new Message()); + } catch (Exception e) { + Log.e(Settings.tag, "StaticMapsActivity.LoadMapsThread.run: " + e.toString()); + } + } + } +} \ No newline at end of file diff --git a/main/src/cgeo/geocaching/StaticMapsProvider.java b/main/src/cgeo/geocaching/StaticMapsProvider.java index d37905b..3176c9f 100644 --- a/main/src/cgeo/geocaching/StaticMapsProvider.java +++ b/main/src/cgeo/geocaching/StaticMapsProvider.java @@ -90,7 +90,7 @@ public class StaticMapsProvider { private static void downloadMaps(final cgCache cache, final String latlonMap, final int edge, final String waypoints) { - Thread staticMapsThread = new Thread("getting static map") { + final Thread staticMapsThread = new Thread("getting static map") { @Override public void run() { downloadMapsInThread(cache, latlonMap, edge, waypoints); diff --git a/main/src/cgeo/geocaching/VisitCacheActivity.java b/main/src/cgeo/geocaching/VisitCacheActivity.java index 18ada72..d0e8a5e 100644 --- a/main/src/cgeo/geocaching/VisitCacheActivity.java +++ b/main/src/cgeo/geocaching/VisitCacheActivity.java @@ -710,9 +710,9 @@ public class VisitCacheActivity extends AbstractActivity implements DateDialog.D } if (cache != null) { - app.putCacheInCache(cache); + cgeoapplication.putCacheInCache(cache); } else { - app.removeCacheFromCache(geocode); + cgeoapplication.removeCacheFromCache(geocode); } } diff --git a/main/src/cgeo/geocaching/apps/cache/navi/StaticMapApp.java b/main/src/cgeo/geocaching/apps/cache/navi/StaticMapApp.java index 9c7064b..6a29895 100644 --- a/main/src/cgeo/geocaching/apps/cache/navi/StaticMapApp.java +++ b/main/src/cgeo/geocaching/apps/cache/navi/StaticMapApp.java @@ -5,7 +5,7 @@ import cgeo.geocaching.SearchResult; import cgeo.geocaching.cgCache; import cgeo.geocaching.cgGeo; import cgeo.geocaching.cgWaypoint; -import cgeo.geocaching.cgeosmaps; +import cgeo.geocaching.StaticMapsActivity; import cgeo.geocaching.activity.ActivityMixin; import cgeo.geocaching.geopoint.Geopoint; @@ -34,7 +34,7 @@ class StaticMapApp extends AbstractNavigationApp { } if (cache.getGeocode() != null) { - final Intent intent = new Intent(activity, cgeosmaps.class); + final Intent intent = new Intent(activity, StaticMapsActivity.class); intent.putExtra("geocode", cache.getGeocode().toUpperCase()); activity.startActivity(intent); return true; diff --git a/main/src/cgeo/geocaching/cgBase.java b/main/src/cgeo/geocaching/cgBase.java index 273b36b..69a35fe 100644 --- a/main/src/cgeo/geocaching/cgBase.java +++ b/main/src/cgeo/geocaching/cgBase.java @@ -2696,7 +2696,7 @@ public class cgBase { } app.markStored(cache.getGeocode(), listId); - app.removeCacheFromCache(cache.getGeocode()); + cgeoapplication.removeCacheFromCache(cache.getGeocode()); if (handler != null) { handler.sendMessage(new Message()); @@ -2709,7 +2709,7 @@ public class cgBase { public static void dropCache(final cgeoapplication app, final cgCache cache, final Handler handler) { try { app.markDropped(cache.getGeocode()); - app.removeCacheFromCache(cache.getGeocode()); + cgeoapplication.removeCacheFromCache(cache.getGeocode()); handler.sendMessage(new Message()); } catch (Exception e) { diff --git a/main/src/cgeo/geocaching/cgCache.java b/main/src/cgeo/geocaching/cgCache.java index b61ae23..220f2fa 100644 --- a/main/src/cgeo/geocaching/cgCache.java +++ b/main/src/cgeo/geocaching/cgCache.java @@ -1,6 +1,6 @@ package cgeo.geocaching; -import cgeo.geocaching.cgData.StorageLocations; +import cgeo.geocaching.cgData.StorageLocation; import cgeo.geocaching.activity.IAbstractActivity; import cgeo.geocaching.connector.ConnectorFactory; import cgeo.geocaching.connector.GCConnector; @@ -92,7 +92,7 @@ public class cgCache implements ICache { private boolean statusCheckedView = false; private String directionImg = ""; private String nameForSorting; - private final EnumSet storageLocation = EnumSet.of(StorageLocations.HEAP); + private final EnumSet storageLocation = EnumSet.of(StorageLocation.HEAP); private static final Pattern NUMBER_PATTERN = Pattern.compile("\\d+"); @@ -1058,7 +1058,7 @@ public class cgCache implements ICache { /** * @return the storageLocation */ - public EnumSet getStorageLocation() { + public EnumSet getStorageLocation() { return storageLocation; } @@ -1066,7 +1066,7 @@ public class cgCache implements ICache { * @param storageLocation * the storageLocation to set */ - public void addStorageLocation(StorageLocations sl) { + public void addStorageLocation(StorageLocation sl) { this.storageLocation.add(sl); } @@ -1121,7 +1121,7 @@ public class cgCache implements ICache { if (waypoint.isUserDefined()) { waypoints.remove(index); cgeoapplication.getInstance().deleteWaypoint(waypoint.getId()); - cgeoapplication.getInstance().removeCacheFromCache(geocode); + cgeoapplication.removeCacheFromCache(geocode); return true; } return false; diff --git a/main/src/cgeo/geocaching/cgData.java b/main/src/cgeo/geocaching/cgData.java index 0363d33..f6039c1 100644 --- a/main/src/cgeo/geocaching/cgData.java +++ b/main/src/cgeo/geocaching/cgData.java @@ -40,8 +40,7 @@ import java.util.regex.Pattern; public class cgData { - // TODO: remove plural - public enum StorageLocations { + public enum StorageLocation { HEAP, CACHE, DATABASE, @@ -1254,7 +1253,7 @@ public class cgData { // remember this cache in the caches cache. it is highly likely that we will need it in a few moments and // this way we also remove any stale instance from the caches cache - cgeoapplication.getInstance().putCacheInCache(cache); + cgeoapplication.putCacheInCache(cache); ContentValues values = new ContentValues(); @@ -1990,7 +1989,7 @@ public class cgData { if (loadFlags.contains(LoadFlag.LOADOFFLINELOG)) { cache.setLogOffline(hasLogOffline(cache.getGeocode())); } - cache.addStorageLocation(StorageLocations.DATABASE); + cache.addStorageLocation(StorageLocation.DATABASE); caches.add(cache); } while (cursor.moveToNext()); diff --git a/main/src/cgeo/geocaching/cgeoapplication.java b/main/src/cgeo/geocaching/cgeoapplication.java index 3bfe69a..021bfca 100644 --- a/main/src/cgeo/geocaching/cgeoapplication.java +++ b/main/src/cgeo/geocaching/cgeoapplication.java @@ -286,13 +286,11 @@ public class cgeoapplication extends Application { return trackable; } - @SuppressWarnings("static-method") - public void removeCacheFromCache(final String geocode) { + public static void removeCacheFromCache(final String geocode) { CacheCache.getInstance().removeCacheFromCache(geocode); } - @SuppressWarnings("static-method") - public void putCacheInCache(final cgCache cache) { + public static void putCacheInCache(final cgCache cache) { CacheCache.getInstance().putCacheInCache(cache); } diff --git a/main/src/cgeo/geocaching/cgeosmaps.java b/main/src/cgeo/geocaching/cgeosmaps.java deleted file mode 100644 index 965542f..0000000 --- a/main/src/cgeo/geocaching/cgeosmaps.java +++ /dev/null @@ -1,147 +0,0 @@ -package cgeo.geocaching; - -import cgeo.geocaching.activity.AbstractActivity; - -import org.apache.commons.collections.CollectionUtils; - -import android.app.ProgressDialog; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.os.Bundle; -import android.os.Handler; -import android.os.Message; -import android.util.Log; -import android.view.LayoutInflater; -import android.widget.ImageView; -import android.widget.LinearLayout; - -import java.util.ArrayList; -import java.util.List; - -public class cgeosmaps extends AbstractActivity { - - private List maps = new ArrayList(); - private String geocode = null; - private LayoutInflater inflater = null; - private ProgressDialog waitDialog = null; - private LinearLayout smapsView = null; - private BitmapFactory factory = null; - private Handler loadMapsHandler = new Handler() { - - @Override - public void handleMessage(Message msg) { - try { - if (CollectionUtils.isEmpty(maps)) { - if (waitDialog != null) { - waitDialog.dismiss(); - } - - showToast(res.getString(R.string.err_detail_not_load_map_static)); - - finish(); - return; - } else { - if (waitDialog != null) { - waitDialog.dismiss(); - } - - if (inflater == null) { - inflater = getLayoutInflater(); - } - - if (smapsView == null) { - smapsView = (LinearLayout) findViewById(R.id.maps_list); - } - smapsView.removeAllViews(); - - for (Bitmap image : maps) { - if (image != null) { - final ImageView map = (ImageView) inflater.inflate(R.layout.map_static_item, null); - map.setImageBitmap(image); - smapsView.addView(map); - } - } - } - } catch (Exception e) { - if (waitDialog != null) { - waitDialog.dismiss(); - } - Log.e(Settings.tag, "cgeosmaps.loadMapsHandler: " + e.toString()); - } - } - }; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - setTheme(); - setContentView(R.layout.map_static); - setTitle(res.getString(R.string.map_static_title)); - - // get parameters - Bundle extras = getIntent().getExtras(); - - // try to get data from extras - if (extras != null) { - geocode = extras.getString("geocode"); - } - - if (geocode == null) { - showToast("Sorry, c:geo forgot for what cache you want to load static maps."); - finish(); - return; - } - - waitDialog = ProgressDialog.show(this, null, res.getString(R.string.map_static_loading), true); - waitDialog.setCancelable(true); - - (new loadMaps()).start(); - } - - @Override - public void onResume() { - super.onResume(); - - } - - private class loadMaps extends Thread { - - @Override - public void run() { - try { - if (factory == null) { - factory = new BitmapFactory(); - } - - for (int level = 1; level <= 5; level++) { - try { - final Bitmap image = BitmapFactory.decodeFile(StaticMapsProvider.getMapFile(geocode, level, false).getPath()); - if (image != null) { - maps.add(image); - } - } catch (Exception e) { - Log.e(Settings.tag, "cgeosmaps.loadMaps.run.1: " + e.toString()); - } - } - - if (maps.isEmpty()) { - for (int level = 1; level <= 5; level++) { - try { - final Bitmap image = BitmapFactory.decodeFile(StaticMapsProvider.getMapFile(geocode, level, false).getPath()); - if (image != null) { - maps.add(image); - } - } catch (Exception e) { - Log.e(Settings.tag, "cgeosmaps.loadMaps.run.2: " + e.toString()); - } - } - } - - loadMapsHandler.sendMessage(new Message()); - } catch (Exception e) { - Log.e(Settings.tag, "cgeosmaps.loadMaps.run: " + e.toString()); - } - } - } -} \ No newline at end of file diff --git a/main/src/cgeo/geocaching/cgeowaypoint.java b/main/src/cgeo/geocaching/cgeowaypoint.java index 4abdfd3..218c61b 100644 --- a/main/src/cgeo/geocaching/cgeowaypoint.java +++ b/main/src/cgeo/geocaching/cgeowaypoint.java @@ -297,7 +297,7 @@ public class cgeowaypoint extends AbstractActivity { public void onClick(View arg0) { if (app.deleteWaypoint(id)) { - app.removeCacheFromCache(geocode); + cgeoapplication.removeCacheFromCache(geocode); finish(); return; diff --git a/main/src/cgeo/geocaching/files/GPXImporter.java b/main/src/cgeo/geocaching/files/GPXImporter.java index d855d69..9ced045 100644 --- a/main/src/cgeo/geocaching/files/GPXImporter.java +++ b/main/src/cgeo/geocaching/files/GPXImporter.java @@ -150,7 +150,7 @@ public class GPXImporter { int storedCaches = 0; for (cgCache cache : caches) { // remove from cache because a cache might be re-imported - app.removeCacheFromCache(cache.getGeocode()); + cgeoapplication.removeCacheFromCache(cache.getGeocode()); app.addCacheToSearch(search, cache); if (progressHandler.isCancelled()) { diff --git a/tests/src/cgeo/geocaching/cgeoApplicationTest.java b/tests/src/cgeo/geocaching/cgeoApplicationTest.java index e868055..21f14aa 100644 --- a/tests/src/cgeo/geocaching/cgeoApplicationTest.java +++ b/tests/src/cgeo/geocaching/cgeoApplicationTest.java @@ -280,7 +280,7 @@ public class cgeoApplicationTest extends ApplicationTestCase { /** Remove cache from DB and cache to ensure that the cache is not loaded from the database */ private static void deleteCacheFromDBAndLogout(String geocode) { cgeoapplication.getInstance().dropCaches(Arrays.asList(geocode)); - cgeoapplication.getInstance().removeCacheFromCache(geocode); + cgeoapplication.removeCacheFromCache(geocode); cgBase.logout(); // Modify login data to avoid an automatic login again -- cgit v1.1