mirror of
https://github.com/apache/cordova-android.git
synced 2026-04-23 00:00:09 +08:00
Change accelerometer to use JavaScript setInterval for watch.
This commit is contained in:
@@ -1,34 +1,156 @@
|
||||
package com.phonegap;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import android.content.Context;
|
||||
import android.webkit.WebView;
|
||||
|
||||
/**
|
||||
* This class manages access to the accelerometer from JavaScript.
|
||||
* One, free running accelerometer listener is created.
|
||||
* It's state is controlled by start() and stop().
|
||||
* JavaScript is responsible for starting, stopping, and retrieving status and values.
|
||||
*
|
||||
* Since there may be some delay between starting and the first available value, when
|
||||
* retrieving values from JavaScript, the thread sleeps until the first value is
|
||||
* received or until 1 sec elapses.
|
||||
*
|
||||
* @author bcurtis
|
||||
*
|
||||
*/
|
||||
public class AccelBroker {
|
||||
private WebView mAppView;
|
||||
private Context mCtx;
|
||||
private HashMap<String, AccelListener> accelListeners;
|
||||
|
||||
public static int STOPPED = 0;
|
||||
public static int STARTING = 1;
|
||||
public static int RUNNING = 2;
|
||||
public static int ERROR_FAILED_TO_START = 3;
|
||||
public static int ERROR_NOT_FOUND = 4;
|
||||
|
||||
public AccelBroker(WebView view, Context ctx)
|
||||
{
|
||||
mCtx = ctx;
|
||||
mAppView = view;
|
||||
accelListeners = new HashMap<String, AccelListener>();
|
||||
private WebView mAppView; // WebView object
|
||||
private Context mCtx; // Activity (DroidGap) object
|
||||
private AccelListener listener; // Accelerator listener
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param view
|
||||
* @param ctx
|
||||
*/
|
||||
public AccelBroker(WebView view, Context ctx)
|
||||
{
|
||||
mCtx = ctx;
|
||||
mAppView = view;
|
||||
|
||||
// Create listener
|
||||
listener = new AccelListener(mCtx, mAppView);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start listening to acceleration sensor.
|
||||
*
|
||||
* @return true if started, false if not
|
||||
*/
|
||||
public boolean start()
|
||||
{
|
||||
// Start listener if necessary
|
||||
if ((listener.status != AccelBroker.RUNNING) && (listener.status != AccelBroker.STARTING)) {
|
||||
listener.start();
|
||||
}
|
||||
|
||||
return ((listener.status == AccelBroker.RUNNING) || (listener.status == AccelBroker.STARTING));
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop listening for acceleration sensor.
|
||||
*
|
||||
* @return true if stopped, false if not
|
||||
*/
|
||||
public boolean stop()
|
||||
{
|
||||
listener.stop();
|
||||
return (listener.status == AccelBroker.STOPPED);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait until sensor is done starting up.
|
||||
* If a request for values is made while sensor is still starting, then delay thread until first reading is made.
|
||||
*/
|
||||
void waitToStart() {
|
||||
if (listener.status == AccelBroker.STARTING) {
|
||||
System.out.println("AccelBroker.waitToStart...");
|
||||
long timeout = 1000; // wait at most 1 sec
|
||||
while ((listener.status == AccelBroker.STARTING) && (timeout > 0)) {
|
||||
try {
|
||||
Thread.sleep(10);
|
||||
timeout = timeout - 10;
|
||||
}
|
||||
catch (InterruptedException e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get result of the last request or update if watching.
|
||||
* If sensor is still starting, wait until 1st value is acquired before returning.
|
||||
*
|
||||
* NOTE: NOT USED - DO WE NEED THIS, SINCE JSON MUST BE PARSED ON JS SIDE?
|
||||
*
|
||||
* @param key listener id
|
||||
* @return String representation of JSON object
|
||||
*/
|
||||
public String getResult() {
|
||||
|
||||
// Wait for startup
|
||||
this.waitToStart();
|
||||
|
||||
// If acceleration values
|
||||
if (listener.status == AccelBroker.RUNNING) {
|
||||
return "{status:" + listener.status + ",value:{x:" + listener.x + ", y:" + listener.y + ", z:" + listener.z + "}}";
|
||||
}
|
||||
|
||||
// If error or not running
|
||||
return "{status:" + listener.status + ",value:null}";
|
||||
}
|
||||
|
||||
/**
|
||||
* Get status of accelerometer sensor.
|
||||
*
|
||||
* @return status
|
||||
*/
|
||||
public int getStatus() {
|
||||
return listener.status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get X value of last accelerometer value.
|
||||
* If sensor is still starting, wait until 1st value is acquired before returning.
|
||||
*
|
||||
* @return x value
|
||||
*/
|
||||
public float getX() {
|
||||
this.waitToStart();
|
||||
return listener.x;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Y value of last accelerometer value.
|
||||
* If sensor is still starting, wait until 1st value is acquired before returning.
|
||||
*
|
||||
* @return y value
|
||||
*/
|
||||
public float getY() {
|
||||
this.waitToStart();
|
||||
return listener.y;
|
||||
}
|
||||
|
||||
public String start(int freq, String key)
|
||||
{
|
||||
AccelListener listener = new AccelListener(key, freq, mCtx, mAppView);
|
||||
listener.start(freq);
|
||||
accelListeners.put(key, listener);
|
||||
return key;
|
||||
}
|
||||
|
||||
public void stop(String key)
|
||||
{
|
||||
AccelListener acc = accelListeners.get(key);
|
||||
acc.stop();
|
||||
|
||||
/**
|
||||
* Get Z value of last accelerometer value.
|
||||
* If sensor is still starting, wait until 1st value is acquired before returning.
|
||||
*
|
||||
* @return z value
|
||||
*/
|
||||
public float getZ() {
|
||||
this.waitToStart();
|
||||
return listener.x;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,69 +12,90 @@ import android.webkit.WebView;
|
||||
|
||||
public class AccelListener implements SensorEventListener{
|
||||
|
||||
WebView mAppView;
|
||||
Context mCtx;
|
||||
String mKey;
|
||||
Sensor mSensor;
|
||||
int mTime = 10000;
|
||||
boolean started = false;
|
||||
|
||||
private SensorManager sensorManager;
|
||||
|
||||
private long lastUpdate = -1;
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
public void start(int time)
|
||||
{
|
||||
mTime = time;
|
||||
List<Sensor> list = this.sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
|
||||
if (list.size() > 0)
|
||||
{
|
||||
this.mSensor = list.get(0);
|
||||
this.sensorManager.registerListener(this, this.mSensor, SensorManager.SENSOR_DELAY_FASTEST);
|
||||
}
|
||||
else
|
||||
{
|
||||
mAppView.loadUrl("javascript:navigator.accelerometer.epicFail(" + mKey + ", 'Failed to start')");
|
||||
}
|
||||
}
|
||||
|
||||
public void stop()
|
||||
{
|
||||
if(started)
|
||||
sensorManager.unregisterListener(this);
|
||||
}
|
||||
|
||||
|
||||
WebView mAppView; // WebView object
|
||||
Context mCtx; // Activity (DroidGap) object
|
||||
|
||||
float x,y,z; // most recent acceleration values
|
||||
long timeStamp; // time of most recent value
|
||||
int status; // status of listener
|
||||
|
||||
public void onAccuracyChanged(Sensor sensor, int accuracy) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
private SensorManager sensorManager;// Sensor manager
|
||||
Sensor mSensor; // Acceleration sensor returned by sensor manager
|
||||
|
||||
/**
|
||||
* Create an accelerometer listener.
|
||||
*
|
||||
* @param ctx The Activity (DroidGap) object
|
||||
* @param appView
|
||||
*/
|
||||
public AccelListener(Context ctx, WebView appView) {
|
||||
this.mCtx = ctx;
|
||||
this.mAppView = appView;
|
||||
this.sensorManager = (SensorManager) mCtx.getSystemService(Context.SENSOR_SERVICE);
|
||||
this.x = 0;
|
||||
this.y = 0;
|
||||
this.z = 0;
|
||||
this.timeStamp = 0;
|
||||
this.status = AccelBroker.STOPPED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start listening for acceleration sensor.
|
||||
*
|
||||
* @return status of listener
|
||||
*/
|
||||
public int start() {
|
||||
|
||||
// Get accelerometer from sensor manager
|
||||
List<Sensor> list = this.sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
|
||||
//list = null; // @test failure AccelBroker.ERROR_FAILED_TO_START
|
||||
|
||||
// If found, then register as listener
|
||||
if ((list != null) && (list.size() > 0)) {
|
||||
this.mSensor = list.get(0);
|
||||
this.sensorManager.registerListener(this, this.mSensor, SensorManager.SENSOR_DELAY_UI); //SENSOR_DELAY_FASTEST);
|
||||
this.status = AccelBroker.STARTING;
|
||||
}
|
||||
|
||||
// If error, then set status to error
|
||||
else {
|
||||
this.status = AccelBroker.ERROR_FAILED_TO_START;
|
||||
}
|
||||
|
||||
return this.status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop listening to acceleration sensor.
|
||||
*/
|
||||
public void stop() {
|
||||
if (this.status == AccelBroker.RUNNING) {
|
||||
this.sensorManager.unregisterListener(this); // unregister listener
|
||||
}
|
||||
this.status = AccelBroker.STOPPED;
|
||||
}
|
||||
|
||||
public void onAccuracyChanged(Sensor sensor, int accuracy) {
|
||||
// TODO Auto-generated method stub
|
||||
}
|
||||
|
||||
/**
|
||||
* Sensor listener event.
|
||||
*
|
||||
* @param SensorEvent event
|
||||
*/
|
||||
public void onSensorChanged(SensorEvent event) {
|
||||
// Only look at accelerometer events
|
||||
if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) {
|
||||
return;
|
||||
}
|
||||
this.status = AccelBroker.RUNNING;
|
||||
|
||||
// Save time that event was received
|
||||
this.timeStamp = System.currentTimeMillis();
|
||||
this.x = event.values[0];
|
||||
this.y = event.values[1];
|
||||
this.z = event.values[2];
|
||||
}
|
||||
|
||||
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 = event.values[0];
|
||||
float y = event.values[1];
|
||||
float z = event.values[2];
|
||||
//mAppView.loadUrl("javascript:gotAccel(" + x + ", " + y + "," + z + " )");
|
||||
mAppView.loadUrl("javascript:navigator.accelerometer.gotCurrentAcceleration(" + mKey + "," + x + "," + y + "," + z + ")");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user