diff options
Diffstat (limited to 'main/src/cgeo/geocaching/maps/CGeoMap.java')
| -rw-r--r-- | main/src/cgeo/geocaching/maps/CGeoMap.java | 131 |
1 files changed, 78 insertions, 53 deletions
diff --git a/main/src/cgeo/geocaching/maps/CGeoMap.java b/main/src/cgeo/geocaching/maps/CGeoMap.java index ce9d4e4..cf90430 100644 --- a/main/src/cgeo/geocaching/maps/CGeoMap.java +++ b/main/src/cgeo/geocaching/maps/CGeoMap.java @@ -85,6 +85,19 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto /** max. number of caches displayed in the Live Map */ public static final int MAX_CACHES = 500; + /**Controls the behaviour of the map*/ + public enum MapMode { + /** Live Map where caches are loaded from online */ + LIVE_ONLINE, + /** Live Map where caches are loaded only from database */ + LIVE_OFFLINE, + /** Map around some coordinates */ + COORDS, + /** Map with a single cache (no reload on move) */ + SINGLE, + /** Map with a list of caches (no reload on move) */ + LIST + } /** Handler Messages */ private static final int HIDE_PROGRESS = 0; private static final int SHOW_PROGRESS = 1; @@ -99,6 +112,8 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto private static final String EXTRAS_WPTTYPE = "wpttype"; private static final String EXTRAS_MAPSTATE = "mapstate"; private static final String EXTRAS_SEARCH = "search"; + private static final String EXTRAS_MAP_MODE = "map_mode"; + private static final int MENU_SELECT_MAPVIEW = 1; private static final int MENU_MAP_LIVE = 2; private static final int MENU_STORE_CACHES = 3; @@ -129,7 +144,8 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto private WaypointType waypointTypeIntent = null; private int[] mapStateIntent = null; // status data - private SearchResult search = null; + /** Last search result used for displaying header */ + private SearchResult lastSearchResult = null; private String[] tokens = null; private boolean noMapTokenShowed = false; // map status data @@ -160,7 +176,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto private static final int[][] INSET_USERMODIFIEDCOORDS = { { 21, 28, 0, 0 }, { 19, 25, 0, 0 } }; // bottom right, 12x12 / 26x26 private static final int[][] INSET_PERSONALNOTE = { { 0, 28, 21, 0 }, { 0, 25, 19, 0 } }; // bottom left, 12x12 / 26x26 - private static SparseArray<LayerDrawable> overlaysCache = new SparseArray<LayerDrawable>(); + private SparseArray<LayerDrawable> overlaysCache = new SparseArray<LayerDrawable>(); private int cachesCnt = 0; /** List of caches in the viewport */ private LeastRecentlyUsedSet<cgCache> caches = null; @@ -173,8 +189,11 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto private long detailProgressTime = 0L; // views private ImageSwitcher myLocSwitch = null; + + /**Controls the map behaviour*/ + private MapMode mapMode = null; // other things - private boolean live = true; // live map (live, dead) or rest (displaying caches on map) + // private boolean live = true; // live map (live, dead) or rest (displaying caches on map) private boolean liveChanged = false; // previous state for loadTimer private boolean centered = false; // if map is already centered private boolean alreadyCentered = false; // -""- for setting my location @@ -205,7 +224,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto // set title final StringBuilder title = new StringBuilder(); - if (live) { + if (mapMode == MapMode.LIVE_ONLINE) { title.append(res.getString(R.string.map_live)); } else { title.append(mapTitle); @@ -220,8 +239,8 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto title.append(']'); } - if (Settings.isDebug() && search != null && StringUtils.isNotBlank(search.getUrl())) { - title.append('[').append(search.getUrl()).append(']'); + if (Settings.isDebug() && lastSearchResult != null && StringUtils.isNotBlank(lastSearchResult.getUrl())) { + title.append('[').append(lastSearchResult.getUrl()).append(']'); } ActivityMixin.setTitle(activity, title.toString()); @@ -363,6 +382,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto // Get parameters from the intent final Bundle extras = activity.getIntent().getExtras(); if (extras != null) { + mapMode = (MapMode) extras.get(EXTRAS_MAP_MODE); searchIntent = (SearchResult) extras.getParcelable(EXTRAS_SEARCH); geocodeIntent = extras.getString(EXTRAS_GEOCODE); coordsIntent = (Geopoint) extras.getParcelable(EXTRAS_COORDS); @@ -370,6 +390,9 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto mapStateIntent = extras.getIntArray(EXTRAS_MAPSTATE); mapTitle = extras.getString(EXTRAS_MAP_TITLE); } + else { + mapMode = Settings.isLiveMap() ? MapMode.LIVE_ONLINE : MapMode.LIVE_OFFLINE; + } if (StringUtils.isBlank(mapTitle)) { mapTitle = res.getString(R.string.map_map); } @@ -429,11 +452,8 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto mapView.getMapController().setZoom(Settings.getMapZoom()); mapView.getMapController().setCenter(Settings.getMapCenter()); - // live map, if no arguments are given - live = (searchIntent == null && geocodeIntent == null && coordsIntent == null); - if (null == mapStateIntent) { - followMyLocation = live; + followMyLocation = mapMode == MapMode.LIVE_OFFLINE || mapMode == MapMode.LIVE_ONLINE; } else { followMyLocation = 1 == mapStateIntent[3]; if ((overlayCaches.getCircles() ? 1 : 0) != mapStateIntent[4]) { @@ -523,6 +543,8 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto mapView.destroyDrawingCache(); } + overlaysCache.clear(); + super.onPause(); } @@ -579,19 +601,14 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto } item = menu.findItem(MENU_MAP_LIVE); // live map - if (live) { - if (Settings.isLiveMap()) { - item.setTitle(res.getString(R.string.map_live_disable)); - } else { - item.setTitle(res.getString(R.string.map_live_enable)); - } + if (mapMode == MapMode.LIVE_ONLINE) { + item.setTitle(res.getString(R.string.map_live_disable)); } else { - item.setEnabled(false); item.setTitle(res.getString(R.string.map_live_enable)); } final Set<String> geocodesInViewport = getGeocodesForCachesInViewport(); - menu.findItem(MENU_STORE_CACHES).setEnabled(live && !isLoading() && CollectionUtils.isNotEmpty(geocodesInViewport) && app.hasUnsavedCaches(new SearchResult(geocodesInViewport))); + menu.findItem(MENU_STORE_CACHES).setEnabled(isLiveMode() && !isLoading() && CollectionUtils.isNotEmpty(geocodesInViewport) && app.hasUnsavedCaches(new SearchResult(geocodesInViewport))); item = menu.findItem(MENU_CIRCLE_MODE); // show circles if (overlayCaches != null && overlayCaches.getCircles()) { @@ -601,9 +618,9 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto } item = menu.findItem(MENU_AS_LIST); - item.setEnabled(live && !isLoading()); + item.setEnabled(isLiveMode() && !isLoading()); - menu.findItem(SUBMENU_STRATEGY).setEnabled(live); + menu.findItem(SUBMENU_STRATEGY).setEnabled(isLiveMode()); } catch (Exception e) { Log.e("cgeomap.onPrepareOptionsMenu: " + e.toString()); } @@ -611,6 +628,10 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto return true; } + private boolean isLiveMode() { + return mapMode == MapMode.LIVE_OFFLINE || mapMode == MapMode.LIVE_ONLINE; + } + @Override public boolean onOptionsItemSelected(MenuItem item) { final int id = item.getItemId(); @@ -621,8 +642,9 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto return true; case MENU_MAP_LIVE: Settings.setLiveMap(!Settings.isLiveMap()); + mapMode = Settings.isLiveMap() ? MapMode.LIVE_ONLINE : MapMode.LIVE_OFFLINE; liveChanged = true; - search = null; + lastSearchResult = null; searchIntent = null; ActivityMixin.invalidateOptionsMenu(activity); return true; @@ -797,6 +819,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto } mapIntent.putExtra(EXTRAS_WPTTYPE, waypointTypeIntent != null ? waypointTypeIntent.id : null); mapIntent.putExtra(EXTRAS_MAP_TITLE, mapTitle); + mapIntent.putExtra(EXTRAS_MAP_MODE, mapMode); final int[] mapState = currentMapState(); if (mapState != null) { @@ -1013,7 +1036,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto if (liveChanged) { moved = true; - } else if (live && Settings.isLiveMap() && !downloaded) { + } else if (mapMode == MapMode.LIVE_ONLINE && !downloaded) { moved = true; } else if (viewport == null) { moved = true; @@ -1124,38 +1147,31 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto @Override public void run() { - /** - * True if we are currently showing the live map or a single points through coordsIntent. - */ - final boolean isLiveMapOrCoords = searchIntent == null && geocodeIntent == null; - try { showProgressHandler.sendEmptyMessage(SHOW_PROGRESS); loadThreadRun = System.currentTimeMillis(); + SearchResult searchResult; // stage 1 - pull and render from the DB only for live map - if (isLiveMapOrCoords) { - // live map - if (!live || !Settings.isLiveMap()) { - search = new SearchResult(app.getStoredInViewport(viewport, Settings.getCacheType())); - } else { - search = new SearchResult(app.getCachedInViewport(viewport, Settings.getCacheType())); - } + if (mapMode == MapMode.LIVE_ONLINE) { + searchResult = new SearchResult(app.getCachedInViewport(viewport, Settings.getCacheType())); + } else if (mapMode == MapMode.LIVE_OFFLINE) { + searchResult = new SearchResult(app.getStoredInViewport(viewport, Settings.getCacheType())); } else { // map started from another activity - search = new SearchResult(searchIntent); + searchResult = new SearchResult(searchIntent); if (geocodeIntent != null) { - search.addGeocode(geocodeIntent); + searchResult.addGeocode(geocodeIntent); } } - if (search != null) { - downloaded = true; - Set<cgCache> cachesFromSearchResult = search.getCachesFromSearchResult(LoadFlags.LOAD_WAYPOINTS); - caches.addAll(cachesFromSearchResult); - } + downloaded = true; + Set<cgCache> cachesFromSearchResult = searchResult.getCachesFromSearchResult(LoadFlags.LOAD_WAYPOINTS); + // to update the caches they have to be removed first + caches.removeAll(cachesFromSearchResult); + caches.addAll(cachesFromSearchResult); - if (live) { + if (isLiveMode()) { final boolean excludeMine = Settings.isExcludeMyCaches(); final boolean excludeDisabled = Settings.isExcludeDisabledCaches(); @@ -1170,8 +1186,9 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto countVisibleCaches(); if (cachesCnt < Settings.getWayPointsThreshold() || geocodeIntent != null) { waypoints.clear(); - if (isLiveMapOrCoords) { + if (isLiveMode() || mapMode == MapMode.COORDS) { //All visible waypoints + //FIXME apply type filter waypoints.addAll(app.getWaypointsInViewport(viewport, Settings.isExcludeMyCaches(), Settings.isExcludeDisabledCaches())); } else { //All waypoints from the viewed caches @@ -1184,9 +1201,10 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto //render displayExecutor.execute(new DisplayRunnable(viewport)); - if (live && Settings.isLiveMap()) { + if (mapMode == MapMode.LIVE_ONLINE) { downloadExecutor.execute(new DownloadRunnable(viewport)); } + lastSearchResult = searchResult; } finally { showProgressHandler.sendEmptyMessage(HIDE_PROGRESS); // hide progress } @@ -1210,6 +1228,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto showProgressHandler.sendEmptyMessage(SHOW_PROGRESS); // show progress int count = 0; + SearchResult searchResult; do { if (tokens == null) { @@ -1219,10 +1238,10 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto } } - search = ConnectorFactory.searchByViewport(viewport.resize(0.8), tokens); - if (search != null) { + searchResult = ConnectorFactory.searchByViewport(viewport.resize(0.8), tokens); + if (searchResult != null) { downloaded = true; - if (search.getError() == StatusCode.NOT_LOGGED_IN) { + if (searchResult.getError() == StatusCode.NOT_LOGGED_IN) { Login.login(); tokens = null; } else { @@ -1233,11 +1252,12 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto } while (count < 2); - if (search != null) { - Set<cgCache> result = search.getCachesFromSearchResult(LoadFlags.LOAD_CACHE_OR_DB); + if (searchResult != null) { + Set<cgCache> result = searchResult.getCachesFromSearchResult(LoadFlags.LOAD_CACHE_OR_DB); // to update the caches they have to be removed first caches.removeAll(result); caches.addAll(result); + lastSearchResult = searchResult; } //render @@ -1277,7 +1297,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto if (!cachesToDisplay.isEmpty()) { // Only show waypoints for single view or setting // when less than showWaypointsthreshold Caches shown - if (cachesToDisplay.size() == 1 || (cachesCnt < Settings.getWayPointsThreshold())) { + if (mapMode == MapMode.SINGLE || (cachesCnt < Settings.getWayPointsThreshold())) { for (cgWaypoint waypoint : waypointsToDisplay) { if (waypoint == null || waypoint.getCoords() == null) { @@ -1642,6 +1662,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto public static void startActivitySearch(final Activity fromActivity, final SearchResult search, final String title) { final Intent mapIntent = newIntent(fromActivity); mapIntent.putExtra(EXTRAS_SEARCH, search); + mapIntent.putExtra(EXTRAS_MAP_MODE, MapMode.LIST); if (StringUtils.isNotBlank(title)) { mapIntent.putExtra(CGeoMap.EXTRAS_MAP_TITLE, title); } @@ -1649,11 +1670,14 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto } public static void startActivityLiveMap(final Activity fromActivity) { - fromActivity.startActivity(newIntent(fromActivity)); + final Intent mapIntent = newIntent(fromActivity); + mapIntent.putExtra(EXTRAS_MAP_MODE, Settings.isLiveMap() ? MapMode.LIVE_ONLINE : MapMode.LIVE_OFFLINE); + fromActivity.startActivity(mapIntent); } public static void startActivityCoords(final Activity fromActivity, final Geopoint coords, final WaypointType type, final String title) { final Intent mapIntent = newIntent(fromActivity); + mapIntent.putExtra(EXTRAS_MAP_MODE, MapMode.COORDS); mapIntent.putExtra(EXTRAS_COORDS, coords); if (type != null) { mapIntent.putExtra(EXTRAS_WPTTYPE, type.id); @@ -1666,6 +1690,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto public static void startActivityGeoCode(final Activity fromActivity, final String geocode) { final Intent mapIntent = newIntent(fromActivity); + mapIntent.putExtra(EXTRAS_MAP_MODE, MapMode.SINGLE); mapIntent.putExtra(EXTRAS_GEOCODE, geocode); mapIntent.putExtra(EXTRAS_MAP_TITLE, geocode); fromActivity.startActivity(mapIntent); @@ -1705,7 +1730,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto .append(cache.getListId() > 0) .toHashCode(); - final LayerDrawable ldFromCache = CGeoMap.overlaysCache.get(hashcode); + final LayerDrawable ldFromCache = overlaysCache.get(hashcode); if (ldFromCache != null) { item.setMarker(ldFromCache); return item; @@ -1763,7 +1788,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto ld.setLayerInset(index++, inset[0], inset[1], inset[2], inset[3]); } - CGeoMap.overlaysCache.put(hashcode, ld); + overlaysCache.put(hashcode, ld); item.setMarker(ld); return item; |
