aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/sensors/GeoDirHandler.java
diff options
context:
space:
mode:
authorSamuel Tardieu <sam@rfc1149.net>2014-03-06 08:46:44 +0100
committerSamuel Tardieu <sam@rfc1149.net>2014-03-06 18:31:58 +0100
commit48b7cbaa0b469794088d41038192366fea802190 (patch)
tree8743ff2a6589957c01aabe4af3434f9d7fe7be7c /main/src/cgeo/geocaching/sensors/GeoDirHandler.java
parentbc29353af3aaa64469968a2055690bdafd299309 (diff)
downloadcgeo-48b7cbaa0b469794088d41038192366fea802190.zip
cgeo-48b7cbaa0b469794088d41038192366fea802190.tar.gz
cgeo-48b7cbaa0b469794088d41038192366fea802190.tar.bz2
refactoring: create cgeo.geocaching.sensors package
Diffstat (limited to 'main/src/cgeo/geocaching/sensors/GeoDirHandler.java')
-rw-r--r--main/src/cgeo/geocaching/sensors/GeoDirHandler.java128
1 files changed, 128 insertions, 0 deletions
diff --git a/main/src/cgeo/geocaching/sensors/GeoDirHandler.java b/main/src/cgeo/geocaching/sensors/GeoDirHandler.java
new file mode 100644
index 0000000..146ff76
--- /dev/null
+++ b/main/src/cgeo/geocaching/sensors/GeoDirHandler.java
@@ -0,0 +1,128 @@
+package cgeo.geocaching.sensors;
+
+import cgeo.geocaching.CgeoApplication;
+import cgeo.geocaching.settings.Settings;
+
+import rx.Scheduler;
+import rx.Subscription;
+import rx.android.schedulers.AndroidSchedulers;
+import rx.functions.Action1;
+import rx.schedulers.Schedulers;
+
+import java.util.concurrent.TimeUnit;
+
+/**
+ * GeoData and Direction handler.
+ * <p>
+ * To use this class, override at least one of {@link #updateDirection(float)} or {@link #updateGeoData(IGeoData)}. You
+ * need to start the handler using one of
+ * <ul>
+ * <li>{@link #startDir()}</li>
+ * <li>{@link #startGeo()}</li>
+ * <li>{@link #startGeoAndDir()}</li>
+ * </ul>
+ * A good place might be the {@code onResume} method of the Activity. Stop the Handler accordingly in {@code onPause}.
+ * </p>
+ */
+public abstract class GeoDirHandler {
+ private static final CgeoApplication app = CgeoApplication.getInstance();
+
+ private Subscription dirSubscription = null;
+ private Subscription geoSubscription = null;
+
+ /**
+ * Update method called when new IGeoData is available.
+ *
+ * @param data
+ * the new data
+ */
+ public void updateGeoData(final IGeoData data) {
+ // Override this in children
+ }
+
+ /**
+ * Update method called when new direction data is available.
+ *
+ * @param direction
+ * the new direction
+ */
+ public void updateDirection(final float direction) {
+ // Override this in children
+ }
+
+ /**
+ * Register the current GeoDirHandler for GeoData information.
+ */
+ public synchronized void startGeo() {
+ geoSubscription = app.currentGeoObject()
+ .subscribeOn(AndroidSchedulers.mainThread())
+ .subscribe(new Action1<IGeoData>() {
+ @Override
+ public void call(final IGeoData geoData) {
+ updateGeoData(geoData);
+ }
+ });
+ }
+
+ /**
+ * Register the current GeoDirHandler for direction information if the preferences
+ * allow it.
+ */
+ public synchronized void startDir() {
+ if (Settings.isUseCompass()) {
+ dirSubscription = app.currentDirObject()
+ .subscribeOn(AndroidSchedulers.mainThread())
+ .subscribe(new Action1<Float>() {
+ @Override
+ public void call(final Float direction) {
+ updateDirection(direction);
+ }
+ });
+ }
+ }
+
+ /**
+ * Register the current GeoDirHandler for GeoData and direction information (if the
+ * preferences allow it).
+ */
+ public void startGeoAndDir() {
+ startGeo();
+ startDir();
+ }
+
+ /**
+ * Unregister the current GeoDirHandler for GeoData information.
+ */
+ public synchronized void stopGeo() {
+ // Delay the unsubscription by 2.5 seconds, so that another activity has
+ // the time to subscribe and the GPS receiver will not be turned down.
+ if (geoSubscription != null) {
+ final Subscription subscription = geoSubscription;
+ geoSubscription = null;
+ Schedulers.newThread().schedule(new Action1<Scheduler.Inner>() {
+ @Override
+ public void call(final Scheduler.Inner inner) {
+ subscription.unsubscribe();
+ }
+ }, 2500, TimeUnit.MILLISECONDS);
+ }
+ }
+
+ /**
+ * Unregister the current GeoDirHandler for direction information.
+ */
+ public synchronized void stopDir() {
+ if (dirSubscription != null) {
+ dirSubscription.unsubscribe();
+ dirSubscription = null;
+ }
+ }
+
+ /**
+ * Unregister the current GeoDirHandler for GeoData and direction information.
+ */
+ public void stopGeoAndDir() {
+ stopGeo();
+ stopDir();
+ }
+}