From 2d52f941c960e5b05af0825da9e2cc0112e78dcd Mon Sep 17 00:00:00 2001 From: Joe Bowser Date: Wed, 18 Nov 2009 11:09:20 -0800 Subject: [PATCH] Updating the Accelerometer to the latest version of Android --- src/com/phonegap/AccelListener.java | 55 +++++++++++++++++------------ 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/src/com/phonegap/AccelListener.java b/src/com/phonegap/AccelListener.java index 629bcbf2..25cbc489 100644 --- a/src/com/phonegap/AccelListener.java +++ b/src/com/phonegap/AccelListener.java @@ -1,19 +1,21 @@ package com.phonegap; -import static android.hardware.SensorManager.DATA_X; -import static android.hardware.SensorManager.DATA_Y; -import static android.hardware.SensorManager.DATA_Z; + +import java.util.List; + +import android.hardware.Sensor; +import android.hardware.SensorEvent; +import android.hardware.SensorEventListener; import android.hardware.SensorManager; import android.content.Context; -import android.hardware.SensorListener; import android.webkit.WebView; -@SuppressWarnings("deprecation") -public class AccelListener implements SensorListener{ +public class AccelListener implements SensorEventListener{ WebView mAppView; Context mCtx; String mKey; + Sensor mSensor; int mTime = 10000; boolean started = false; @@ -26,16 +28,21 @@ public class AccelListener implements SensorListener{ mCtx = ctx; mAppView = appView; sensorManager = (SensorManager) mCtx.getSystemService(Context.SENSOR_SERVICE); + } public void start(int time) { mTime = time; - if (!started) + List list = this.sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER); + if (list.size() > 0) { - sensorManager.registerListener(this, - SensorManager.SENSOR_ACCELEROMETER, - SensorManager.SENSOR_DELAY_GAME); + this.mSensor = list.get(0); + this.sensorManager.registerListener(this, this.mSensor, SensorManager.SENSOR_DELAY_NORMAL); + } + else + { + // Call fail } } @@ -45,23 +52,25 @@ public class AccelListener implements SensorListener{ sensorManager.unregisterListener(this); } - public void onAccuracyChanged(int sensor, int accuracy) { - // This should call the FAIL method - } - public void onSensorChanged(int sensor, float[] values) { - if (sensor != SensorManager.SENSOR_ACCELEROMETER || values.length < 3) - return; + + public void onAccuracyChanged(Sensor sensor, int accuracy) { + // TODO Auto-generated method stub + + } + + public void onSensorChanged(SensorEvent event) { + if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) + return; long curTime = System.currentTimeMillis(); - if (lastUpdate == -1 || (curTime - lastUpdate) > mTime) { - + if (lastUpdate == -1 || (curTime - lastUpdate) > mTime) { lastUpdate = curTime; - - float x = values[DATA_X]; - float y = values[DATA_Y]; - float z = values[DATA_Z]; + + float x = event.values[0]; + float y = event.values[1]; + float z = event.values[2]; mAppView.loadUrl("javascript:gotAccel(" + x + ", " + y + "," + z + " )"); - } + } }