package ch.hearc.motioncontrol.listeners;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.FloatMath;

import ch.hearc.motioncontrol.interfaces.OnShakeListener;

public class ShakeListener implements SensorEventListener {

    /*
     * The gForce that is necessary to register as shake.
     * Must be greater than 1G (one earth gravity unit).
     * You can install "G-Force", by Blake La Pierre
     * from the Google Play Store and run it to see how
     *  many G's it takes to register a shake
     */
    private static final float SHAKE_THRESHOLD_GRAVITY = 2.7F;
    private static final int SHAKE_SLOP_TIME_MS = 500;
    private static final int SHAKE_COUNT_RESET_TIME_MS = 3000;

    private OnShakeListener mListener;
    private long mShakeTimestamp;
    private int mShakeCount;

    public void setOnShakeListener(OnShakeListener listener) {
        this.mListener = listener;
    }


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

    @Override
    public void onSensorChanged(SensorEvent event) {

        if (mListener != null) {
            double x = event.values[0];
            double y = event.values[1];
            double z = event.values[2];

            double gX = x / SensorManager.GRAVITY_EARTH;
            double gY = y / SensorManager.GRAVITY_EARTH;
            double gZ = z / SensorManager.GRAVITY_EARTH;

            // gForce will be close to 1 when there is no movement.
            double gForce = Math.sqrt(gX * gX + gY * gY + gZ * gZ);

            if (gForce > SHAKE_THRESHOLD_GRAVITY) {
                final long now = System.currentTimeMillis();
                // ignore shake events too close to each other (500ms)
                if (mShakeTimestamp + SHAKE_SLOP_TIME_MS > now) {
                    return;
                }

                // reset the shake count after 3 seconds of no shakes
                if (mShakeTimestamp + SHAKE_COUNT_RESET_TIME_MS < now) {
                    mShakeCount = 0;
                }

                mShakeTimestamp = now;
                mShakeCount++;

                mListener.onShake(mShakeCount);
            }
        }
    }
}



@@ -35,16 +35,58 @@
 teners;%0A
+import android.content.SharedPreferences;%0A
 import a
@@ -226,24 +226,69 @@
 sorManager;%0A
+import android.preference.PreferenceManager;%0A
 import andro
@@ -894,16 +894,44 @@
  3000;%0A%0A
+    private boolean enable;%0A
     priv
@@ -1023,16 +1023,45 @@
 keCount;
+%0A    SharedPreferences prefs;
 %0A%0A    pu
@@ -1089,16 +1089,41 @@
 istener(
+SharedPreferences prefs, 
 OnShakeL
@@ -1173,24 +1173,50 @@
 = listener;%0A
+        this.prefs=prefs;%0A
     %7D%0A%0A%0A    
@@ -1382,17 +1382,97 @@
 vent) %7B%0A
-%0A
+        if (prefs.getBoolean(%22enable-shake-device-to-erase-history%22, true))%7B%0A    
         
@@ -1504,24 +1504,28 @@
             
+    
 double x = e
@@ -1540,16 +1540,20 @@
 ues%5B0%5D;%0A
+    
         
@@ -1592,24 +1592,28 @@
             
+   
+ 
 double z = e
@@ -1629,16 +1629,20 @@
 es%5B2%5D;%0A%0A
+    
         
@@ -1694,32 +1694,36 @@
 TH;%0A            
+ 
+   
 double gY = y / 
@@ -1755,32 +1755,36 @@
 TH;%0A            
+   
+ 
 double gZ = z / 
@@ -1813,16 +1813,20 @@
 EARTH;%0A%0A
+    
         
@@ -1889,32 +1889,36 @@
 nt.%0A            
+ 
+   
 double gForce = 
@@ -1966,24 +1966,28 @@
             
+  
+  
 if (gForce %3E
@@ -2010,24 +2010,28 @@
 _GRAVITY) %7B%0A
+    
             
@@ -2087,32 +2087,36 @@
                 
+    
 // ignore shake 
@@ -2162,32 +2162,36 @@
                 
+  
+  
 if (mShakeTimest
@@ -2244,16 +2244,20 @@
         
+    
 return;%0A
@@ -2264,32 +2264,36 @@
                 
+    
 %7D%0A%0A             
@@ -2295,16 +2295,20 @@
         
+ 
+   
 // reset
@@ -2353,16 +2353,20 @@
  shakes%0A
+    
         
@@ -2442,32 +2442,36 @@
                 
+    
 mShakeCount = 0;
@@ -2479,35 +2479,43 @@
                 
+ 
-%7D%0A%0A
+   %7D%0A%0A    
                 
@@ -2537,16 +2537,20 @@
  = now;%0A
+    
         
@@ -2589,16 +2589,20 @@
         
+    
 mListene
@@ -2625,16 +2625,34 @@
 Count);%0A
+                %7D%0A
         

Update de l'option "Shake"