You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by he...@apache.org on 2013/04/30 23:30:36 UTC
[1/2] git commit: removed redundant code
Updated Branches:
refs/heads/master d0c8408b2 -> 57ebfb7a3
removed redundant code
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/d8d7e5d3
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/tree/d8d7e5d3
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/diff/d8d7e5d3
Branch: refs/heads/master
Commit: d8d7e5d3f4da39cc1af649b0b70cd5cb18395dcf
Parents: d0c8408
Author: hermwong <he...@gmail.com>
Authored: Tue Apr 30 14:28:44 2013 -0700
Committer: hermwong <he...@gmail.com>
Committed: Tue Apr 30 14:28:44 2013 -0700
----------------------------------------------------------------------
www/device-motion.js | 38 --------------------------------------
1 files changed, 0 insertions(+), 38 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/blob/d8d7e5d3/www/device-motion.js
----------------------------------------------------------------------
diff --git a/www/device-motion.js b/www/device-motion.js
deleted file mode 100644
index b6cf929..0000000
--- a/www/device-motion.js
+++ /dev/null
@@ -1,38 +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.
- *
-*/
-
-// TODO: add the path to the requires
-var Accelerometer = require('');
-var watchID;
-
-module.exports = {
- getCurrentAcceleration:function(win, fail) {
- navigator.accelerometer.getCurrentAcceleration(win,fail);
- }
-
- watchAcceleration:function(win, fail, [options]) {
- watchID = navigator.accelerometer.watchAccelerometer(win, fail, [options]);
- }
-
- clearWatch:function() {
- navigator.accelerometer.clearWatch(watchID);
- }
-};
[2/2] git commit: added Acceleration & accelerometer code from
Cordova-JS, updated plugin.xml to reflect changes
Posted by he...@apache.org.
added Acceleration & accelerometer code from Cordova-JS, updated plugin.xml to reflect changes
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/57ebfb7a
Tree: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/tree/57ebfb7a
Diff: http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/diff/57ebfb7a
Branch: refs/heads/master
Commit: 57ebfb7a305e8c3cb26c0c9eaf32b7d509695be6
Parents: d8d7e5d
Author: hermwong <he...@gmail.com>
Authored: Tue Apr 30 14:29:45 2013 -0700
Committer: hermwong <he...@gmail.com>
Committed: Tue Apr 30 14:29:45 2013 -0700
----------------------------------------------------------------------
plugin.xml | 11 ++-
www/Acceleration.js | 29 ++++++++
www/accelerometer.js | 170 +++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 207 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/blob/57ebfb7a/plugin.xml
----------------------------------------------------------------------
diff --git a/plugin.xml b/plugin.xml
index 60fba9f..f239da6 100644
--- a/plugin.xml
+++ b/plugin.xml
@@ -7,10 +7,14 @@ id="org.apache.cordova.core.AccelListener"
<name>Device Motion</name>
- <js-module src="www/device-motion.js" name="accelerometer">
+ <js-module src="www/Acceleration.js" name="Acceleration">
+ <clobbers target="Acceleration" />
+ </js-module>
+
+ <js-module src="www/accelerometer.js" name="accelerometer">
<clobbers target="navigator.accelerometer" />
</js-module>
-
+
<!-- android -->
<platform name="android">
@@ -19,7 +23,8 @@ id="org.apache.cordova.core.AccelListener"
</config-file>
<source-file src="src/android/AccelListener.java" target-dir="src/org/apache/cordova/core" />
- </platform>
+
+ </platform>
<!-- ios -->
<platform name="ios">
http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/blob/57ebfb7a/www/Acceleration.js
----------------------------------------------------------------------
diff --git a/www/Acceleration.js b/www/Acceleration.js
new file mode 100644
index 0000000..d1669b5
--- /dev/null
+++ b/www/Acceleration.js
@@ -0,0 +1,29 @@
+/*
+ *
+ * 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.
+ *
+*/
+
+var Acceleration = function(x, y, z, timestamp) {
+ this.x = x;
+ this.y = y;
+ this.z = z;
+ this.timestamp = timestamp || (new Date()).getTime();
+};
+
+module.exports = Acceleration;
http://git-wip-us.apache.org/repos/asf/cordova-plugin-device-motion/blob/57ebfb7a/www/accelerometer.js
----------------------------------------------------------------------
diff --git a/www/accelerometer.js b/www/accelerometer.js
new file mode 100644
index 0000000..da50487
--- /dev/null
+++ b/www/accelerometer.js
@@ -0,0 +1,170 @@
+/*
+ *
+ * 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('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 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();
+ }
+
+ 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;