From a9ef0d404b66843b218d69aa5a4aed7a124376ee Mon Sep 17 00:00:00 2001 From: Erkan Molla Date: Sun, 1 Nov 2015 09:49:11 +0100 Subject: [PATCH] Added ability to specify the sensor delay --- .../com/squareup/seismic/ShakeDetector.java | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/library/src/main/java/com/squareup/seismic/ShakeDetector.java b/library/src/main/java/com/squareup/seismic/ShakeDetector.java index b4fc6c1..34d7bae 100644 --- a/library/src/main/java/com/squareup/seismic/ShakeDetector.java +++ b/library/src/main/java/com/squareup/seismic/ShakeDetector.java @@ -22,7 +22,17 @@ public class ShakeDetector implements SensorEventListener { public static final int SENSITIVITY_MEDIUM = 13; public static final int SENSITIVITY_HARD = 15; + /** get sensor data as fast as possible */ + public static final int SENSOR_DELAY_FASTEST = SensorManager.SENSOR_DELAY_FASTEST; + /** rate suitable for games */ + public static final int SENSOR_DELAY_GAME = SensorManager.SENSOR_DELAY_GAME; + /** rate suitable for the user interface */ + public static final int SENSOR_DELAY_UI = SensorManager.SENSOR_DELAY_UI; + /** rate (default) suitable for screen orientation changes */ + public static final int SENSOR_DELAY_NORMAL = SensorManager.SENSOR_DELAY_NORMAL; + private static final int DEFAULT_ACCELERATION_THRESHOLD = SENSITIVITY_MEDIUM; + private static final int DEFAULT_SENSOR_DELAY = SENSOR_DELAY_FASTEST; /** * When the magnitude of total acceleration exceeds this @@ -30,6 +40,19 @@ public class ShakeDetector implements SensorEventListener { */ private int accelerationThreshold = DEFAULT_ACCELERATION_THRESHOLD; + /** + * The rate sensor events are delivered at. This is only a hint to the system. + * Events may be received faster or slower than the specified rate. + * Usually events are received faster. The value must be one of + * {@link #SENSOR_DELAY_NORMAL}, {@link #SENSOR_DELAY_UI}, + * {@link #SENSOR_DELAY_GAME}, or {@link #SENSOR_DELAY_FASTEST} or, + * the desired delay between events in microseconds. + * Specifying the delay in microseconds only works from Android 2.3 + * (API level 9) onwards. For earlier releases, you must use one of + * the SENSOR_DELAY_* constants. + */ + private int sensorDelay = DEFAULT_SENSOR_DELAY; + /** Listens for shakes. */ public interface Listener { /** Called on the main thread when the device is shaken. */ @@ -63,8 +86,7 @@ public boolean start(SensorManager sensorManager) { // If this phone has an accelerometer, listen to it. if (accelerometer != null) { this.sensorManager = sensorManager; - sensorManager.registerListener(this, accelerometer, - SensorManager.SENSOR_DELAY_FASTEST); + sensorManager.registerListener(this, accelerometer, sensorDelay); } return accelerometer != null; } @@ -109,6 +131,11 @@ public void setSensitivity(int accelerationThreshold) { this.accelerationThreshold = accelerationThreshold; } + /** Sets the sensor delay. */ + public void setSensorDelay(int sensorDelay) { + this.sensorDelay = sensorDelay; + } + /** Queue of samples. Keeps a running average. */ static class SampleQueue {