Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ef759ca1 authored by Kevin Gabayan's avatar Kevin Gabayan Committed by android-build-merger
Browse files

Merge \"Wakelock timeout for AnyMotionDetector. Bug: 29959125\" into nyc-mr1-dev

am: 33759a99

Change-Id: I9ca9b5835b0fbddca4447fcb8e6fc606880bf69e
parents f7d96e55 33759a99
Loading
Loading
Loading
Loading
+50 −28
Original line number Diff line number Diff line
@@ -70,6 +70,9 @@ public class AnyMotionDetector {
    /** The interval between accelerometer orientation measurements. */
    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
     * too stale to be used.
@@ -141,16 +144,19 @@ public class AnyMotionDetector {
                mCurrentGravityVector = null;
                mPreviousGravityVector = null;
                mWakeLock.acquire();
                Message wakelockTimeoutMsg = Message.obtain(mHandler, mWakelockTimeout);
                mHandler.sendMessageDelayed(wakelockTimeoutMsg, WAKELOCK_TIMEOUT_MILLIS);
                startOrientationMeasurementLocked();
            }
        }
    }

    public void stop() {
        if (mState == STATE_ACTIVE) {
        synchronized (mLock) {
            if (mState == STATE_ACTIVE) {
                mState = STATE_INACTIVE;
                if (DEBUG) Slog.d(TAG, "Moved from STATE_ACTIVE to STATE_INACTIVE.");
            }
            if (mMeasurementInProgress) {
                mMeasurementInProgress = false;
                mSensorManager.unregisterListener(mListener);
@@ -159,7 +165,9 @@ public class AnyMotionDetector {
            mHandler.removeCallbacks(mSensorRestart);
            mCurrentGravityVector = null;
            mPreviousGravityVector = null;
            if (mWakeLock.isHeld()) {
                mWakeLock.release();
                mHandler.removeCallbacks(mWakelockTimeout);
            }
        }
    }
@@ -173,9 +181,8 @@ public class AnyMotionDetector {
                mMeasurementInProgress = true;
                mRunningStats.reset();
            }
            Message msg = Message.obtain(mHandler, mMeasurementTimeout);
            msg.setAsynchronous(true);
            mHandler.sendMessageDelayed(msg, ACCELEROMETER_DATA_TIMEOUT_MILLIS);
            Message measurementTimeoutMsg = Message.obtain(mHandler, mMeasurementTimeout);
            mHandler.sendMessageDelayed(measurementTimeoutMsg, ACCELEROMETER_DATA_TIMEOUT_MILLIS);
        }
    }

@@ -186,10 +193,12 @@ public class AnyMotionDetector {
        if (mMeasurementInProgress) {
            mSensorManager.unregisterListener(mListener);
            mHandler.removeCallbacks(mMeasurementTimeout);
            long detectionEndTime = SystemClock.elapsedRealtime();
            mMeasurementInProgress = false;
            mPreviousGravityVector = mCurrentGravityVector;
            mCurrentGravityVector = mRunningStats.getRunningAverage();
            if (mRunningStats.getSampleCount() == 0) {
                Slog.w(TAG, "No accelerometer data acquired for orientation measurement.");
            }
            if (DEBUG) {
                Slog.d(TAG, "mRunningStats = " + mRunningStats.toString());
                String currentGravityVectorString = (mCurrentGravityVector == null) ?
@@ -203,7 +212,10 @@ public class AnyMotionDetector {
            status = getStationaryStatus();
            if (DEBUG) Slog.d(TAG, "getStationaryStatus() returned " + status);
            if (status != RESULT_UNKNOWN) {
                if (mWakeLock.isHeld()) {
                    mWakeLock.release();
                    mHandler.removeCallbacks(mWakelockTimeout);
                }
                if (DEBUG) {
                    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 +
                        " milliseconds.");
                Message msg = Message.obtain(mHandler, mSensorRestart);
                msg.setAsynchronous(true);
                mHandler.sendMessageDelayed(msg, ORIENTATION_MEASUREMENT_INTERVAL_MILLIS);
            }
        }
@@ -271,6 +282,7 @@ public class AnyMotionDetector {
                }
            }
            if (status != RESULT_UNKNOWN) {
                mHandler.removeCallbacks(mWakelockTimeout);
                mCallback.onAnyMotionResult(status);
            }
        }
@@ -300,11 +312,21 @@ public class AnyMotionDetector {
                status = stopOrientationMeasurementLocked();
            }
            if (status != RESULT_UNKNOWN) {
                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.
     */
+3 −4
Original line number Diff line number Diff line
@@ -973,13 +973,12 @@ public class DeviceIdleController extends SystemService
                cancelSensingTimeoutAlarmLocked();
            }
        }
        if (result == AnyMotionDetector.RESULT_MOVED) {
            if (DEBUG) Slog.d(TAG, "RESULT_MOVED received.");
        if ((result == AnyMotionDetector.RESULT_MOVED) ||
            (result == AnyMotionDetector.RESULT_UNKNOWN)) {
            synchronized (this) {
                handleMotionDetectedLocked(mConstants.INACTIVE_TIMEOUT, "sense_motion");
                handleMotionDetectedLocked(mConstants.INACTIVE_TIMEOUT, "non_stationary");
            }
        } else if (result == AnyMotionDetector.RESULT_STATIONARY) {
            if (DEBUG) Slog.d(TAG, "RESULT_STATIONARY received.");
            if (mState == STATE_SENSING) {
                // If we are currently sensing, it is time to move to locating.
                synchronized (this) {