Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 19 | import android.hardware.Sensor; |
| 20 | import android.hardware.SensorEvent; |
| 21 | import android.hardware.SensorEventListener; |
| 22 | import android.hardware.SensorManager; |
| 23 | import android.os.Handler; |
| 24 | import android.os.Message; |
| 25 | import android.os.PowerManager; |
| 26 | import android.os.SystemClock; |
| 27 | import android.util.Slog; |
| 28 | |
| 29 | import java.lang.Float; |
| 30 | |
| 31 | /** |
| 32 | * Determines if the device has been set upon a stationary object. |
| 33 | */ |
| 34 | public class AnyMotionDetector { |
| 35 | interface DeviceIdleCallback { |
| 36 | public void onAnyMotionResult(int result); |
| 37 | } |
| 38 | |
| 39 | private static final String TAG = "AnyMotionDetector"; |
| 40 | |
| 41 | private static final boolean DEBUG = false; |
| 42 | |
| 43 | /** Stationary status is unknown due to insufficient orientation measurements. */ |
| 44 | public static final int RESULT_UNKNOWN = -1; |
| 45 | |
| 46 | /** Device is stationary, e.g. still on a table. */ |
| 47 | public static final int RESULT_STATIONARY = 0; |
| 48 | |
| 49 | /** Device has been moved. */ |
| 50 | public static final int RESULT_MOVED = 1; |
| 51 | |
| 52 | /** Orientation measurements are being performed or are planned. */ |
| 53 | private static final int STATE_INACTIVE = 0; |
| 54 | |
| 55 | /** No orientation measurements are being performed or are planned. */ |
| 56 | private static final int STATE_ACTIVE = 1; |
| 57 | |
| 58 | /** Current measurement state. */ |
| 59 | private int mState; |
| 60 | |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 61 | /** Threshold energy above which the device is considered moving. */ |
| 62 | private final float THRESHOLD_ENERGY = 5f; |
| 63 | |
| 64 | /** The duration of the accelerometer orientation measurement. */ |
| 65 | private static final long ORIENTATION_MEASUREMENT_DURATION_MILLIS = 2500; |
| 66 | |
| 67 | /** The maximum duration we will collect accelerometer data. */ |
| 68 | private static final long ACCELEROMETER_DATA_TIMEOUT_MILLIS = 3000; |
| 69 | |
| 70 | /** The interval between accelerometer orientation measurements. */ |
| 71 | private static final long ORIENTATION_MEASUREMENT_INTERVAL_MILLIS = 5000; |
| 72 | |
| 73 | /** |
| 74 | * The duration in milliseconds after which an orientation measurement is considered |
| 75 | * too stale to be used. |
| 76 | */ |
| 77 | private static final int STALE_MEASUREMENT_TIMEOUT_MILLIS = 2 * 60 * 1000; |
| 78 | |
| 79 | /** The accelerometer sampling interval. */ |
| 80 | private static final int SAMPLING_INTERVAL_MILLIS = 40; |
| 81 | |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 82 | private final Handler mHandler; |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 83 | private final Object mLock = new Object(); |
| 84 | private Sensor mAccelSensor; |
| 85 | private SensorManager mSensorManager; |
| 86 | private PowerManager.WakeLock mWakeLock; |
| 87 | |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 88 | /** Threshold angle in degrees beyond which the device is considered moving. */ |
| 89 | private final float mThresholdAngle; |
| 90 | |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 91 | /** The minimum number of samples required to detect AnyMotion. */ |
| 92 | private int mNumSufficientSamples; |
| 93 | |
| 94 | /** True if an orientation measurement is in progress. */ |
| 95 | private boolean mMeasurementInProgress; |
| 96 | |
| 97 | /** The most recent gravity vector. */ |
| 98 | private Vector3 mCurrentGravityVector = null; |
| 99 | |
| 100 | /** The second most recent gravity vector. */ |
| 101 | private Vector3 mPreviousGravityVector = null; |
| 102 | |
| 103 | /** Running sum of squared errors. */ |
| 104 | private RunningSignalStats mRunningStats; |
| 105 | |
| 106 | private DeviceIdleCallback mCallback = null; |
| 107 | |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 108 | public AnyMotionDetector(PowerManager pm, Handler handler, SensorManager sm, |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 109 | DeviceIdleCallback callback, float thresholdAngle) { |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 110 | if (DEBUG) Slog.d(TAG, "AnyMotionDetector instantiated."); |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 111 | mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 112 | mWakeLock.setReferenceCounted(false); |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 113 | mHandler = handler; |
| 114 | mSensorManager = sm; |
| 115 | mAccelSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); |
| 116 | mMeasurementInProgress = false; |
| 117 | mState = STATE_INACTIVE; |
| 118 | mCallback = callback; |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 119 | mThresholdAngle = thresholdAngle; |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 120 | mRunningStats = new RunningSignalStats(); |
| 121 | mNumSufficientSamples = (int) Math.ceil( |
| 122 | ((double)ORIENTATION_MEASUREMENT_DURATION_MILLIS / SAMPLING_INTERVAL_MILLIS)); |
| 123 | if (DEBUG) Slog.d(TAG, "mNumSufficientSamples = " + mNumSufficientSamples); |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * Acquire accel data until we determine AnyMotion status. |
| 128 | */ |
| 129 | public void checkForAnyMotion() { |
| 130 | if (DEBUG) Slog.d(TAG, "checkForAnyMotion(). mState = " + mState); |
| 131 | if (mState != STATE_ACTIVE) { |
| 132 | mState = STATE_ACTIVE; |
| 133 | if (DEBUG) Slog.d(TAG, "Moved from STATE_INACTIVE to STATE_ACTIVE."); |
| 134 | mCurrentGravityVector = null; |
| 135 | mPreviousGravityVector = null; |
| 136 | startOrientationMeasurement(); |
| 137 | } |
| 138 | } |
| 139 | |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 140 | public void stop() { |
| 141 | if (mState == STATE_ACTIVE) { |
| 142 | mState = STATE_INACTIVE; |
| 143 | if (DEBUG) Slog.d(TAG, "Moved from STATE_ACTIVE to STATE_INACTIVE."); |
| 144 | if (mMeasurementInProgress) { |
| 145 | mMeasurementInProgress = false; |
| 146 | mSensorManager.unregisterListener(mListener); |
| 147 | } |
| 148 | mHandler.removeCallbacks(mMeasurementTimeout); |
| 149 | mHandler.removeCallbacks(mSensorRestart); |
| 150 | mWakeLock.release(); |
| 151 | mCurrentGravityVector = null; |
| 152 | mPreviousGravityVector = null; |
| 153 | } |
| 154 | } |
| 155 | |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 156 | private void startOrientationMeasurement() { |
| 157 | if (DEBUG) Slog.d(TAG, "startOrientationMeasurement: mMeasurementInProgress=" + |
| 158 | mMeasurementInProgress + ", (mAccelSensor != null)=" + (mAccelSensor != null)); |
| 159 | |
| 160 | if (!mMeasurementInProgress && mAccelSensor != null) { |
| 161 | if (mSensorManager.registerListener(mListener, mAccelSensor, |
| 162 | SAMPLING_INTERVAL_MILLIS * 1000)) { |
| 163 | mWakeLock.acquire(); |
| 164 | mMeasurementInProgress = true; |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 165 | mRunningStats.reset(); |
| 166 | } |
| 167 | |
| 168 | Message msg = Message.obtain(mHandler, mMeasurementTimeout); |
| 169 | msg.setAsynchronous(true); |
| 170 | mHandler.sendMessageDelayed(msg, ACCELEROMETER_DATA_TIMEOUT_MILLIS); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | private int stopOrientationMeasurementLocked() { |
| 175 | if (DEBUG) Slog.d(TAG, "stopOrientationMeasurement. mMeasurementInProgress=" + |
| 176 | mMeasurementInProgress); |
| 177 | int status = RESULT_UNKNOWN; |
| 178 | if (mMeasurementInProgress) { |
| 179 | mSensorManager.unregisterListener(mListener); |
| 180 | mHandler.removeCallbacks(mMeasurementTimeout); |
Dianne Hackborn | 42df4fb | 2015-08-14 16:43:14 -0700 | [diff] [blame] | 181 | mWakeLock.release(); |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 182 | long detectionEndTime = SystemClock.elapsedRealtime(); |
| 183 | mMeasurementInProgress = false; |
| 184 | mPreviousGravityVector = mCurrentGravityVector; |
| 185 | mCurrentGravityVector = mRunningStats.getRunningAverage(); |
| 186 | if (DEBUG) { |
| 187 | Slog.d(TAG, "mRunningStats = " + mRunningStats.toString()); |
| 188 | String currentGravityVectorString = (mCurrentGravityVector == null) ? |
| 189 | "null" : mCurrentGravityVector.toString(); |
| 190 | String previousGravityVectorString = (mPreviousGravityVector == null) ? |
| 191 | "null" : mPreviousGravityVector.toString(); |
| 192 | Slog.d(TAG, "mCurrentGravityVector = " + currentGravityVectorString); |
| 193 | Slog.d(TAG, "mPreviousGravityVector = " + previousGravityVectorString); |
| 194 | } |
| 195 | mRunningStats.reset(); |
| 196 | status = getStationaryStatus(); |
| 197 | if (DEBUG) Slog.d(TAG, "getStationaryStatus() returned " + status); |
| 198 | if (status != RESULT_UNKNOWN) { |
| 199 | if (DEBUG) Slog.d(TAG, "Moved from STATE_ACTIVE to STATE_INACTIVE. status = " + |
| 200 | status); |
| 201 | mState = STATE_INACTIVE; |
| 202 | } else { |
| 203 | /* |
| 204 | * Unknown due to insufficient measurements. Schedule another orientation |
| 205 | * measurement. |
| 206 | */ |
| 207 | if (DEBUG) Slog.d(TAG, "stopOrientationMeasurementLocked(): another measurement" + |
| 208 | " scheduled in " + ORIENTATION_MEASUREMENT_INTERVAL_MILLIS + |
| 209 | " milliseconds."); |
| 210 | Message msg = Message.obtain(mHandler, mSensorRestart); |
| 211 | msg.setAsynchronous(true); |
| 212 | mHandler.sendMessageDelayed(msg, ORIENTATION_MEASUREMENT_INTERVAL_MILLIS); |
| 213 | } |
| 214 | } |
| 215 | return status; |
| 216 | } |
| 217 | |
| 218 | /* |
| 219 | * Updates mStatus to the current AnyMotion status. |
| 220 | */ |
| 221 | public int getStationaryStatus() { |
| 222 | if ((mPreviousGravityVector == null) || (mCurrentGravityVector == null)) { |
| 223 | return RESULT_UNKNOWN; |
| 224 | } |
| 225 | Vector3 previousGravityVectorNormalized = mPreviousGravityVector.normalized(); |
| 226 | Vector3 currentGravityVectorNormalized = mCurrentGravityVector.normalized(); |
| 227 | float angle = previousGravityVectorNormalized.angleBetween(currentGravityVectorNormalized); |
Joe LaPenna | 23d681b | 2015-08-27 15:12:11 -0700 | [diff] [blame] | 228 | if (DEBUG) Slog.d(TAG, "getStationaryStatus: angle = " + angle |
| 229 | + " energy = " + mRunningStats.getEnergy()); |
| 230 | if ((angle < mThresholdAngle) && (mRunningStats.getEnergy() < THRESHOLD_ENERGY)) { |
Kevin Gabayan | 89ecf82 | 2015-05-18 12:10:07 -0700 | [diff] [blame] | 231 | return RESULT_STATIONARY; |
| 232 | } else if (Float.isNaN(angle)) { |
| 233 | /** |
| 234 | * Floating point rounding errors have caused the angle calcuation's dot product to |
| 235 | * exceed 1.0. In such case, we report RESULT_MOVED to prevent devices from rapidly |
| 236 | * retrying this measurement. |
| 237 | */ |
| 238 | return RESULT_MOVED; |
| 239 | } |
| 240 | long diffTime = mCurrentGravityVector.timeMillisSinceBoot - |
| 241 | mPreviousGravityVector.timeMillisSinceBoot; |
| 242 | if (diffTime > STALE_MEASUREMENT_TIMEOUT_MILLIS) { |
| 243 | if (DEBUG) Slog.d(TAG, "getStationaryStatus: mPreviousGravityVector is too stale at " + |
| 244 | diffTime + " ms ago. Returning RESULT_UNKNOWN."); |
| 245 | return RESULT_UNKNOWN; |
| 246 | } |
| 247 | return RESULT_MOVED; |
| 248 | } |
| 249 | |
| 250 | private final SensorEventListener mListener = new SensorEventListener() { |
| 251 | @Override |
| 252 | public void onSensorChanged(SensorEvent event) { |
| 253 | int status = RESULT_UNKNOWN; |
| 254 | synchronized (mLock) { |
| 255 | Vector3 accelDatum = new Vector3(SystemClock.elapsedRealtime(), event.values[0], |
| 256 | event.values[1], event.values[2]); |
| 257 | mRunningStats.accumulate(accelDatum); |
| 258 | |
| 259 | // If we have enough samples, stop accelerometer data acquisition. |
| 260 | if (mRunningStats.getSampleCount() >= mNumSufficientSamples) { |
| 261 | status = stopOrientationMeasurementLocked(); |
| 262 | } |
| 263 | } |
| 264 | if (status != RESULT_UNKNOWN) { |
| 265 | mCallback.onAnyMotionResult(status); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public void onAccuracyChanged(Sensor sensor, int accuracy) { |
| 271 | } |
| 272 | }; |
| 273 | |
| 274 | private final Runnable mSensorRestart = new Runnable() { |
| 275 | @Override |
| 276 | public void run() { |
| 277 | synchronized (mLock) { |
| 278 | startOrientationMeasurement(); |
| 279 | } |
| 280 | } |
| 281 | }; |
| 282 | |
| 283 | private final Runnable mMeasurementTimeout = new Runnable() { |
| 284 | @Override |
| 285 | public void run() { |
| 286 | int status = RESULT_UNKNOWN; |
| 287 | synchronized (mLock) { |
| 288 | if (DEBUG) Slog.i(TAG, "mMeasurementTimeout. Failed to collect sufficient accel " + |
| 289 | "data within " + ACCELEROMETER_DATA_TIMEOUT_MILLIS + " ms. Stopping " + |
| 290 | "orientation measurement."); |
| 291 | status = stopOrientationMeasurementLocked(); |
| 292 | } |
| 293 | if (status != RESULT_UNKNOWN) { |
| 294 | mCallback.onAnyMotionResult(status); |
| 295 | } |
| 296 | } |
| 297 | }; |
| 298 | |
| 299 | /** |
| 300 | * A timestamped three dimensional vector and some vector operations. |
| 301 | */ |
| 302 | private static class Vector3 { |
| 303 | public long timeMillisSinceBoot; |
| 304 | public float x; |
| 305 | public float y; |
| 306 | public float z; |
| 307 | |
| 308 | public Vector3(long timeMillisSinceBoot, float x, float y, float z) { |
| 309 | this.timeMillisSinceBoot = timeMillisSinceBoot; |
| 310 | this.x = x; |
| 311 | this.y = y; |
| 312 | this.z = z; |
| 313 | } |
| 314 | |
| 315 | private float norm() { |
| 316 | return (float) Math.sqrt(dotProduct(this)); |
| 317 | } |
| 318 | |
| 319 | private Vector3 normalized() { |
| 320 | float mag = norm(); |
| 321 | return new Vector3(timeMillisSinceBoot, x / mag, y / mag, z / mag); |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Returns the angle between this 3D vector and another given 3D vector. |
| 326 | * Assumes both have already been normalized. |
| 327 | * |
| 328 | * @param other The other Vector3 vector. |
| 329 | * @return angle between this vector and the other given one. |
| 330 | */ |
| 331 | public float angleBetween(Vector3 other) { |
| 332 | double degrees = Math.toDegrees(Math.acos(this.dotProduct(other))); |
| 333 | float returnValue = (float) degrees; |
| 334 | Slog.d(TAG, "angleBetween: this = " + this.toString() + |
| 335 | ", other = " + other.toString()); |
| 336 | Slog.d(TAG, " degrees = " + degrees + ", returnValue = " + returnValue); |
| 337 | return returnValue; |
| 338 | } |
| 339 | |
| 340 | @Override |
| 341 | public String toString() { |
| 342 | String msg = ""; |
| 343 | msg += "timeMillisSinceBoot=" + timeMillisSinceBoot; |
| 344 | msg += " | x=" + x; |
| 345 | msg += ", y=" + y; |
| 346 | msg += ", z=" + z; |
| 347 | return msg; |
| 348 | } |
| 349 | |
| 350 | public float dotProduct(Vector3 v) { |
| 351 | return x * v.x + y * v.y + z * v.z; |
| 352 | } |
| 353 | |
| 354 | public Vector3 times(float val) { |
| 355 | return new Vector3(timeMillisSinceBoot, x * val, y * val, z * val); |
| 356 | } |
| 357 | |
| 358 | public Vector3 plus(Vector3 v) { |
| 359 | return new Vector3(v.timeMillisSinceBoot, x + v.x, y + v.y, z + v.z); |
| 360 | } |
| 361 | |
| 362 | public Vector3 minus(Vector3 v) { |
| 363 | return new Vector3(v.timeMillisSinceBoot, x - v.x, y - v.y, z - v.z); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Maintains running statistics on the signal revelant to AnyMotion detection, including: |
| 369 | * <ul> |
| 370 | * <li>running average. |
| 371 | * <li>running sum-of-squared-errors as the energy of the signal derivative. |
| 372 | * <ul> |
| 373 | */ |
| 374 | private static class RunningSignalStats { |
| 375 | Vector3 previousVector; |
| 376 | Vector3 currentVector; |
| 377 | Vector3 runningSum; |
| 378 | float energy; |
| 379 | int sampleCount; |
| 380 | |
| 381 | public RunningSignalStats() { |
| 382 | reset(); |
| 383 | } |
| 384 | |
| 385 | public void reset() { |
| 386 | previousVector = null; |
| 387 | currentVector = null; |
| 388 | runningSum = new Vector3(0, 0, 0, 0); |
| 389 | energy = 0; |
| 390 | sampleCount = 0; |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Apply a 3D vector v as the next element in the running SSE. |
| 395 | */ |
| 396 | public void accumulate(Vector3 v) { |
| 397 | if (v == null) { |
| 398 | if (DEBUG) Slog.i(TAG, "Cannot accumulate a null vector."); |
| 399 | return; |
| 400 | } |
| 401 | sampleCount++; |
| 402 | runningSum = runningSum.plus(v); |
| 403 | previousVector = currentVector; |
| 404 | currentVector = v; |
| 405 | if (previousVector != null) { |
| 406 | Vector3 dv = currentVector.minus(previousVector); |
| 407 | float incrementalEnergy = dv.x * dv.x + dv.y * dv.y + dv.z * dv.z; |
| 408 | energy += incrementalEnergy; |
| 409 | if (DEBUG) Slog.i(TAG, "Accumulated vector " + currentVector.toString() + |
| 410 | ", runningSum = " + runningSum.toString() + |
| 411 | ", incrementalEnergy = " + incrementalEnergy + |
| 412 | ", energy = " + energy); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | public Vector3 getRunningAverage() { |
| 417 | if (sampleCount > 0) { |
| 418 | return runningSum.times((float)(1.0f / sampleCount)); |
| 419 | } |
| 420 | return null; |
| 421 | } |
| 422 | |
| 423 | public float getEnergy() { |
| 424 | return energy; |
| 425 | } |
| 426 | |
| 427 | public int getSampleCount() { |
| 428 | return sampleCount; |
| 429 | } |
| 430 | |
| 431 | @Override |
| 432 | public String toString() { |
| 433 | String msg = ""; |
| 434 | String currentVectorString = (currentVector == null) ? |
| 435 | "null" : currentVector.toString(); |
| 436 | String previousVectorString = (previousVector == null) ? |
| 437 | "null" : previousVector.toString(); |
| 438 | msg += "previousVector = " + previousVectorString; |
| 439 | msg += ", currentVector = " + currentVectorString; |
| 440 | msg += ", sampleCount = " + sampleCount; |
| 441 | msg += ", energy = " + energy; |
| 442 | return msg; |
| 443 | } |
| 444 | } |
| 445 | } |