mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-26 20:33:07 +08:00
[CB-463] updated js and rewrote accel plugin again to support the start/stop approach. optimized. single callback used for message passing
This commit is contained in:
parent
15ddef26f4
commit
2d5dcf24da
@ -1,6 +1,6 @@
|
|||||||
// commit 7b6ae77e5030060e8e99fe0b79ddcf9d698bf375
|
// commit 4a4ba9985c920850fe3f229abc60de984e196ab5
|
||||||
|
|
||||||
// File generated at :: Mon May 14 2012 13:03:22 GMT-0700 (PDT)
|
// File generated at :: Fri May 18 2012 13:43:11 GMT-0700 (PDT)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Licensed to the Apache Software Foundation (ASF) under one
|
Licensed to the Apache Software Foundation (ASF) under one
|
||||||
@ -3373,13 +3373,57 @@ var utils = require("cordova/utils"),
|
|||||||
exec = require("cordova/exec"),
|
exec = require("cordova/exec"),
|
||||||
Acceleration = require('cordova/plugin/Acceleration');
|
Acceleration = require('cordova/plugin/Acceleration');
|
||||||
|
|
||||||
|
// Is the accel sensor running?
|
||||||
|
var running = false;
|
||||||
|
|
||||||
// Keeps reference to watchAcceleration calls.
|
// Keeps reference to watchAcceleration calls.
|
||||||
var timers = {};
|
var timers = {};
|
||||||
|
|
||||||
|
// Array of listeners; used to keep track of when we should call start and stop.
|
||||||
|
var listeners = [];
|
||||||
|
|
||||||
// Last returned acceleration object from native
|
// Last returned acceleration object from native
|
||||||
var accel = null;
|
var accel = null;
|
||||||
|
|
||||||
|
// Tells native to start.
|
||||||
|
function start() {
|
||||||
|
exec(function(a) {
|
||||||
|
var tempListeners = listeners.slice(0);
|
||||||
|
accel = new Acceleration(a.x, a.y, a.z, a.timestamp);
|
||||||
|
for (var i = 0, l = tempListeners.length; i < l; i++) {
|
||||||
|
tempListeners[i].win(accel);
|
||||||
|
}
|
||||||
|
}, function(e) {
|
||||||
|
var tempListeners = listeners.slice(0);
|
||||||
|
for (var i = 0, l = tempListeners.length; i < l; i++) {
|
||||||
|
tempListeners[i].fail(e);
|
||||||
|
}
|
||||||
|
}, "Accelerometer", "start", []);
|
||||||
|
running = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tells native to stop.
|
||||||
|
function stop() {
|
||||||
|
exec(null, null, "Accelerometer", "stop", []);
|
||||||
|
running = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Adds a callback pair to the listeners array
|
||||||
|
function createCallbackPair(win, fail) {
|
||||||
|
return {win:win, fail:fail};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Removes a win/fail listener pair from the listeners array
|
||||||
|
function removeListeners(l) {
|
||||||
|
var idx = listeners.indexOf(l);
|
||||||
|
if (idx > -1) {
|
||||||
|
listeners.splice(idx, 1);
|
||||||
|
if (listeners.length === 0) {
|
||||||
|
stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var accelerometer = {
|
var accelerometer = {
|
||||||
/**
|
/**
|
||||||
* Asynchronously aquires the current acceleration.
|
* Asynchronously aquires the current acceleration.
|
||||||
@ -3394,13 +3438,22 @@ var accelerometer = {
|
|||||||
throw "getCurrentAcceleration must be called with at least a success callback function as first parameter.";
|
throw "getCurrentAcceleration must be called with at least a success callback function as first parameter.";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var p;
|
||||||
var win = function(a) {
|
var win = function(a) {
|
||||||
accel = new Acceleration(a.x, a.y, a.z, a.timestamp);
|
successCallback(a);
|
||||||
successCallback(accel);
|
removeListeners(p);
|
||||||
|
};
|
||||||
|
var fail = function(e) {
|
||||||
|
errorCallback(e);
|
||||||
|
removeListeners(p);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get acceleration
|
p = createCallbackPair(win, fail);
|
||||||
exec(win, errorCallback, "Accelerometer", "getAcceleration", []);
|
listeners.push(p);
|
||||||
|
|
||||||
|
if (!running) {
|
||||||
|
start();
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -3422,24 +3475,28 @@ var accelerometer = {
|
|||||||
|
|
||||||
// Keep reference to watch id, and report accel readings as often as defined in frequency
|
// Keep reference to watch id, and report accel readings as often as defined in frequency
|
||||||
var id = utils.createUUID();
|
var id = utils.createUUID();
|
||||||
timers[id] = window.setInterval(function() {
|
|
||||||
if (accel) {
|
|
||||||
successCallback(accel);
|
|
||||||
}
|
|
||||||
}, frequency);
|
|
||||||
|
|
||||||
// Success callback from native just updates the accel object.
|
var p = createCallbackPair(function(){}, function(e) {
|
||||||
var win = function(a) {
|
errorCallback(e);
|
||||||
accel = new Acceleration(a.x, a.y, a.z, a.timestamp);
|
removeListeners(p);
|
||||||
|
});
|
||||||
|
listeners.push(p);
|
||||||
|
|
||||||
|
timers[id] = {
|
||||||
|
timer:window.setInterval(function() {
|
||||||
|
if (accel) {
|
||||||
|
successCallback(accel);
|
||||||
|
}
|
||||||
|
}, frequency),
|
||||||
|
listeners:p
|
||||||
};
|
};
|
||||||
|
|
||||||
// Fail callback clears the watch and sends an error back.
|
if (running) {
|
||||||
var fail = function(err) {
|
// If we're already running then immediately invoke the success callback
|
||||||
accelerometer.clearWatch(id);
|
successCallback(accel);
|
||||||
errorCallback(err);
|
} else {
|
||||||
};
|
start();
|
||||||
|
}
|
||||||
exec(win, fail, "Accelerometer", "addWatch", [id, frequency]);
|
|
||||||
|
|
||||||
return id;
|
return id;
|
||||||
},
|
},
|
||||||
@ -3452,9 +3509,9 @@ var accelerometer = {
|
|||||||
clearWatch: function(id) {
|
clearWatch: function(id) {
|
||||||
// Stop javascript timer & remove from timer list
|
// Stop javascript timer & remove from timer list
|
||||||
if (id && timers[id]) {
|
if (id && timers[id]) {
|
||||||
window.clearInterval(timers[id]);
|
window.clearInterval(timers[id].timer);
|
||||||
|
removeListeners(timers[id].listeners);
|
||||||
delete timers[id];
|
delete timers[id];
|
||||||
exec(null, null, "Accelerometer", "clearWatch", [id]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -58,9 +58,8 @@ public class AccelListener extends Plugin implements SensorEventListener {
|
|||||||
|
|
||||||
private SensorManager sensorManager; // Sensor manager
|
private SensorManager sensorManager; // Sensor manager
|
||||||
private Sensor mSensor; // Acceleration sensor returned by sensor manager
|
private Sensor mSensor; // Acceleration sensor returned by sensor manager
|
||||||
|
|
||||||
private HashMap<String, String> watches = new HashMap<String, String>();
|
private String callbackId; // Keeps track of the single "start" callback ID passed in from JS
|
||||||
private List<String> callbacks = new ArrayList<String>();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create an accelerometer listener.
|
* Create an accelerometer listener.
|
||||||
@ -93,62 +92,28 @@ public class AccelListener extends Plugin implements SensorEventListener {
|
|||||||
* @return A PluginResult object with a status and message.
|
* @return A PluginResult object with a status and message.
|
||||||
*/
|
*/
|
||||||
public PluginResult execute(String action, JSONArray args, String callbackId) {
|
public PluginResult execute(String action, JSONArray args, String callbackId) {
|
||||||
PluginResult.Status status = PluginResult.Status.NO_RESULT;
|
PluginResult.Status status = PluginResult.Status.NO_RESULT;
|
||||||
String message = "";
|
String message = "";
|
||||||
PluginResult result = new PluginResult(status, message);
|
PluginResult result = new PluginResult(status, message);
|
||||||
result.setKeepCallback(true);
|
result.setKeepCallback(true);
|
||||||
try {
|
|
||||||
if (action.equals("getAcceleration")) {
|
|
||||||
if (this.status != AccelListener.RUNNING) {
|
|
||||||
// If not running, then this is an async call, so don't worry about waiting
|
|
||||||
// We drop the callback onto our stack, call start, and let start and the sensor callback fire off the callback down the road
|
|
||||||
this.callbacks.add(callbackId);
|
|
||||||
this.start();
|
|
||||||
} else {
|
|
||||||
return new PluginResult(PluginResult.Status.OK, this.getAccelerationJSON());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (action.equals("addWatch")) {
|
|
||||||
String watchId = args.getString(0);
|
|
||||||
this.watches.put(watchId, callbackId);
|
|
||||||
if (this.status != AccelListener.RUNNING) {
|
|
||||||
this.start();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (action.equals("clearWatch")) {
|
|
||||||
String watchId = args.getString(0);
|
|
||||||
if (this.watches.containsKey(watchId)) {
|
|
||||||
this.watches.remove(watchId);
|
|
||||||
if (this.size() == 0) {
|
|
||||||
this.stop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return new PluginResult(PluginResult.Status.OK);
|
|
||||||
} else {
|
|
||||||
// Unsupported action
|
|
||||||
return new PluginResult(PluginResult.Status.INVALID_ACTION);
|
|
||||||
}
|
|
||||||
} catch (JSONException e) {
|
|
||||||
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
if (action.equals("start")) {
|
||||||
* Identifies if action to be executed returns a value and should be run synchronously.
|
this.callbackId = callbackId;
|
||||||
*
|
if (this.status != AccelListener.RUNNING) {
|
||||||
* @param action The action to execute
|
// If not running, then this is an async call, so don't worry about waiting
|
||||||
* @return T=returns value
|
// We drop the callback onto our stack, call start, and let start and the sensor callback fire off the callback down the road
|
||||||
*/
|
this.start();
|
||||||
public boolean isSynch(String action) {
|
}
|
||||||
if (action.equals("getAcceleration") && this.status == AccelListener.RUNNING) {
|
|
||||||
return true;
|
|
||||||
} else if (action.equals("addWatch") && this.status == AccelListener.RUNNING) {
|
|
||||||
return true;
|
|
||||||
} else if (action.equals("clearWatch")) {
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
return false;
|
else if (action.equals("stop")) {
|
||||||
|
if (this.status == AccelListener.RUNNING) {
|
||||||
|
this.stop();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Unsupported action
|
||||||
|
return new PluginResult(PluginResult.Status.INVALID_ACTION);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -162,10 +127,7 @@ public class AccelListener extends Plugin implements SensorEventListener {
|
|||||||
//--------------------------------------------------------------------------
|
//--------------------------------------------------------------------------
|
||||||
// LOCAL METHODS
|
// LOCAL METHODS
|
||||||
//--------------------------------------------------------------------------
|
//--------------------------------------------------------------------------
|
||||||
|
//
|
||||||
private int size() {
|
|
||||||
return this.watches.size() + this.callbacks.size();
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* Start listening for acceleration sensor.
|
* Start listening for acceleration sensor.
|
||||||
*
|
*
|
||||||
@ -267,74 +229,47 @@ public class AccelListener extends Plugin implements SensorEventListener {
|
|||||||
this.z = event.values[2];
|
this.z = event.values[2];
|
||||||
|
|
||||||
this.win();
|
this.win();
|
||||||
|
|
||||||
if (this.size() == 0) {
|
|
||||||
this.stop();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sends an error back to JS
|
||||||
private void fail(int code, String message) {
|
private void fail(int code, String message) {
|
||||||
// Error object
|
// Error object
|
||||||
JSONObject errorObj = new JSONObject();
|
JSONObject errorObj = new JSONObject();
|
||||||
try {
|
try {
|
||||||
errorObj.put("code", code);
|
errorObj.put("code", code);
|
||||||
errorObj.put("message", message);
|
errorObj.put("message", message);
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
// TODO Auto-generated catch block
|
e.printStackTrace();
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
PluginResult err = new PluginResult(PluginResult.Status.ERROR, errorObj);
|
|
||||||
|
|
||||||
for (String callbackId: this.callbacks)
|
|
||||||
{
|
|
||||||
this.error(err, callbackId);
|
|
||||||
}
|
}
|
||||||
this.callbacks.clear();
|
PluginResult err = new PluginResult(PluginResult.Status.ERROR, errorObj);
|
||||||
|
|
||||||
err.setKeepCallback(true);
|
err.setKeepCallback(true);
|
||||||
|
|
||||||
Iterator it = this.watches.entrySet().iterator();
|
this.error(err, this.callbackId);
|
||||||
while (it.hasNext()) {
|
|
||||||
Map.Entry pairs = (Map.Entry)it.next();
|
|
||||||
this.error(err, (String)pairs.getValue());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void win() {
|
private void win() {
|
||||||
// Success return object
|
// Success return object
|
||||||
PluginResult result = new PluginResult(PluginResult.Status.OK, this.getAccelerationJSON());
|
PluginResult result = new PluginResult(PluginResult.Status.OK, this.getAccelerationJSON());
|
||||||
|
|
||||||
for (String callbackId: this.callbacks)
|
|
||||||
{
|
|
||||||
this.success(result, callbackId);
|
|
||||||
}
|
|
||||||
this.callbacks.clear();
|
|
||||||
|
|
||||||
result.setKeepCallback(true);
|
result.setKeepCallback(true);
|
||||||
|
|
||||||
Iterator it = this.watches.entrySet().iterator();
|
this.success(result, this.callbackId);
|
||||||
while (it.hasNext()) {
|
|
||||||
Map.Entry pairs = (Map.Entry)it.next();
|
|
||||||
this.success(result, (String)pairs.getValue());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setStatus(int status) {
|
private void setStatus(int status) {
|
||||||
this.status = status;
|
this.status = status;
|
||||||
}
|
}
|
||||||
|
|
||||||
private JSONObject getAccelerationJSON() {
|
private JSONObject getAccelerationJSON() {
|
||||||
JSONObject r = new JSONObject();
|
JSONObject r = new JSONObject();
|
||||||
try {
|
try {
|
||||||
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);
|
||||||
r.put("timestamp", this.timestamp);
|
r.put("timestamp", this.timestamp);
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
// TODO Auto-generated catch block
|
e.printStackTrace();
|
||||||
e.printStackTrace();
|
}
|
||||||
}
|
return r;
|
||||||
return r;
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user