cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject [1/2] git commit: Added support for the browser
Date Fri, 05 Sep 2014 17:57:48 GMT
Repository: cordova-plugin-device-motion
Updated Branches:
  refs/heads/master 965b134c6 -> 3ba491b3e


Added support for the browser


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/7dca3b5d
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/tree/7dca3b5d
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/diff/7dca3b5d

Branch: refs/heads/master
Commit: 7dca3b5dbda031fd4f1699f0192ce29db6a0bff8
Parents: 965b134
Author: Suraj Pindoria <suraj.pindoria@yahoo.com>
Authored: Fri Aug 29 16:39:53 2014 -0700
Committer: Suraj Pindoria <suraj.pindoria@yahoo.com>
Committed: Fri Aug 29 16:39:53 2014 -0700

----------------------------------------------------------------------
 plugin.xml                        |  11 +++
 src/browser/AccelerometerProxy.js |  45 +++++++++
 src/browser/accelerometer.js      | 175 +++++++++++++++++++++++++++++++++
 3 files changed, 231 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/blob/7dca3b5d/plugin.xml
----------------------------------------------------------------------
diff --git a/plugin.xml b/plugin.xml
index c016c8d..8b75dba 100644
--- a/plugin.xml
+++ b/plugin.xml
@@ -148,4 +148,15 @@
             <runs/>
         </js-module>
     </platform>
+
+    <!-- browser -->
+    <platform name="browser">
+        <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/7dca3b5d/src/browser/AccelerometerProxy.js
----------------------------------------------------------------------
diff --git a/src/browser/AccelerometerProxy.js b/src/browser/AccelerometerProxy.js
new file mode 100644
index 0000000..838ae54
--- /dev/null
+++ b/src/browser/AccelerometerProxy.js
@@ -0,0 +1,45 @@
+/*
+ *
+ * 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.
+ *
+*/
+
+
+function listener(success) {
+    var accel = {};
+
+    accel.x = (Math.round(((Math.random() * 2) - 1) * 100) / 100);
+    accel.y = (Math.round(((Math.random() * 2) - 1) * 100) / 100);
+    accel.z = (Math.round(((Math.random() * 2) - 1) * 100) / 100);
+    accel.timestamp = new Date().getTime();
+
+    success(accel);
+
+    window.removeEventListener('devicemotion', listener, false);
+}
+
+var Accelerometer = {
+    start: function start(success, error) {
+        return window.addEventListener('devicemotion', function(){
+            listener(success);
+        }, false);
+    }
+};
+
+module.exports = Accelerometer;
+require('cordova/exec/proxy').add('Accelerometer', Accelerometer);

http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/blob/7dca3b5d/src/browser/accelerometer.js
----------------------------------------------------------------------
diff --git a/src/browser/accelerometer.js b/src/browser/accelerometer.js
new file mode 100644
index 0000000..548cf9f
--- /dev/null
+++ b/src/browser/accelerometer.js
@@ -0,0 +1,175 @@
+/*
+ *
+ * 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;


Mime
View raw message