forked from github/cordova-android
Optimize accelerometer for plugin manager.
This commit is contained in:
parent
4f360c2680
commit
3c9bae3402
@ -1,23 +1,22 @@
|
|||||||
com.phonegap.AccelListenerProxy = function() {
|
com.phonegap.AccelListenerProxy = function() {
|
||||||
this.className = "com.phonegap.AccelListener";
|
this.className = "com.phonegap.AccelListener";
|
||||||
|
this.status = -1; // not set yet
|
||||||
};
|
};
|
||||||
com.phonegap.AccelListenerProxy.prototype.getStatus = function() {
|
com.phonegap.AccelListenerProxy.prototype.getStatus = function() {
|
||||||
return PhoneGap.exec(this.className, "getStatus", []);
|
if (this.status == -1) { // if not set, then request status
|
||||||
|
this.status = PhoneGap.exec(this.className, "getStatus", []);
|
||||||
|
}
|
||||||
|
return this.status;
|
||||||
|
};
|
||||||
|
com.phonegap.AccelListenerProxy.prototype.onStatus = function(status) {
|
||||||
|
console.log("AccelListener.onStatus("+status+")");
|
||||||
|
this.status = status;
|
||||||
};
|
};
|
||||||
com.phonegap.AccelListenerProxy.prototype.getAcceleration = function() {
|
com.phonegap.AccelListenerProxy.prototype.getAcceleration = function() {
|
||||||
var r = PhoneGap.exec(this.className, "getAcceleration", []);
|
var r = PhoneGap.exec(this.className, "getAcceleration", []);
|
||||||
var a = new Acceleration(r.x,r.y,r.z);
|
var a = new Acceleration(r.x,r.y,r.z);
|
||||||
return a;
|
return a;
|
||||||
};
|
};
|
||||||
com.phonegap.AccelListenerProxy.prototype.getX = function() {
|
|
||||||
return PhoneGap.exec(this.className, "getX", []);
|
|
||||||
};
|
|
||||||
com.phonegap.AccelListenerProxy.prototype.getY = function() {
|
|
||||||
return PhoneGap.exec(this.className, "getY", []);
|
|
||||||
};
|
|
||||||
com.phonegap.AccelListenerProxy.prototype.getZ = function() {
|
|
||||||
return PhoneGap.exec(this.className, "getZ", []);
|
|
||||||
};
|
|
||||||
com.phonegap.AccelListenerProxy.prototype.start = function() {
|
com.phonegap.AccelListenerProxy.prototype.start = function() {
|
||||||
return PhoneGap.exec(this.className, "start", []);
|
return PhoneGap.exec(this.className, "start", []);
|
||||||
};
|
};
|
||||||
@ -97,17 +96,16 @@ Accelerometer.prototype.getCurrentAcceleration = function(successCallback, error
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If not running, then start it
|
// If not running, then start it
|
||||||
else {
|
else if (status >= 0) {
|
||||||
com.phonegap.AccelListener.start();
|
com.phonegap.AccelListener.start();
|
||||||
|
|
||||||
// Wait until started
|
// Wait until started
|
||||||
var timer = setInterval(function() {
|
var timer = setInterval(function() {
|
||||||
var status = com.phonegap.AccelListener.getStatus();
|
var status = com.phonegap.AccelListener.getStatus();
|
||||||
if (status != Accelerometer.STARTING) {
|
|
||||||
clearInterval(timer);
|
|
||||||
|
|
||||||
// If accelerometer is running
|
// If accelerometer is running
|
||||||
if (status == Accelerometer.RUNNING) {
|
if (status == Accelerometer.RUNNING) {
|
||||||
|
clearInterval(timer);
|
||||||
try {
|
try {
|
||||||
var accel = com.phonegap.AccelListener.getAcceleration();
|
var accel = com.phonegap.AccelListener.getAcceleration();
|
||||||
successCallback(accel);
|
successCallback(accel);
|
||||||
@ -117,7 +115,8 @@ Accelerometer.prototype.getCurrentAcceleration = function(successCallback, error
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If accelerometer error
|
// If accelerometer error
|
||||||
else {
|
else if (status == Accelerometer.ERROR_FAILED_TO_START) {
|
||||||
|
clearInterval(timer);
|
||||||
console.log("Accelerometer Error: "+ Accelerometer.ERROR_MSG[status]);
|
console.log("Accelerometer Error: "+ Accelerometer.ERROR_MSG[status]);
|
||||||
try {
|
try {
|
||||||
if (errorCallback) {
|
if (errorCallback) {
|
||||||
@ -127,7 +126,6 @@ Accelerometer.prototype.getCurrentAcceleration = function(successCallback, error
|
|||||||
console.log("Accelerometer Error in errorCallback: " + e);
|
console.log("Accelerometer Error in errorCallback: " + e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}, 10);
|
}, 10);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -181,7 +179,7 @@ Accelerometer.prototype.watchAcceleration = function(successCallback, errorCallb
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If accelerometer had error
|
// If accelerometer had error
|
||||||
else if (status != Accelerometer.STARTING) {
|
else if (status == Accelerometer.ERROR_FAILED_TO_START) {
|
||||||
console.log("Accelerometer Error: "+ Accelerometer.ERROR_MSG[status]);
|
console.log("Accelerometer Error: "+ Accelerometer.ERROR_MSG[status]);
|
||||||
try {
|
try {
|
||||||
navigator.accelerometer.clearWatch(id);
|
navigator.accelerometer.clearWatch(id);
|
||||||
|
@ -7,7 +7,6 @@ import org.json.JSONException;
|
|||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import com.phonegap.api.Plugin;
|
import com.phonegap.api.Plugin;
|
||||||
import com.phonegap.api.PluginManager;
|
|
||||||
import com.phonegap.api.PluginResult;
|
import com.phonegap.api.PluginResult;
|
||||||
|
|
||||||
import android.hardware.Sensor;
|
import android.hardware.Sensor;
|
||||||
@ -50,7 +49,7 @@ public class AccelListener implements SensorEventListener, Plugin{
|
|||||||
this.y = 0;
|
this.y = 0;
|
||||||
this.z = 0;
|
this.z = 0;
|
||||||
this.timeStamp = 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);
|
return new PluginResult(status, 0);
|
||||||
}
|
}
|
||||||
else if (action.equals("getAcceleration")) {
|
else if (action.equals("getAcceleration")) {
|
||||||
|
if (this.status == AccelListener.STOPPED) {
|
||||||
|
this.start(); // Start if not already running
|
||||||
|
}
|
||||||
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);
|
||||||
return new PluginResult(status, r);
|
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")) {
|
else if (action.equals("setTimeout")) {
|
||||||
try {
|
try {
|
||||||
float timeout = Float.parseFloat(args.getString(0));
|
float timeout = Float.parseFloat(args.getString(0));
|
||||||
@ -195,13 +185,13 @@ public class AccelListener implements SensorEventListener, Plugin{
|
|||||||
if ((list != null) && (list.size() > 0)) {
|
if ((list != null) && (list.size() > 0)) {
|
||||||
this.mSensor = list.get(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_FASTEST);
|
||||||
this.status = AccelListener.STARTING;
|
this.setStatus(AccelListener.STARTING);
|
||||||
this.lastAccessTime = System.currentTimeMillis();
|
this.lastAccessTime = System.currentTimeMillis();
|
||||||
}
|
}
|
||||||
|
|
||||||
// If error, then set status to error
|
// If error, then set status to error
|
||||||
else {
|
else {
|
||||||
this.status = AccelListener.ERROR_FAILED_TO_START;
|
this.setStatus(AccelListener.ERROR_FAILED_TO_START);
|
||||||
}
|
}
|
||||||
|
|
||||||
return this.status;
|
return this.status;
|
||||||
@ -214,7 +204,7 @@ public class AccelListener implements SensorEventListener, Plugin{
|
|||||||
if (this.status != AccelListener.STOPPED) {
|
if (this.status != AccelListener.STOPPED) {
|
||||||
this.sensorManager.unregisterListener(this);
|
this.sensorManager.unregisterListener(this);
|
||||||
}
|
}
|
||||||
this.status = AccelListener.STOPPED;
|
this.setStatus(AccelListener.STOPPED);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onAccuracyChanged(Sensor sensor, int accuracy) {
|
public void onAccuracyChanged(Sensor sensor, int accuracy) {
|
||||||
@ -232,6 +222,9 @@ public class AccelListener implements SensorEventListener, Plugin{
|
|||||||
if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) {
|
if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (this.status == AccelListener.STOPPED) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Save time that event was received
|
// Save time that event was received
|
||||||
this.timeStamp = System.currentTimeMillis();
|
this.timeStamp = System.currentTimeMillis();
|
||||||
@ -239,7 +232,7 @@ public class AccelListener implements SensorEventListener, Plugin{
|
|||||||
this.y = event.values[1];
|
this.y = event.values[1];
|
||||||
this.z = event.values[2];
|
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 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) {
|
||||||
@ -256,36 +249,6 @@ public class AccelListener implements SensorEventListener, Plugin{
|
|||||||
return this.status;
|
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.
|
* Set the timeout to turn off accelerometer sensor if getX() hasn't been called.
|
||||||
*
|
*
|
||||||
@ -304,4 +267,15 @@ public class AccelListener implements SensorEventListener, Plugin{
|
|||||||
return this.TIMEOUT;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user