/**
 * 
 */
package com.astoev.cave.survey.service.azimuth;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.view.Surface;

/**
 * Azimuth processor that works with orientation sensor. Note that Orientation sensor is deprecated since api9
 * 
 * @author jmitrev
 */
public class OrientationAzimuthProcessor extends AzimuthProcessor {

	/** Orientation sensor */
    private Sensor orientationSensor;
	
	/** Last read value from the sensor*/
    private float lastValue;
    
	
    /**
     * Constructor for OrientationAzimuthProcessor
     * 
     * @param contextArg  - context to use
     * @param listenerArg - listener to notify on value change
     */
    public OrientationAzimuthProcessor(Context contextArg, AzimuthChangedListener listenerArg){
    	super(contextArg, listenerArg);
    }
    
	/**
	 * @see android.hardware.SensorEventListener#onSensorChanged(android.hardware.SensorEvent)
	 */
	@Override
	public void onSensorChanged(SensorEvent event) {
		float[] data = event.values;
		
		float currentValue = data[0] < 0 ? data[0] + 360 : data[0];
		
		// handle device rotation
		int rotation = getRotation();
		switch (rotation) {
		case Surface.ROTATION_0:
			break;
		case Surface.ROTATION_90:
			currentValue = currentValue + 90;
			if (currentValue > 360){
				currentValue %= 360;
			}
			break;
		case Surface.ROTATION_180:
			currentValue = currentValue - 180;
			if (currentValue < 0){
				currentValue += 360;
			}
			break;
		case Surface.ROTATION_270:
			currentValue = currentValue - 90;
			if (currentValue < 0){
				currentValue += 360;
			}
			break;
		default:
			break;
		}
		
		lastValue = currentValue;
		
		listener.onAzimuthChanged(lastValue);
	}

	/**
	 * @see com.astoev.cave.survey.service.azimuth.AzimuthProcessor#startListening()
	 */
	@Override
	public void startListening() {
		if (canReadAzimuth()){
			orientationSensor = getSensor();
            
            sensorManager.registerListener(this, orientationSensor, SENSOR_DELAY);
		}
	}

	/**
	 * @see com.astoev.cave.survey.service.azimuth.AzimuthProcessor#stopListening()
	 */
	@Override
	public void stopListening() {
		if (sensorManager != null && orientationSensor != null){
			sensorManager.unregisterListener(this, orientationSensor);
		}
	}

	/**
	 * @see com.astoev.cave.survey.service.azimuth.AzimuthProcessor#getLastValue()
	 */
	@Override
	public float getLastValue() {
		return lastValue;
	}
	
	/**
	 * @see com.astoev.cave.survey.service.azimuth.AzimuthProcessor#getSensor()
	 */
	@SuppressWarnings("deprecation")
	@Override
	public Sensor getSensor() {
		if (orientationSensor == null){
			orientationSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
		}
		return orientationSensor;
	}

}

@@ -1422,32 +1422,18 @@
 ntValue 
++
 =
- currentValue -
  180;%0A%09%09
@@ -1442,34 +1442,36 @@
 f (currentValue 
-%3C 
+%3E 36
 0)%7B%0A%09%09%09%09currentV
@@ -1467,33 +1467,33 @@
 %09%09%09currentValue 
-+
+%25
 = 360;%0A%09%09%09%7D%0A%09%09%09b
@@ -1547,26 +1547,13 @@
 lue 
++
 = 
-currentValue - 9
+27
 0;%0A%09
@@ -1571,18 +1571,20 @@
 ntValue 
-%3C 
+%3E 36
 0)%7B%0A%09%09%09%09
@@ -1596,17 +1596,17 @@
 ntValue 
-+
+%25
 = 360;%0A%09

change rotation calculation