Optimize accelerometer for plugin manager.

This commit is contained in:
Bryce Curtis
2010-09-08 17:09:22 -05:00
parent 4f360c2680
commit 3c9bae3402
2 changed files with 51 additions and 79 deletions
+22 -48
View File
@@ -7,7 +7,6 @@ import org.json.JSONException;
import org.json.JSONObject;
import com.phonegap.api.Plugin;
import com.phonegap.api.PluginManager;
import com.phonegap.api.PluginResult;
import android.hardware.Sensor;
@@ -50,7 +49,7 @@ public class AccelListener implements SensorEventListener, Plugin{
this.y = 0;
this.z = 0;
this.timeStamp = 0;
this.status = AccelListener.STOPPED;
this.setStatus(AccelListener.STOPPED);
}
/**
@@ -99,24 +98,15 @@ public class AccelListener implements SensorEventListener, Plugin{
return new PluginResult(status, 0);
}
else if (action.equals("getAcceleration")) {
if (this.status == AccelListener.STOPPED) {
this.start(); // Start if not already running
}
JSONObject r = new JSONObject();
r.put("x", this.x);
r.put("y", this.y);
r.put("z", this.z);
return new PluginResult(status, r);
}
else if (action.equals("getX")) {
float f = this.getX();
return new PluginResult(status, f);
}
else if (action.equals("getY")) {
float f = this.getY();
return new PluginResult(status, f);
}
else if (action.equals("getZ")) {
float f = this.getZ();
return new PluginResult(status, f);
}
else if (action.equals("setTimeout")) {
try {
float timeout = Float.parseFloat(args.getString(0));
@@ -195,13 +185,13 @@ public class AccelListener implements SensorEventListener, Plugin{
if ((list != null) && (list.size() > 0)) {
this.mSensor = list.get(0);
this.sensorManager.registerListener(this, this.mSensor, SensorManager.SENSOR_DELAY_FASTEST);
this.status = AccelListener.STARTING;
this.setStatus(AccelListener.STARTING);
this.lastAccessTime = System.currentTimeMillis();
}
// If error, then set status to error
else {
this.status = AccelListener.ERROR_FAILED_TO_START;
this.setStatus(AccelListener.ERROR_FAILED_TO_START);
}
return this.status;
@@ -214,7 +204,7 @@ public class AccelListener implements SensorEventListener, Plugin{
if (this.status != AccelListener.STOPPED) {
this.sensorManager.unregisterListener(this);
}
this.status = AccelListener.STOPPED;
this.setStatus(AccelListener.STOPPED);
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
@@ -232,6 +222,9 @@ public class AccelListener implements SensorEventListener, Plugin{
if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) {
return;
}
if (this.status == AccelListener.STOPPED) {
return;
}
// Save time that event was received
this.timeStamp = System.currentTimeMillis();
@@ -239,7 +232,7 @@ public class AccelListener implements SensorEventListener, Plugin{
this.y = event.values[1];
this.z = event.values[2];
this.status = AccelListener.RUNNING;
this.setStatus(AccelListener.RUNNING);
// If values haven't been read for TIMEOUT time, then turn off accelerometer sensor to save power
if ((this.timeStamp - this.lastAccessTime) > this.TIMEOUT) {
@@ -256,36 +249,6 @@ public class AccelListener implements SensorEventListener, Plugin{
return this.status;
}
/**
* Get X value of last accelerometer value.
*
* @return x value
*/
public float getX() {
this.lastAccessTime = System.currentTimeMillis();
return this.x;
}
/**
* Get Y value of last accelerometer value.
*
* @return y value
*/
public float getY() {
this.lastAccessTime = System.currentTimeMillis();
return this.y;
}
/**
* Get Z value of last accelerometer value.
*
* @return z value
*/
public float getZ() {
this.lastAccessTime = System.currentTimeMillis();
return this.x;
}
/**
* Set the timeout to turn off accelerometer sensor if getX() hasn't been called.
*
@@ -303,5 +266,16 @@ public class AccelListener implements SensorEventListener, Plugin{
public float getTimeout() {
return this.TIMEOUT;
}
/**
* Set the status and send it to JavaScript.
* @param status
*/
private void setStatus(int status) {
if (this.status != status) {
ctx.sendJavascript("com.phonegap.AccelListener.onStatus("+status+")");
}
this.status = status;
}
}