You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by bo...@apache.org on 2012/05/24 18:31:08 UTC

[10/15] android commit: [CB-463] added accuracy checking to native accel implementation, this way getCurrentAcceleration returns fairly accurate results

[CB-463] added accuracy checking to native accel implementation, this way getCurrentAcceleration returns fairly accurate results


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/commit/cb98bbce
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/tree/cb98bbce
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/diff/cb98bbce

Branch: refs/heads/CordovaWebView
Commit: cb98bbce1f9a99fa5e8af44886979f8614cb390f
Parents: 24adc6d
Author: Fil Maj <ma...@gmail.com>
Authored: Mon May 14 15:21:41 2012 -0700
Committer: Fil Maj <ma...@gmail.com>
Committed: Fri May 18 15:20:54 2012 -0700

----------------------------------------------------------------------
 .../src/org/apache/cordova/AccelListener.java      |   36 +++++++++++----
 1 files changed, 26 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/cb98bbce/framework/src/org/apache/cordova/AccelListener.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/AccelListener.java b/framework/src/org/apache/cordova/AccelListener.java
index 40ddec4..7596045 100755
--- a/framework/src/org/apache/cordova/AccelListener.java
+++ b/framework/src/org/apache/cordova/AccelListener.java
@@ -54,6 +54,7 @@ public class AccelListener extends Plugin implements SensorEventListener {
     private float x,y,z;						        // most recent acceleration values
     private long timestamp;					        // time of most recent value
     private int status;							        // status of listener
+    private int accuracy = SensorManager.SENSOR_STATUS_UNRELIABLE;
 
     private SensorManager sensorManager;    // Sensor manager
     private Sensor mSensor;						      // Acceleration sensor returned by sensor manager
@@ -184,7 +185,7 @@ public class AccelListener extends Plugin implements SensorEventListener {
     	// If found, then register as listener
     	if ((list != null) && (list.size() > 0)) {
     		this.mSensor = list.get(0);
-    		this.sensorManager.registerListener(this, this.mSensor, SensorManager.SENSOR_DELAY_FASTEST);
+    		this.sensorManager.registerListener(this, this.mSensor, SensorManager.SENSOR_DELAY_UI);
     		this.setStatus(AccelListener.STARTING);
     	} else {
     		this.setStatus(AccelListener.ERROR_FAILED_TO_START);
@@ -217,6 +218,7 @@ public class AccelListener extends Plugin implements SensorEventListener {
             this.sensorManager.unregisterListener(this);
         }
         this.setStatus(AccelListener.STOPPED);
+        this.accuracy = SensorManager.SENSOR_STATUS_UNRELIABLE;
     }
 
     /**
@@ -226,6 +228,17 @@ public class AccelListener extends Plugin implements SensorEventListener {
      * @param accuracy
      */
     public void onAccuracyChanged(Sensor sensor, int accuracy) {
+    	// Only look at accelerometer events
+        if (sensor.getType() != Sensor.TYPE_ACCELEROMETER) {
+            return;
+        }
+        
+        // If not running, then just return
+        if (this.status == AccelListener.STOPPED) {
+            return;
+        }
+        Log.d("ACCEL", "accuracy is now " + accuracy);
+        this.accuracy = accuracy;
     }
 
     /**
@@ -246,16 +259,19 @@ public class AccelListener extends Plugin implements SensorEventListener {
         
         this.setStatus(AccelListener.RUNNING);
         
-        // Save time that event was received
-        this.timestamp = System.currentTimeMillis();
-        this.x = event.values[0];
-        this.y = event.values[1];
-        this.z = event.values[2];
+        if (this.accuracy >= SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM) {
 
-        this.win();
-        
-        if (this.size() == 0) {
-        	this.stop();
+        	// Save time that event was received
+        	this.timestamp = System.currentTimeMillis();
+        	this.x = event.values[0];
+        	this.y = event.values[1];
+        	this.z = event.values[2];
+
+        	this.win();
+
+        	if (this.size() == 0) {
+        		this.stop();
+        	}
         }
     }