diff options
Diffstat (limited to 'main/src/cgeo/geocaching/sensors/GeoDirHandler.java')
| -rw-r--r-- | main/src/cgeo/geocaching/sensors/GeoDirHandler.java | 61 |
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(); + } + +} |
