Cleanup accelerometer code.

This commit is contained in:
Bryce Curtis 2010-09-09 11:00:45 -05:00
parent 3c9bae3402
commit 77801de1ae

View File

@ -34,7 +34,7 @@ public class AccelListener implements SensorEventListener, Plugin{
DroidGap ctx; // DroidGap object DroidGap ctx; // DroidGap object
float x,y,z; // most recent acceleration values float x,y,z; // most recent acceleration values
long timeStamp; // time of most recent value long timestamp; // time of most recent value
int status; // status of listener int status; // status of listener
long lastAccessTime; // time the value was last retrieved long lastAccessTime; // time the value was last retrieved
@ -48,7 +48,7 @@ public class AccelListener implements SensorEventListener, Plugin{
this.x = 0; this.x = 0;
this.y = 0; this.y = 0;
this.z = 0; this.z = 0;
this.timeStamp = 0; this.timestamp = 0;
this.setStatus(AccelListener.STOPPED); this.setStatus(AccelListener.STOPPED);
} }
@ -98,13 +98,16 @@ public class AccelListener implements SensorEventListener, Plugin{
return new PluginResult(status, 0); return new PluginResult(status, 0);
} }
else if (action.equals("getAcceleration")) { else if (action.equals("getAcceleration")) {
// Start if not already running
if (this.status == AccelListener.STOPPED) { if (this.status == AccelListener.STOPPED) {
this.start(); // Start if not already running this.start();
} }
JSONObject r = new JSONObject(); JSONObject r = new JSONObject();
r.put("x", this.x); r.put("x", this.x);
r.put("y", this.y); r.put("y", this.y);
r.put("z", this.z); r.put("z", this.z);
// TODO: Should timestamp be sent?
r.put("timestamp", this.timestamp);
return new PluginResult(status, r); return new PluginResult(status, r);
} }
else if (action.equals("setTimeout")) { else if (action.equals("setTimeout")) {
@ -207,8 +210,13 @@ public class AccelListener implements SensorEventListener, Plugin{
this.setStatus(AccelListener.STOPPED); this.setStatus(AccelListener.STOPPED);
} }
/**
* Called when the accuracy of the sensor has changed.
*
* @param sensor
* @param accuracy
*/
public void onAccuracyChanged(Sensor sensor, int accuracy) { public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
} }
/** /**
@ -222,12 +230,14 @@ public class AccelListener implements SensorEventListener, Plugin{
if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) { if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) {
return; return;
} }
// If not running, then just return
if (this.status == AccelListener.STOPPED) { if (this.status == AccelListener.STOPPED) {
return; return;
} }
// Save time that event was received // Save time that event was received
this.timeStamp = System.currentTimeMillis(); this.timestamp = System.currentTimeMillis();
this.x = event.values[0]; this.x = event.values[0];
this.y = event.values[1]; this.y = event.values[1];
this.z = event.values[2]; this.z = event.values[2];
@ -235,7 +245,7 @@ public class AccelListener implements SensorEventListener, Plugin{
this.setStatus(AccelListener.RUNNING); this.setStatus(AccelListener.RUNNING);
// If values haven't been read for TIMEOUT time, then turn off accelerometer sensor to save power // If values haven't been read for TIMEOUT time, then turn off accelerometer sensor to save power
if ((this.timeStamp - this.lastAccessTime) > this.TIMEOUT) { if ((this.timestamp - this.lastAccessTime) > this.TIMEOUT) {
this.stop(); this.stop();
} }
} }