mirror of
https://github.com/apache/cordova-android.git
synced 2026-04-23 00:00:09 +08:00
Change compass listener and optimize accelerometer listener.
This commit is contained in:
@@ -1,147 +0,0 @@
|
||||
package com.phonegap;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
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(id) and stop(id).
|
||||
* JavaScript is responsible for starting, stopping, and retrieving status and values.
|
||||
* When all listener ids that were started are stopped, the accelerometer listener is stopped.
|
||||
*/
|
||||
public class AccelBroker {
|
||||
|
||||
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;
|
||||
|
||||
private WebView mAppView; // WebView object
|
||||
private DroidGap mCtx; // DroidGap object
|
||||
private AccelListener listener; // Accelerator listener
|
||||
private HashMap<String,Integer> listenerIds; // List of listener ids
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param view
|
||||
* @param ctx
|
||||
*/
|
||||
public AccelBroker(WebView view, DroidGap ctx)
|
||||
{
|
||||
mCtx = ctx;
|
||||
mAppView = view;
|
||||
|
||||
// Create listener
|
||||
listener = new AccelListener(mCtx, mAppView);
|
||||
|
||||
listenerIds = new HashMap<String,Integer>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Start listening to acceleration sensor.
|
||||
*
|
||||
* @param String id The id of the listener
|
||||
* @return true if started, false if not
|
||||
*/
|
||||
public boolean start(String id)
|
||||
{
|
||||
// Track start for listener
|
||||
listenerIds.put(id, 1);
|
||||
|
||||
// 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.
|
||||
*
|
||||
* @param String id The id of the listener
|
||||
* @return true if stopped, false if not
|
||||
*/
|
||||
public boolean stop(String id)
|
||||
{
|
||||
// Stop tracking listener
|
||||
if (listenerIds.containsKey(id)) {
|
||||
listenerIds.remove(id);
|
||||
}
|
||||
|
||||
// If no more listeners, then stop accelerometer
|
||||
if (listenerIds.isEmpty()) {
|
||||
listener.stop();
|
||||
}
|
||||
|
||||
return (listener.status == AccelBroker.STOPPED);
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy listener
|
||||
*/
|
||||
public void destroy() {
|
||||
listener.destroy();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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() {
|
||||
|
||||
// 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.
|
||||
*
|
||||
* @return x value
|
||||
*/
|
||||
public float getX() {
|
||||
return listener.x;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Y value of last accelerometer value.
|
||||
*
|
||||
* @return y value
|
||||
*/
|
||||
public float getY() {
|
||||
return listener.y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Z value of last accelerometer value.
|
||||
*
|
||||
* @return z value
|
||||
*/
|
||||
public float getZ() {
|
||||
return listener.x;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -15,12 +15,20 @@ import android.webkit.WebView;
|
||||
*/
|
||||
public class AccelListener implements SensorEventListener{
|
||||
|
||||
public static int STOPPED = 0;
|
||||
public static int STARTING = 1;
|
||||
public static int RUNNING = 2;
|
||||
public static int ERROR_FAILED_TO_START = 3;
|
||||
|
||||
public float TIMEOUT = 30000; // Timeout in msec to shut off listener
|
||||
|
||||
WebView mAppView; // WebView object
|
||||
DroidGap mCtx; // DroidGap object
|
||||
|
||||
float x,y,z; // most recent acceleration values
|
||||
long timeStamp; // time of most recent value
|
||||
int status; // status of listener
|
||||
long lastAccessTime; // time the value was last retrieved
|
||||
|
||||
private SensorManager sensorManager;// Sensor manager
|
||||
Sensor mSensor; // Acceleration sensor returned by sensor manager
|
||||
@@ -31,7 +39,7 @@ public class AccelListener implements SensorEventListener{
|
||||
* @param ctx The Activity (DroidGap) object
|
||||
* @param appView
|
||||
*/
|
||||
public AccelListener(DroidGap ctx, WebView appView) {
|
||||
public AccelListener(WebView appView, DroidGap ctx) {
|
||||
this.mCtx = ctx;
|
||||
this.mAppView = appView;
|
||||
this.sensorManager = (SensorManager) mCtx.getSystemService(Context.SENSOR_SERVICE);
|
||||
@@ -39,7 +47,7 @@ public class AccelListener implements SensorEventListener{
|
||||
this.y = 0;
|
||||
this.z = 0;
|
||||
this.timeStamp = 0;
|
||||
this.status = AccelBroker.STOPPED;
|
||||
this.status = AccelListener.STOPPED;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -48,21 +56,26 @@ public class AccelListener implements SensorEventListener{
|
||||
* @return status of listener
|
||||
*/
|
||||
public int start() {
|
||||
|
||||
|
||||
// If already starting or running, then just return
|
||||
if ((this.status == AccelListener.RUNNING) || (this.status == AccelListener.STARTING)) {
|
||||
return this.status;
|
||||
}
|
||||
|
||||
// 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_FASTEST);
|
||||
this.status = AccelBroker.STARTING;
|
||||
this.status = AccelListener.STARTING;
|
||||
this.lastAccessTime = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
// If error, then set status to error
|
||||
else {
|
||||
this.status = AccelBroker.ERROR_FAILED_TO_START;
|
||||
this.status = AccelListener.ERROR_FAILED_TO_START;
|
||||
}
|
||||
|
||||
return this.status;
|
||||
@@ -72,10 +85,10 @@ public class AccelListener implements SensorEventListener{
|
||||
* Stop listening to acceleration sensor.
|
||||
*/
|
||||
public void stop() {
|
||||
if (this.status == AccelBroker.RUNNING) {
|
||||
if (this.status != AccelListener.STOPPED) {
|
||||
this.sensorManager.unregisterListener(this);
|
||||
}
|
||||
this.status = AccelBroker.STOPPED;
|
||||
this.status = AccelListener.STOPPED;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,7 +96,7 @@ public class AccelListener implements SensorEventListener{
|
||||
* Stop listener.
|
||||
*/
|
||||
public void destroy() {
|
||||
this.sensorManager.unregisterListener(this);
|
||||
this.stop();
|
||||
}
|
||||
|
||||
public void onAccuracyChanged(Sensor sensor, int accuracy) {
|
||||
@@ -108,7 +121,69 @@ public class AccelListener implements SensorEventListener{
|
||||
this.y = event.values[1];
|
||||
this.z = event.values[2];
|
||||
|
||||
this.status = AccelBroker.RUNNING;
|
||||
this.status = AccelListener.RUNNING;
|
||||
|
||||
// If values haven't been read for TIMEOUT time, then turn off accelerometer sensor to save power
|
||||
if ((this.timeStamp - this.lastAccessTime) > this.TIMEOUT) {
|
||||
this.stop();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get status of accelerometer sensor.
|
||||
*
|
||||
* @return status
|
||||
*/
|
||||
public int getStatus() {
|
||||
return this.status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get X value of last accelerometer value.
|
||||
*
|
||||
* @return x value
|
||||
*/
|
||||
public float getX() {
|
||||
this.lastAccessTime = System.currentTimeMillis();
|
||||
return this.x;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Y value of last accelerometer value.
|
||||
*
|
||||
* @return y value
|
||||
*/
|
||||
public float getY() {
|
||||
this.lastAccessTime = System.currentTimeMillis();
|
||||
return this.y;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Z value of last accelerometer value.
|
||||
*
|
||||
* @return z value
|
||||
*/
|
||||
public float getZ() {
|
||||
this.lastAccessTime = System.currentTimeMillis();
|
||||
return this.x;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the timeout to turn off accelerometer sensor if getX() hasn't been called.
|
||||
*
|
||||
* @param timeout Timeout in msec.
|
||||
*/
|
||||
public void setTimeout(float timeout) {
|
||||
this.TIMEOUT = timeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the timeout to turn off accelerometer sensor if getX() hasn't been called.
|
||||
*
|
||||
* @return timeout in msec
|
||||
*/
|
||||
public float getTimeout() {
|
||||
return this.TIMEOUT;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -10,8 +10,7 @@ import android.content.Context;
|
||||
import android.webkit.WebView;
|
||||
|
||||
/**
|
||||
* This class listens to the compass sensor and calls navigator.compass.setHeading(heading)
|
||||
* method in JavaScript every sensor change event it receives.
|
||||
* This class listens to the compass sensor and stores the latest heading value.
|
||||
*/
|
||||
public class CompassListener implements SensorEventListener{
|
||||
|
||||
@@ -19,11 +18,16 @@ public class CompassListener implements SensorEventListener{
|
||||
public static int STARTING = 1;
|
||||
public static int RUNNING = 2;
|
||||
public static int ERROR_FAILED_TO_START = 3;
|
||||
|
||||
public float TIMEOUT = 30000; // Timeout in msec to shut off listener
|
||||
|
||||
WebView mAppView; // WebView object
|
||||
DroidGap mCtx; // Activity (DroidGap) object
|
||||
|
||||
int status; // status of listener
|
||||
float heading; // most recent heading value
|
||||
long timeStamp; // time of most recent value
|
||||
long lastAccessTime; // time the value was last retrieved
|
||||
|
||||
private SensorManager sensorManager;// Sensor manager
|
||||
Sensor mSensor; // Compass sensor returned by sensor manager
|
||||
@@ -39,6 +43,8 @@ public class CompassListener implements SensorEventListener{
|
||||
this.mCtx = ctx;
|
||||
this.mAppView = appView;
|
||||
this.sensorManager = (SensorManager) mCtx.getSystemService(Context.SENSOR_SERVICE);
|
||||
this.timeStamp = 0;
|
||||
this.status = CompassListener.STOPPED;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -47,6 +53,11 @@ public class CompassListener implements SensorEventListener{
|
||||
* @return status of listener
|
||||
*/
|
||||
public int start() {
|
||||
|
||||
// If already starting or running, then just return
|
||||
if ((this.status == CompassListener.RUNNING) || (this.status == CompassListener.STARTING)) {
|
||||
return this.status;
|
||||
}
|
||||
|
||||
// Get accelerometer from sensor manager
|
||||
List<Sensor> list = this.sensorManager.getSensorList(Sensor.TYPE_ORIENTATION);
|
||||
@@ -56,6 +67,7 @@ public class CompassListener implements SensorEventListener{
|
||||
this.mSensor = list.get(0);
|
||||
this.sensorManager.registerListener(this, this.mSensor, SensorManager.SENSOR_DELAY_NORMAL);
|
||||
this.status = CompassListener.STARTING;
|
||||
this.lastAccessTime = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
// If error, then set status to error
|
||||
@@ -97,9 +109,51 @@ public class CompassListener implements SensorEventListener{
|
||||
// We only care about the orientation as far as it refers to Magnetic North
|
||||
float heading = event.values[0];
|
||||
|
||||
// Save heading
|
||||
this.timeStamp = System.currentTimeMillis();
|
||||
this.heading = heading;
|
||||
this.status = CompassListener.RUNNING;
|
||||
|
||||
// TODO This is very expensive to process every event. Should this use polling from JS instead?
|
||||
mCtx.sendJavascript("navigator.compass.setHeading(" + heading + ");");
|
||||
|
||||
// If heading hasn't been read for TIMEOUT time, then turn off compass sensor to save power
|
||||
if ((this.timeStamp - this.lastAccessTime) > this.TIMEOUT) {
|
||||
this.stop();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get status of compass sensor.
|
||||
*
|
||||
* @return status
|
||||
*/
|
||||
public int getStatus() {
|
||||
return this.status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the most recent compass heading.
|
||||
*
|
||||
* @return heading
|
||||
*/
|
||||
public float getHeading() {
|
||||
this.lastAccessTime = System.currentTimeMillis();
|
||||
return this.heading;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the timeout to turn off compass sensor if getHeading() hasn't been called.
|
||||
*
|
||||
* @param timeout Timeout in msec.
|
||||
*/
|
||||
public void setTimeout(float timeout) {
|
||||
this.TIMEOUT = timeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the timeout to turn off compass sensor if getHeading() hasn't been called.
|
||||
*
|
||||
* @return timeout in msec
|
||||
*/
|
||||
public float getTimeout() {
|
||||
return this.TIMEOUT;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ public class DroidGap extends Activity {
|
||||
|
||||
private Device gap;
|
||||
private GeoBroker geo;
|
||||
private AccelBroker accel;
|
||||
private AccelListener accel;
|
||||
private CameraLauncher launcher;
|
||||
private ContactManager mContacts;
|
||||
private FileUtils fs;
|
||||
@@ -225,7 +225,7 @@ public class DroidGap extends Activity {
|
||||
{
|
||||
callbackServer = new CallbackServer();
|
||||
gap = new Device(appView, this);
|
||||
accel = new AccelBroker(appView, this);
|
||||
accel = new AccelListener(appView, this);
|
||||
launcher = new CameraLauncher(appView, this);
|
||||
mContacts = new ContactManager(appView, this);
|
||||
fs = new FileUtils(appView);
|
||||
|
||||
Reference in New Issue
Block a user