diff --git a/framework/assets/js/accelerometer.js b/framework/assets/js/accelerometer.js index fac351b8..3923a56a 100755 --- a/framework/assets/js/accelerometer.js +++ b/framework/assets/js/accelerometer.js @@ -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) { this.x = x; @@ -55,20 +23,17 @@ function Accelerometer() { 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"]; /** * Asynchronously aquires the current acceleration. * * @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 {AccelerationOptions} options The options for getting the accelerometer data such as timeout. + * @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. (OPTIONAL) */ Accelerometer.prototype.getCurrentAcceleration = function(successCallback, errorCallback, options) { + console.log("Accelerometer.getCurrentAcceleration()"); // successCallback required if (typeof successCallback != "function") { @@ -82,60 +47,16 @@ Accelerometer.prototype.getCurrentAcceleration = function(successCallback, error return; } - // Get current acceleration status - var status = com.phonegap.AccelListener.getStatus(); - - // 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); - } + // Get acceleration + PhoneGap.execAsync(successCallback, errorCallback, "Accelerometer", "getAcceleration", []); }; /** * 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} errorCallback The function to call when there is an error getting the acceleration data. - * @param {AccelerationOptions} options The options for getting the accelerometer data such as timeout. + * @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. (OPTIONAL) * @return String The watch id that must be passed to #clearWatch to stop watching. */ Accelerometer.prototype.watchAcceleration = function(successCallback, errorCallback, options) { @@ -156,40 +77,18 @@ Accelerometer.prototype.watchAcceleration = function(successCallback, errorCallb } // Make sure accelerometer timeout > frequency + 10 sec - var timeout = com.phonegap.AccelListener.getTimeout(); - if (timeout < (frequency + 10000)) { - com.phonegap.AccelListener.setTimeout(frequency + 10000); // set to frequency + 10 sec - } - - var id = PhoneGap.createUUID(); - com.phonegap.AccelListener.start(); + PhoneGap.execAsync( + function(timeout) { + if (timeout < (frequency + 10000)) { + PhoneGap.execAsync(null, null, "Accelerometer", "setTimeout", [frequency + 10000]); + } + }, + function(e) { }, "Accelerometer", "getTimeout", []); // Start watch timer + var id = PhoneGap.createUUID(); navigator.accelerometer.timers[id] = setInterval(function() { - var status = com.phonegap.AccelListener.getStatus(); - - // 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); - } - } + PhoneGap.execAsync(successCallback, errorCallback, "Accelerometer", "getAcceleration", []); }, (frequency ? frequency : 1)); return id; diff --git a/framework/src/com/phonegap/AccelListener.java b/framework/src/com/phonegap/AccelListener.java index 21d7c502..8d26812c 100755 --- a/framework/src/com/phonegap/AccelListener.java +++ b/framework/src/com/phonegap/AccelListener.java @@ -98,9 +98,20 @@ public class AccelListener implements SensorEventListener, Plugin{ return new PluginResult(status, 0); } else if (action.equals("getAcceleration")) { - // Start if not already running - if (this.status == AccelListener.STOPPED) { - this.start(); + // If not running, then this is an async call, so don't worry about waiting + if (this.status != AccelListener.RUNNING) { + 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(); r.put("x", this.x); @@ -140,7 +151,18 @@ public class AccelListener implements SensorEventListener, Plugin{ * @return T=returns value */ 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; } @@ -293,9 +315,6 @@ public class AccelListener implements SensorEventListener, Plugin{ * @param status */ private void setStatus(int status) { - if (this.status != status) { - ctx.sendJavascript("com.phonegap.AccelListener.onStatus("+status+")"); - } this.status = status; }