Added compass support to BB10.

- overrides the common compass to support start/stop native calls
- added proxy to deviceorientation objects for bb10


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/commit/3d45eab3
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/tree/3d45eab3
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/diff/3d45eab3

Branch: refs/heads/master
Commit: 3d45eab39de778a44e11acf912048e20f5963bbd
Parents: dd9c857
Author: Gord Tanner <gtan...@gmail.com>
Authored: Thu Aug 30 16:21:23 2012 -0400
Committer: Gord Tanner <gtan...@gmail.com>
Committed: Thu Sep 27 17:35:51 2012 -0400

----------------------------------------------------------------------
 lib/webworks/qnx/plugin/compass.js     |  141 +++++++++++++++++++++++++++
 lib/webworks/qnx/plugin/manager.js     |    1 +
 lib/webworks/qnx/plugin/qnx/compass.js |   24 +++++
 3 files changed, 166 insertions(+), 0 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/3d45eab3/lib/webworks/qnx/plugin/compass.js
----------------------------------------------------------------------
diff --git a/lib/webworks/qnx/plugin/compass.js 
b/lib/webworks/qnx/plugin/compass.js
new file mode 100644
index 0000000..7e07ff3
--- /dev/null
+++ b/lib/webworks/qnx/plugin/compass.js
@@ -0,0 +1,141 @@
+var exec = require('cordova/exec'),
+    utils = require('cordova/utils'),
+    CompassHeading = require('cordova/plugin/CompassHeading'),
+    CompassError = require('cordova/plugin/CompassError'),
+    timers = {},
+    listeners = [],
+    heading = null,
+    running = false,
+    start = function () {
+        exec(function (result) {
+            heading = new CompassHeading(result.magneticHeading, 
result.trueHeading, result.headingAccuracy, result.timestamp);
+            listeners.forEach(function (l) {
+                l.win(heading);
+            });
+        }, function (e) {
+            listeners.forEach(function (l) {
+                l.fail(e);
+            });
+        },
+        "Compass", "start", []);
+        running = true;
+    },
+    stop = function () {
+        exec(null, null, "Compass", "stop", []);
+        running = false;
+    },
+    createCallbackPair = function (win, fail) {
+        return {win:win, fail:fail};
+    },
+    removeListeners = function (l) {
+        var idx = listeners.indexOf(l);
+        if (idx > -1) {
+            listeners.splice(idx, 1);
+            if (listeners.length === 0) {
+                stop();
+            }
+        }
+    },
+    compass = {
+        /**
+         * Asynchronously acquires the current heading.
+         * @param {Function} successCallback The function to call when the 
heading
+         * data is available
+         * @param {Function} errorCallback The function to call when there is 
an error
+         * getting the heading data.
+         * @param {CompassOptions} options The options for getting the heading 
data (not used).
+         */
+        getCurrentHeading:function(successCallback, errorCallback, options) {
+            if (typeof successCallback !== "function") {
+                throw "getCurrentHeading must be called with at least a 
success callback function as first parameter.";
+            }
+
+            var p;
+            var win = function(a) {
+                removeListeners(p);
+                successCallback(a);
+            };
+            var fail = function(e) {
+                removeListeners(p);
+                errorCallback(e);
+            };
+
+            p = createCallbackPair(win, fail);
+            listeners.push(p);
+
+            if (!running) {
+                start();
+            }
+        },
+
+        /**
+         * Asynchronously acquires the heading repeatedly at a given interval.
+         * @param {Function} successCallback The function to call each time 
the heading
+         * data is available
+         * @param {Function} errorCallback The function to call when there is 
an error
+         * getting the heading data.
+         * @param {HeadingOptions} options The options for getting the heading 
data
+         * such as timeout and the frequency of the watch. For iOS, filter 
parameter
+         * specifies to watch via a distance filter rather than time.
+         */
+        watchHeading:function(successCallback, errorCallback, options) {
+            var frequency = (options !== undefined && options.frequency !== 
undefined) ? options.frequency : 100;
+            var filter = (options !== undefined && options.filter !== 
undefined) ? options.filter : 0;
+
+            // successCallback required
+            if (typeof successCallback !== "function") {
+              console.log("Compass Error: successCallback is not a function");
+              return;
+            }
+
+            // errorCallback optional
+            if (errorCallback && (typeof errorCallback !== "function")) {
+              console.log("Compass Error: errorCallback is not a function");
+              return;
+            }
+            // Keep reference to watch id, and report heading readings as 
often as defined in frequency
+            var id = utils.createUUID();
+
+            var p = createCallbackPair(function(){}, function(e) {
+                removeListeners(p);
+                errorCallback(e);
+            });
+            listeners.push(p);
+
+            timers[id] = {
+                timer:window.setInterval(function() {
+                    if (heading) {
+                        successCallback(heading);
+                    }
+                }, 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(heading) {
+                    successCallback(heading);
+                }
+            } else {
+                start();
+            }
+
+            return id;
+        },
+
+        /**
+         * Clears the specified heading watch.
+         * @param {String} watchId The ID of the watch returned from 
#watchHeading.
+         */
+        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 = compass;

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/3d45eab3/lib/webworks/qnx/plugin/manager.js
----------------------------------------------------------------------
diff --git a/lib/webworks/qnx/plugin/manager.js 
b/lib/webworks/qnx/plugin/manager.js
index cfa6ebd..c08fb17 100644
--- a/lib/webworks/qnx/plugin/manager.js
+++ b/lib/webworks/qnx/plugin/manager.js
@@ -25,6 +25,7 @@ var cordova = require('cordova'),
         'Accelerometer' : require('cordova/plugin/webworks/accelerometer'),
         'Device' : require('cordova/plugin/qnx/device'),
         'Battery' : require('cordova/plugin/qnx/battery'),
+        'Compass' : require('cordova/plugin/qnx/compass'),
         'Logger' : require('cordova/plugin/webworks/logger'),
         'Notification' : require('cordova/plugin/webworks/notification'),
         'Media': require('cordova/plugin/webworks/media')

http://git-wip-us.apache.org/repos/asf/incubator-cordova-js/blob/3d45eab3/lib/webworks/qnx/plugin/qnx/compass.js
----------------------------------------------------------------------
diff --git a/lib/webworks/qnx/plugin/qnx/compass.js 
b/lib/webworks/qnx/plugin/qnx/compass.js
new file mode 100644
index 0000000..6800d69
--- /dev/null
+++ b/lib/webworks/qnx/plugin/qnx/compass.js
@@ -0,0 +1,24 @@
+var cordova = require('cordova'),
+    callback;
+
+module.exports = {
+    start: function (args, win, fail) {
+        window.removeEventListener("deviceorientation", callback);
+        callback = function (orientation) {
+            var heading = 360 - orientation.alpha;
+            win({
+                magneticHeading: heading,
+                trueHeading: heading,
+                headingAccuracy: 0,
+                timestamp: orientation.timeStamp
+            });
+        };
+
+        window.addEventListener("deviceorientation", callback);
+        return { "status" : cordova.callbackStatus.NO_RESULT, "message" : 
"WebWorks Is On It" };
+    },
+    stop: function (args, win, fail) {
+        window.removeEventListener("deviceorientation", callback);
+        return { "status" : cordova.callbackStatus.NO_RESULT, "message" : 
"WebWorks Is On It" };
+    }
+}

Reply via email to