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 3ee7589bb8
commit 2d52f941c9

View File

@ -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<Sensor> 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,21 +52,23 @@ public class AccelListener implements SensorListener{
sensorManager.unregisterListener(this);
}
public void onAccuracyChanged(int sensor, int accuracy) {
// This should call the FAIL method
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
public void onSensorChanged(int sensor, float[] values) {
if (sensor != SensorManager.SENSOR_ACCELEROMETER || values.length < 3)
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
return;
long curTime = System.currentTimeMillis();
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 + " )");
}
}