aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/sensors/GeoDirHandler.java
blob: d127784da28ff4d664dad3de11950736031c0526 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package cgeo.geocaching.sensors;

import cgeo.geocaching.CgeoApplication;
import cgeo.geocaching.settings.Settings;
import cgeo.geocaching.utils.AngleUtils;

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.
 * <p>
 * To use this class, override {@link #updateGeoDir(IGeoData, float)}. You need to start the handler using
 * {@link #start(int)}. A good place to do so might be the {@code onResume} method of the Activity. Stop the Handler
 * accordingly in {@code onPause}.
 *
 * The direction is always relative to the top of the device (natural direction), and that it must
 * be fixed using {@link cgeo.geocaching.utils.AngleUtils#getDirectionNow(float)}. When the direction is derived from the GPS,
 * it is altered so that the fix can still be applied as if the information came from the compass.
 */
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;
    public static final int LOW_POWER = 1 << 4;

    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(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(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
     *
     * 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(final IGeoData geoData, final float direction) {
    }

    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;
        return useGPSBearing ? AngleUtils.reverseDirectionNow(geoData.getBearing()) : direction;
    }

    /**
     * Register the current GeoDirHandler for GeoData and direction information (if the
     * preferences allow it).
     */
    public Subscription start(final int flags) {
        final CompositeSubscription subscriptions = new CompositeSubscription();
        final boolean lowPower = (flags & LOW_POWER) != 0;
        if ((flags & UPDATE_GEODATA) != 0) {
            subscriptions.add(app.geoDataObservable(lowPower).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(lowPower), 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;
    }

}