aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/sensors/RotationProvider.java
blob: c63e39f5160018cbb5791df5818cb0bb6a1e4222 (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
package cgeo.geocaching.sensors;

import cgeo.geocaching.utils.Log;
import cgeo.geocaching.utils.RxUtils.LooperCallbacks;

import rx.Observable;

import android.annotation.TargetApi;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

public class RotationProvider extends LooperCallbacks<Float> implements SensorEventListener {

    private final SensorManager sensorManager;
    private final Sensor rotationSensor;
    private final float[] rotationMatrix = new float[16];
    private final float[] orientation = new float[4];
    private final float[] values = new float[4];

    @TargetApi(19)
    protected RotationProvider(final Context context, final boolean lowPower) {
        sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
        // The geomagnetic rotation vector introduced in Android 4.4 (API 19) requires less power. Favour it
        // even if it is more sensible to noise in low-power settings.
        final Sensor sensor = lowPower ? sensorManager.getDefaultSensor(Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR) : null;
        if (sensor != null) {
            rotationSensor = sensor;
            Log.d("RotationProvider: geomagnetic (low-power) sensor found");
        } else {
            rotationSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
            if (rotationSensor != null) {
                Log.d("RotationProvider: sensor found");
            } else {
                Log.w("RotationProvider: no rotation sensor on this device");
            }
        }
    }

    public static boolean hasRotationSensor(final Context context) {
        return ((SensorManager) context.getSystemService(Context.SENSOR_SERVICE)).getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR) != null;
    }

    @TargetApi(19)
    public static boolean hasGeomagneticRotationSensor(final Context context) {
        return ((SensorManager) context.getSystemService(Context.SENSOR_SERVICE)).getDefaultSensor(Sensor.TYPE_GEOMAGNETIC_ROTATION_VECTOR) != null;
    }

    @Override
    public void onSensorChanged(final SensorEvent event) {
        // On some Samsung devices, SensorManager#getRotationMatrixFromVector throws an exception if the rotation
        // vector has more than 4 elements. Since only the four first elements are used, we can truncate the vector
        // without losing precision.
        if (event.values.length > 4) {
            System.arraycopy(event.values, 0, values, 0, 4);
            SensorManager.getRotationMatrixFromVector(rotationMatrix, values);
        } else {
            SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values);
        }
        SensorManager.getOrientation(rotationMatrix, orientation);
        subject.onNext((float) (orientation[0] * 180 / Math.PI));
    }

    @Override
    public void onAccuracyChanged(final Sensor sensor, final int accuracy) {
    }

    @Override
    public void onStart() {
        if (rotationSensor != null) {
            Log.d("RotationProvider: starting the rotation provider");
            try {
                sensorManager.registerListener(this, rotationSensor, SensorManager.SENSOR_DELAY_NORMAL);
            } catch (final Exception e) {
                Log.w("RotationProvider: unable to register listener", e);
                subject.onError(e);
            }
        } else {
            subject.onError(new RuntimeException("rotation sensor is absent on this device"));
        }
    }

    @Override
    public void onStop() {
        if (rotationSensor != null) {
            Log.d("RotationProvider: stopping the rotation provider");
            sensorManager.unregisterListener(this);
        }
    }

    public static Observable<Float> create(final Context context, final boolean lowPower) {
        return Observable.create(new RotationProvider(context, lowPower)).onBackpressureDrop();
    }

}