diff options
Diffstat (limited to 'main/src/cgeo/geocaching/sensors/GeoDirHandler.java')
| -rw-r--r-- | main/src/cgeo/geocaching/sensors/GeoDirHandler.java | 52 |
1 files changed, 52 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..e0b4da8 --- /dev/null +++ b/main/src/cgeo/geocaching/sensors/GeoDirHandler.java @@ -0,0 +1,52 @@ +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(); + + /** + * 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 Subscription start() { + return app.geoDirObservable().subscribe(new Action1<ImmutablePair<IGeoData, Float>>() { + @Override + public void call(final ImmutablePair<IGeoData, Float> geoDir) { + handleGeoDir(geoDir); + } + }, AndroidSchedulers.mainThread()); + } + +} |
