diff options
Diffstat (limited to 'main/src/cgeo/geocaching/maps/CGeoMap.java')
| -rw-r--r-- | main/src/cgeo/geocaching/maps/CGeoMap.java | 153 |
1 files changed, 87 insertions, 66 deletions
diff --git a/main/src/cgeo/geocaching/maps/CGeoMap.java b/main/src/cgeo/geocaching/maps/CGeoMap.java index 840d7c9..f057fea 100644 --- a/main/src/cgeo/geocaching/maps/CGeoMap.java +++ b/main/src/cgeo/geocaching/maps/CGeoMap.java @@ -27,6 +27,7 @@ import cgeo.geocaching.maps.interfaces.MapFactory; import cgeo.geocaching.maps.interfaces.MapViewImpl; import cgeo.geocaching.maps.interfaces.OnDragListener; import cgeo.geocaching.maps.interfaces.OtherCachersOverlayItemImpl; +import cgeo.geocaching.utils.CancellableHandler; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; @@ -59,7 +60,6 @@ import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.UUID; /** * Class representing the Map in c:geo @@ -71,7 +71,7 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory private static final String EXTRAS_LATITUDE = "latitude"; private static final String EXTRAS_WPTTYPE = "wpttype"; private static final String EXTRAS_MAPSTATE = "mapstate"; - private static final String EXTRAS_SEARCHID = "searchid"; + private static final String EXTRAS_SEARCH = "search"; private static final String EXTRAS_DETAIL = "detail"; private static final int MENU_SELECT_MAPVIEW = 1; private static final int MENU_MAP_LIVE = 2; @@ -101,13 +101,13 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory private cgUpdateDir dirUpdate = new UpdateDir(); // from intent private boolean fromDetailIntent = false; - private String searchIdIntent = null; + private cgSearch searchIntent = null; private String geocodeIntent = null; private Geopoint coordsIntent = null; private WaypointType waypointTypeIntent = null; private int[] mapStateIntent = null; // status data - private UUID searchId = null; + private cgSearch search = null; private String token = null; private boolean noMapTokenShowed = false; // map status data @@ -189,7 +189,7 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory ActivityMixin.setTitle(activity, title.toString()); } else if (what == 1 && mapView != null) { - mapView.invalidate(); + mapView.repaintRequired(null); } } }; @@ -207,10 +207,11 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory } } }; - final private Handler loadDetailsHandler = new Handler() { + + final private class LoadDetailsHandler extends CancellableHandler { @Override - public void handleMessage(Message msg) { + public void handleRegularMessage(Message msg) { if (msg.what == 0) { if (waitDialog != null) { int secondsElapsed = (int) ((System.currentTimeMillis() - detailProgressTime) / 1000); @@ -244,7 +245,22 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory } } } - }; + + @Override + public void handleCancel(final Object extra) { + if (loadDetailsThread != null) { + loadDetailsThread.stopIt(); + } + + if (geo == null) { + geo = app.startGeo(activity, geoUpdate, base, 0, 0); + } + if (Settings.isUseCompass() && dir == null) { + dir = app.startDir(activity, dirUpdate); + } + } + } + final private Handler noMapTokenHandler = new Handler() { @Override @@ -322,7 +338,7 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory overlayScale = mapView.createAddScaleOverlay(activity); } - mapView.invalidate(); + mapView.repaintRequired(null); mapController = mapView.getMapController(); mapController.setZoom(Settings.getMapZoom()); @@ -339,7 +355,7 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory Bundle extras = activity.getIntent().getExtras(); if (extras != null) { fromDetailIntent = extras.getBoolean(EXTRAS_DETAIL); - searchIdIntent = extras.getString(EXTRAS_SEARCHID); + searchIntent = (cgSearch) extras.getParcelable(EXTRAS_SEARCH); geocodeIntent = extras.getString(EXTRAS_GEOCODE); final double latitudeIntent = extras.getDouble(EXTRAS_LATITUDE); final double longitudeIntent = extras.getDouble(EXTRAS_LONGITUDE); @@ -348,9 +364,6 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory mapStateIntent = extras.getIntArray(EXTRAS_MAPSTATE); mapTitle = extras.getString(EXTRAS_MAP_TITLE); - if ("".equals(searchIdIntent)) { - searchIdIntent = null; - } if (coordsIntent.getLatitude() == 0.0 || coordsIntent.getLongitude() == 0.0) { coordsIntent = null; } @@ -361,15 +374,15 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory } // live map, if no arguments are given - live = (searchIdIntent == null && geocodeIntent == null && coordsIntent == null); + live = (searchIntent == null && geocodeIntent == null && coordsIntent == null); if (null == mapStateIntent) { followMyLocation = live; } else { followMyLocation = 1 == mapStateIntent[3] ? true : false; } - if (geocodeIntent != null || searchIdIntent != null || coordsIntent != null || mapStateIntent != null) { - centerMap(geocodeIntent, searchIdIntent, coordsIntent, mapStateIntent); + if (geocodeIntent != null || searchIntent != null || coordsIntent != null || mapStateIntent != null) { + centerMap(geocodeIntent, searchIntent, coordsIntent, mapStateIntent); } // prepare my location button @@ -551,7 +564,7 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory item.setTitle(res.getString(R.string.map_live_enable)); } - menu.findItem(MENU_STORE_CACHES).setEnabled(live && !isLoading() && CollectionUtils.isNotEmpty(caches) && app.hasUnsavedCaches(searchId)); + menu.findItem(MENU_STORE_CACHES).setEnabled(live && !isLoading() && CollectionUtils.isNotEmpty(caches) && app.hasUnsavedCaches(search)); item = menu.findItem(MENU_CIRCLE_MODE); // show circles if (overlayCaches != null && overlayCaches.getCircles()) { @@ -584,8 +597,8 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory case MENU_MAP_LIVE: Settings.setLiveMap(!Settings.isLiveMap()); liveChanged = true; - searchId = null; - searchIdIntent = null; + search = null; + searchIntent = null; return true; case MENU_STORE_CACHES: if (live && !isLoading() && CollectionUtils.isNotEmpty(caches)) { @@ -601,9 +614,9 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory final int mapSpanLon = mapView.getLongitudeSpan(); for (cgCache oneCache : cachesProtected) { - if (oneCache != null && oneCache.coords != null) { - if (cgBase.isCacheInViewPort(mapCenterLat, mapCenterLon, mapSpanLat, mapSpanLon, oneCache.coords) && !app.isOffline(oneCache.geocode, null)) { - geocodes.add(oneCache.geocode); + if (oneCache != null && oneCache.getCoords() != null) { + if (cgBase.isCacheInViewPort(mapCenterLat, mapCenterLon, mapSpanLat, mapSpanLon, oneCache.getCoords()) && !app.isOffline(oneCache.getGeocode(), null)) { + geocodes.add(oneCache.getGeocode()); } } } @@ -621,9 +634,12 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory return true; } + final LoadDetailsHandler loadDetailsHandler = new LoadDetailsHandler(); + waitDialog = new ProgressDialog(activity); waitDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); waitDialog.setCancelable(true); + waitDialog.setCancelMessage(loadDetailsHandler.cancelMessage()); waitDialog.setMax(detailTotal); waitDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { @@ -667,16 +683,17 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory } overlayCaches.switchCircles(); - mapView.invalidate(); + mapView.repaintRequired(overlayCaches); return true; - case MENU_AS_LIST: + case MENU_AS_LIST: { final cgSearch search = new cgSearch(); search.totalCnt = caches.size(); for (cgCache cache : caches) { - search.addGeocode(cache.geocode); + search.addGeocode(cache.getGeocode()); } cgeocaches.startActivityMap(activity, app.addSearch(search, caches, true, 0)); return true; + } default: if (SUBMENU_VIEW_GOOGLE_MAP <= id && SUBMENU_VIEW_MF_OFFLINE >= id) { item.setChecked(true); @@ -692,7 +709,7 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory Intent mapIntent = new Intent(activity, Settings.getMapFactory().getMapClass()); mapIntent.putExtra(EXTRAS_DETAIL, fromDetailIntent); - mapIntent.putExtra(EXTRAS_SEARCHID, searchIdIntent); + mapIntent.putExtra(EXTRAS_SEARCH, searchIntent); mapIntent.putExtra(EXTRAS_GEOCODE, geocodeIntent); if (coordsIntent != null) { mapIntent.putExtra(EXTRAS_LATITUDE, coordsIntent.getLatitude()); @@ -713,9 +730,8 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory } return true; - } - else { - NavigationAppFactory.onMenuItemSelected(item, geo, activity, res, caches != null && caches.size() > 0 ? caches.get(0) : null, searchId, null, coordsIntent); + } else { + NavigationAppFactory.onMenuItemSelected(item, geo, activity, res, caches != null && caches.size() > 0 ? caches.get(0) : null, search, null, coordsIntent); } break; } @@ -834,7 +850,7 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory if (overlayPosition != null && mapView != null && (geo == null || geo.speedNow == null || geo.speedNow <= 5)) { // use compass when speed is lower than 18 km/h overlayPosition.setHeading(dir.directionNow); - mapView.invalidate(); + mapView.repaintRequired(overlayPosition); } } } @@ -1118,17 +1134,17 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory // stage 1 - pull and render from the DB only - if (fromDetailIntent || StringUtils.isNotEmpty(searchIdIntent)) { - searchId = UUID.fromString(searchIdIntent); + if (fromDetailIntent || searchIntent != null) { + search = searchIntent; } else { if (!live || !Settings.isLiveMap()) { - searchId = app.getStoredInViewport(centerLat, centerLon, spanLat, spanLon, Settings.getCacheType()); + search = app.getStoredInViewport(centerLat, centerLon, spanLat, spanLon, Settings.getCacheType()); } else { - searchId = app.getCachedInViewport(centerLat, centerLon, spanLat, spanLon, Settings.getCacheType()); + search = app.getCachedInViewport(centerLat, centerLon, spanLat, spanLon, Settings.getCacheType()); } } - if (searchId != null) { + if (search != null) { downloaded = true; } @@ -1139,7 +1155,7 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory return; } - caches = app.getCaches(searchId, true); + caches = app.getCaches(search, true); //if in live map and stored caches are found / disables are also shown. if (live && Settings.isLiveMap()) { @@ -1148,7 +1164,7 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory for (int i = caches.size() - 1; i >= 0; i--) { cgCache cache = caches.get(i); - if ((cache.found && excludeMine) || (cache.own && excludeMine) || (cache.disabled && excludeDisabled)) { + if ((cache.isFound() && excludeMine) || (cache.isOwn() && excludeMine) || (cache.isDisabled() && excludeDisabled)) { caches.remove(i); } } @@ -1245,8 +1261,8 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory return; } - searchId = base.searchByViewport(token, latMin, latMax, lonMin, lonMax, 0); - if (searchId != null) { + search = base.searchByViewport(token, latMin, latMax, lonMin, lonMax, 0); + if (search != null) { downloaded = true; } @@ -1259,7 +1275,7 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory //TODO Portree Only overwrite if we got some. Otherwise maybe error icon //TODO Merge not to show locally found caches - caches = app.getCaches(searchId, centerLat, centerLon, spanLat, spanLon); + caches = app.getCaches(search, centerLat, centerLon, spanLat, spanLon); if (stop) { displayHandler.sendEmptyMessage(0); @@ -1317,25 +1333,25 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory return; } - if (cacheOne.coords == null) { + if (cacheOne.getCoords() == null) { continue; } // display cache waypoints - if (cacheOne.waypoints != null + if (cacheOne.getWaypoints() != null // Only show waypoints for single view or setting // when less than showWaypointsthreshold Caches shown && (cachesProtected.size() == 1 || (cachesProtected.size() < Settings.getWayPointsThreshold())) - && !cacheOne.waypoints.isEmpty()) { - for (cgWaypoint oneWaypoint : cacheOne.waypoints) { - if (oneWaypoint.coords == null) { + && !cacheOne.getWaypoints().isEmpty()) { + for (cgWaypoint oneWaypoint : cacheOne.getWaypoints()) { + if (oneWaypoint.getCoords() == null) { continue; } - items.add(getWaypointItem(new cgCoord(oneWaypoint), oneWaypoint.type)); + items.add(getWaypointItem(new cgCoord(oneWaypoint), oneWaypoint.getWaypointType())); } } - items.add(getCacheItem(new cgCoord(cacheOne), cacheOne.type, cacheOne.own, cacheOne.found, cacheOne.disabled)); + items.add(getCacheItem(new cgCoord(cacheOne), cacheOne.getType(), cacheOne.isOwn(), cacheOne.isFound(), cacheOne.isDisabled())); } overlayCaches.updateItems(items); @@ -1549,9 +1565,9 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory if (coordsIntent != null) { final cgCoord coord = new cgCoord(); - coord.type = "waypoint"; - coord.coords = coordsIntent; - coord.name = "some place"; + coord.setType("waypoint"); + coord.setCoords(coordsIntent); + coord.setName("some place"); coordinates.add(coord); final CachesOverlayItemImpl item = Settings.getMapFactory().getCachesOverlayItem(coord, null); @@ -1640,18 +1656,17 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory private class LoadDetails extends Thread { - private Handler handler = null; - private List<String> geocodes = null; - private volatile boolean stop = false; + final private CancellableHandler handler; + final private List<String> geocodes; private long last = 0L; - public LoadDetails(Handler handlerIn, List<String> geocodesIn) { - handler = handlerIn; - geocodes = geocodesIn; + public LoadDetails(final CancellableHandler handler, final List<String> geocodes) { + this.handler = handler; + this.geocodes = geocodes; } public void stopIt() { - stop = true; + handler.cancel(); } @Override @@ -1669,7 +1684,7 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory for (String geocode : geocodes) { try { - if (stop) { + if (handler.isCancelled()) { break; } @@ -1687,7 +1702,7 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory } } - if (stop) { + if (handler.isCancelled()) { Log.i(Settings.tag, "Stopped storing process."); break; @@ -1703,6 +1718,7 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory handler.sendEmptyMessage(0); } + // FIXME: what does this yield() do here? yield(); last = System.currentTimeMillis(); @@ -1731,8 +1747,8 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory } } - // move map to view results of searchIdIntent - private void centerMap(String geocodeCenter, String searchIdCenter, final Geopoint coordsCenter, int[] mapState) { + // move map to view results of searchIntent + private void centerMap(String geocodeCenter, final cgSearch searchCenter, final Geopoint coordsCenter, int[] mapState) { if (!centered && mapState != null) { try { @@ -1744,14 +1760,14 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory centered = true; alreadyCentered = true; - } else if (!centered && (geocodeCenter != null || searchIdIntent != null)) { + } else if (!centered && (geocodeCenter != null || searchIntent != null)) { try { List<Object> viewport = null; if (geocodeCenter != null) { viewport = app.getBounds(geocodeCenter); } else { - viewport = app.getBounds(UUID.fromString(searchIdCenter)); + viewport = app.getBounds(searchCenter); } if (viewport == null) { @@ -1851,11 +1867,13 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory } // close activity and open homescreen + @Override public void goHome(View view) { ActivityMixin.goHome(activity); } // open manual entry + @Override public void goManual(View view) { ActivityMixin.goManual(activity, "c:geo-live-map"); } @@ -1868,10 +1886,10 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory return imageView; } - public static void startActivitySearch(final Activity fromActivity, final UUID searchId, final String title, boolean detail) { + public static void startActivitySearch(final Activity fromActivity, final cgSearch search, final String title, boolean detail) { Intent mapIntent = new Intent(fromActivity, Settings.getMapFactory().getMapClass()); mapIntent.putExtra(EXTRAS_DETAIL, detail); - mapIntent.putExtra(EXTRAS_SEARCHID, searchId.toString()); + mapIntent.putExtra(EXTRAS_SEARCH, search); if (StringUtils.isNotBlank(title)) { mapIntent.putExtra(CGeoMap.EXTRAS_MAP_TITLE, title); } @@ -1882,7 +1900,7 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory context.startActivity(new Intent(context, Settings.getMapFactory().getMapClass())); } - public static void startActivityCoords(final Context context, final Geopoint coords, final WaypointType type) { + public static void startActivityCoords(final Context context, final Geopoint coords, final WaypointType type, final String title) { Intent mapIntent = new Intent(context, Settings.getMapFactory().getMapClass()); mapIntent.putExtra(EXTRAS_DETAIL, false); mapIntent.putExtra(EXTRAS_LATITUDE, coords.getLatitude()); @@ -1890,6 +1908,9 @@ public class CGeoMap extends AbstractMap implements OnDragListener, ViewFactory if (type != null) { mapIntent.putExtra(EXTRAS_WPTTYPE, type.id); } + if (StringUtils.isNotBlank(title)) { + mapIntent.putExtra(EXTRAS_MAP_TITLE, title); + } context.startActivity(mapIntent); } |
