mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-22 00:32:55 +08:00
Optimize accelerometer to use new PluginManager.
This commit is contained in:
parent
863807a7a5
commit
0a7762743e
@ -1,35 +1,3 @@
|
|||||||
com.phonegap.AccelListenerProxy = function() {
|
|
||||||
this.className = "com.phonegap.AccelListener";
|
|
||||||
this.status = -1; // not set yet
|
|
||||||
};
|
|
||||||
com.phonegap.AccelListenerProxy.prototype.getStatus = function() {
|
|
||||||
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.start = function() {
|
|
||||||
return PhoneGap.exec(this.className, "start", []);
|
|
||||||
};
|
|
||||||
com.phonegap.AccelListenerProxy.prototype.stop = function() {
|
|
||||||
return PhoneGap.exec(this.className, "stop", []);
|
|
||||||
};
|
|
||||||
com.phonegap.AccelListenerProxy.prototype.setTimeout = function(timeout) {
|
|
||||||
return PhoneGap.exec(this.className, "setTimeout", [timeout]);
|
|
||||||
};
|
|
||||||
com.phonegap.AccelListenerProxy.prototype.getTimeout = function() {
|
|
||||||
return PhoneGap.exec(this.className, "getTimeout", []);
|
|
||||||
};
|
|
||||||
com.phonegap.AccelListener = new com.phonegap.AccelListenerProxy();
|
|
||||||
|
|
||||||
function Acceleration(x, y, z) {
|
function Acceleration(x, y, z) {
|
||||||
this.x = x;
|
this.x = x;
|
||||||
@ -55,20 +23,17 @@ function Accelerometer() {
|
|||||||
this.timers = {};
|
this.timers = {};
|
||||||
};
|
};
|
||||||
|
|
||||||
Accelerometer.STOPPED = 0;
|
|
||||||
Accelerometer.STARTING = 1;
|
|
||||||
Accelerometer.RUNNING = 2;
|
|
||||||
Accelerometer.ERROR_FAILED_TO_START = 3;
|
|
||||||
Accelerometer.ERROR_MSG = ["Not running", "Starting", "", "Failed to start"];
|
Accelerometer.ERROR_MSG = ["Not running", "Starting", "", "Failed to start"];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Asynchronously aquires the current acceleration.
|
* Asynchronously aquires the current acceleration.
|
||||||
*
|
*
|
||||||
* @param {Function} successCallback The function to call when the acceleration data is available
|
* @param {Function} successCallback The function to call when the acceleration data is available
|
||||||
* @param {Function} errorCallback The function to call when there is an error getting the acceleration data.
|
* @param {Function} errorCallback The function to call when there is an error getting the acceleration data. (OPTIONAL)
|
||||||
* @param {AccelerationOptions} options The options for getting the accelerometer data such as timeout.
|
* @param {AccelerationOptions} options The options for getting the accelerometer data such as timeout. (OPTIONAL)
|
||||||
*/
|
*/
|
||||||
Accelerometer.prototype.getCurrentAcceleration = function(successCallback, errorCallback, options) {
|
Accelerometer.prototype.getCurrentAcceleration = function(successCallback, errorCallback, options) {
|
||||||
|
console.log("Accelerometer.getCurrentAcceleration()");
|
||||||
|
|
||||||
// successCallback required
|
// successCallback required
|
||||||
if (typeof successCallback != "function") {
|
if (typeof successCallback != "function") {
|
||||||
@ -82,60 +47,16 @@ Accelerometer.prototype.getCurrentAcceleration = function(successCallback, error
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get current acceleration status
|
// Get acceleration
|
||||||
var status = com.phonegap.AccelListener.getStatus();
|
PhoneGap.execAsync(successCallback, errorCallback, "Accelerometer", "getAcceleration", []);
|
||||||
|
|
||||||
// If running, then call successCallback
|
|
||||||
if (status == Accelerometer.RUNNING) {
|
|
||||||
try {
|
|
||||||
var accel = com.phonegap.AccelListener.getAcceleration();
|
|
||||||
successCallback(accel);
|
|
||||||
} catch (e) {
|
|
||||||
console.log("Accelerometer Error in successCallback: " + e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If not running, then start it
|
|
||||||
else if (status >= 0) {
|
|
||||||
com.phonegap.AccelListener.start();
|
|
||||||
|
|
||||||
// Wait until started
|
|
||||||
var timer = setInterval(function() {
|
|
||||||
var status = com.phonegap.AccelListener.getStatus();
|
|
||||||
|
|
||||||
// If accelerometer is running
|
|
||||||
if (status == Accelerometer.RUNNING) {
|
|
||||||
clearInterval(timer);
|
|
||||||
try {
|
|
||||||
var accel = com.phonegap.AccelListener.getAcceleration();
|
|
||||||
successCallback(accel);
|
|
||||||
} catch (e) {
|
|
||||||
console.log("Accelerometer Error in successCallback: " + 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);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Asynchronously aquires the acceleration repeatedly at a given interval.
|
* Asynchronously aquires the acceleration repeatedly at a given interval.
|
||||||
*
|
*
|
||||||
* @param {Function} successCallback The function to call each time the acceleration data is available
|
* @param {Function} successCallback The function to call each time the acceleration data is available
|
||||||
* @param {Function} errorCallback The function to call when there is an error getting the acceleration data.
|
* @param {Function} errorCallback The function to call when there is an error getting the acceleration data. (OPTIONAL)
|
||||||
* @param {AccelerationOptions} options The options for getting the accelerometer data such as timeout.
|
* @param {AccelerationOptions} options The options for getting the accelerometer data such as timeout. (OPTIONAL)
|
||||||
* @return String The watch id that must be passed to #clearWatch to stop watching.
|
* @return String The watch id that must be passed to #clearWatch to stop watching.
|
||||||
*/
|
*/
|
||||||
Accelerometer.prototype.watchAcceleration = function(successCallback, errorCallback, options) {
|
Accelerometer.prototype.watchAcceleration = function(successCallback, errorCallback, options) {
|
||||||
@ -156,40 +77,18 @@ Accelerometer.prototype.watchAcceleration = function(successCallback, errorCallb
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Make sure accelerometer timeout > frequency + 10 sec
|
// Make sure accelerometer timeout > frequency + 10 sec
|
||||||
var timeout = com.phonegap.AccelListener.getTimeout();
|
PhoneGap.execAsync(
|
||||||
if (timeout < (frequency + 10000)) {
|
function(timeout) {
|
||||||
com.phonegap.AccelListener.setTimeout(frequency + 10000); // set to frequency + 10 sec
|
if (timeout < (frequency + 10000)) {
|
||||||
}
|
PhoneGap.execAsync(null, null, "Accelerometer", "setTimeout", [frequency + 10000]);
|
||||||
|
}
|
||||||
var id = PhoneGap.createUUID();
|
},
|
||||||
com.phonegap.AccelListener.start();
|
function(e) { }, "Accelerometer", "getTimeout", []);
|
||||||
|
|
||||||
// Start watch timer
|
// Start watch timer
|
||||||
|
var id = PhoneGap.createUUID();
|
||||||
navigator.accelerometer.timers[id] = setInterval(function() {
|
navigator.accelerometer.timers[id] = setInterval(function() {
|
||||||
var status = com.phonegap.AccelListener.getStatus();
|
PhoneGap.execAsync(successCallback, errorCallback, "Accelerometer", "getAcceleration", []);
|
||||||
|
|
||||||
// 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If accelerometer had error
|
|
||||||
else if (status == Accelerometer.ERROR_FAILED_TO_START) {
|
|
||||||
console.log("Accelerometer Error: "+ Accelerometer.ERROR_MSG[status]);
|
|
||||||
try {
|
|
||||||
navigator.accelerometer.clearWatch(id);
|
|
||||||
if (errorCallback) {
|
|
||||||
errorCallback(status);
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
console.log("Accelerometer Error in errorCallback: " + e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, (frequency ? frequency : 1));
|
}, (frequency ? frequency : 1));
|
||||||
|
|
||||||
return id;
|
return id;
|
||||||
|
@ -98,9 +98,20 @@ 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 not running, then this is an async call, so don't worry about waiting
|
||||||
if (this.status == AccelListener.STOPPED) {
|
if (this.status != AccelListener.RUNNING) {
|
||||||
this.start();
|
int r = this.start();
|
||||||
|
if (r == AccelListener.ERROR_FAILED_TO_START) {
|
||||||
|
return new PluginResult(PluginResult.Status.IO_EXCEPTION, AccelListener.ERROR_FAILED_TO_START);
|
||||||
|
}
|
||||||
|
// Wait until running
|
||||||
|
while (this.status == STARTING) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(100);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
JSONObject r = new JSONObject();
|
JSONObject r = new JSONObject();
|
||||||
r.put("x", this.x);
|
r.put("x", this.x);
|
||||||
@ -140,7 +151,18 @@ public class AccelListener implements SensorEventListener, Plugin{
|
|||||||
* @return T=returns value
|
* @return T=returns value
|
||||||
*/
|
*/
|
||||||
public boolean hasReturnValue(String action) {
|
public boolean hasReturnValue(String action) {
|
||||||
// TODO
|
if (action.equals("getStatus")) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else if (action.equals("getAcceleration")) {
|
||||||
|
// Can only return value if RUNNING
|
||||||
|
if (this.status == RUNNING) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (action.equals("getTimeout")) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -293,9 +315,6 @@ public class AccelListener implements SensorEventListener, Plugin{
|
|||||||
* @param status
|
* @param status
|
||||||
*/
|
*/
|
||||||
private void setStatus(int status) {
|
private void setStatus(int status) {
|
||||||
if (this.status != status) {
|
|
||||||
ctx.sendJavascript("com.phonegap.AccelListener.onStatus("+status+")");
|
|
||||||
}
|
|
||||||
this.status = status;
|
this.status = status;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user