From 3c9bae34029feb81fba7c2c3d724ccaf8c4c14e9 Mon Sep 17 00:00:00 2001 From: Bryce Curtis Date: Wed, 8 Sep 2010 17:09:22 -0500 Subject: [PATCH] Optimize accelerometer for plugin manager. --- framework/assets/js/accelerometer.js | 60 ++++++++-------- framework/src/com/phonegap/AccelListener.java | 70 ++++++------------- 2 files changed, 51 insertions(+), 79 deletions(-) diff --git a/framework/assets/js/accelerometer.js b/framework/assets/js/accelerometer.js index e9a93cba..fac351b8 100755 --- a/framework/assets/js/accelerometer.js +++ b/framework/assets/js/accelerometer.js @@ -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); diff --git a/framework/src/com/phonegap/AccelListener.java b/framework/src/com/phonegap/AccelListener.java index 13e0ca0c..a17737c4 100755 --- a/framework/src/com/phonegap/AccelListener.java +++ b/framework/src/com/phonegap/AccelListener.java @@ -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; + } }