aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/sensors/GeoDirHandler.java
diff options
context:
space:
mode:
authorSamuel Tardieu <sam@rfc1149.net>2014-03-08 10:04:01 +0100
committerSamuel Tardieu <sam@rfc1149.net>2014-03-08 10:04:01 +0100
commite0ab26ff7af5a1df16b21af21bd683b53fabaaa8 (patch)
tree6ac0baa569386174b5acf33b683507bf008f0092 /main/src/cgeo/geocaching/sensors/GeoDirHandler.java
parent534c9c813a2bce2c65dc13663df9e3ad14fd30f1 (diff)
parent291a622a9d8b1e5c791157a61e5ef467710be42d (diff)
downloadcgeo-e0ab26ff7af5a1df16b21af21bd683b53fabaaa8.zip
cgeo-e0ab26ff7af5a1df16b21af21bd683b53fabaaa8.tar.gz
cgeo-e0ab26ff7af5a1df16b21af21bd683b53fabaaa8.tar.bz2
Merge branch 'use-combined-provider' into upstream
Diffstat (limited to 'main/src/cgeo/geocaching/sensors/GeoDirHandler.java')
-rw-r--r--main/src/cgeo/geocaching/sensors/GeoDirHandler.java61
1 files changed, 61 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..523fb1f
--- /dev/null
+++ b/main/src/cgeo/geocaching/sensors/GeoDirHandler.java
@@ -0,0 +1,61 @@
+package cgeo.geocaching.sensors;
+
+import cgeo.geocaching.CgeoApplication;
+import cgeo.geocaching.settings.Settings;
+
+import org.apache.commons.lang3.tuple.ImmutablePair;
+import rx.Subscription;
+import rx.android.schedulers.AndroidSchedulers;
+import rx.functions.Action1;
+
+/**
+ * GeoData and Direction handler.
+ * <p>
+ * To use this class, override {@link #updateGeoDir(IGeoData, Float)}. You need to start the handler using
+ * {@link #start()}. A good place to do so might be the {@code onResume} method of the Activity. Stop the Handler
+ * accordingly in {@code onPause}.
+ */
+public abstract class GeoDirHandler {
+ private static final CgeoApplication app = CgeoApplication.getInstance();
+
+ private Subscription subscription = null;
+
+ /**
+ * Update method called when new data is available.
+ *
+ * @param geoData the new geographical data
+ * @param direction the new direction
+ *
+ * If the device goes fast enough, or if the compass use is not enabled in the settings,
+ * the GPS direction information will be used instead of the compass one.
+ */
+ public void updateGeoDir(@SuppressWarnings("unused") final IGeoData geoData, @SuppressWarnings("unused") final float direction) {
+ }
+
+ private void handleGeoDir(final ImmutablePair<IGeoData, Float> geoDir) {
+ final IGeoData geoData = geoDir.left;
+ final boolean useGPSBearing = !Settings.isUseCompass() || geoData.getSpeed() > 5;
+ updateGeoDir(geoData, useGPSBearing ? geoData.getBearing() : geoDir.right);
+ }
+
+ /**
+ * Register the current GeoDirHandler for GeoData and direction information (if the
+ * preferences allow it).
+ */
+ public void start() {
+ subscription = app.geoDirObservable().subscribe(new Action1<ImmutablePair<IGeoData, Float>>() {
+ @Override
+ public void call(final ImmutablePair<IGeoData, Float> geoDir) {
+ handleGeoDir(geoDir);
+ }
+ }, AndroidSchedulers.mainThread());
+ }
+
+ /**
+ * Unregister the current GeoDirHandler for GeoData information.
+ */
+ public void stop() {
+ subscription.unsubscribe();
+ }
+
+}