[CB-463] updated js and rewrote accel plugin again to support the start/stop 
approach. optimized. single callback used for message passing


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/commit/2d5dcf24
Tree: 
http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/tree/2d5dcf24
Diff: 
http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/diff/2d5dcf24

Branch: refs/heads/CordovaWebView
Commit: 2d5dcf24da18c210e08ccfcd473e1881a60bc429
Parents: 15ddef2
Author: Fil Maj <maj....@gmail.com>
Authored: Fri May 18 13:50:45 2012 -0700
Committer: Fil Maj <maj....@gmail.com>
Committed: Fri May 18 15:22:56 2012 -0700

----------------------------------------------------------------------
 framework/assets/js/cordova.android.js             |  101 +++++++--
 .../src/org/apache/cordova/AccelListener.java      |  167 +++++----------
 2 files changed, 130 insertions(+), 138 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/2d5dcf24/framework/assets/js/cordova.android.js
----------------------------------------------------------------------
diff --git a/framework/assets/js/cordova.android.js 
b/framework/assets/js/cordova.android.js
index 957be77..cdf619d 100644
--- a/framework/assets/js/cordova.android.js
+++ b/framework/assets/js/cordova.android.js
@@ -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
@@ -3373,13 +3373,57 @@ var utils = require("cordova/utils"),
     exec = require("cordova/exec"),
     Acceleration = require('cordova/plugin/Acceleration');
 
+// Is the accel sensor running?
+var running = false;
 
 // Keeps reference to watchAcceleration calls.
 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
 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 = {
     /**
      * 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.";
         }
 
+        var p;
         var win = function(a) {
-            accel = new Acceleration(a.x, a.y, a.z, a.timestamp);
-            successCallback(accel);
+            successCallback(a);
+            removeListeners(p);
+        };
+        var fail = function(e) {
+            errorCallback(e);
+            removeListeners(p);
         };
 
-        // Get acceleration
-        exec(win, errorCallback, "Accelerometer", "getAcceleration", []);
+        p = createCallbackPair(win, fail);
+        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
         var id = utils.createUUID();
-        timers[id] = window.setInterval(function() {
-            if (accel) {
-                successCallback(accel);
-            }
-        }, frequency);
 
-        // Success callback from native just updates the accel object.
-        var win = function(a) {
-            accel = new Acceleration(a.x, a.y, a.z, a.timestamp);
-        };
+        var p = createCallbackPair(function(){}, function(e) {
+            errorCallback(e);
+            removeListeners(p);
+        });
+        listeners.push(p);
 
-        // Fail callback clears the watch and sends an error back.
-        var fail = function(err) {
-            accelerometer.clearWatch(id);
-            errorCallback(err);
+        timers[id] = {
+            timer:window.setInterval(function() {
+                if (accel) {
+                    successCallback(accel);
+                }
+            }, frequency),
+            listeners:p
         };
 
-        exec(win, fail, "Accelerometer", "addWatch", [id, frequency]);
+        if (running) {
+            // If we're already running then immediately invoke the success 
callback
+            successCallback(accel);
+        } else {
+            start();
+        }
 
         return id;
     },
@@ -3452,9 +3509,9 @@ var accelerometer = {
     clearWatch: function(id) {
         // Stop javascript timer & remove from timer list
         if (id && timers[id]) {
-            window.clearInterval(timers[id]);
+            window.clearInterval(timers[id].timer);
+            removeListeners(timers[id].listeners);
             delete timers[id];
-            exec(null, null, "Accelerometer", "clearWatch", [id]);
         }
     }
 };

http://git-wip-us.apache.org/repos/asf/incubator-cordova-android/blob/2d5dcf24/framework/src/org/apache/cordova/AccelListener.java
----------------------------------------------------------------------
diff --git a/framework/src/org/apache/cordova/AccelListener.java 
b/framework/src/org/apache/cordova/AccelListener.java
index bc8bf17..551e8bc 100755
--- a/framework/src/org/apache/cordova/AccelListener.java
+++ b/framework/src/org/apache/cordova/AccelListener.java
@@ -58,9 +58,8 @@ public class AccelListener extends Plugin implements 
SensorEventListener {
 
     private SensorManager sensorManager;    // Sensor manager
     private Sensor mSensor;                                                  
// Acceleration sensor returned by sensor manager
-    
-    private HashMap<String, String> watches = new HashMap<String, String>();
-    private List<String> callbacks = new ArrayList<String>();
+
+    private String callbackId;              // Keeps track of the single 
"start" callback ID passed in from JS
 
     /**
      * Create an accelerometer listener.
@@ -93,62 +92,28 @@ public class AccelListener extends Plugin implements 
SensorEventListener {
      * @return                                 A PluginResult object with a 
status and message.
      */
     public PluginResult execute(String action, JSONArray args, String 
callbackId) {
-       PluginResult.Status status = PluginResult.Status.NO_RESULT;
-       String message = "";
-       PluginResult result = new PluginResult(status, message);
-       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;
-    }
+        PluginResult.Status status = PluginResult.Status.NO_RESULT;
+        String message = "";
+        PluginResult result = new PluginResult(status, message);
+        result.setKeepCallback(true);  
 
-    /**
-     * Identifies if action to be executed returns a value and should be run 
synchronously.
-     * 
-     * @param action   The action to execute
-     * @return                 T=returns value
-     */
-    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;
+        if (action.equals("start")) {
+            this.callbackId = callbackId;
+            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.start();
+            }
+        }
+        else if (action.equals("stop")) {
+            if (this.status == AccelListener.RUNNING) {
+                this.stop();
+            }
+        } else {
+          // Unsupported action
+            return new PluginResult(PluginResult.Status.INVALID_ACTION);
         }
-        return false;
+        return result;
     }
     
     /**
@@ -162,10 +127,7 @@ public class AccelListener extends Plugin implements 
SensorEventListener {
     
//--------------------------------------------------------------------------
     // LOCAL METHODS
     
//--------------------------------------------------------------------------
-
-    private int size() {
-       return this.watches.size() + this.callbacks.size();
-    }
+    //
     /**
      * Start listening for acceleration sensor.
      * 
@@ -267,74 +229,47 @@ public class AccelListener extends Plugin implements 
SensorEventListener {
                this.z = event.values[2];
 
                this.win();
-
-               if (this.size() == 0) {
-                       this.stop();
-               }
         }
     }
 
+    // Sends an error back to JS
     private void fail(int code, String message) {
-       // Error object
-       JSONObject errorObj = new JSONObject();
-       try {
-                       errorObj.put("code", code);
-               errorObj.put("message", message);
-               } catch (JSONException e) {
-                       // TODO Auto-generated catch block
-                       e.printStackTrace();
-               }
-       PluginResult err = new PluginResult(PluginResult.Status.ERROR, 
errorObj);
-       
-        for (String callbackId: this.callbacks)
-        {
-               this.error(err, callbackId);
+        // Error object
+        JSONObject errorObj = new JSONObject();
+        try {
+            errorObj.put("code", code);
+            errorObj.put("message", message);
+        } catch (JSONException e) {
+            e.printStackTrace();
         }
-        this.callbacks.clear();
-        
+        PluginResult err = new PluginResult(PluginResult.Status.ERROR, 
errorObj);
         err.setKeepCallback(true);
-        
-        Iterator it = this.watches.entrySet().iterator();
-        while (it.hasNext()) {
-            Map.Entry pairs = (Map.Entry)it.next();
-            this.error(err, (String)pairs.getValue());
-        }
+
+        this.error(err, this.callbackId);
     }
     
     private void win() {
-       // Success return object
-       PluginResult result = new PluginResult(PluginResult.Status.OK, 
this.getAccelerationJSON());
-       
-       for (String callbackId: this.callbacks)
-        {
-               this.success(result, callbackId);
-        }
-        this.callbacks.clear();
-        
+        // Success return object
+        PluginResult result = new PluginResult(PluginResult.Status.OK, 
this.getAccelerationJSON());
         result.setKeepCallback(true);
-        
-        Iterator it = this.watches.entrySet().iterator();
-        while (it.hasNext()) {
-            Map.Entry pairs = (Map.Entry)it.next();
-            this.success(result, (String)pairs.getValue());
-        }
+
+        this.success(result, this.callbackId);
     }
 
-       private void setStatus(int status) {
+    private void setStatus(int status) {
         this.status = status;
     }
        
-       private JSONObject getAccelerationJSON() {
-               JSONObject r = new JSONObject();
-               try {
-                       r.put("x", this.x);
-                       r.put("y", this.y);
-                       r.put("z", this.z);
-                       r.put("timestamp", this.timestamp);
-               } catch (JSONException e) {
-                       // TODO Auto-generated catch block
-                       e.printStackTrace();
-               }
-               return r;
-       }
+    private JSONObject getAccelerationJSON() {
+        JSONObject r = new JSONObject();
+        try {
+            r.put("x", this.x);
+            r.put("y", this.y);
+            r.put("z", this.z);
+            r.put("timestamp", this.timestamp);
+        } catch (JSONException e) {
+            e.printStackTrace();
+        }
+        return r;
+    }
 }

Reply via email to