Skip to content

Commit

Permalink
[CB-463] added accuracy checking to native accel implementation, this…
Browse files Browse the repository at this point in the history
… way getCurrentAcceleration returns fairly accurate results
  • Loading branch information
filmaj committed May 18, 2012
1 parent 24adc6d commit cb98bbc
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions framework/src/org/apache/cordova/AccelListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -184,7 +185,7 @@ private int start() {
// 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);
Expand Down Expand Up @@ -217,6 +218,7 @@ private void stop() {
this.sensorManager.unregisterListener(this);
}
this.setStatus(AccelListener.STOPPED);
this.accuracy = SensorManager.SENSOR_STATUS_UNRELIABLE;
}

/**
Expand All @@ -226,6 +228,17 @@ private void stop() {
* @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;
}

/**
Expand All @@ -246,16 +259,19 @@ public void onSensorChanged(SensorEvent event) {

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();
}
}
}

Expand Down

0 comments on commit cb98bbc

Please sign in to comment.