diff options
| author | Samuel Tardieu <sam@rfc1149.net> | 2014-03-10 15:38:10 +0100 |
|---|---|---|
| committer | Samuel Tardieu <sam@rfc1149.net> | 2014-03-10 15:45:12 +0100 |
| commit | 638e06a1cd98a12bb1e0f86bd0770141abdabae9 (patch) | |
| tree | 9c9b65c92328184d91d01491d14ca4ab467ad7b7 | |
| parent | e6c28990874bccf5dadd6bd67a41e4d7463d4b3b (diff) | |
| download | cgeo-638e06a1cd98a12bb1e0f86bd0770141abdabae9.zip cgeo-638e06a1cd98a12bb1e0f86bd0770141abdabae9.tar.gz cgeo-638e06a1cd98a12bb1e0f86bd0770141abdabae9.tar.bz2 | |
Use a subscription model for GeoDirHandler
5 files changed, 40 insertions, 73 deletions
diff --git a/main/src/cgeo/geocaching/CacheListActivity.java b/main/src/cgeo/geocaching/CacheListActivity.java index cde7a79..f07c771 100644 --- a/main/src/cgeo/geocaching/CacheListActivity.java +++ b/main/src/cgeo/geocaching/CacheListActivity.java @@ -57,6 +57,7 @@ import org.apache.commons.collections4.ListUtils; import org.apache.commons.lang3.StringUtils; import org.eclipse.jdt.annotation.NonNull; +import rx.Subscription; import rx.functions.Action1; import android.app.Activity; @@ -135,6 +136,7 @@ public class CacheListActivity extends AbstractListActivity implements FilteredA }; private ContextMenuInfo lastMenuInfo; private String contextMenuGeocode = ""; + private Subscription resumeSubscription; // FIXME: This method has mostly been replaced by the loaders. But it still contains a license agreement check. public void handleCachesLoaded() { @@ -454,7 +456,7 @@ public class CacheListActivity extends AbstractListActivity implements FilteredA public void onResume() { super.onResume(); - geoDirHandler.start(); + resumeSubscription = geoDirHandler.start(); adapter.setSelectMode(false); setAdapterCurrentCoordinates(true); @@ -485,7 +487,7 @@ public class CacheListActivity extends AbstractListActivity implements FilteredA @Override public void onPause() { - geoDirHandler.stop(); + resumeSubscription.unsubscribe(); super.onPause(); } diff --git a/main/src/cgeo/geocaching/activity/AbstractActivity.java b/main/src/cgeo/geocaching/activity/AbstractActivity.java index 43fbcf2..fd83043 100644 --- a/main/src/cgeo/geocaching/activity/AbstractActivity.java +++ b/main/src/cgeo/geocaching/activity/AbstractActivity.java @@ -82,6 +82,7 @@ public abstract class AbstractActivity extends FragmentActivity implements IAbst @Override public void onPause() { resumeSubscription.unsubscribe(); + super.onPause(); } protected static void disableSuggestions(final EditText edit) { diff --git a/main/src/cgeo/geocaching/maps/CGeoMap.java b/main/src/cgeo/geocaching/maps/CGeoMap.java index 8b6764d..61b321d 100644 --- a/main/src/cgeo/geocaching/maps/CGeoMap.java +++ b/main/src/cgeo/geocaching/maps/CGeoMap.java @@ -41,6 +41,8 @@ import cgeo.geocaching.utils.Log; 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; @@ -67,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; @@ -87,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 { @@ -144,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; @@ -489,8 +495,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto @Override public void onResume() { super.onResume(); - - geoDirUpdate.start(); + resumeSubscription = Subscriptions.from(geoDirUpdate.start(), startTimer()); if (!CollectionUtils.isEmpty(dirtyCaches)) { for (String geocode : dirtyCaches) { @@ -506,14 +511,11 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto // Update display displayExecutor.execute(new DisplayRunnable(mapView.getViewport())); } - - startTimer(); } @Override public void onPause() { - stopTimer(); - geoDirUpdate.stop(); + resumeSubscription.unsubscribe(); savePrefs(); if (mapView != null) { @@ -966,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 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(); @@ -1039,17 +1019,22 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto } } 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); } /** @@ -1267,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 diff --git a/main/src/cgeo/geocaching/sensors/GeoDirHandler.java b/main/src/cgeo/geocaching/sensors/GeoDirHandler.java index 917b9ff..e0b4da8 100644 --- a/main/src/cgeo/geocaching/sensors/GeoDirHandler.java +++ b/main/src/cgeo/geocaching/sensors/GeoDirHandler.java @@ -18,8 +18,6 @@ import rx.functions.Action1; public abstract class GeoDirHandler { private static final CgeoApplication app = CgeoApplication.getInstance(); - private Subscription subscription = null; - /** * Update method called when new data is available. * @@ -43,20 +41,12 @@ public abstract class GeoDirHandler { * preferences allow it). */ public Subscription start() { - subscription = app.geoDirObservable().subscribe(new Action1<ImmutablePair<IGeoData, Float>>() { + return app.geoDirObservable().subscribe(new Action1<ImmutablePair<IGeoData, Float>>() { @Override public void call(final ImmutablePair<IGeoData, Float> geoDir) { handleGeoDir(geoDir); } }, AndroidSchedulers.mainThread()); - return subscription; - } - - /** - * Unregister the current GeoDirHandler for GeoData information. - */ - public void stop() { - subscription.unsubscribe(); } } diff --git a/main/src/cgeo/geocaching/speech/SpeechService.java b/main/src/cgeo/geocaching/speech/SpeechService.java index b13218c..086227d 100644 --- a/main/src/cgeo/geocaching/speech/SpeechService.java +++ b/main/src/cgeo/geocaching/speech/SpeechService.java @@ -17,6 +17,7 @@ import android.os.IBinder; import android.speech.tts.TextToSpeech; import android.speech.tts.TextToSpeech.Engine; import android.speech.tts.TextToSpeech.OnInitListener; +import rx.Subscription; import java.util.Locale; @@ -75,6 +76,7 @@ public class SpeechService extends Service implements OnInitListener { private long lastSpeechTime = 0; private float lastSpeechDistance = 0.0f; private Geopoint target; + private Subscription initSubscription; @Override public IBinder onBind(Intent intent) { @@ -106,7 +108,7 @@ public class SpeechService extends Service implements OnInitListener { @Override public void onDestroy() { - geoDirHandler.stop(); + initSubscription.unsubscribe(); if (tts != null) { tts.stop(); tts.shutdown(); @@ -141,7 +143,7 @@ public class SpeechService extends Service implements OnInitListener { initialized = true; - geoDirHandler.start(); + initSubscription = geoDirHandler.start(); } @Override |
