diff options
Diffstat (limited to 'main/src/cgeo/geocaching/maps/CGeoMap.java')
| -rw-r--r-- | main/src/cgeo/geocaching/maps/CGeoMap.java | 442 |
1 files changed, 218 insertions, 224 deletions
diff --git a/main/src/cgeo/geocaching/maps/CGeoMap.java b/main/src/cgeo/geocaching/maps/CGeoMap.java index e424bcc..7c70266 100644 --- a/main/src/cgeo/geocaching/maps/CGeoMap.java +++ b/main/src/cgeo/geocaching/maps/CGeoMap.java @@ -8,7 +8,6 @@ import cgeo.geocaching.Settings; import cgeo.geocaching.StoredList; import cgeo.geocaching.UpdateDirectionCallback; import cgeo.geocaching.UpdateLocationCallback; -import cgeo.geocaching.cgBase; import cgeo.geocaching.cgCache; import cgeo.geocaching.cgDirection; import cgeo.geocaching.cgGeo; @@ -18,6 +17,7 @@ import cgeo.geocaching.cgeocaches; import cgeo.geocaching.activity.ActivityMixin; import cgeo.geocaching.connector.ConnectorFactory; import cgeo.geocaching.connector.gc.GCBase; +import cgeo.geocaching.connector.gc.Login; import cgeo.geocaching.enumerations.CacheType; import cgeo.geocaching.enumerations.LiveMapStrategy.Strategy; import cgeo.geocaching.enumerations.LoadFlags; @@ -35,9 +35,8 @@ import cgeo.geocaching.maps.interfaces.MapProvider; import cgeo.geocaching.maps.interfaces.MapViewImpl; import cgeo.geocaching.maps.interfaces.OnMapDragListener; import cgeo.geocaching.maps.interfaces.OtherCachersOverlayItemImpl; -import cgeo.geocaching.network.Login; -import cgeo.geocaching.utils.BoundedList; import cgeo.geocaching.utils.CancellableHandler; +import cgeo.geocaching.utils.LeastRecentlyUsedSet; import cgeo.geocaching.utils.Log; import org.apache.commons.collections.CollectionUtils; @@ -95,8 +94,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto //Menu private static final String EXTRAS_GEOCODE = "geocode"; - private static final String EXTRAS_LONGITUDE = "longitude"; - private static final String EXTRAS_LATITUDE = "latitude"; + private static final String EXTRAS_COORDS = "coords"; private static final String EXTRAS_WPTTYPE = "wpttype"; private static final String EXTRAS_MAPSTATE = "mapstate"; private static final String EXTRAS_SEARCH = "search"; @@ -115,6 +113,9 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto private static final String EXTRAS_MAP_TITLE = "mapTitle"; + private static final String BUNDLE_MAP_SOURCE = "mapSource"; + private static final String BUNDLE_MAP_STATE = "mapState"; + private Resources res = null; private MapProvider mapProvider = null; private Activity activity = null; @@ -136,14 +137,8 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto private boolean noMapTokenShowed = false; // map status data private boolean followMyLocation = false; - private Integer centerLatitude = null; - private Integer centerLongitude = null; - private Integer spanLatitude = null; - private Integer spanLongitude = null; - private Integer centerLatitudeUsers = null; - private Integer centerLongitudeUsers = null; - private Integer spanLatitudeUsers = null; - private Integer spanLongitudeUsers = null; + private Viewport viewport = null; + private Viewport viewportUsers = null; private int zoom = -100; // threads private LoadTimer loadTimer = null; @@ -171,7 +166,9 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto private static Map<Integer, LayerDrawable> overlaysCache = new HashMap<Integer, LayerDrawable>(); private int cachesCnt = 0; /** List of caches in the viewport */ - private final BoundedList<cgCache> caches = new BoundedList<cgCache>(MAX_CACHES); + private final LeastRecentlyUsedSet<cgCache> caches = new LeastRecentlyUsedSet<cgCache>(MAX_CACHES); + /** List of waypoints in the viewport */ + private final LeastRecentlyUsedSet<cgWaypoint> waypoints = new LeastRecentlyUsedSet<cgWaypoint>(MAX_CACHES); // storing for offline private ProgressDialog waitDialog = null; private int detailTotal = 0; @@ -327,24 +324,23 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto */ private String mapTitle; + /* Current source id */ + private int currentSourceId; + public CGeoMap(MapActivityImpl activity) { super(activity); } protected void countVisibleCaches() { - final ArrayList<cgCache> protectedCaches = new ArrayList<cgCache>(caches); + final List<cgCache> protectedCaches = caches.getAsList(); int count = 0; if (protectedCaches.size() > 0) { - final GeoPointImpl mapCenter = mapView.getMapViewCenter(); - final int mapCenterLat = mapCenter.getLatitudeE6(); - final int mapCenterLon = mapCenter.getLongitudeE6(); - final int mapSpanLat = mapView.getLatitudeSpan(); - final int mapSpanLon = mapView.getLongitudeSpan(); + final Viewport viewport = mapView.getViewport(); - for (cgCache cache : protectedCaches) { + for (final cgCache cache : protectedCaches) { if (cache != null && cache.getCoords() != null) { - if (Viewport.isCacheInViewPort(mapCenterLat, mapCenterLon, mapSpanLat, mapSpanLon, cache.getCoords())) { + if (viewport.contains(cache)) { count++; } } @@ -354,6 +350,12 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto } @Override + public void onSaveInstanceState(final Bundle outState) { + outState.putInt(BUNDLE_MAP_SOURCE, currentSourceId); + outState.putIntArray(BUNDLE_MAP_STATE, currentMapState()); + } + + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -363,6 +365,34 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto app = (cgeoapplication) activity.getApplication(); mapProvider = Settings.getMapProvider(); + + // Get parameters from the intent + final Bundle extras = activity.getIntent().getExtras(); + if (extras != null) { + searchIntent = (SearchResult) extras.getParcelable(EXTRAS_SEARCH); + geocodeIntent = extras.getString(EXTRAS_GEOCODE); + coordsIntent = (Geopoint) extras.getParcelable(EXTRAS_COORDS); + waypointTypeIntent = WaypointType.findById(extras.getString(EXTRAS_WPTTYPE)); + mapStateIntent = extras.getIntArray(EXTRAS_MAPSTATE); + mapTitle = extras.getString(EXTRAS_MAP_TITLE); + } + if (StringUtils.isBlank(mapTitle)) { + mapTitle = res.getString(R.string.map_map); + } + + // Get fresh map information from the bundle if any + if (savedInstanceState != null) { + currentSourceId = savedInstanceState.getInt(BUNDLE_MAP_SOURCE, Settings.getMapSource()); + mapStateIntent = savedInstanceState.getIntArray(BUNDLE_MAP_STATE); + } else { + currentSourceId = Settings.getMapSource(); + } + + // If recreating from an obsolete map source, we may need a restart + if (changeMapSource(Settings.getMapSource())) { + return; + } + // reset status noMapTokenShowed = false; @@ -420,26 +450,6 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto dirUpdate.updateDirection(dir); } - // get parameters - Bundle extras = activity.getIntent().getExtras(); - if (extras != null) { - searchIntent = (SearchResult) extras.getParcelable(EXTRAS_SEARCH); - geocodeIntent = extras.getString(EXTRAS_GEOCODE); - final double latitudeIntent = extras.getDouble(EXTRAS_LATITUDE); - final double longitudeIntent = extras.getDouble(EXTRAS_LONGITUDE); - coordsIntent = new Geopoint(latitudeIntent, longitudeIntent); - waypointTypeIntent = WaypointType.findById(extras.getString(EXTRAS_WPTTYPE)); - mapStateIntent = extras.getIntArray(EXTRAS_MAPSTATE); - mapTitle = extras.getString(EXTRAS_MAP_TITLE); - - if (coordsIntent.getLatitude() == 0.0 || coordsIntent.getLongitude() == 0.0) { - coordsIntent = null; - } - } - - if (StringUtils.isBlank(mapTitle)) { - mapTitle = res.getString(R.string.map_map); - } // live map, if no arguments are given live = (searchIntent == null && geocodeIntent == null && coordsIntent == null); @@ -447,7 +457,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto if (null == mapStateIntent) { followMyLocation = live; } else { - followMyLocation = 1 == mapStateIntent[3] ? true : false; + followMyLocation = 1 == mapStateIntent[3]; } if (geocodeIntent != null || searchIntent != null || coordsIntent != null || mapStateIntent != null) { centerMap(geocodeIntent, searchIntent, coordsIntent, mapStateIntent); @@ -484,6 +494,10 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto public void onResume() { super.onResume(); + if (changeMapSource(Settings.getMapSource())) { + return; + } + app.setAction(StringUtils.defaultIfBlank(geocodeIntent, null)); if (geo == null) { geo = app.startGeo(geoUpdate); @@ -507,12 +521,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto } dirtyCaches.clear(); // Update display - GeoPointImpl mapCenterNow = mapView.getMapViewCenter(); - int centerLatitudeNow = mapCenterNow.getLatitudeE6(); - int centerLongitudeNow = mapCenterNow.getLongitudeE6(); - int spanLatitudeNow = mapView.getLatitudeSpan(); - int spanLongitudeNow = mapView.getLongitudeSpan(); - displayExecutor.execute(new DisplayRunnable(centerLatitudeNow, centerLongitudeNow, spanLatitudeNow, spanLongitudeNow)); + displayExecutor.execute(new DisplayRunnable(mapView.getViewport())); } startTimer(); @@ -671,7 +680,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto menu.findItem(SUBMENU_STRATEGY).setEnabled(live); } catch (Exception e) { - Log.e(Settings.tag, "cgeomap.onPrepareOptionsMenu: " + e.toString()); + Log.e("cgeomap.onPrepareOptionsMenu: " + e.toString()); } return true; @@ -696,25 +705,22 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto if (live && !isLoading() && CollectionUtils.isNotEmpty(caches)) { final List<String> geocodes = new ArrayList<String>(); - List<cgCache> cachesProtected = new ArrayList<cgCache>(caches); + final List<cgCache> cachesProtected = caches.getAsList(); + try { if (cachesProtected.size() > 0) { - final GeoPointImpl mapCenter = mapView.getMapViewCenter(); - final int mapCenterLat = mapCenter.getLatitudeE6(); - final int mapCenterLon = mapCenter.getLongitudeE6(); - final int mapSpanLat = mapView.getLatitudeSpan(); - final int mapSpanLon = mapView.getLongitudeSpan(); + final Viewport viewport = mapView.getViewport(); - for (cgCache cache : cachesProtected) { + for (final cgCache cache : cachesProtected) { if (cache != null && cache.getCoords() != null) { - if (Viewport.isCacheInViewPort(mapCenterLat, mapCenterLon, mapSpanLat, mapSpanLon, cache.getCoords()) && !app.isOffline(cache.getGeocode(), null)) { + if (viewport.contains(cache) && !app.isOffline(cache.getGeocode(), null)) { geocodes.add(cache.getGeocode()); } } } } } catch (Exception e) { - Log.e(Settings.tag, "cgeomap.onOptionsItemSelected.#4: " + e.toString()); + Log.e("cgeomap.onOptionsItemSelected.#4: " + e.toString()); } detailTotal = geocodes.size(); @@ -748,7 +754,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto dir = app.startDir(activity, dirUpdate); } } catch (Exception e) { - Log.e(Settings.tag, "cgeocaches.onPrepareOptionsMenu.onCancel: " + e.toString()); + Log.e("cgeocaches.onPrepareOptionsMenu.onCancel: " + e.toString()); } } }); @@ -807,35 +813,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto item.setChecked(true); int mapSource = MapProviderFactory.getMapSourceFromMenuId(id); - boolean mapRestartRequired = switchMapSource(mapSource); - - if (mapRestartRequired) { - // close old mapview - activity.finish(); - - // prepare information to restart a similar view - Intent mapIntent = new Intent(activity, Settings.getMapProvider().getMapClass()); - - mapIntent.putExtra(EXTRAS_SEARCH, searchIntent); - mapIntent.putExtra(EXTRAS_GEOCODE, geocodeIntent); - if (coordsIntent != null) { - mapIntent.putExtra(EXTRAS_LATITUDE, coordsIntent.getLatitude()); - mapIntent.putExtra(EXTRAS_LONGITUDE, coordsIntent.getLongitude()); - } - mapIntent.putExtra(EXTRAS_WPTTYPE, waypointTypeIntent != null ? waypointTypeIntent.id : null); - int[] mapState = new int[4]; - GeoPointImpl mapCenter = mapView.getMapViewCenter(); - mapState[0] = mapCenter.getLatitudeE6(); - mapState[1] = mapCenter.getLongitudeE6(); - mapState[2] = mapView.getMapZoomLevel(); - mapState[3] = followMyLocation ? 1 : 0; - mapIntent.putExtra(EXTRAS_MAPSTATE, mapState); - mapIntent.putExtra(EXTRAS_MAP_TITLE, mapTitle); - - // start the new map - activity.startActivity(mapIntent); - - } + changeMapSource(mapSource); return true; } @@ -843,16 +821,72 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto return false; } - private boolean switchMapSource(int sourceId) { - boolean mapRestartRequired = !MapProviderFactory.isSameProvider(Settings.getMapSource(), sourceId); + /** + * Restart the current activity if the map provider has changed, or change the map source if needed. + * + * @param mapSource + * the new map source, which can be the same as the current one + * @return true if a restart is needed, false otherwise + */ + private boolean changeMapSource(final int mapSource) { + final boolean restartRequired = !MapProviderFactory.isSameProvider(currentSourceId, mapSource); - Settings.setMapSource(sourceId); + Settings.setMapSource(mapSource); + currentSourceId = mapSource; - if (!mapRestartRequired) { + if (restartRequired) { + mapRestart(); + } else if (mapView != null) { mapView.setMapSource(); } - return mapRestartRequired; + return restartRequired; + } + + /** + * Restart the current activity with the default map source. + */ + private void mapRestart() { + // close old mapview + activity.finish(); + + // prepare information to restart a similar view + Intent mapIntent = new Intent(activity, Settings.getMapProvider().getMapClass()); + + mapIntent.putExtra(EXTRAS_SEARCH, searchIntent); + mapIntent.putExtra(EXTRAS_GEOCODE, geocodeIntent); + if (coordsIntent != null) { + mapIntent.putExtra(EXTRAS_COORDS, coordsIntent); + } + mapIntent.putExtra(EXTRAS_WPTTYPE, waypointTypeIntent != null ? waypointTypeIntent.id : null); + mapIntent.putExtra(EXTRAS_MAP_TITLE, mapTitle); + + final int[] mapState = currentMapState(); + if (mapState != null) { + mapIntent.putExtra(EXTRAS_MAPSTATE, mapState); + } + + // start the new map + activity.startActivity(mapIntent); + } + + /** + * Get the current map state from the map view if it exists or from the mapStateIntent field otherwise. + * + * @return the current map state as an array of int, or null if no map state is available + */ + private int[] currentMapState() { + if (mapView != null) { + int[] mapState = new int[4]; + GeoPointImpl mapCenter = mapView.getMapViewCenter(); + mapState[0] = mapCenter.getLatitudeE6(); + mapState[1] = mapCenter.getLongitudeE6(); + mapState[2] = mapView.getMapZoomLevel(); + mapState[3] = followMyLocation ? 1 : 0; + return mapState; + } else { + return mapStateIntent; + } } private void savePrefs() { @@ -908,7 +942,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto } } catch (Exception e) { - Log.w(Settings.tag, "Failed to update location."); + Log.w("Failed to update location."); } } } @@ -981,11 +1015,8 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto if (mapView != null) { // get current viewport - GeoPointImpl mapCenterNow = mapView.getMapViewCenter(); - int centerLatitudeNow = mapCenterNow.getLatitudeE6(); - int centerLongitudeNow = mapCenterNow.getLongitudeE6(); - int spanLatitudeNow = mapView.getLatitudeSpan(); - int spanLongitudeNow = mapView.getLongitudeSpan(); + final Viewport viewportNow = mapView.getViewport(); + int zoomNow = mapView.getMapZoomLevel(); // check if map moved or zoomed //TODO Portree Use Rectangle inside with bigger search window. That will stop reloading on every move @@ -995,20 +1026,16 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto moved = true; } else if (live && Settings.isLiveMap() && !downloaded) { moved = true; - } else if (centerLatitude == null || centerLongitude == null) { + } else if (viewport == null) { moved = true; - } else if (spanLatitude == null || spanLongitude == null) { + } else if (zoomNow != zoom) { moved = true; - } else if (((Math.abs(spanLatitudeNow - spanLatitude) > 50) || (Math.abs(spanLongitudeNow - spanLongitude) > 50) || // changed zoom - (Math.abs(centerLatitudeNow - centerLatitude) > (spanLatitudeNow / 4)) || (Math.abs(centerLongitudeNow - centerLongitude) > (spanLongitudeNow / 4)) // map moved - ) && (cachesCnt <= 0 || CollectionUtils.isEmpty(caches) - || !Viewport.isInViewPort(centerLatitude, centerLongitude, centerLatitudeNow, centerLongitudeNow, spanLatitude, spanLongitude, spanLatitudeNow, spanLongitudeNow))) { + } else if (mapMoved(viewport, viewportNow) && (cachesCnt <= 0 || CollectionUtils.isEmpty(caches) || !viewport.includes(viewportNow))) { moved = true; } // update title on any change - int zoomNow = mapView.getMapZoomLevel(); - if (moved || zoomNow != zoom || spanLatitudeNow != spanLatitude || spanLongitudeNow != spanLongitude || centerLatitudeNow != centerLatitude || centerLongitudeNow != centerLongitude) { + if (moved || zoomNow != zoom || !viewportNow.equals(viewport)) { displayHandler.sendEmptyMessage(UPDATE_TITLE); } zoom = zoomNow; @@ -1020,19 +1047,15 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto long currentTime = System.currentTimeMillis(); if (1000 < (currentTime - loadThreadRun)) { - centerLatitude = centerLatitudeNow; - centerLongitude = centerLongitudeNow; - spanLatitude = spanLatitudeNow; - spanLongitude = spanLongitudeNow; - - loadExecutor.execute(new LoadRunnable(centerLatitude, centerLongitude, spanLatitude, spanLongitude)); + viewport = viewportNow; + loadExecutor.execute(new LoadRunnable(viewport)); } } } yield(); } catch (Exception e) { - Log.w(Settings.tag, "cgeomap.LoadTimer.run: " + e.toString()); + Log.w("cgeomap.LoadTimer.run: " + e.toString()); } } } @@ -1069,11 +1092,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto if (mapView != null) { // get current viewport - GeoPointImpl mapCenterNow = mapView.getMapViewCenter(); - int centerLatitudeNow = mapCenterNow.getLatitudeE6(); - int centerLongitudeNow = mapCenterNow.getLongitudeE6(); - int spanLatitudeNow = mapView.getLatitudeSpan(); - int spanLongitudeNow = mapView.getLongitudeSpan(); + final Viewport viewportNow = mapView.getViewport(); // check if map moved or zoomed boolean moved = false; @@ -1082,30 +1101,22 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto if (60000 < (currentTime - go4CacheThreadRun)) { moved = true; - } else if (centerLatitudeUsers == null || centerLongitudeUsers == null) { - moved = true; - } else if (spanLatitudeUsers == null || spanLongitudeUsers == null) { + } else if (viewportUsers == null) { moved = true; - } else if (((Math.abs(spanLatitudeNow - spanLatitudeUsers) > 50) || (Math.abs(spanLongitudeNow - spanLongitudeUsers) > 50) || // changed zoom - (Math.abs(centerLatitudeNow - centerLatitudeUsers) > (spanLatitudeNow / 4)) || (Math.abs(centerLongitudeNow - centerLongitudeUsers) > (spanLongitudeNow / 4)) // map moved - ) && !Viewport.isInViewPort(centerLatitudeUsers, centerLongitudeUsers, centerLatitudeNow, centerLongitudeNow, spanLatitudeUsers, spanLongitudeUsers, spanLatitudeNow, spanLongitudeNow)) { + } else if (mapMoved(viewportUsers, viewportNow) && !viewportUsers.includes(viewportNow)) { moved = true; } // save new values if (moved && (1000 < (currentTime - go4CacheThreadRun))) { - centerLatitudeUsers = centerLatitudeNow; - centerLongitudeUsers = centerLongitudeNow; - spanLatitudeUsers = spanLatitudeNow; - spanLongitudeUsers = spanLongitudeNow; - - Go4CacheExecutor.execute(new Go4CacheRunnable(centerLatitudeNow, centerLongitudeNow, spanLatitudeNow, spanLongitudeNow)); + viewportUsers = viewportNow; + Go4CacheExecutor.execute(new Go4CacheRunnable(viewportUsers)); } } yield(); } catch (Exception e) { - Log.w(Settings.tag, "cgeomap.LoadUsersTimer.run: " + e.toString()); + Log.w("cgeomap.LoadUsersTimer.run: " + e.toString()); } } } @@ -1118,8 +1129,8 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto private class LoadRunnable extends DoRunnable { - public LoadRunnable(long centerLatIn, long centerLonIn, long spanLatIn, long spanLonIn) { - super(centerLatIn, centerLonIn, spanLatIn, spanLonIn); + public LoadRunnable(final Viewport viewport) { + super(viewport); } @Override @@ -1138,34 +1149,50 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto } else { // live map if (!live || !Settings.isLiveMap()) { - search = new SearchResult(app.getStoredInViewport(centerLat, centerLon, spanLat, spanLon, Settings.getCacheType())); + search = new SearchResult(app.getStoredInViewport(viewport, Settings.getCacheType())); } else { - search = new SearchResult(app.getCachedInViewport(centerLat, centerLon, spanLat, spanLon, Settings.getCacheType())); + search = new SearchResult(app.getCachedInViewport(viewport, Settings.getCacheType())); } } if (search != null) { downloaded = true; - caches.addAll(search.getCachesFromSearchResult(LoadFlags.LOAD_WAYPOINTS)); + Set<cgCache> cachesFromSearchResult = search.getCachesFromSearchResult(LoadFlags.LOAD_WAYPOINTS); + caches.addAll(cachesFromSearchResult); } if (live) { final boolean excludeMine = Settings.isExcludeMyCaches(); final boolean excludeDisabled = Settings.isExcludeDisabledCaches(); - final ArrayList<cgCache> tempList = new ArrayList<cgCache>(caches); + final List<cgCache> tempList = caches.getAsList(); + for (cgCache cache : tempList) { if ((cache.isFound() && excludeMine) || (cache.isOwn() && excludeMine) || (cache.isDisabled() && excludeDisabled)) { caches.remove(cache); } } } + countVisibleCaches(); + if (cachesCnt < Settings.getWayPointsThreshold()) + { + waypoints.clear(); + if (searchIntent == null && geocodeIntent == null) { + //All visible waypoints + waypoints.addAll(app.getWaypointsInViewport(viewport, Settings.isExcludeMyCaches(), Settings.isExcludeDisabledCaches())); + } else { + //All waypoints from the viewed caches + for (cgCache c : caches.getAsList()) { + waypoints.addAll(c.getWaypoints()); + } + } + } //render - displayExecutor.execute(new DisplayRunnable(centerLat, centerLon, spanLat, spanLon)); + displayExecutor.execute(new DisplayRunnable(viewport)); if (live && Settings.isLiveMap()) { - downloadExecutor.execute(new DownloadRunnable(centerLat, centerLon, spanLat, spanLon)); + downloadExecutor.execute(new DownloadRunnable(viewport)); } } finally { showProgressHandler.sendEmptyMessage(HIDE_PROGRESS); // hide progress @@ -1180,8 +1207,8 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto private class DownloadRunnable extends DoRunnable { - public DownloadRunnable(long centerLatIn, long centerLonIn, long spanLatIn, long spanLonIn) { - super(centerLatIn, centerLonIn, spanLatIn, spanLonIn); + public DownloadRunnable(final Viewport viewport) { + super(viewport); } @Override @@ -1199,8 +1226,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto } } - final Viewport viewport = new Viewport(new Geopoint(centerLat / 1e6, centerLon / 1e6), 0.8 * spanLat / 1e6, 0.8 * spanLon / 1e6); - search = ConnectorFactory.searchByViewport(viewport, tokens); + search = ConnectorFactory.searchByViewport(viewport.resize(0.8), tokens); if (search != null) { downloaded = true; if (search.getError() == StatusCode.NOT_LOGGED_IN) { @@ -1222,10 +1248,10 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto } //render - displayExecutor.execute(new DisplayRunnable(centerLat, centerLon, spanLat, spanLon)); + displayExecutor.execute(new DisplayRunnable(viewport)); } catch (ThreadDeath e) { - Log.d(Settings.tag, "DownloadThread stopped"); + Log.d("DownloadThread stopped"); displayHandler.sendEmptyMessage(UPDATE_TITLE); } finally { showProgressHandler.sendEmptyMessage(HIDE_PROGRESS); // hide progress @@ -1238,8 +1264,8 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto */ private class DisplayRunnable extends DoRunnable { - public DisplayRunnable(long centerLatIn, long centerLonIn, long spanLatIn, long spanLonIn) { - super(centerLatIn, centerLonIn, spanLatIn, spanLonIn); + public DisplayRunnable(final Viewport viewport) { + super(viewport); } @Override @@ -1251,29 +1277,28 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto } // display caches - final List<cgCache> cachesToDisplay = new ArrayList<cgCache>(caches); + final List<cgCache> cachesToDisplay = caches.getAsList(); + final List<cgWaypoint> waypointsToDisplay = new ArrayList<cgWaypoint>(waypoints); final List<CachesOverlayItemImpl> itemsToDisplay = new ArrayList<CachesOverlayItemImpl>(); if (!cachesToDisplay.isEmpty()) { + // Only show waypoints for single view or setting + // when less than showWaypointsthreshold Caches shown + if (cachesToDisplay.size() == 1 || (cachesCnt < Settings.getWayPointsThreshold())) { + for (cgWaypoint waypoint : waypointsToDisplay) { + + if (waypoint.getCoords() == null) { + continue; + } + + itemsToDisplay.add(getItem(waypoint, null, waypoint)); + } + } for (cgCache cache : cachesToDisplay) { if (cache.getCoords() == null) { continue; } - - // display cache waypoints - if (cache.hasWaypoints() - // Only show waypoints for single view or setting - // when less than showWaypointsthreshold Caches shown - && (cachesToDisplay.size() == 1 || (cachesToDisplay.size() < Settings.getWayPointsThreshold()))) { - for (cgWaypoint waypoint : cache.getWaypoints()) { - if (waypoint.getCoords() == null) { - continue; - } - - itemsToDisplay.add(getItem(waypoint, null, waypoint)); - } - } itemsToDisplay.add(getItem(cache, cache, null)); } @@ -1289,7 +1314,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto displayHandler.sendEmptyMessage(UPDATE_TITLE); } catch (ThreadDeath e) { - Log.d(Settings.tag, "DisplayThread stopped"); + Log.d("DisplayThread stopped"); displayHandler.sendEmptyMessage(UPDATE_TITLE); } finally { showProgressHandler.sendEmptyMessage(HIDE_PROGRESS); @@ -1303,21 +1328,15 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto private class Go4CacheRunnable extends DoRunnable { - public Go4CacheRunnable(long centerLatIn, long centerLonIn, long spanLatIn, long spanLonIn) { - super(centerLatIn, centerLonIn, spanLatIn, spanLonIn); + public Go4CacheRunnable(final Viewport viewport) { + super(viewport); } @Override public void run() { - final Geopoint center = new Geopoint((int) centerLat, (int) centerLon); - final Viewport viewport = new Viewport(center, spanLat / 1e6 * 1.5, spanLon / 1e6 * 1.5); - - try { - go4CacheThreadRun = System.currentTimeMillis(); - List<Go4CacheUser> go4CacheUsers = Go4Cache.getGeocachersInViewport(Settings.getUsername(), viewport); - go4CacheDisplayExecutor.execute(new Go4CacheDisplayRunnable(go4CacheUsers, centerLat, centerLon, spanLat, spanLon)); - } finally { - } + go4CacheThreadRun = System.currentTimeMillis(); + List<Go4CacheUser> go4CacheUsers = Go4Cache.getGeocachersInViewport(Settings.getUsername(), viewport.resize(1.5)); + go4CacheDisplayExecutor.execute(new Go4CacheDisplayRunnable(go4CacheUsers, viewport)); } } @@ -1328,8 +1347,8 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto private List<Go4CacheUser> users = null; - public Go4CacheDisplayRunnable(List<Go4CacheUser> usersIn, long centerLatIn, long centerLonIn, long spanLatIn, long spanLonIn) { - super(centerLatIn, centerLonIn, spanLatIn, spanLonIn); + public Go4CacheDisplayRunnable(List<Go4CacheUser> usersIn, final Viewport viewport) { + super(viewport); users = usersIn; } @@ -1397,16 +1416,10 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto private abstract class DoRunnable implements Runnable { - protected long centerLat = 0L; - protected long centerLon = 0L; - protected long spanLat = 0L; - protected long spanLon = 0L; + final protected Viewport viewport; - public DoRunnable(long centerLatIn, long centerLonIn, long spanLatIn, long spanLonIn) { - centerLat = centerLatIn; - centerLon = centerLonIn; - spanLat = spanLatIn; - spanLon = spanLonIn; + public DoRunnable(final Viewport viewport) { + this.viewport = viewport; } public abstract void run(); @@ -1478,15 +1491,15 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto } if (handler.isCancelled()) { - Log.i(Settings.tag, "Stopped storing process."); + Log.i("Stopped storing process."); break; } - cgBase.storeCache(activity, null, geocode, StoredList.STANDARD_LIST_ID, false, handler); + cgCache.storeCache(activity, null, geocode, StoredList.STANDARD_LIST_ID, false, handler); } } catch (Exception e) { - Log.e(Settings.tag, "cgeocaches.LoadDetails.run: " + e.toString()); + Log.e("cgeocaches.LoadDetails.run: " + e.toString()); } finally { // one more cache over detailProgress++; @@ -1504,6 +1517,13 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto } } + private static boolean mapMoved(final Viewport referenceViewport, final Viewport newViewport) { + return Math.abs(newViewport.getLatitudeSpan() - referenceViewport.getLatitudeSpan()) > 50e-6 || + Math.abs(newViewport.getLongitudeSpan() - referenceViewport.getLongitudeSpan()) > 50e-6 || + Math.abs(newViewport.center.getLatitude() - referenceViewport.center.getLatitude()) > referenceViewport.getLatitudeSpan() / 4 || + Math.abs(newViewport.center.getLongitude() - referenceViewport.center.getLongitude()) > referenceViewport.getLongitudeSpan() / 4; + } + // center map to desired location private void centerMap(final Geopoint coords) { if (coords == null) { @@ -1537,46 +1557,21 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto alreadyCentered = true; } else if (!centered && (geocodeCenter != null || searchIntent != null)) { try { - List<Number> viewport = null; + Viewport viewport = null; if (geocodeCenter != null) { viewport = app.getBounds(geocodeCenter); - } else { - if (searchCenter != null) { - viewport = app.getBounds(searchCenter.getGeocodes()); - } - } - - if (viewport == null || viewport.size() < 5) { - return; + } else if (searchCenter != null) { + viewport = app.getBounds(searchCenter.getGeocodes()); } - int cnt = (Integer) viewport.get(0); - if (cnt <= 0) { + if (viewport == null) { return; } - int minLat = (int) ((Double) viewport.get(1) * 1e6); - int maxLat = (int) ((Double) viewport.get(2) * 1e6); - int maxLon = (int) ((Double) viewport.get(3) * 1e6); - int minLon = (int) ((Double) viewport.get(4) * 1e6); - - int centerLat = 0; - int centerLon = 0; - - if ((Math.abs(maxLat) - Math.abs(minLat)) != 0) { - centerLat = minLat + ((maxLat - minLat) / 2); - } else { - centerLat = maxLat; - } - if ((Math.abs(maxLon) - Math.abs(minLon)) != 0) { - centerLon = minLon + ((maxLon - minLon) / 2); - } else { - centerLon = maxLon; - } - mapController.setCenter(mapProvider.getGeoPointBase(new Geopoint(centerLat, centerLon))); - if (Math.abs(maxLat - minLat) != 0 && Math.abs(maxLon - minLon) != 0) { - mapController.zoomToSpan(Math.abs(maxLat - minLat), Math.abs(maxLon - minLon)); + mapController.setCenter(mapProvider.getGeoPointBase(viewport.center)); + if (viewport.getLatitudeSpan() != 0 && viewport.getLongitudeSpan() != 0) { + mapController.zoomToSpan((int) (viewport.getLatitudeSpan() * 1e6), (int) (viewport.getLongitudeSpan() * 1e6)); } } catch (Exception e) { // nothing at all @@ -1666,8 +1661,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto public static void startActivityCoords(final Activity fromActivity, final Geopoint coords, final WaypointType type, final String title) { final Intent mapIntent = newIntent(fromActivity); - mapIntent.putExtra(EXTRAS_LATITUDE, coords.getLatitude()); - mapIntent.putExtra(EXTRAS_LONGITUDE, coords.getLongitude()); + mapIntent.putExtra(EXTRAS_COORDS, coords); if (type != null) { mapIntent.putExtra(EXTRAS_WPTTYPE, type.id); } |
