diff options
Diffstat (limited to 'main/src/cgeo/geocaching/sensors/GeoDirHandler.java')
| -rw-r--r-- | main/src/cgeo/geocaching/sensors/GeoDirHandler.java | 84 |
1 files changed, 73 insertions, 11 deletions
diff --git a/main/src/cgeo/geocaching/sensors/GeoDirHandler.java b/main/src/cgeo/geocaching/sensors/GeoDirHandler.java index 588bd84..37a0c5b 100644 --- a/main/src/cgeo/geocaching/sensors/GeoDirHandler.java +++ b/main/src/cgeo/geocaching/sensors/GeoDirHandler.java @@ -4,10 +4,13 @@ import cgeo.geocaching.CgeoApplication; import cgeo.geocaching.settings.Settings; import org.apache.commons.lang3.tuple.ImmutablePair; - +import rx.Observable; import rx.Subscription; import rx.android.schedulers.AndroidSchedulers; import rx.functions.Action1; +import rx.functions.Func1; +import rx.functions.Func2; +import rx.subscriptions.CompositeSubscription; /** * GeoData and Direction handler. @@ -17,10 +20,34 @@ import rx.functions.Action1; * accordingly in {@code onPause}. */ public abstract class GeoDirHandler { + + public static final int UPDATE_GEODATA = 1 << 1; + public static final int UPDATE_DIRECTION = 1 << 2; + public static final int UPDATE_GEODIR = 1 << 3; + private static final CgeoApplication app = CgeoApplication.getInstance(); /** + * Update method called when new geodata is available. This method is called on the UI thread. + * {@link #start(int)} must be called with the {@link #UPDATE_GEODATA} flag set. + * + * @param geoData the new geographical data + */ + public void updateGeoData(@SuppressWarnings("unused") final IGeoData geoData) { + } + + /** + * Update method called when new direction is available. This method is called on the UI thread. + * {@link #start(int)} must be called with the {@link #UPDATE_DIRECTION} flag set. + * + * @param direction the new direction + */ + public void updateDirection(@SuppressWarnings("unused") final float direction) { + } + + /** * Update method called when new data is available. This method is called on the UI thread. + * {@link #start(int)} must be called with the {@link #UPDATE_GEODIR} flag set. * * @param geoData the new geographical data * @param direction the new direction @@ -31,23 +58,58 @@ public abstract class GeoDirHandler { 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; + private static Observable<Float> fixedDirection() { + return app.directionObservable().map(new Func1<Float, Float>() { + @Override + public Float call(final Float direction) { + final IGeoData geoData = app.currentGeo(); + return fixDirection(geoData, direction); + } + }); + + } + + private static float fixDirection(final IGeoData geoData, final float direction) { final boolean useGPSBearing = !Settings.isUseCompass() || geoData.getSpeed() > 5; - updateGeoDir(geoData, useGPSBearing ? geoData.getBearing() : geoDir.right); + return useGPSBearing ? geoData.getBearing() : direction; } /** * Register the current GeoDirHandler for GeoData and direction information (if the * preferences allow it). */ - public Subscription start() { - return app.geoDirObservable().observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<ImmutablePair<IGeoData, Float>>() { - @Override - public void call(final ImmutablePair<IGeoData, Float> geoDir) { - handleGeoDir(geoDir); - } - }); + public Subscription start(final int flags) { + final CompositeSubscription subscriptions = new CompositeSubscription(); + if ((flags & UPDATE_GEODATA) != 0) { + subscriptions.add(app.geoDataObservable().observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<IGeoData>() { + @Override + public void call(final IGeoData geoData) { + updateGeoData(geoData); + } + })); + } + if ((flags & UPDATE_DIRECTION) != 0) { + subscriptions.add(fixedDirection().observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Float>() { + @Override + public void call(final Float direction) { + updateDirection(direction); + } + })); + } + if ((flags & UPDATE_GEODIR) != 0) { + subscriptions.add(Observable.combineLatest(app.geoDataObservable(), app.directionObservable(), new Func2<IGeoData, Float, ImmutablePair<IGeoData, Float>>() { + @Override + public ImmutablePair<IGeoData, Float> call(final IGeoData geoData, final Float direction) { + return ImmutablePair.of(geoData, fixDirection(geoData, direction)); + } + }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<ImmutablePair<IGeoData, Float>>() { + @Override + public void call(final ImmutablePair<IGeoData, Float> geoDir) { + updateGeoDir(geoDir.left, geoDir.right); + } + })); + } + return subscriptions; } } |
