mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-20 23:56:20 +08:00
Optimize accelerometer for plugin manager.
This commit is contained in:
parent
4f360c2680
commit
3c9bae3402
@ -1,23 +1,22 @@
|
||||
com.phonegap.AccelListenerProxy = function() {
|
||||
this.className = "com.phonegap.AccelListener";
|
||||
this.status = -1; // not set yet
|
||||
};
|
||||
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() {
|
||||
var r = PhoneGap.exec(this.className, "getAcceleration", []);
|
||||
var a = new Acceleration(r.x,r.y,r.z);
|
||||
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() {
|
||||
return PhoneGap.exec(this.className, "start", []);
|
||||
};
|
||||
@ -97,35 +96,34 @@ Accelerometer.prototype.getCurrentAcceleration = function(successCallback, error
|
||||
}
|
||||
|
||||
// If not running, then start it
|
||||
else {
|
||||
else if (status >= 0) {
|
||||
com.phonegap.AccelListener.start();
|
||||
|
||||
// Wait until started
|
||||
var timer = setInterval(function() {
|
||||
var status = com.phonegap.AccelListener.getStatus();
|
||||
if (status != Accelerometer.STARTING) {
|
||||
|
||||
// If accelerometer is running
|
||||
if (status == Accelerometer.RUNNING) {
|
||||
clearInterval(timer);
|
||||
|
||||
// If accelerometer is running
|
||||
if (status == Accelerometer.RUNNING) {
|
||||
try {
|
||||
var accel = com.phonegap.AccelListener.getAcceleration();
|
||||
successCallback(accel);
|
||||
} catch (e) {
|
||||
console.log("Accelerometer Error in successCallback: " + e);
|
||||
}
|
||||
try {
|
||||
var accel = com.phonegap.AccelListener.getAcceleration();
|
||||
successCallback(accel);
|
||||
} catch (e) {
|
||||
console.log("Accelerometer Error in successCallback: " + e);
|
||||
}
|
||||
}
|
||||
|
||||
// If accelerometer error
|
||||
else {
|
||||
console.log("Accelerometer Error: "+ Accelerometer.ERROR_MSG[status]);
|
||||
try {
|
||||
if (errorCallback) {
|
||||
errorCallback(status);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log("Accelerometer Error in errorCallback: " + e);
|
||||
// If accelerometer error
|
||||
else if (status == Accelerometer.ERROR_FAILED_TO_START) {
|
||||
clearInterval(timer);
|
||||
console.log("Accelerometer Error: "+ Accelerometer.ERROR_MSG[status]);
|
||||
try {
|
||||
if (errorCallback) {
|
||||
errorCallback(status);
|
||||
}
|
||||
} catch (e) {
|
||||
console.log("Accelerometer Error in errorCallback: " + e);
|
||||
}
|
||||
}
|
||||
}, 10);
|
||||
@ -181,7 +179,7 @@ Accelerometer.prototype.watchAcceleration = function(successCallback, errorCallb
|
||||
}
|
||||
|
||||
// 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]);
|
||||
try {
|
||||
navigator.accelerometer.clearWatch(id);
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user