package cgeo.geocaching.sensors;

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

import rx.Observable;

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];

    protected RotationProvider(final Context context) {
        sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
        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");
        }
    }

    @Override
    public void onSensorChanged(final SensorEvent event) {
        SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values);
        SensorManager.getOrientation(rotationMatrix, orientation);
        subscriber.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");
            sensorManager.registerListener(this, rotationSensor, SensorManager.SENSOR_DELAY_NORMAL);
        } else {
            subscriber.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) {
        return Observable.create(new RotationProvider(context));
    }

}

@@ -23,24 +23,66 @@
 g.sensors;%0A%0A
+import cgeo.geocaching.settings.Settings;%0A
 import cgeo.
@@ -181,16 +181,53 @@
 vable;%0A%0A
+import android.annotation.TargetApi;%0A
 import a
@@ -700,16 +700,35 @@
 at%5B4%5D;%0A%0A
+    @TargetApi(19)%0A
     prot
@@ -873,25 +873,210 @@
         
-rotationS
+// The geomagnetic rotation vector introduced in Android 4.4 (API 19) requires less power. Favour it%0A        // even if it is more sensible to noise in low-power settings.%0A        final Sensor s
 ensor = 
@@ -1118,16 +1118,28 @@
 or.TYPE_
+GEOMAGNETIC_
 ROTATION
@@ -1148,16 +1148,301 @@
 ECTOR);%0A
+        if (sensor != null && Settings.useLowPowerMode()) %7B%0A            rotationSensor = sensor;%0A            Log.d(%22RotationProvider: geomagnetic (low-power) sensor found%22);%0A        %7D else %7B%0A            rotationSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);%0A    
         
@@ -1463,32 +1463,36 @@
 nsor != null) %7B%0A
+    
             Log.
@@ -1528,32 +1528,36 @@
 ound%22);%0A        
+    
 %7D else %7B%0A       
@@ -1557,24 +1557,28 @@
             
+   
+ 
 Log.w(%22Rotat
@@ -1623,24 +1623,38 @@
 s device%22);%0A
+            %7D%0A
         %7D%0A  

Allow the use of low-power rotation sensor on KitKat