You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ti...@apache.org on 2015/09/09 02:15:35 UTC

cordova-plugin-device-motion git commit: CB-9426 Fix exception when using device motion plugin on browser platform.

Repository: cordova-plugin-device-motion
Updated Branches:
  refs/heads/master b0f8e436e -> 8f1ad44ee


CB-9426 Fix exception when using device motion plugin on browser platform.

The plugin's plugin.xml defines a general <js-module> with the name 'accelerometer'. It defines another <js-module> for the browser platform (that merges with the general module) with the same name. Modules with the same name is not allowed, so Cordova throws an exception at runtime when trying to define the browser version of the module.

Since the only difference in the browser version of the module is to fake up devicemotion events, I've deleted the browser version and added that logic to the general module, with the following change: rather than starting a new event timer everytime watchAcceleration() is called (which could happen multiple times resulting in multiple superfluous timers), we have a single event timer going as long as there are active watchers.


Project: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/commit/8f1ad44e
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/tree/8f1ad44e
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/diff/8f1ad44e

Branch: refs/heads/master
Commit: 8f1ad44eec7ca768257faae92b1b447bf9b02dbb
Parents: b0f8e43
Author: Tim Barham <ti...@microsoft.com>
Authored: Wed Jul 29 10:19:43 2015 +1000
Committer: Tim Barham <ti...@microsoft.com>
Committed: Wed Jul 29 11:58:40 2015 +1000

----------------------------------------------------------------------
 plugin.xml                   |   4 -
 src/browser/accelerometer.js | 175 --------------------------------------
 www/accelerometer.js         |  17 ++++
 3 files changed, 17 insertions(+), 179 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/blob/8f1ad44e/plugin.xml
----------------------------------------------------------------------
diff --git a/plugin.xml b/plugin.xml
index 2caf016..2f0ea15 100644
--- a/plugin.xml
+++ b/plugin.xml
@@ -161,9 +161,5 @@
         <js-module src="src/browser/AccelerometerProxy.js" name="AccelerometerProxy">
             <runs/>
         </js-module>
-
-        <js-module src="src/browser/accelerometer.js" name="accelerometer">
-            <merges target="navigator.accelerometer" />
-        </js-module>
     </platform>
 </plugin>

http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/blob/8f1ad44e/src/browser/accelerometer.js
----------------------------------------------------------------------
diff --git a/src/browser/accelerometer.js b/src/browser/accelerometer.js
deleted file mode 100644
index 548cf9f..0000000
--- a/src/browser/accelerometer.js
+++ /dev/null
@@ -1,175 +0,0 @@
-/*
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- *
-*/
-
-/**
- * This class provides access to device accelerometer data.
- * @constructor
- */
-var argscheck = require('cordova/argscheck'),
-    utils = require("cordova/utils"),
-    exec = require("cordova/exec"),
-    Acceleration = require('./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 acquires 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. (OPTIONAL)
-     * @param {AccelerationOptions} options The options for getting the accelerometer data such as timeout. (OPTIONAL)
-     */
-    getCurrentAcceleration: function(successCallback, errorCallback, options) {
-        argscheck.checkArgs('fFO', 'accelerometer.getCurrentAcceleration', arguments);
-
-        var p;
-        var win = function(a) {
-            removeListeners(p);
-            successCallback(a);
-        };
-        var fail = function(e) {
-            removeListeners(p);
-            errorCallback && errorCallback(e);
-        };
-
-        p = createCallbackPair(win, fail);
-        listeners.push(p);
-
-        if (!running) {
-            start();
-        }
-    },
-
-    /**
-     * Asynchronously acquires 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. (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.
-     */
-    watchAcceleration: function(successCallback, errorCallback, options) {
-        argscheck.checkArgs('fFO', 'accelerometer.watchAcceleration', arguments);
-        // Default interval (10 sec)
-        var frequency = (options && options.frequency && typeof options.frequency == 'number') ? options.frequency : 10000;
-
-        // Keep reference to watch id, and report accel readings as often as defined in frequency
-        var id = utils.createUUID();
-
-        var p = createCallbackPair(function(){}, function(e) {
-            removeListeners(p);
-            errorCallback && errorCallback(e);
-        });
-        listeners.push(p);
-
-        timers[id] = {
-            timer:window.setInterval(function() {
-                if (accel) {
-                    successCallback(accel);
-                }
-            }, frequency),
-            listeners:p
-        };
-
-        if (running) {
-            // If we're already running then immediately invoke the success callback
-            // but only if we have retrieved a value, sample code does not check for null ...
-            if (accel) {
-                successCallback(accel);
-            }
-        } else {
-            start();
-        }
-
-        // Continuously calls the devicemotion listener and gets new accelerometer values
-        devicemotion = new Event('devicemotion')
-        window.setInterval(function() {
-            window.dispatchEvent(devicemotion);
-        }, 200);
-
-        return id;
-    },
-
-    /**
-     * Clears the specified accelerometer watch.
-     *
-     * @param {String} id       The id of the watch returned from #watchAcceleration.
-     */
-    clearWatch: function(id) {
-        // Stop javascript timer & remove from timer list
-        if (id && timers[id]) {
-            window.clearInterval(timers[id].timer);
-            removeListeners(timers[id].listeners);
-            delete timers[id];
-        }
-    }
-};
-module.exports = accelerometer;

http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/blob/8f1ad44e/www/accelerometer.js
----------------------------------------------------------------------
diff --git a/www/accelerometer.js b/www/accelerometer.js
index 900899c..cfd016a 100644
--- a/www/accelerometer.js
+++ b/www/accelerometer.js
@@ -40,6 +40,9 @@ var listeners = [];
 // Last returned acceleration object from native
 var accel = null;
 
+// Timer used when faking up devicemotion events
+var eventTimerId = null;
+
 // Tells native to start.
 function start() {
     exec(function (a) {
@@ -161,6 +164,14 @@ var accelerometer = {
             start();
         }
 
+        if (cordova.platformId === "browser" && !eventTimerId) {
+            // Start firing devicemotion events if we haven't already
+            var devicemotionEvent = new Event('devicemotion');
+            eventTimerId = window.setInterval(function() {
+                window.dispatchEvent(devicemotionEvent);
+            }, 200);
+        }
+
         return id;
     },
 
@@ -175,6 +186,12 @@ var accelerometer = {
             window.clearInterval(timers[id].timer);
             removeListeners(timers[id].listeners);
             delete timers[id];
+
+            if (eventTimerId && Object.keys(timers).length === 0) {
+                // No more watchers, so stop firing 'devicemotion' events
+                window.clearInterval(eventTimerId);
+                eventTimerId = null;
+            }
         }
     }
 };


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org