Updating the Accelerometer to the latest version of Android

This commit is contained in:
Joe Bowser 2009-11-18 11:09:20 -08:00
parent 324c023d5f
commit c570a8e160

View File

@ -1,19 +1,21 @@
package com.phonegap; package com.phonegap;
import static android.hardware.SensorManager.DATA_X;
import static android.hardware.SensorManager.DATA_Y; import java.util.List;
import static android.hardware.SensorManager.DATA_Z;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager; import android.hardware.SensorManager;
import android.content.Context; import android.content.Context;
import android.hardware.SensorListener;
import android.webkit.WebView; import android.webkit.WebView;
@SuppressWarnings("deprecation") public class AccelListener implements SensorEventListener{
public class AccelListener implements SensorListener{
WebView mAppView; WebView mAppView;
Context mCtx; Context mCtx;
String mKey; String mKey;
Sensor mSensor;
int mTime = 10000; int mTime = 10000;
boolean started = false; boolean started = false;
@ -26,16 +28,21 @@ public class AccelListener implements SensorListener{
mCtx = ctx; mCtx = ctx;
mAppView = appView; mAppView = appView;
sensorManager = (SensorManager) mCtx.getSystemService(Context.SENSOR_SERVICE); sensorManager = (SensorManager) mCtx.getSystemService(Context.SENSOR_SERVICE);
} }
public void start(int time) public void start(int time)
{ {
mTime = time; mTime = time;
if (!started) List<Sensor> list = this.sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
if (list.size() > 0)
{ {
sensorManager.registerListener(this, this.mSensor = list.get(0);
SensorManager.SENSOR_ACCELEROMETER, this.sensorManager.registerListener(this, this.mSensor, SensorManager.SENSOR_DELAY_NORMAL);
SensorManager.SENSOR_DELAY_GAME); }
else
{
// Call fail
} }
} }
@ -45,23 +52,25 @@ public class AccelListener implements SensorListener{
sensorManager.unregisterListener(this); 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) public void onAccuracyChanged(Sensor sensor, int accuracy) {
return; // TODO Auto-generated method stub
}
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
return;
long curTime = System.currentTimeMillis(); long curTime = System.currentTimeMillis();
if (lastUpdate == -1 || (curTime - lastUpdate) > mTime) { if (lastUpdate == -1 || (curTime - lastUpdate) > mTime) {
lastUpdate = curTime; lastUpdate = curTime;
float x = values[DATA_X]; float x = event.values[0];
float y = values[DATA_Y]; float y = event.values[1];
float z = values[DATA_Z]; float z = event.values[2];
mAppView.loadUrl("javascript:gotAccel(" + x + ", " + y + "," + z + " )"); mAppView.loadUrl("javascript:gotAccel(" + x + ", " + y + "," + z + " )");
} }
} }