diff options
Diffstat (limited to 'main/src/cgeo/geocaching/maps/CGeoMap.java')
| -rw-r--r-- | main/src/cgeo/geocaching/maps/CGeoMap.java | 180 |
1 files changed, 51 insertions, 129 deletions
diff --git a/main/src/cgeo/geocaching/maps/CGeoMap.java b/main/src/cgeo/geocaching/maps/CGeoMap.java index c98ba72..61b321d 100644 --- a/main/src/cgeo/geocaching/maps/CGeoMap.java +++ b/main/src/cgeo/geocaching/maps/CGeoMap.java @@ -3,9 +3,7 @@ package cgeo.geocaching.maps; import cgeo.geocaching.CacheListActivity; import cgeo.geocaching.CgeoApplication; import cgeo.geocaching.DataStore; -import cgeo.geocaching.DirectionProvider; import cgeo.geocaching.Geocache; -import cgeo.geocaching.IGeoData; import cgeo.geocaching.R; import cgeo.geocaching.SearchResult; import cgeo.geocaching.Waypoint; @@ -31,18 +29,21 @@ import cgeo.geocaching.maps.interfaces.MapProvider; import cgeo.geocaching.maps.interfaces.MapSource; import cgeo.geocaching.maps.interfaces.MapViewImpl; import cgeo.geocaching.maps.interfaces.OnMapDragListener; +import cgeo.geocaching.sensors.GeoDirHandler; +import cgeo.geocaching.sensors.IGeoData; import cgeo.geocaching.settings.Settings; import cgeo.geocaching.ui.dialog.LiveMapInfoDialogBuilder; import cgeo.geocaching.utils.AngleUtils; import cgeo.geocaching.utils.CancellableHandler; -import cgeo.geocaching.utils.GeoDirHandler; import cgeo.geocaching.utils.LeastRecentlyUsedSet; import cgeo.geocaching.utils.Log; -import cgeo.geocaching.utils.RunnableWithArgument; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.builder.HashCodeBuilder; +import rx.Scheduler; +import rx.Subscription; +import rx.functions.Action1; import android.app.Activity; import android.app.AlertDialog; @@ -68,6 +69,9 @@ import android.widget.ImageView; import android.widget.ImageView.ScaleType; import android.widget.TextView; import android.widget.ViewSwitcher.ViewFactory; +import rx.schedulers.Schedulers; +import rx.subscriptions.CompositeSubscription; +import rx.subscriptions.Subscriptions; import java.io.File; import java.util.ArrayList; @@ -88,6 +92,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto /** max. number of caches displayed in the Live Map */ public static final int MAX_CACHES = 500; + private CompositeSubscription resumeSubscription; /** Controls the behavior of the map */ public enum MapMode { @@ -145,7 +150,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto private Viewport viewport = null; private int zoom = -100; // threads - private LoadTimer loadTimer = null; + private Subscription loadTimer; private LoadDetails loadDetailsThread = null; /** Time of last {@link LoadRunnable} run */ private volatile long loadThreadRun = 0L; @@ -289,8 +294,6 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto waitDialog.dismiss(); waitDialog.setOnCancelListener(null); } - - geoDirUpdate.startDir(); } } @@ -299,8 +302,6 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto if (loadDetailsThread != null) { loadDetailsThread.stopIt(); } - - geoDirUpdate.startDir(); } } @@ -466,7 +467,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto prepareFilterBar(); - if (!app.isLiveMapHintShown() && !Settings.getHideLiveMapHint()) { + if (!app.isLiveMapHintShownInThisSession() && !Settings.getHideLiveMapHint() && Settings.getLiveMapHintShowCount() <= 3) { LiveMapInfoDialogBuilder.create(activity).show(); } } @@ -494,8 +495,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto @Override public void onResume() { super.onResume(); - - addGeoDirObservers(); + resumeSubscription = Subscriptions.from(geoDirUpdate.start(), startTimer()); if (!CollectionUtils.isEmpty(dirtyCaches)) { for (String geocode : dirtyCaches) { @@ -511,22 +511,11 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto // Update display displayExecutor.execute(new DisplayRunnable(mapView.getViewport())); } - - startTimer(); - } - - private void addGeoDirObservers() { - geoDirUpdate.startGeoAndDir(); - } - - private void deleteGeoDirObservers() { - geoDirUpdate.stopGeoAndDir(); } @Override public void onPause() { - stopTimer(); - deleteGeoDirObservers(); + resumeSubscription.unsubscribe(); savePrefs(); if (mapView != null) { @@ -661,9 +650,9 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto if (Settings.getChooseList()) { // let user select list to store cache in new StoredList.UserInterface(activity).promptForListSelection(R.string.list_title, - new RunnableWithArgument<Integer>() { + new Action1<Integer>() { @Override - public void run(final Integer selectedListId) { + public void call(final Integer selectedListId) { storeCaches(geocodes, selectedListId); } }, true, StoredList.TEMPORARY_LIST_ID); @@ -686,7 +675,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto markersInvalidated = true; ActivityMixin.invalidateOptionsMenu(activity); if (!Settings.isExcludeMyCaches()) { - Tile.Cache.clear(); + Tile.cache.clear(); } return true; case R.id.menu_theme_mode: @@ -800,7 +789,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto * @return true if a restart is needed, false otherwise */ private boolean changeMapSource(final MapSource mapSource) { - final boolean restartRequired = !MapProviderFactory.isSameActivity(Settings.getMapSource(), mapSource); + final boolean restartRequired = !MapProviderFactory.isSameActivity(MapProviderFactory.getMapSource(currentSourceId), mapSource); Settings.setMapSource(mapSource); currentSourceId = mapSource.getNumericalId(); @@ -898,26 +887,14 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto private long timeLastPositionOverlayCalculation = 0; @Override - protected void updateGeoData(final IGeoData geo) { + public void updateGeoDir(final IGeoData geo, final float dir) { if (geo.isPseudoLocation()) { locationValid = false; } else { locationValid = true; currentLocation = geo.getLocation(); - - if (!Settings.isUseCompass() || geo.getSpeed() > 5) { // use GPS when speed is higher than 18 km/h - currentHeading = geo.getBearing(); - } - - repaintPositionOverlay(); - } - } - - @Override - public void updateDirection(final float direction) { - if (app.currentGeo().getSpeed() <= 5) { // use compass when speed is lower than 18 km/h - currentHeading = DirectionProvider.getDirectionNow(activity, direction); + currentHeading = dir; repaintPositionOverlay(); } } @@ -991,47 +968,25 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto * Starts the {@link LoadTimer}. */ - public synchronized void startTimer() { + private Subscription startTimer() { if (coordsIntent != null) { // display just one point - (new DisplayPointThread()).start(); + displayPoint(coordsIntent); + loadTimer = Subscriptions.empty(); } else { - // start timer - stopTimer(); - loadTimer = new LoadTimer(); - loadTimer.start(); - } - } - - private synchronized void stopTimer() { - if (loadTimer != null) { - loadTimer.stopIt(); - loadTimer = null; + loadTimer = startLoadTimer(); } + return loadTimer; } /** * loading timer Triggers every 250ms and checks for viewport change and starts a {@link LoadRunnable}. */ - private class LoadTimer extends Thread { - - public LoadTimer() { - super("Load Timer"); - } - - private volatile boolean stop = false; - - public void stopIt() { - stop = true; - } - - @Override - public void run() { - - while (!stop) { + private Subscription startLoadTimer() { + return Schedulers.newThread().schedulePeriodically(new Action1<Scheduler.Inner>() { + @Override + public void call(Scheduler.Inner inner) { try { - sleep(250); - if (mapView != null) { // get current viewport final Viewport viewportNow = mapView.getViewport(); @@ -1063,19 +1018,23 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto } } - yield(); } catch (Exception e) { - Log.w("CGeoMap.LoadTimer.run", e); + Log.w("CGeoMap.startLoadtimer.start", e); } } - } - - public boolean isLoading() { - return loadExecutor.getActiveCount() > 0 || - downloadExecutor.getActiveCount() > 0 || - displayExecutor.getActiveCount() > 0; - } + }, 250, 250, TimeUnit.MILLISECONDS); + } + /** + * get if map is loading something + * + * @return + */ + public boolean isLoading() { + return !loadTimer.isUnsubscribed() && + (loadExecutor.getActiveCount() > 0 || + downloadExecutor.getActiveCount() > 0 || + displayExecutor.getActiveCount() > 0); } /** @@ -1182,7 +1141,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto } } } - final SearchResult searchResult = ConnectorFactory.searchByViewport(viewport.resize(0.8), tokens); + final SearchResult searchResult = ConnectorFactory.searchByViewport(viewport.resize(0.8), tokens).toBlockingObservable().single(); downloaded = true; Set<Geocache> result = searchResult.getCachesFromSearchResult(LoadFlags.LOAD_CACHE_OR_DB); @@ -1271,32 +1230,16 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto } } - /** - * Thread to display one point. Started on opening if in single mode. - */ - private class DisplayPointThread extends Thread { - - @Override - public void run() { - if (mapView == null || caches == null) { - return; - } - - if (coordsIntent != null) { - final Waypoint waypoint = new Waypoint("some place", waypointTypeIntent != null ? waypointTypeIntent : WaypointType.WAYPOINT, false); - waypoint.setCoords(coordsIntent); - - final CachesOverlayItemImpl item = getWaypointItem(waypoint); - overlayCaches.updateItems(item); - displayHandler.sendEmptyMessage(INVALIDATE_MAP); + private void displayPoint(final Geopoint coords) { + final Waypoint waypoint = new Waypoint("some place", waypointTypeIntent != null ? waypointTypeIntent : WaypointType.WAYPOINT, false); + waypoint.setCoords(coords); - cachesCnt = 1; - } else { - cachesCnt = 0; - } + final CachesOverlayItemImpl item = getWaypointItem(waypoint); + overlayCaches.updateItems(item); + displayHandler.sendEmptyMessage(INVALIDATE_MAP); + displayHandler.sendEmptyMessage(UPDATE_TITLE); - displayHandler.sendEmptyMessage(UPDATE_TITLE); - } + cachesCnt = 1; } private static abstract class DoRunnable implements Runnable { @@ -1309,19 +1252,6 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto } /** - * get if map is loading something - * - * @return - */ - private synchronized boolean isLoading() { - if (loadTimer != null) { - return loadTimer.isLoading(); - } - - return false; - } - - /** * store caches, invoked by "store offline" menu item * * @param listId @@ -1343,8 +1273,6 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto if (loadDetailsThread != null) { loadDetailsThread.stopIt(); } - - geoDirUpdate.startDir(); } catch (Exception e) { Log.e("CGeoMap.storeCaches.onCancel", e); } @@ -1392,8 +1320,6 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto return; } - deleteGeoDirObservers(); - for (final String geocode : geocodes) { try { if (handler.isCancelled()) { @@ -1410,14 +1336,10 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto detailProgress++; handler.sendEmptyMessage(UPDATE_PROGRESS); } - - // FIXME: what does this yield() do here? - yield(); } // we're done handler.sendEmptyMessage(FINISHED_LOADING_DETAILS); - addGeoDirObservers(); } } @@ -1609,7 +1531,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto } private CachesOverlayItemImpl getCacheItem(final Geocache cache) { - final CachesOverlayItemImpl item = mapItemFactory.getCachesOverlayItem(cache, cache.getType().applyDistanceRule()); + final CachesOverlayItemImpl item = mapItemFactory.getCachesOverlayItem(cache, cache.applyDistanceRule()); final int hashcode = new HashCodeBuilder() .append(cache.isReliableLatLon()) |
