Merge "Wakelock timeout for AnyMotionDetector. Bug: 29959125" into nyc-mr1-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
33759a993d
@@ -70,6 +70,9 @@ public class AnyMotionDetector {
|
|||||||
/** The interval between accelerometer orientation measurements. */
|
/** The interval between accelerometer orientation measurements. */
|
||||||
private static final long ORIENTATION_MEASUREMENT_INTERVAL_MILLIS = 5000;
|
private static final long ORIENTATION_MEASUREMENT_INTERVAL_MILLIS = 5000;
|
||||||
|
|
||||||
|
/** The maximum duration we will hold a wakelock to determine stationary status. */
|
||||||
|
private static final long WAKELOCK_TIMEOUT_MILLIS = 30000;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The duration in milliseconds after which an orientation measurement is considered
|
* The duration in milliseconds after which an orientation measurement is considered
|
||||||
* too stale to be used.
|
* too stale to be used.
|
||||||
@@ -141,25 +144,30 @@ public class AnyMotionDetector {
|
|||||||
mCurrentGravityVector = null;
|
mCurrentGravityVector = null;
|
||||||
mPreviousGravityVector = null;
|
mPreviousGravityVector = null;
|
||||||
mWakeLock.acquire();
|
mWakeLock.acquire();
|
||||||
|
Message wakelockTimeoutMsg = Message.obtain(mHandler, mWakelockTimeout);
|
||||||
|
mHandler.sendMessageDelayed(wakelockTimeoutMsg, WAKELOCK_TIMEOUT_MILLIS);
|
||||||
startOrientationMeasurementLocked();
|
startOrientationMeasurementLocked();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop() {
|
public void stop() {
|
||||||
if (mState == STATE_ACTIVE) {
|
synchronized (mLock) {
|
||||||
synchronized (mLock) {
|
if (mState == STATE_ACTIVE) {
|
||||||
mState = STATE_INACTIVE;
|
mState = STATE_INACTIVE;
|
||||||
if (DEBUG) Slog.d(TAG, "Moved from STATE_ACTIVE to STATE_INACTIVE.");
|
if (DEBUG) Slog.d(TAG, "Moved from STATE_ACTIVE to STATE_INACTIVE.");
|
||||||
if (mMeasurementInProgress) {
|
}
|
||||||
mMeasurementInProgress = false;
|
if (mMeasurementInProgress) {
|
||||||
mSensorManager.unregisterListener(mListener);
|
mMeasurementInProgress = false;
|
||||||
}
|
mSensorManager.unregisterListener(mListener);
|
||||||
mHandler.removeCallbacks(mMeasurementTimeout);
|
}
|
||||||
mHandler.removeCallbacks(mSensorRestart);
|
mHandler.removeCallbacks(mMeasurementTimeout);
|
||||||
mCurrentGravityVector = null;
|
mHandler.removeCallbacks(mSensorRestart);
|
||||||
mPreviousGravityVector = null;
|
mCurrentGravityVector = null;
|
||||||
|
mPreviousGravityVector = null;
|
||||||
|
if (mWakeLock.isHeld()) {
|
||||||
mWakeLock.release();
|
mWakeLock.release();
|
||||||
|
mHandler.removeCallbacks(mWakelockTimeout);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -173,9 +181,8 @@ public class AnyMotionDetector {
|
|||||||
mMeasurementInProgress = true;
|
mMeasurementInProgress = true;
|
||||||
mRunningStats.reset();
|
mRunningStats.reset();
|
||||||
}
|
}
|
||||||
Message msg = Message.obtain(mHandler, mMeasurementTimeout);
|
Message measurementTimeoutMsg = Message.obtain(mHandler, mMeasurementTimeout);
|
||||||
msg.setAsynchronous(true);
|
mHandler.sendMessageDelayed(measurementTimeoutMsg, ACCELEROMETER_DATA_TIMEOUT_MILLIS);
|
||||||
mHandler.sendMessageDelayed(msg, ACCELEROMETER_DATA_TIMEOUT_MILLIS);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,10 +193,12 @@ public class AnyMotionDetector {
|
|||||||
if (mMeasurementInProgress) {
|
if (mMeasurementInProgress) {
|
||||||
mSensorManager.unregisterListener(mListener);
|
mSensorManager.unregisterListener(mListener);
|
||||||
mHandler.removeCallbacks(mMeasurementTimeout);
|
mHandler.removeCallbacks(mMeasurementTimeout);
|
||||||
long detectionEndTime = SystemClock.elapsedRealtime();
|
|
||||||
mMeasurementInProgress = false;
|
mMeasurementInProgress = false;
|
||||||
mPreviousGravityVector = mCurrentGravityVector;
|
mPreviousGravityVector = mCurrentGravityVector;
|
||||||
mCurrentGravityVector = mRunningStats.getRunningAverage();
|
mCurrentGravityVector = mRunningStats.getRunningAverage();
|
||||||
|
if (mRunningStats.getSampleCount() == 0) {
|
||||||
|
Slog.w(TAG, "No accelerometer data acquired for orientation measurement.");
|
||||||
|
}
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
Slog.d(TAG, "mRunningStats = " + mRunningStats.toString());
|
Slog.d(TAG, "mRunningStats = " + mRunningStats.toString());
|
||||||
String currentGravityVectorString = (mCurrentGravityVector == null) ?
|
String currentGravityVectorString = (mCurrentGravityVector == null) ?
|
||||||
@@ -203,7 +212,10 @@ public class AnyMotionDetector {
|
|||||||
status = getStationaryStatus();
|
status = getStationaryStatus();
|
||||||
if (DEBUG) Slog.d(TAG, "getStationaryStatus() returned " + status);
|
if (DEBUG) Slog.d(TAG, "getStationaryStatus() returned " + status);
|
||||||
if (status != RESULT_UNKNOWN) {
|
if (status != RESULT_UNKNOWN) {
|
||||||
mWakeLock.release();
|
if (mWakeLock.isHeld()) {
|
||||||
|
mWakeLock.release();
|
||||||
|
mHandler.removeCallbacks(mWakelockTimeout);
|
||||||
|
}
|
||||||
if (DEBUG) {
|
if (DEBUG) {
|
||||||
Slog.d(TAG, "Moved from STATE_ACTIVE to STATE_INACTIVE. status = " + status);
|
Slog.d(TAG, "Moved from STATE_ACTIVE to STATE_INACTIVE. status = " + status);
|
||||||
}
|
}
|
||||||
@@ -217,7 +229,6 @@ public class AnyMotionDetector {
|
|||||||
" scheduled in " + ORIENTATION_MEASUREMENT_INTERVAL_MILLIS +
|
" scheduled in " + ORIENTATION_MEASUREMENT_INTERVAL_MILLIS +
|
||||||
" milliseconds.");
|
" milliseconds.");
|
||||||
Message msg = Message.obtain(mHandler, mSensorRestart);
|
Message msg = Message.obtain(mHandler, mSensorRestart);
|
||||||
msg.setAsynchronous(true);
|
|
||||||
mHandler.sendMessageDelayed(msg, ORIENTATION_MEASUREMENT_INTERVAL_MILLIS);
|
mHandler.sendMessageDelayed(msg, ORIENTATION_MEASUREMENT_INTERVAL_MILLIS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -271,6 +282,7 @@ public class AnyMotionDetector {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (status != RESULT_UNKNOWN) {
|
if (status != RESULT_UNKNOWN) {
|
||||||
|
mHandler.removeCallbacks(mWakelockTimeout);
|
||||||
mCallback.onAnyMotionResult(status);
|
mCallback.onAnyMotionResult(status);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -290,20 +302,30 @@ public class AnyMotionDetector {
|
|||||||
};
|
};
|
||||||
|
|
||||||
private final Runnable mMeasurementTimeout = new Runnable() {
|
private final Runnable mMeasurementTimeout = new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
int status = RESULT_UNKNOWN;
|
int status = RESULT_UNKNOWN;
|
||||||
synchronized (mLock) {
|
synchronized (mLock) {
|
||||||
if (DEBUG) Slog.i(TAG, "mMeasurementTimeout. Failed to collect sufficient accel " +
|
if (DEBUG) Slog.i(TAG, "mMeasurementTimeout. Failed to collect sufficient accel " +
|
||||||
"data within " + ACCELEROMETER_DATA_TIMEOUT_MILLIS + " ms. Stopping " +
|
"data within " + ACCELEROMETER_DATA_TIMEOUT_MILLIS + " ms. Stopping " +
|
||||||
"orientation measurement.");
|
"orientation measurement.");
|
||||||
status = stopOrientationMeasurementLocked();
|
status = stopOrientationMeasurementLocked();
|
||||||
}
|
}
|
||||||
if (status != RESULT_UNKNOWN) {
|
if (status != RESULT_UNKNOWN) {
|
||||||
mCallback.onAnyMotionResult(status);
|
mHandler.removeCallbacks(mWakelockTimeout);
|
||||||
}
|
mCallback.onAnyMotionResult(status);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private final Runnable mWakelockTimeout = new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
synchronized (mLock) {
|
||||||
|
stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A timestamped three dimensional vector and some vector operations.
|
* A timestamped three dimensional vector and some vector operations.
|
||||||
|
|||||||
@@ -973,13 +973,12 @@ public class DeviceIdleController extends SystemService
|
|||||||
cancelSensingTimeoutAlarmLocked();
|
cancelSensingTimeoutAlarmLocked();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (result == AnyMotionDetector.RESULT_MOVED) {
|
if ((result == AnyMotionDetector.RESULT_MOVED) ||
|
||||||
if (DEBUG) Slog.d(TAG, "RESULT_MOVED received.");
|
(result == AnyMotionDetector.RESULT_UNKNOWN)) {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
handleMotionDetectedLocked(mConstants.INACTIVE_TIMEOUT, "sense_motion");
|
handleMotionDetectedLocked(mConstants.INACTIVE_TIMEOUT, "non_stationary");
|
||||||
}
|
}
|
||||||
} else if (result == AnyMotionDetector.RESULT_STATIONARY) {
|
} else if (result == AnyMotionDetector.RESULT_STATIONARY) {
|
||||||
if (DEBUG) Slog.d(TAG, "RESULT_STATIONARY received.");
|
|
||||||
if (mState == STATE_SENSING) {
|
if (mState == STATE_SENSING) {
|
||||||
// If we are currently sensing, it is time to move to locating.
|
// If we are currently sensing, it is time to move to locating.
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
|
|||||||
Reference in New Issue
Block a user