From 64671184a63206bfe7d8f1f8a56bdbb0d1fa1952 Mon Sep 17 00:00:00 2001 From: Joe Bowser Date: Wed, 28 Apr 2010 15:46:15 -0700 Subject: [PATCH] Initial Commit - Fixing Accelerometer --- framework/assets/js/accelerometer.js | 56 +++++++++++-------- framework/src/com/phonegap/AccelBroker.java | 33 +++++++++++ framework/src/com/phonegap/AccelListener.java | 13 +++-- framework/src/com/phonegap/DroidGap.java | 4 +- 4 files changed, 77 insertions(+), 29 deletions(-) create mode 100644 framework/src/com/phonegap/AccelBroker.java diff --git a/framework/assets/js/accelerometer.js b/framework/assets/js/accelerometer.js index f6820157..e4a1a40b 100644 --- a/framework/assets/js/accelerometer.js +++ b/framework/assets/js/accelerometer.js @@ -4,20 +4,11 @@ function Acceleration(x, y, z) this.y = y; this.z = z; this.timestamp = new Date().getTime(); + this.win = null; + this.fail = null; } -// Need to define these for android -_accel = {}; -_accel.x = 0; -_accel.y = 0; -_accel.z = 0; - -function gotAccel(x, y, z) -{ - _accel.x = x; - _accel.y = y; - _accel.z = z; -} +var accelListeners = []; /** * This class provides access to device accelerometer data. @@ -45,12 +36,29 @@ Accelerometer.prototype.getCurrentAcceleration = function(successCallback, error // Created for iPhone, Iphone passes back _accel obj litteral if (typeof successCallback == "function") { - var accel = new Acceleration(_accel.x,_accel.y,_accel.z); - Accelerometer.lastAcceleration = accel; - successCallback(accel); + if(this.lastAcceleration) + successCallback(accel); } } + +Accelerometer.prototype.gotAccel = function(key, x, y, z) +{ + var a = Acceleration(x,y,z); + a.x = x; + a.y = y; + a.x = z; + a.win = accelListener[key].win; + a.fail = accelListener[key].fail; + this.timestamp = new Date().getTime(); + this.lastAcceleration = a; + accelListener[key] = a; + if (typeof a.win == "function") { + a.win(a); + } +} + + /** * Asynchronously aquires the acceleration repeatedly at a given interval. * @param {Function} successCallback The function to call each time the acceleration @@ -64,11 +72,12 @@ Accelerometer.prototype.getCurrentAcceleration = function(successCallback, error Accelerometer.prototype.watchAcceleration = function(successCallback, errorCallback, options) { // TODO: add the interval id to a list so we can clear all watches var frequency = (options != undefined)? options.frequency : 10000; - - Accel.start(frequency); - return setInterval(function() { - navigator.accelerometer.getCurrentAcceleration(successCallback, errorCallback, options); - }, frequency); + var accel = Acceleration(0,0,0); + accel.win = successCallback; + accel.fail = errorCallback; + accel.opts = options; + var key = accelListeners.push( accel ) - 1; + Accel.start(frequency, key); } /** @@ -76,8 +85,11 @@ Accelerometer.prototype.watchAcceleration = function(successCallback, errorCallb * @param {String} watchId The ID of the watch returned from #watchAcceleration. */ Accelerometer.prototype.clearWatch = function(watchId) { - Accel.stop(); - clearInterval(watchId); + Accel.stop(watchId); +} + +Accelerometer.prototype.epicFail = function(watchId, message) { + accelWatcher[key].fail(); } PhoneGap.addConstructor(function() { diff --git a/framework/src/com/phonegap/AccelBroker.java b/framework/src/com/phonegap/AccelBroker.java new file mode 100644 index 00000000..a07f2652 --- /dev/null +++ b/framework/src/com/phonegap/AccelBroker.java @@ -0,0 +1,33 @@ +package com.phonegap; + +import java.util.HashMap; + +import android.content.Context; +import android.webkit.WebView; + +public class AccelBroker { + private WebView mAppView; + private Context mCtx; + private HashMap accelListeners; + + public AccelBroker(WebView view, Context ctx) + { + mCtx = ctx; + mAppView = view; + accelListeners = new HashMap(); + } + + public String start(int freq, String key) + { + AccelListener listener = new AccelListener(key, freq, mCtx, mAppView); + accelListeners.put(key, listener); + return key; + } + + public void stop(String key) + { + AccelListener acc = accelListeners.get(key); + acc.stop(); + } + +} diff --git a/framework/src/com/phonegap/AccelListener.java b/framework/src/com/phonegap/AccelListener.java index ce5edb3e..2ce75a78 100644 --- a/framework/src/com/phonegap/AccelListener.java +++ b/framework/src/com/phonegap/AccelListener.java @@ -15,7 +15,7 @@ public class AccelListener implements SensorEventListener{ WebView mAppView; Context mCtx; String mKey; - Sensor mSensor; + Sensor mSensor; int mTime = 10000; boolean started = false; @@ -23,10 +23,12 @@ public class AccelListener implements SensorEventListener{ private long lastUpdate = -1; - public AccelListener(Context ctx, WebView appView) + public AccelListener(String key, int freq, Context ctx, WebView appView) { mCtx = ctx; mAppView = appView; + mKey = key; + mTime = freq; sensorManager = (SensorManager) mCtx.getSystemService(Context.SENSOR_SERVICE); } @@ -42,7 +44,7 @@ public class AccelListener implements SensorEventListener{ } else { - // Call fail + mAppView.loadUrl("javascript:navigator.accelerometer.epicFail(" + mKey + ", 'Failed to start')"); } } @@ -68,8 +70,9 @@ public class AccelListener implements SensorEventListener{ float x = event.values[0]; float y = event.values[1]; - float z = event.values[2]; - mAppView.loadUrl("javascript:gotAccel(" + x + ", " + y + "," + z + " )"); + float z = event.values[2]; + //mAppView.loadUrl("javascript:gotAccel(" + x + ", " + y + "," + z + " )"); + mAppView.loadUrl("javascript:navigator.accelerometer.gotAccel(" + mKey + "," + x + "," + y + "," + z + ")"); } } diff --git a/framework/src/com/phonegap/DroidGap.java b/framework/src/com/phonegap/DroidGap.java index 6c46ac3b..d3669a72 100644 --- a/framework/src/com/phonegap/DroidGap.java +++ b/framework/src/com/phonegap/DroidGap.java @@ -55,7 +55,7 @@ public class DroidGap extends Activity { private PhoneGap gap; private GeoBroker geo; - private AccelListener accel; + private AccelBroker accel; private CameraLauncher launcher; private ContactManager mContacts; private FileUtils fs; @@ -135,7 +135,7 @@ public class DroidGap extends Activity { { gap = new PhoneGap(this, appView); geo = new GeoBroker(appView, this); - accel = new AccelListener(this, appView); + accel = new AccelBroker(appView, this); launcher = new CameraLauncher(appView, this); mContacts = new ContactManager(this, appView); fs = new FileUtils(appView);