package de.tud.ess;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.AttributeSet;
import android.widget.ListView;

public class HeadListView extends ListView implements SensorEventListener {

  private Sensor mSensor;
  private int mLastAccuracy;
  private SensorManager mSensorManager;
  private float mStartX = 10;
  private static final int SENSOR_RATE_uS = 200000;
  private static final float VELOCITY = -1000; // from rad to pixels

  
  public HeadListView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
  }

  public HeadListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
  }

  public HeadListView(Context context) {
    super(context);
    init();
  }

  public void init() {
    mSensorManager = (SensorManager) getContext().getSystemService(Context.SENSOR_SERVICE);
    mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
  }
  
  public void activate() {
    if (mSensor == null)
      return;
    
    mSensorManager.registerListener(this, mSensor, SENSOR_RATE_uS);
    mStartX = 10;
  }
  
  public void deactivate() {
    mSensorManager.unregisterListener(this);
    mStartX = 10;
  }
  
  @Override
  public void onAccuracyChanged(Sensor sensor, int accuracy) {
    mLastAccuracy = accuracy;
  }

  @Override
  public void onSensorChanged(SensorEvent event) {
    float[] mat = new float[9], 
            orientation = new float[3];
    
    if (mLastAccuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
      return;
    
    SensorManager.getRotationMatrixFromVector(mat, event.values);
    SensorManager.remapCoordinateSystem(mat, SensorManager.AXIS_X, SensorManager.AXIS_Z, mat);
    SensorManager.getOrientation(mat, orientation);
    
    float z = orientation[0], // see https://developers.google.com/glass/develop/gdk/location-sensors/index
          x = orientation[1],
          y = orientation[2];
    
    if (mStartX  == 10)
      mStartX = x;

    int MAX_RANGE = 800;
    int value = (int) ((mStartX-x) * VELOCITY);
    int adjValue = (int) (value + (MAX_RANGE / 2));
    adjValue = Math.min(adjValue, MAX_RANGE);
    adjValue = Math.max(adjValue, 0);
    
    int interval = (int) MAX_RANGE /  getCount();
    int position =  (int) adjValue / interval;

    setSelection(position);
    
  }

}

@@ -237,24 +237,50 @@
 ributeSet;%0D%0A
+import android.util.Log;%0D%0A
 import andro
@@ -381,16 +381,62 @@
 er %7B%0D%0A%0D%0A
+  private static final float INVALID_X = 10;%0D%0A
   privat
@@ -547,26 +547,33 @@
 t mStartX = 
-10
+INVALID_X
 ;%0D%0A  private
@@ -660,36 +660,62 @@
 Y = 
--1000; // from rad to pixels
+(float) (Math.PI / 180 * 2); // scroll one item per 2%C2%B0
 %0D%0A%0D%0A
@@ -1319,24 +1319,50 @@
 urn;%0D%0A    %0D%0A
+    mStartX = INVALID_X;%0D%0A
     mSensorM
@@ -1418,35 +1418,16 @@
 E_uS);%0D%0A
-    mStartX = 10;%0D%0A
   %7D%0D%0A  %0D
@@ -1513,26 +1513,33 @@
   mStartX = 
-10
+INVALID_X
 ;%0D%0A  %7D%0D%0A  %0D%0A
@@ -2305,18 +2305,40 @@
 rtX  == 
-10
+INVALID_X %7C%7C mStartX %3E x
 )%0D%0A     
@@ -2356,34 +2356,16 @@
 x;%0D%0A
-%0D%0A
     
-int MAX_RANGE = 800;
+    
 %0D%0A  
@@ -2370,21 +2370,24 @@
     int 
-value
+position
  = (int)
@@ -2400,14 +2400,19 @@
 artX
--
+ - 
 x) * 
+-1/
 VELO
@@ -2427,208 +2427,18 @@
     
-int adjValue = (int) (value + (MAX_RANGE / 2));%0D%0A    adjValue = Math.min(adjValue, MAX_RANGE);%0D%0A    adjValue = Math.max(adjValue, 0);%0D%0A    %0D%0A    int interval = (int) MAX_RANGE /  getCount();%0D%0A    int 
+%0D%0A    if (
 posi
@@ -2446,41 +2446,17 @@
 ion 
+%3E
 = 
- (int) adjValue / interval;%0D%0A%0D%0A
+0)%0D%0A  
     

simplify headlistview formula