mirror of
https://github.com/apache/cordova-android.git
synced 2026-03-16 00:00:02 +08:00
Accelerometer updates:
- Removed thread delay to get accelerometer values. - Override Activity lifecycle methods to manage JavaScript timers and enable window.onunload to be called. This is needed for accelerometer to shut down correctly.
This commit is contained in:
@@ -1,20 +1,16 @@
|
||||
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().
|
||||
* It's state is controlled by start(id) and stop(id).
|
||||
* 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
|
||||
*
|
||||
* When all listener ids that were started are stopped, the accelerometer listener is stopped.
|
||||
*/
|
||||
public class AccelBroker {
|
||||
|
||||
@@ -27,6 +23,7 @@ public class AccelBroker {
|
||||
private WebView mAppView; // WebView object
|
||||
private Context mCtx; // Activity (DroidGap) object
|
||||
private AccelListener listener; // Accelerator listener
|
||||
private HashMap<String,Integer> listenerIds; // List of listener ids
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
@@ -41,15 +38,21 @@ public class AccelBroker {
|
||||
|
||||
// Create listener
|
||||
listener = new AccelListener(mCtx, mAppView);
|
||||
|
||||
listenerIds = new HashMap<String,Integer>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Start listening to acceleration sensor.
|
||||
*
|
||||
* @return true if started, false if not
|
||||
* @param String id The id of the listener
|
||||
* @return true if started, false if not
|
||||
*/
|
||||
public boolean start()
|
||||
{
|
||||
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();
|
||||
@@ -61,33 +64,31 @@ public class AccelBroker {
|
||||
/**
|
||||
* Stop listening for acceleration sensor.
|
||||
*
|
||||
* @return true if stopped, false if not
|
||||
* @param String id The id of the listener
|
||||
* @return true if stopped, false if not
|
||||
*/
|
||||
public boolean stop()
|
||||
{
|
||||
listener.stop();
|
||||
return (listener.status == AccelBroker.STOPPED);
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* Destroy listener
|
||||
*/
|
||||
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) {
|
||||
}
|
||||
}
|
||||
}
|
||||
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.
|
||||
@@ -98,10 +99,7 @@ public class AccelBroker {
|
||||
* @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 + "}}";
|
||||
@@ -122,34 +120,28 @@ public class AccelBroker {
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.phonegap;
|
||||
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import android.hardware.Sensor;
|
||||
@@ -10,6 +9,10 @@ import android.hardware.SensorManager;
|
||||
import android.content.Context;
|
||||
import android.webkit.WebView;
|
||||
|
||||
/**
|
||||
* This class listens to the accelerometer sensor and stores the latest
|
||||
* acceleration values x,y,z.
|
||||
*/
|
||||
public class AccelListener implements SensorEventListener{
|
||||
|
||||
WebView mAppView; // WebView object
|
||||
@@ -53,7 +56,7 @@ public class AccelListener implements SensorEventListener{
|
||||
// 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.sensorManager.registerListener(this, this.mSensor, SensorManager.SENSOR_DELAY_FASTEST);
|
||||
this.status = AccelBroker.STARTING;
|
||||
}
|
||||
|
||||
@@ -70,10 +73,18 @@ public class AccelListener implements SensorEventListener{
|
||||
*/
|
||||
public void stop() {
|
||||
if (this.status == AccelBroker.RUNNING) {
|
||||
this.sensorManager.unregisterListener(this); // unregister listener
|
||||
this.sensorManager.unregisterListener(this);
|
||||
}
|
||||
this.status = AccelBroker.STOPPED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by AccelBroker when listener is to be shut down.
|
||||
* Stop listener.
|
||||
*/
|
||||
public void destroy() {
|
||||
this.sensorManager.unregisterListener(this);
|
||||
}
|
||||
|
||||
public void onAccuracyChanged(Sensor sensor, int accuracy) {
|
||||
// TODO Auto-generated method stub
|
||||
@@ -85,17 +96,19 @@ public class AccelListener implements SensorEventListener{
|
||||
* @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];
|
||||
|
||||
this.status = AccelBroker.RUNNING;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
43
framework/src/com/phonegap/DroidGap.java
Normal file → Executable file
43
framework/src/com/phonegap/DroidGap.java
Normal file → Executable file
@@ -139,11 +139,54 @@ public class DroidGap extends Activity {
|
||||
}
|
||||
|
||||
@Override
|
||||
/**
|
||||
* Called by the system when the device configuration changes while your activity is running.
|
||||
*
|
||||
* @param Configuration newConfig
|
||||
*/
|
||||
public void onConfigurationChanged(Configuration newConfig) {
|
||||
//don't reload the current page when the orientation is changed
|
||||
super.onConfigurationChanged(newConfig);
|
||||
}
|
||||
|
||||
@Override
|
||||
/**
|
||||
* Called when the system is about to start resuming a previous activity.
|
||||
*/
|
||||
protected void onPause(){
|
||||
super.onPause();
|
||||
|
||||
// Pause JavaScript timers (including setInterval)
|
||||
appView.pauseTimers();
|
||||
}
|
||||
|
||||
@Override
|
||||
/**
|
||||
* Called when the activity will start interacting with the user.
|
||||
*/
|
||||
protected void onResume(){
|
||||
super.onResume();
|
||||
|
||||
// Resume JavaScript timers (including setInterval)
|
||||
appView.resumeTimers();
|
||||
}
|
||||
|
||||
@Override
|
||||
/**
|
||||
* The final call you receive before your activity is destroyed.
|
||||
*/
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
|
||||
// Load blank page so that JavaScript onunload is called
|
||||
appView.loadUrl("about:blank");
|
||||
|
||||
// Clean up objects
|
||||
if (accel != null) {
|
||||
accel.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
private void bindBrowser(WebView appView)
|
||||
{
|
||||
gap = new Device(appView, this);
|
||||
|
||||
Reference in New Issue
Block a user