-package com.android.music;
-
-import android.content.Context;
-import android.hardware.Sensor;
-import android.hardware.SensorEvent;
-import android.hardware.SensorEventListener;
-import android.hardware.SensorManager;
-import android.os.SystemClock;
-import com.android.music.R;
-
-public class Shaker {
- private SensorManager mgr = null;
- private long lastShakeTimestamp = 0;
- private double threshold = 1.0d;
- private long gap = 0;
- private Shaker.Callback cb = null;
-
- public Shaker(Context ctxt, double threshold, long gap, Shaker.Callback cb) {
- this.threshold = threshold * threshold;
- this.threshold = this.threshold * SensorManager.GRAVITY_EARTH
- * SensorManager.GRAVITY_EARTH;
- this.gap = gap;
- this.cb = cb;
-
- mgr = (SensorManager) ctxt.getSystemService(Context.SENSOR_SERVICE);
- mgr.registerListener(listener,
- mgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
- SensorManager.SENSOR_DELAY_UI);
- }
-
- public void close() {
- mgr.unregisterListener(listener);
- }
-
- private void isShaking() {
- long now = SystemClock.uptimeMillis();
-
- if (lastShakeTimestamp == 0) {
- lastShakeTimestamp = now;
-
- if (cb != null) {
- cb.shakingStarted();
- }
- } else {
- lastShakeTimestamp = now;
- }
- }
-
- private void isNotShaking() {
- long now = SystemClock.uptimeMillis();
-
- if (lastShakeTimestamp > 0) {
- if (now - lastShakeTimestamp > gap) {
- lastShakeTimestamp = 0;
-
- if (cb != null) {
- cb.shakingStopped();
- }
- }
- }
- }
-
- public interface Callback {
- void shakingStarted();
-
- void shakingStopped();
- }
-
- private SensorEventListener listener = new SensorEventListener() {
- public void onSensorChanged(SensorEvent e) {
- if (e.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
- double netForce = e.values[0] * e.values[0];
-
- netForce += e.values[1] * e.values[1];
- netForce += e.values[2] * e.values[2];
-
- if (threshold < netForce) {
- isShaking();
- } else {
- isNotShaking();
- }
- }
- }
-
- public void onAccuracyChanged(Sensor sensor, int accuracy) {
- // unused
- }
- };
+package com.android.music;\r
+\r
+import android.content.Context;\r
+import android.hardware.Sensor;\r
+import android.hardware.SensorEvent;\r
+import android.hardware.SensorEventListener;\r
+import android.hardware.SensorManager;\r
+import android.os.SystemClock;\r
+\r
+public class Shaker {\r
+ private SensorManager mgr = null;\r
+ private long lastShakeTimestamp = 0;\r
+ private double threshold = 1.0d;\r
+ private long gap = 0;\r
+ private Shaker.Callback cb = null;\r
+\r
+ public Shaker(Context ctxt, double threshold, long gap, Shaker.Callback cb) {\r
+ this.threshold = threshold * threshold;\r
+ this.threshold = this.threshold * SensorManager.GRAVITY_EARTH\r
+ * SensorManager.GRAVITY_EARTH;\r
+ this.gap = gap;\r
+ this.cb = cb;\r
+\r
+ mgr = (SensorManager) ctxt.getSystemService(Context.SENSOR_SERVICE);\r
+ mgr.registerListener(listener,\r
+ mgr.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),\r
+ SensorManager.SENSOR_DELAY_UI);\r
+ }\r
+\r
+ public void close() {\r
+ mgr.unregisterListener(listener);\r
+ }\r
+\r
+ private void isShaking() {\r
+ long now = SystemClock.uptimeMillis();\r
+ try {\r
+ if (lastShakeTimestamp == 0) {\r
+ lastShakeTimestamp = now;\r
+\r
+ if (cb != null) {\r
+ cb.shakingStarted();\r
+ }\r
+ } else {\r
+ lastShakeTimestamp = now;\r
+ }\r
+ } catch (NullPointerException e) {\r
+\r
+ }\r
+ }\r
+\r
+ private void isNotShaking() {\r
+ long now = SystemClock.uptimeMillis();\r
+\r
+ if (lastShakeTimestamp > 0) {\r
+ if (now - lastShakeTimestamp > gap) {\r
+ lastShakeTimestamp = 0;\r
+\r
+ if (cb != null) {\r
+ cb.shakingStopped();\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ public interface Callback {\r
+ void shakingStarted();\r
+\r
+ void shakingStopped();\r
+ }\r
+\r
+ private SensorEventListener listener = new SensorEventListener() {\r
+ public void onSensorChanged(SensorEvent e) {\r
+ if (e.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {\r
+ double netForce = e.values[0] * e.values[0];\r
+\r
+ netForce += e.values[1] * e.values[1];\r
+ netForce += e.values[2] * e.values[2];\r
+\r
+ if (threshold < netForce) {\r
+ isShaking();\r
+ } else {\r
+ isNotShaking();\r
+ }\r
+ }\r
+ }\r
+\r
+ public void onAccuracyChanged(Sensor sensor, int accuracy) {\r
+ // unused\r
+ }\r
+ };\r
}
\ No newline at end of file