The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Jorim Jaggi | b10e33f | 2015-02-04 21:57:40 +0100 | [diff] [blame] | 17 | package com.android.server.policy; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
| 20 | import android.hardware.Sensor; |
| 21 | import android.hardware.SensorEvent; |
| 22 | import android.hardware.SensorEventListener; |
| 23 | import android.hardware.SensorManager; |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 24 | import android.os.Handler; |
Adrian Roos | 3595be4 | 2015-03-05 16:31:15 +0100 | [diff] [blame] | 25 | import android.os.SystemClock; |
Jeff Brown | daf5d89 | 2012-05-07 18:30:18 -0700 | [diff] [blame] | 26 | import android.os.SystemProperties; |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 27 | import android.text.TextUtils; |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 28 | import android.util.Slog; |
Jeff Brown | 600f003 | 2014-05-22 17:06:00 -0700 | [diff] [blame] | 29 | |
| 30 | import java.io.PrintWriter; |
Jeff Brown | e97c9a2 | 2015-06-10 22:12:54 -0700 | [diff] [blame] | 31 | import java.util.Arrays; |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 32 | import java.util.List; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * A special helper class used by the WindowManager |
Jeff Brown | c0347aa | 2011-09-23 17:26:09 -0700 | [diff] [blame] | 36 | * for receiving notifications from the SensorManager when |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | * the orientation of the device has changed. |
Dianne Hackborn | e5439f2 | 2010-10-02 16:53:50 -0700 | [diff] [blame] | 38 | * |
| 39 | * NOTE: If changing anything here, please run the API demo |
| 40 | * "App/Activity/Screen Orientation" to ensure that all orientation |
| 41 | * modes still work correctly. |
| 42 | * |
Jeff Brown | daf5d89 | 2012-05-07 18:30:18 -0700 | [diff] [blame] | 43 | * You can also visualize the behavior of the WindowOrientationListener. |
| 44 | * Refer to frameworks/base/tools/orientationplot/README.txt for details. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | */ |
| 46 | public abstract class WindowOrientationListener { |
| 47 | private static final String TAG = "WindowOrientationListener"; |
Jeff Brown | daf5d89 | 2012-05-07 18:30:18 -0700 | [diff] [blame] | 48 | private static final boolean LOG = SystemProperties.getBoolean( |
| 49 | "debug.orientation.log", false); |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 50 | |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 51 | private static final boolean USE_GRAVITY_SENSOR = false; |
| 52 | |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 53 | private Handler mHandler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | private SensorManager mSensorManager; |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 55 | private boolean mEnabled; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | private int mRate; |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 57 | private String mSensorType; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | private Sensor mSensor; |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 59 | private OrientationJudge mOrientationJudge; |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 60 | private int mCurrentRotation = -1; |
| 61 | |
| 62 | private final Object mLock = new Object(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * Creates a new WindowOrientationListener. |
| 66 | * |
| 67 | * @param context for the WindowOrientationListener. |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 68 | * @param handler Provides the Looper for receiving sensor updates. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | */ |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 70 | public WindowOrientationListener(Context context, Handler handler) { |
| 71 | this(context, handler, SensorManager.SENSOR_DELAY_UI); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | } |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 73 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | /** |
| 75 | * Creates a new WindowOrientationListener. |
| 76 | * |
| 77 | * @param context for the WindowOrientationListener. |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 78 | * @param handler Provides the Looper for receiving sensor updates. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | * @param rate at which sensor events are processed (see also |
| 80 | * {@link android.hardware.SensorManager SensorManager}). Use the default |
| 81 | * value of {@link android.hardware.SensorManager#SENSOR_DELAY_NORMAL |
| 82 | * SENSOR_DELAY_NORMAL} for simple screen orientation change detection. |
Steve Howard | 1ba101f | 2010-02-23 14:30:13 -0800 | [diff] [blame] | 83 | * |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 84 | * This constructor is private since no one uses it. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | */ |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 86 | private WindowOrientationListener(Context context, Handler handler, int rate) { |
| 87 | mHandler = handler; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE); |
| 89 | mRate = rate; |
Trevor Bunker | 8e4591c | 2016-01-15 09:00:55 -0800 | [diff] [blame] | 90 | mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_DEVICE_ORIENTATION); |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 91 | |
Trevor Bunker | 8e4591c | 2016-01-15 09:00:55 -0800 | [diff] [blame] | 92 | if (mSensor != null) { |
| 93 | mOrientationJudge = new OrientationSensorJudge(); |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | if (mOrientationJudge == null) { |
| 97 | mSensor = mSensorManager.getDefaultSensor(USE_GRAVITY_SENSOR |
| 98 | ? Sensor.TYPE_GRAVITY : Sensor.TYPE_ACCELEROMETER); |
| 99 | if (mSensor != null) { |
| 100 | // Create listener only if sensors do exist |
| 101 | mOrientationJudge = new AccelSensorJudge(context); |
| 102 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Enables the WindowOrientationListener so it will monitor the sensor and call |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 108 | * {@link #onProposedRotationChanged(int)} when the device orientation changes. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | */ |
| 110 | public void enable() { |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 111 | synchronized (mLock) { |
| 112 | if (mSensor == null) { |
Jeff Brown | e97c9a2 | 2015-06-10 22:12:54 -0700 | [diff] [blame] | 113 | Slog.w(TAG, "Cannot detect sensors. Not enabled"); |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 114 | return; |
Jeff Brown | daf5d89 | 2012-05-07 18:30:18 -0700 | [diff] [blame] | 115 | } |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 116 | if (mEnabled == false) { |
| 117 | if (LOG) { |
Jeff Brown | e97c9a2 | 2015-06-10 22:12:54 -0700 | [diff] [blame] | 118 | Slog.d(TAG, "WindowOrientationListener enabled"); |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 119 | } |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 120 | mOrientationJudge.resetLocked(); |
| 121 | mSensorManager.registerListener(mOrientationJudge, mSensor, mRate, mHandler); |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 122 | mEnabled = true; |
| 123 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Disables the WindowOrientationListener. |
| 129 | */ |
| 130 | public void disable() { |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 131 | synchronized (mLock) { |
| 132 | if (mSensor == null) { |
Jeff Brown | e97c9a2 | 2015-06-10 22:12:54 -0700 | [diff] [blame] | 133 | Slog.w(TAG, "Cannot detect sensors. Invalid disable"); |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 134 | return; |
Jeff Brown | daf5d89 | 2012-05-07 18:30:18 -0700 | [diff] [blame] | 135 | } |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 136 | if (mEnabled == true) { |
| 137 | if (LOG) { |
Jeff Brown | e97c9a2 | 2015-06-10 22:12:54 -0700 | [diff] [blame] | 138 | Slog.d(TAG, "WindowOrientationListener disabled"); |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 139 | } |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 140 | mSensorManager.unregisterListener(mOrientationJudge); |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 141 | mEnabled = false; |
| 142 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
Adrian Roos | 3595be4 | 2015-03-05 16:31:15 +0100 | [diff] [blame] | 146 | public void onTouchStart() { |
| 147 | synchronized (mLock) { |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 148 | if (mOrientationJudge != null) { |
| 149 | mOrientationJudge.onTouchStartLocked(); |
Adrian Roos | 1274196 | 2015-03-31 11:20:30 -0700 | [diff] [blame] | 150 | } |
Adrian Roos | 3595be4 | 2015-03-05 16:31:15 +0100 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | public void onTouchEnd() { |
| 155 | long whenElapsedNanos = SystemClock.elapsedRealtimeNanos(); |
| 156 | |
| 157 | synchronized (mLock) { |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 158 | if (mOrientationJudge != null) { |
| 159 | mOrientationJudge.onTouchEndLocked(whenElapsedNanos); |
Adrian Roos | 1274196 | 2015-03-31 11:20:30 -0700 | [diff] [blame] | 160 | } |
Adrian Roos | 3595be4 | 2015-03-05 16:31:15 +0100 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 164 | /** |
Jeff Brown | c0347aa | 2011-09-23 17:26:09 -0700 | [diff] [blame] | 165 | * Sets the current rotation. |
| 166 | * |
| 167 | * @param rotation The current rotation. |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 168 | */ |
Jeff Brown | c0347aa | 2011-09-23 17:26:09 -0700 | [diff] [blame] | 169 | public void setCurrentRotation(int rotation) { |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 170 | synchronized (mLock) { |
| 171 | mCurrentRotation = rotation; |
| 172 | } |
Jeff Brown | c0347aa | 2011-09-23 17:26:09 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Gets the proposed rotation. |
| 177 | * |
| 178 | * This method only returns a rotation if the orientation listener is certain |
| 179 | * of its proposal. If the rotation is indeterminate, returns -1. |
| 180 | * |
| 181 | * @return The proposed rotation, or -1 if unknown. |
| 182 | */ |
| 183 | public int getProposedRotation() { |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 184 | synchronized (mLock) { |
| 185 | if (mEnabled) { |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 186 | return mOrientationJudge.getProposedRotationLocked(); |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 187 | } |
| 188 | return -1; |
Suchi Amalapurapu | 63104ed | 2009-12-15 14:06:08 -0800 | [diff] [blame] | 189 | } |
Dianne Hackborn | e4fbd62 | 2009-03-27 18:09:16 -0700 | [diff] [blame] | 190 | } |
Steve Howard | 5f531ae | 2010-08-05 17:14:53 -0700 | [diff] [blame] | 191 | |
| 192 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | * Returns true if sensor is enabled and false otherwise |
| 194 | */ |
| 195 | public boolean canDetectOrientation() { |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 196 | synchronized (mLock) { |
| 197 | return mSensor != null; |
| 198 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | } |
Steve Howard | 1ba101f | 2010-02-23 14:30:13 -0800 | [diff] [blame] | 200 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | /** |
The Android Open Source Project | ba87e3e | 2009-03-13 13:04:22 -0700 | [diff] [blame] | 202 | * Called when the rotation view of the device has changed. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | * |
Jeff Brown | c0347aa | 2011-09-23 17:26:09 -0700 | [diff] [blame] | 204 | * This method is called whenever the orientation becomes certain of an orientation. |
| 205 | * It is called each time the orientation determination transitions from being |
| 206 | * uncertain to being certain again, even if it is the same orientation as before. |
| 207 | * |
Michael Wright | 74f6c79 | 2015-08-25 21:15:12 +0100 | [diff] [blame] | 208 | * This should only be called on the Handler thread. |
| 209 | * |
Steve Howard | 1ba101f | 2010-02-23 14:30:13 -0800 | [diff] [blame] | 210 | * @param rotation The new orientation of the device, one of the Surface.ROTATION_* constants. |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 211 | * @see android.view.Surface |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | */ |
Jeff Brown | c0347aa | 2011-09-23 17:26:09 -0700 | [diff] [blame] | 213 | public abstract void onProposedRotationChanged(int rotation); |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 214 | |
Jeff Brown | 600f003 | 2014-05-22 17:06:00 -0700 | [diff] [blame] | 215 | public void dump(PrintWriter pw, String prefix) { |
| 216 | synchronized (mLock) { |
| 217 | pw.println(prefix + TAG); |
| 218 | prefix += " "; |
| 219 | pw.println(prefix + "mEnabled=" + mEnabled); |
| 220 | pw.println(prefix + "mCurrentRotation=" + mCurrentRotation); |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 221 | pw.println(prefix + "mSensorType=" + mSensorType); |
Jeff Brown | 600f003 | 2014-05-22 17:06:00 -0700 | [diff] [blame] | 222 | pw.println(prefix + "mSensor=" + mSensor); |
| 223 | pw.println(prefix + "mRate=" + mRate); |
| 224 | |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 225 | if (mOrientationJudge != null) { |
| 226 | mOrientationJudge.dumpLocked(pw, prefix); |
Jaewan Kim | 0abc28e | 2014-06-26 09:13:11 +0900 | [diff] [blame] | 227 | } |
Jeff Brown | 600f003 | 2014-05-22 17:06:00 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 231 | abstract class OrientationJudge implements SensorEventListener { |
| 232 | // Number of nanoseconds per millisecond. |
| 233 | protected static final long NANOS_PER_MS = 1000000; |
| 234 | |
| 235 | // Number of milliseconds per nano second. |
| 236 | protected static final float MILLIS_PER_NANO = 0.000001f; |
| 237 | |
| 238 | // The minimum amount of time that must have elapsed since the screen was last touched |
| 239 | // before the proposed rotation can change. |
| 240 | protected static final long PROPOSAL_MIN_TIME_SINCE_TOUCH_END_NANOS = |
| 241 | 500 * NANOS_PER_MS; |
| 242 | |
| 243 | /** |
| 244 | * Gets the proposed rotation. |
| 245 | * |
| 246 | * This method only returns a rotation if the orientation listener is certain |
| 247 | * of its proposal. If the rotation is indeterminate, returns -1. |
| 248 | * |
| 249 | * Should only be called when holding WindowOrientationListener lock. |
| 250 | * |
| 251 | * @return The proposed rotation, or -1 if unknown. |
| 252 | */ |
| 253 | public abstract int getProposedRotationLocked(); |
| 254 | |
| 255 | /** |
| 256 | * Notifies the orientation judge that the screen is being touched. |
| 257 | * |
| 258 | * Should only be called when holding WindowOrientationListener lock. |
| 259 | */ |
| 260 | public abstract void onTouchStartLocked(); |
| 261 | |
| 262 | /** |
| 263 | * Notifies the orientation judge that the screen is no longer being touched. |
| 264 | * |
| 265 | * Should only be called when holding WindowOrientationListener lock. |
| 266 | * |
| 267 | * @param whenElapsedNanos Given in the elapsed realtime nanos time base. |
| 268 | */ |
| 269 | public abstract void onTouchEndLocked(long whenElapsedNanos); |
| 270 | |
| 271 | /** |
| 272 | * Resets the state of the judge. |
| 273 | * |
| 274 | * Should only be called when holding WindowOrientationListener lock. |
| 275 | */ |
| 276 | public abstract void resetLocked(); |
| 277 | |
| 278 | /** |
| 279 | * Dumps internal state of the orientation judge. |
| 280 | * |
| 281 | * Should only be called when holding WindowOrientationListener lock. |
| 282 | */ |
| 283 | public abstract void dumpLocked(PrintWriter pw, String prefix); |
| 284 | |
| 285 | @Override |
| 286 | public abstract void onAccuracyChanged(Sensor sensor, int accuracy); |
| 287 | |
| 288 | @Override |
| 289 | public abstract void onSensorChanged(SensorEvent event); |
| 290 | } |
| 291 | |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 292 | /** |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 293 | * This class filters the raw accelerometer data and tries to detect actual changes in |
| 294 | * orientation. This is a very ill-defined problem so there are a lot of tweakable parameters, |
| 295 | * but here's the outline: |
| 296 | * |
| 297 | * - Low-pass filter the accelerometer vector in cartesian coordinates. We do it in |
| 298 | * cartesian space because the orientation calculations are sensitive to the |
| 299 | * absolute magnitude of the acceleration. In particular, there are singularities |
| 300 | * in the calculation as the magnitude approaches 0. By performing the low-pass |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 301 | * filtering early, we can eliminate most spurious high-frequency impulses due to noise. |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 302 | * |
| 303 | * - Convert the acceleromter vector from cartesian to spherical coordinates. |
| 304 | * Since we're dealing with rotation of the device, this is the sensible coordinate |
| 305 | * system to work in. The zenith direction is the Z-axis, the direction the screen |
| 306 | * is facing. The radial distance is referred to as the magnitude below. |
| 307 | * The elevation angle is referred to as the "tilt" below. |
| 308 | * The azimuth angle is referred to as the "orientation" below (and the azimuth axis is |
| 309 | * the Y-axis). |
| 310 | * See http://en.wikipedia.org/wiki/Spherical_coordinate_system for reference. |
| 311 | * |
| 312 | * - If the tilt angle is too close to horizontal (near 90 or -90 degrees), do nothing. |
| 313 | * The orientation angle is not meaningful when the device is nearly horizontal. |
| 314 | * The tilt angle thresholds are set differently for each orientation and different |
| 315 | * limits are applied when the device is facing down as opposed to when it is facing |
| 316 | * forward or facing up. |
| 317 | * |
| 318 | * - When the orientation angle reaches a certain threshold, consider transitioning |
| 319 | * to the corresponding orientation. These thresholds have some hysteresis built-in |
| 320 | * to avoid oscillations between adjacent orientations. |
| 321 | * |
Jeff Brown | c0347aa | 2011-09-23 17:26:09 -0700 | [diff] [blame] | 322 | * - Wait for the device to settle for a little bit. Once that happens, issue the |
| 323 | * new orientation proposal. |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 324 | * |
| 325 | * Details are explained inline. |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 326 | * |
| 327 | * See http://en.wikipedia.org/wiki/Low-pass_filter#Discrete-time_realization for |
| 328 | * signal processing background. |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 329 | */ |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 330 | final class AccelSensorJudge extends OrientationJudge { |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 331 | // We work with all angles in degrees in this class. |
| 332 | private static final float RADIANS_TO_DEGREES = (float) (180 / Math.PI); |
| 333 | |
| 334 | // Indices into SensorEvent.values for the accelerometer sensor. |
| 335 | private static final int ACCELEROMETER_DATA_X = 0; |
| 336 | private static final int ACCELEROMETER_DATA_Y = 1; |
| 337 | private static final int ACCELEROMETER_DATA_Z = 2; |
| 338 | |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 339 | // The minimum amount of time that a predicted rotation must be stable before it |
| 340 | // is accepted as a valid rotation proposal. This value can be quite small because |
| 341 | // the low-pass filter already suppresses most of the noise so we're really just |
| 342 | // looking for quick confirmation that the last few samples are in agreement as to |
| 343 | // the desired orientation. |
| 344 | private static final long PROPOSAL_SETTLE_TIME_NANOS = 40 * NANOS_PER_MS; |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 345 | |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 346 | // The minimum amount of time that must have elapsed since the device last exited |
| 347 | // the flat state (time since it was picked up) before the proposed rotation |
| 348 | // can change. |
| 349 | private static final long PROPOSAL_MIN_TIME_SINCE_FLAT_ENDED_NANOS = 500 * NANOS_PER_MS; |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 350 | |
Jeff Brown | daf5d89 | 2012-05-07 18:30:18 -0700 | [diff] [blame] | 351 | // The minimum amount of time that must have elapsed since the device stopped |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 352 | // swinging (time since device appeared to be in the process of being put down |
| 353 | // or put away into a pocket) before the proposed rotation can change. |
| 354 | private static final long PROPOSAL_MIN_TIME_SINCE_SWING_ENDED_NANOS = 300 * NANOS_PER_MS; |
Jeff Brown | c0347aa | 2011-09-23 17:26:09 -0700 | [diff] [blame] | 355 | |
Jeff Brown | daf5d89 | 2012-05-07 18:30:18 -0700 | [diff] [blame] | 356 | // The minimum amount of time that must have elapsed since the device stopped |
| 357 | // undergoing external acceleration before the proposed rotation can change. |
| 358 | private static final long PROPOSAL_MIN_TIME_SINCE_ACCELERATION_ENDED_NANOS = |
| 359 | 500 * NANOS_PER_MS; |
| 360 | |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 361 | // If the tilt angle remains greater than the specified angle for a minimum of |
| 362 | // the specified time, then the device is deemed to be lying flat |
| 363 | // (just chillin' on a table). |
Jeff Brown | e97c9a2 | 2015-06-10 22:12:54 -0700 | [diff] [blame] | 364 | private static final float FLAT_ANGLE = 80; |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 365 | private static final long FLAT_TIME_NANOS = 1000 * NANOS_PER_MS; |
| 366 | |
| 367 | // If the tilt angle has increased by at least delta degrees within the specified amount |
| 368 | // of time, then the device is deemed to be swinging away from the user |
| 369 | // down towards flat (tilt = 90). |
| 370 | private static final float SWING_AWAY_ANGLE_DELTA = 20; |
| 371 | private static final long SWING_TIME_NANOS = 300 * NANOS_PER_MS; |
Jeff Brown | c0347aa | 2011-09-23 17:26:09 -0700 | [diff] [blame] | 372 | |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 373 | // The maximum sample inter-arrival time in milliseconds. |
| 374 | // If the acceleration samples are further apart than this amount in time, we reset the |
| 375 | // state of the low-pass filter and orientation properties. This helps to handle |
| 376 | // boundary conditions when the device is turned on, wakes from suspend or there is |
| 377 | // a significant gap in samples. |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 378 | private static final long MAX_FILTER_DELTA_TIME_NANOS = 1000 * NANOS_PER_MS; |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 379 | |
Jeff Brown | c0347aa | 2011-09-23 17:26:09 -0700 | [diff] [blame] | 380 | // The acceleration filter time constant. |
| 381 | // |
| 382 | // This time constant is used to tune the acceleration filter such that |
| 383 | // impulses and vibrational noise (think car dock) is suppressed before we |
| 384 | // try to calculate the tilt and orientation angles. |
| 385 | // |
| 386 | // The filter time constant is related to the filter cutoff frequency, which is the |
| 387 | // frequency at which signals are attenuated by 3dB (half the passband power). |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 388 | // Each successive octave beyond this frequency is attenuated by an additional 6dB. |
| 389 | // |
Jeff Brown | c0347aa | 2011-09-23 17:26:09 -0700 | [diff] [blame] | 390 | // Given a time constant t in seconds, the filter cutoff frequency Fc in Hertz |
| 391 | // is given by Fc = 1 / (2pi * t). |
| 392 | // |
| 393 | // The higher the time constant, the lower the cutoff frequency, so more noise |
| 394 | // will be suppressed. |
| 395 | // |
| 396 | // Filtering adds latency proportional the time constant (inversely proportional |
| 397 | // to the cutoff frequency) so we don't want to make the time constant too |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 398 | // large or we can lose responsiveness. Likewise we don't want to make it too |
| 399 | // small or we do a poor job suppressing acceleration spikes. |
| 400 | // Empirically, 100ms seems to be too small and 500ms is too large. |
| 401 | private static final float FILTER_TIME_CONSTANT_MS = 200.0f; |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 402 | |
| 403 | /* State for orientation detection. */ |
| 404 | |
| 405 | // Thresholds for minimum and maximum allowable deviation from gravity. |
| 406 | // |
| 407 | // If the device is undergoing external acceleration (being bumped, in a car |
| 408 | // that is turning around a corner or a plane taking off) then the magnitude |
| 409 | // may be substantially more or less than gravity. This can skew our orientation |
| 410 | // detection by making us think that up is pointed in a different direction. |
| 411 | // |
| 412 | // Conversely, if the device is in freefall, then there will be no gravity to |
| 413 | // measure at all. This is problematic because we cannot detect the orientation |
| 414 | // without gravity to tell us which way is up. A magnitude near 0 produces |
| 415 | // singularities in the tilt and orientation calculations. |
| 416 | // |
| 417 | // In both cases, we postpone choosing an orientation. |
Jeff Brown | daf5d89 | 2012-05-07 18:30:18 -0700 | [diff] [blame] | 418 | // |
| 419 | // However, we need to tolerate some acceleration because the angular momentum |
| 420 | // of turning the device can skew the observed acceleration for a short period of time. |
| 421 | private static final float NEAR_ZERO_MAGNITUDE = 1; // m/s^2 |
| 422 | private static final float ACCELERATION_TOLERANCE = 4; // m/s^2 |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 423 | private static final float MIN_ACCELERATION_MAGNITUDE = |
Jeff Brown | daf5d89 | 2012-05-07 18:30:18 -0700 | [diff] [blame] | 424 | SensorManager.STANDARD_GRAVITY - ACCELERATION_TOLERANCE; |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 425 | private static final float MAX_ACCELERATION_MAGNITUDE = |
Jeff Brown | daf5d89 | 2012-05-07 18:30:18 -0700 | [diff] [blame] | 426 | SensorManager.STANDARD_GRAVITY + ACCELERATION_TOLERANCE; |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 427 | |
| 428 | // Maximum absolute tilt angle at which to consider orientation data. Beyond this (i.e. |
Jeff Brown | e97c9a2 | 2015-06-10 22:12:54 -0700 | [diff] [blame] | 429 | // when screen is facing the sky or ground), we completely ignore orientation data |
| 430 | // because it's too unstable. |
| 431 | private static final int MAX_TILT = 80; |
| 432 | |
| 433 | // The tilt angle below which we conclude that the user is holding the device |
| 434 | // overhead reading in bed and lock into that state. |
| 435 | private static final int TILT_OVERHEAD_ENTER = -40; |
| 436 | |
| 437 | // The tilt angle above which we conclude that the user would like a rotation |
| 438 | // change to occur and unlock from the overhead state. |
| 439 | private static final int TILT_OVERHEAD_EXIT = -15; |
| 440 | |
| 441 | // The gap angle in degrees between adjacent orientation angles for hysteresis. |
| 442 | // This creates a "dead zone" between the current orientation and a proposed |
| 443 | // adjacent orientation. No orientation proposal is made when the orientation |
| 444 | // angle is within the gap between the current orientation and the adjacent |
| 445 | // orientation. |
| 446 | private static final int ADJACENT_ORIENTATION_ANGLE_GAP = 45; |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 447 | |
| 448 | // The tilt angle range in degrees for each orientation. |
| 449 | // Beyond these tilt angles, we don't even consider transitioning into the |
| 450 | // specified orientation. We place more stringent requirements on unnatural |
| 451 | // orientations than natural ones to make it less likely to accidentally transition |
| 452 | // into those states. |
| 453 | // The first value of each pair is negative so it applies a limit when the device is |
| 454 | // facing down (overhead reading in bed). |
| 455 | // The second value of each pair is positive so it applies a limit when the device is |
| 456 | // facing up (resting on a table). |
| 457 | // The ideal tilt angle is 0 (when the device is vertical) so the limits establish |
| 458 | // how close to vertical the device must be in order to change orientation. |
Jeff Brown | e97c9a2 | 2015-06-10 22:12:54 -0700 | [diff] [blame] | 459 | private final int[][] mTiltToleranceConfig = new int[][] { |
| 460 | /* ROTATION_0 */ { -25, 70 }, // note: these are overridden by config.xml |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 461 | /* ROTATION_90 */ { -25, 65 }, |
| 462 | /* ROTATION_180 */ { -25, 60 }, |
| 463 | /* ROTATION_270 */ { -25, 65 } |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 464 | }; |
| 465 | |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 466 | // Timestamp and value of the last accelerometer sample. |
| 467 | private long mLastFilteredTimestampNanos; |
| 468 | private float mLastFilteredX, mLastFilteredY, mLastFilteredZ; |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 469 | |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 470 | // The last proposed rotation, -1 if unknown. |
| 471 | private int mProposedRotation; |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 472 | |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 473 | // Value of the current predicted rotation, -1 if unknown. |
| 474 | private int mPredictedRotation; |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 475 | |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 476 | // Timestamp of when the predicted rotation most recently changed. |
| 477 | private long mPredictedRotationTimestampNanos; |
| 478 | |
| 479 | // Timestamp when the device last appeared to be flat for sure (the flat delay elapsed). |
| 480 | private long mFlatTimestampNanos; |
Jeff Brown | 600f003 | 2014-05-22 17:06:00 -0700 | [diff] [blame] | 481 | private boolean mFlat; |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 482 | |
| 483 | // Timestamp when the device last appeared to be swinging. |
| 484 | private long mSwingTimestampNanos; |
Jeff Brown | 600f003 | 2014-05-22 17:06:00 -0700 | [diff] [blame] | 485 | private boolean mSwinging; |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 486 | |
Jeff Brown | daf5d89 | 2012-05-07 18:30:18 -0700 | [diff] [blame] | 487 | // Timestamp when the device last appeared to be undergoing external acceleration. |
| 488 | private long mAccelerationTimestampNanos; |
Jeff Brown | 600f003 | 2014-05-22 17:06:00 -0700 | [diff] [blame] | 489 | private boolean mAccelerating; |
| 490 | |
Adrian Roos | 3595be4 | 2015-03-05 16:31:15 +0100 | [diff] [blame] | 491 | // Timestamp when the last touch to the touch screen ended |
| 492 | private long mTouchEndedTimestampNanos = Long.MIN_VALUE; |
| 493 | private boolean mTouched; |
| 494 | |
Jeff Brown | 600f003 | 2014-05-22 17:06:00 -0700 | [diff] [blame] | 495 | // Whether we are locked into an overhead usage mode. |
| 496 | private boolean mOverhead; |
Jeff Brown | daf5d89 | 2012-05-07 18:30:18 -0700 | [diff] [blame] | 497 | |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 498 | // History of observed tilt angles. |
Jeff Brown | e97c9a2 | 2015-06-10 22:12:54 -0700 | [diff] [blame] | 499 | private static final int TILT_HISTORY_SIZE = 200; |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 500 | private float[] mTiltHistory = new float[TILT_HISTORY_SIZE]; |
| 501 | private long[] mTiltHistoryTimestampNanos = new long[TILT_HISTORY_SIZE]; |
| 502 | private int mTiltHistoryIndex; |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 503 | |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 504 | public AccelSensorJudge(Context context) { |
Jeff Brown | e97c9a2 | 2015-06-10 22:12:54 -0700 | [diff] [blame] | 505 | // Load tilt tolerance configuration. |
| 506 | int[] tiltTolerance = context.getResources().getIntArray( |
| 507 | com.android.internal.R.array.config_autoRotationTiltTolerance); |
| 508 | if (tiltTolerance.length == 8) { |
| 509 | for (int i = 0; i < 4; i++) { |
| 510 | int min = tiltTolerance[i * 2]; |
| 511 | int max = tiltTolerance[i * 2 + 1]; |
| 512 | if (min >= -90 && min <= max && max <= 90) { |
| 513 | mTiltToleranceConfig[i][0] = min; |
| 514 | mTiltToleranceConfig[i][1] = max; |
| 515 | } else { |
| 516 | Slog.wtf(TAG, "config_autoRotationTiltTolerance contains invalid range: " |
| 517 | + "min=" + min + ", max=" + max); |
| 518 | } |
| 519 | } |
| 520 | } else { |
| 521 | Slog.wtf(TAG, "config_autoRotationTiltTolerance should have exactly 8 elements"); |
| 522 | } |
| 523 | } |
| 524 | |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 525 | @Override |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 526 | public int getProposedRotationLocked() { |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 527 | return mProposedRotation; |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 528 | } |
| 529 | |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 530 | @Override |
Jeff Brown | 600f003 | 2014-05-22 17:06:00 -0700 | [diff] [blame] | 531 | public void dumpLocked(PrintWriter pw, String prefix) { |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 532 | pw.println(prefix + "AccelSensorJudge"); |
| 533 | prefix += " "; |
Jeff Brown | 600f003 | 2014-05-22 17:06:00 -0700 | [diff] [blame] | 534 | pw.println(prefix + "mProposedRotation=" + mProposedRotation); |
| 535 | pw.println(prefix + "mPredictedRotation=" + mPredictedRotation); |
| 536 | pw.println(prefix + "mLastFilteredX=" + mLastFilteredX); |
| 537 | pw.println(prefix + "mLastFilteredY=" + mLastFilteredY); |
| 538 | pw.println(prefix + "mLastFilteredZ=" + mLastFilteredZ); |
Michael Wright | 6eabf57 | 2015-08-12 15:35:31 +0100 | [diff] [blame] | 539 | final long delta = SystemClock.elapsedRealtimeNanos() - mLastFilteredTimestampNanos; |
| 540 | pw.println(prefix + "mLastFilteredTimestampNanos=" + mLastFilteredTimestampNanos |
| 541 | + " (" + (delta * 0.000001f) + "ms ago)"); |
Jeff Brown | 600f003 | 2014-05-22 17:06:00 -0700 | [diff] [blame] | 542 | pw.println(prefix + "mTiltHistory={last: " + getLastTiltLocked() + "}"); |
| 543 | pw.println(prefix + "mFlat=" + mFlat); |
| 544 | pw.println(prefix + "mSwinging=" + mSwinging); |
| 545 | pw.println(prefix + "mAccelerating=" + mAccelerating); |
| 546 | pw.println(prefix + "mOverhead=" + mOverhead); |
Adrian Roos | 3595be4 | 2015-03-05 16:31:15 +0100 | [diff] [blame] | 547 | pw.println(prefix + "mTouched=" + mTouched); |
Jeff Brown | e97c9a2 | 2015-06-10 22:12:54 -0700 | [diff] [blame] | 548 | pw.print(prefix + "mTiltToleranceConfig=["); |
| 549 | for (int i = 0; i < 4; i++) { |
| 550 | if (i != 0) { |
| 551 | pw.print(", "); |
| 552 | } |
| 553 | pw.print("["); |
| 554 | pw.print(mTiltToleranceConfig[i][0]); |
| 555 | pw.print(", "); |
| 556 | pw.print(mTiltToleranceConfig[i][1]); |
| 557 | pw.print("]"); |
| 558 | } |
| 559 | pw.println("]"); |
Jeff Brown | 600f003 | 2014-05-22 17:06:00 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 562 | @Override |
| 563 | public void onAccuracyChanged(Sensor sensor, int accuracy) { |
| 564 | } |
| 565 | |
| 566 | @Override |
| 567 | public void onSensorChanged(SensorEvent event) { |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 568 | int proposedRotation; |
| 569 | int oldProposedRotation; |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 570 | |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 571 | synchronized (mLock) { |
| 572 | // The vector given in the SensorEvent points straight up (towards the sky) under |
| 573 | // ideal conditions (the phone is not accelerating). I'll call this up vector |
| 574 | // elsewhere. |
| 575 | float x = event.values[ACCELEROMETER_DATA_X]; |
| 576 | float y = event.values[ACCELEROMETER_DATA_Y]; |
| 577 | float z = event.values[ACCELEROMETER_DATA_Z]; |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 578 | |
Jeff Brown | daf5d89 | 2012-05-07 18:30:18 -0700 | [diff] [blame] | 579 | if (LOG) { |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 580 | Slog.v(TAG, "Raw acceleration vector: " |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 581 | + "x=" + x + ", y=" + y + ", z=" + z |
Neil Fuller | 33253a4 | 2014-10-01 11:55:10 +0100 | [diff] [blame] | 582 | + ", magnitude=" + Math.sqrt(x * x + y * y + z * z)); |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 583 | } |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 584 | |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 585 | // Apply a low-pass filter to the acceleration up vector in cartesian space. |
| 586 | // Reset the orientation listener state if the samples are too far apart in time |
| 587 | // or when we see values of (0, 0, 0) which indicates that we polled the |
| 588 | // accelerometer too soon after turning it on and we don't have any data yet. |
| 589 | final long now = event.timestamp; |
| 590 | final long then = mLastFilteredTimestampNanos; |
| 591 | final float timeDeltaMS = (now - then) * 0.000001f; |
| 592 | final boolean skipSample; |
| 593 | if (now < then |
| 594 | || now > then + MAX_FILTER_DELTA_TIME_NANOS |
| 595 | || (x == 0 && y == 0 && z == 0)) { |
Jeff Brown | daf5d89 | 2012-05-07 18:30:18 -0700 | [diff] [blame] | 596 | if (LOG) { |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 597 | Slog.v(TAG, "Resetting orientation listener."); |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 598 | } |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 599 | resetLocked(); |
| 600 | skipSample = true; |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 601 | } else { |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 602 | final float alpha = timeDeltaMS / (FILTER_TIME_CONSTANT_MS + timeDeltaMS); |
| 603 | x = alpha * (x - mLastFilteredX) + mLastFilteredX; |
| 604 | y = alpha * (y - mLastFilteredY) + mLastFilteredY; |
| 605 | z = alpha * (z - mLastFilteredZ) + mLastFilteredZ; |
| 606 | if (LOG) { |
| 607 | Slog.v(TAG, "Filtered acceleration vector: " |
| 608 | + "x=" + x + ", y=" + y + ", z=" + z |
Neil Fuller | 33253a4 | 2014-10-01 11:55:10 +0100 | [diff] [blame] | 609 | + ", magnitude=" + Math.sqrt(x * x + y * y + z * z)); |
Jeff Brown | daf5d89 | 2012-05-07 18:30:18 -0700 | [diff] [blame] | 610 | } |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 611 | skipSample = false; |
| 612 | } |
| 613 | mLastFilteredTimestampNanos = now; |
| 614 | mLastFilteredX = x; |
| 615 | mLastFilteredY = y; |
| 616 | mLastFilteredZ = z; |
Jeff Brown | daf5d89 | 2012-05-07 18:30:18 -0700 | [diff] [blame] | 617 | |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 618 | boolean isAccelerating = false; |
| 619 | boolean isFlat = false; |
| 620 | boolean isSwinging = false; |
| 621 | if (!skipSample) { |
| 622 | // Calculate the magnitude of the acceleration vector. |
Neil Fuller | 33253a4 | 2014-10-01 11:55:10 +0100 | [diff] [blame] | 623 | final float magnitude = (float) Math.sqrt(x * x + y * y + z * z); |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 624 | if (magnitude < NEAR_ZERO_MAGNITUDE) { |
Jeff Brown | daf5d89 | 2012-05-07 18:30:18 -0700 | [diff] [blame] | 625 | if (LOG) { |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 626 | Slog.v(TAG, "Ignoring sensor data, magnitude too close to zero."); |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 627 | } |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 628 | clearPredictedRotationLocked(); |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 629 | } else { |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 630 | // Determine whether the device appears to be undergoing external |
| 631 | // acceleration. |
| 632 | if (isAcceleratingLocked(magnitude)) { |
| 633 | isAccelerating = true; |
| 634 | mAccelerationTimestampNanos = now; |
Jeff Brown | c0347aa | 2011-09-23 17:26:09 -0700 | [diff] [blame] | 635 | } |
| 636 | |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 637 | // Calculate the tilt angle. |
| 638 | // This is the angle between the up vector and the x-y plane (the plane of |
| 639 | // the screen) in a range of [-90, 90] degrees. |
| 640 | // -90 degrees: screen horizontal and facing the ground (overhead) |
| 641 | // 0 degrees: screen vertical |
| 642 | // 90 degrees: screen horizontal and facing the sky (on table) |
| 643 | final int tiltAngle = (int) Math.round( |
| 644 | Math.asin(z / magnitude) * RADIANS_TO_DEGREES); |
| 645 | addTiltHistoryEntryLocked(now, tiltAngle); |
| 646 | |
| 647 | // Determine whether the device appears to be flat or swinging. |
| 648 | if (isFlatLocked(now)) { |
| 649 | isFlat = true; |
| 650 | mFlatTimestampNanos = now; |
| 651 | } |
| 652 | if (isSwingingLocked(now, tiltAngle)) { |
| 653 | isSwinging = true; |
| 654 | mSwingTimestampNanos = now; |
Jeff Brown | c0347aa | 2011-09-23 17:26:09 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 657 | // If the tilt angle is too close to horizontal then we cannot determine |
| 658 | // the orientation angle of the screen. |
Jeff Brown | 600f003 | 2014-05-22 17:06:00 -0700 | [diff] [blame] | 659 | if (tiltAngle <= TILT_OVERHEAD_ENTER) { |
| 660 | mOverhead = true; |
| 661 | } else if (tiltAngle >= TILT_OVERHEAD_EXIT) { |
| 662 | mOverhead = false; |
| 663 | } |
| 664 | if (mOverhead) { |
| 665 | if (LOG) { |
| 666 | Slog.v(TAG, "Ignoring sensor data, device is overhead: " |
| 667 | + "tiltAngle=" + tiltAngle); |
| 668 | } |
| 669 | clearPredictedRotationLocked(); |
| 670 | } else if (Math.abs(tiltAngle) > MAX_TILT) { |
Jeff Brown | daf5d89 | 2012-05-07 18:30:18 -0700 | [diff] [blame] | 671 | if (LOG) { |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 672 | Slog.v(TAG, "Ignoring sensor data, tilt angle too high: " |
| 673 | + "tiltAngle=" + tiltAngle); |
Jeff Brown | c0347aa | 2011-09-23 17:26:09 -0700 | [diff] [blame] | 674 | } |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 675 | clearPredictedRotationLocked(); |
Jeff Brown | c0347aa | 2011-09-23 17:26:09 -0700 | [diff] [blame] | 676 | } else { |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 677 | // Calculate the orientation angle. |
| 678 | // This is the angle between the x-y projection of the up vector onto |
| 679 | // the +y-axis, increasing clockwise in a range of [0, 360] degrees. |
| 680 | int orientationAngle = (int) Math.round( |
| 681 | -Math.atan2(-x, y) * RADIANS_TO_DEGREES); |
| 682 | if (orientationAngle < 0) { |
| 683 | // atan2 returns [-180, 180]; normalize to [0, 360] |
| 684 | orientationAngle += 360; |
Jeff Brown | c0347aa | 2011-09-23 17:26:09 -0700 | [diff] [blame] | 685 | } |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 686 | |
| 687 | // Find the nearest rotation. |
| 688 | int nearestRotation = (orientationAngle + 45) / 90; |
| 689 | if (nearestRotation == 4) { |
| 690 | nearestRotation = 0; |
| 691 | } |
| 692 | |
| 693 | // Determine the predicted orientation. |
| 694 | if (isTiltAngleAcceptableLocked(nearestRotation, tiltAngle) |
| 695 | && isOrientationAngleAcceptableLocked(nearestRotation, |
| 696 | orientationAngle)) { |
| 697 | updatePredictedRotationLocked(now, nearestRotation); |
| 698 | if (LOG) { |
| 699 | Slog.v(TAG, "Predicted: " |
| 700 | + "tiltAngle=" + tiltAngle |
| 701 | + ", orientationAngle=" + orientationAngle |
| 702 | + ", predictedRotation=" + mPredictedRotation |
| 703 | + ", predictedRotationAgeMS=" |
| 704 | + ((now - mPredictedRotationTimestampNanos) |
| 705 | * 0.000001f)); |
| 706 | } |
| 707 | } else { |
| 708 | if (LOG) { |
| 709 | Slog.v(TAG, "Ignoring sensor data, no predicted rotation: " |
| 710 | + "tiltAngle=" + tiltAngle |
| 711 | + ", orientationAngle=" + orientationAngle); |
| 712 | } |
| 713 | clearPredictedRotationLocked(); |
| 714 | } |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 715 | } |
| 716 | } |
| 717 | } |
Jeff Brown | 600f003 | 2014-05-22 17:06:00 -0700 | [diff] [blame] | 718 | mFlat = isFlat; |
| 719 | mSwinging = isSwinging; |
| 720 | mAccelerating = isAccelerating; |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 721 | |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 722 | // Determine new proposed rotation. |
| 723 | oldProposedRotation = mProposedRotation; |
| 724 | if (mPredictedRotation < 0 || isPredictedRotationAcceptableLocked(now)) { |
| 725 | mProposedRotation = mPredictedRotation; |
| 726 | } |
| 727 | proposedRotation = mProposedRotation; |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 728 | |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 729 | // Write final statistics about where we are in the orientation detection process. |
| 730 | if (LOG) { |
| 731 | Slog.v(TAG, "Result: currentRotation=" + mCurrentRotation |
| 732 | + ", proposedRotation=" + proposedRotation |
| 733 | + ", predictedRotation=" + mPredictedRotation |
| 734 | + ", timeDeltaMS=" + timeDeltaMS |
| 735 | + ", isAccelerating=" + isAccelerating |
| 736 | + ", isFlat=" + isFlat |
| 737 | + ", isSwinging=" + isSwinging |
Jeff Brown | 600f003 | 2014-05-22 17:06:00 -0700 | [diff] [blame] | 738 | + ", isOverhead=" + mOverhead |
Adrian Roos | 3595be4 | 2015-03-05 16:31:15 +0100 | [diff] [blame] | 739 | + ", isTouched=" + mTouched |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 740 | + ", timeUntilSettledMS=" + remainingMS(now, |
| 741 | mPredictedRotationTimestampNanos + PROPOSAL_SETTLE_TIME_NANOS) |
| 742 | + ", timeUntilAccelerationDelayExpiredMS=" + remainingMS(now, |
| 743 | mAccelerationTimestampNanos + PROPOSAL_MIN_TIME_SINCE_ACCELERATION_ENDED_NANOS) |
| 744 | + ", timeUntilFlatDelayExpiredMS=" + remainingMS(now, |
| 745 | mFlatTimestampNanos + PROPOSAL_MIN_TIME_SINCE_FLAT_ENDED_NANOS) |
| 746 | + ", timeUntilSwingDelayExpiredMS=" + remainingMS(now, |
Adrian Roos | 3595be4 | 2015-03-05 16:31:15 +0100 | [diff] [blame] | 747 | mSwingTimestampNanos + PROPOSAL_MIN_TIME_SINCE_SWING_ENDED_NANOS) |
| 748 | + ", timeUntilTouchDelayExpiredMS=" + remainingMS(now, |
| 749 | mTouchEndedTimestampNanos + PROPOSAL_MIN_TIME_SINCE_TOUCH_END_NANOS)); |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 750 | } |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | // Tell the listener. |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 754 | if (proposedRotation != oldProposedRotation && proposedRotation >= 0) { |
Jeff Brown | daf5d89 | 2012-05-07 18:30:18 -0700 | [diff] [blame] | 755 | if (LOG) { |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 756 | Slog.v(TAG, "Proposed rotation changed! proposedRotation=" + proposedRotation |
Jeff Brown | c0347aa | 2011-09-23 17:26:09 -0700 | [diff] [blame] | 757 | + ", oldProposedRotation=" + oldProposedRotation); |
| 758 | } |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 759 | onProposedRotationChanged(proposedRotation); |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 763 | @Override |
| 764 | public void onTouchStartLocked() { |
| 765 | mTouched = true; |
| 766 | } |
| 767 | |
| 768 | @Override |
| 769 | public void onTouchEndLocked(long whenElapsedNanos) { |
| 770 | mTouched = false; |
| 771 | mTouchEndedTimestampNanos = whenElapsedNanos; |
| 772 | } |
| 773 | |
| 774 | @Override |
| 775 | public void resetLocked() { |
| 776 | mLastFilteredTimestampNanos = Long.MIN_VALUE; |
| 777 | mProposedRotation = -1; |
| 778 | mFlatTimestampNanos = Long.MIN_VALUE; |
| 779 | mFlat = false; |
| 780 | mSwingTimestampNanos = Long.MIN_VALUE; |
| 781 | mSwinging = false; |
| 782 | mAccelerationTimestampNanos = Long.MIN_VALUE; |
| 783 | mAccelerating = false; |
| 784 | mOverhead = false; |
| 785 | clearPredictedRotationLocked(); |
| 786 | clearTiltHistoryLocked(); |
| 787 | } |
| 788 | |
| 789 | |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 790 | /** |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 791 | * Returns true if the tilt angle is acceptable for a given predicted rotation. |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 792 | */ |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 793 | private boolean isTiltAngleAcceptableLocked(int rotation, int tiltAngle) { |
Jeff Brown | e97c9a2 | 2015-06-10 22:12:54 -0700 | [diff] [blame] | 794 | return tiltAngle >= mTiltToleranceConfig[rotation][0] |
| 795 | && tiltAngle <= mTiltToleranceConfig[rotation][1]; |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | /** |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 799 | * Returns true if the orientation angle is acceptable for a given predicted rotation. |
Jeff Brown | c0347aa | 2011-09-23 17:26:09 -0700 | [diff] [blame] | 800 | * |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 801 | * This function takes into account the gap between adjacent orientations |
| 802 | * for hysteresis. |
| 803 | */ |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 804 | private boolean isOrientationAngleAcceptableLocked(int rotation, int orientationAngle) { |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 805 | // If there is no current rotation, then there is no gap. |
Jeff Brown | c0347aa | 2011-09-23 17:26:09 -0700 | [diff] [blame] | 806 | // The gap is used only to introduce hysteresis among advertised orientation |
| 807 | // changes to avoid flapping. |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 808 | final int currentRotation = mCurrentRotation; |
Jeff Brown | c0347aa | 2011-09-23 17:26:09 -0700 | [diff] [blame] | 809 | if (currentRotation >= 0) { |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 810 | // If the specified rotation is the same or is counter-clockwise adjacent |
| 811 | // to the current rotation, then we set a lower bound on the orientation angle. |
Jeff Brown | c0347aa | 2011-09-23 17:26:09 -0700 | [diff] [blame] | 812 | // For example, if currentRotation is ROTATION_0 and proposed is ROTATION_90, |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 813 | // then we want to check orientationAngle > 45 + GAP / 2. |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 814 | if (rotation == currentRotation |
| 815 | || rotation == (currentRotation + 1) % 4) { |
| 816 | int lowerBound = rotation * 90 - 45 |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 817 | + ADJACENT_ORIENTATION_ANGLE_GAP / 2; |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 818 | if (rotation == 0) { |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 819 | if (orientationAngle >= 315 && orientationAngle < lowerBound + 360) { |
| 820 | return false; |
| 821 | } |
| 822 | } else { |
| 823 | if (orientationAngle < lowerBound) { |
| 824 | return false; |
| 825 | } |
| 826 | } |
| 827 | } |
| 828 | |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 829 | // If the specified rotation is the same or is clockwise adjacent, |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 830 | // then we set an upper bound on the orientation angle. |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 831 | // For example, if currentRotation is ROTATION_0 and rotation is ROTATION_270, |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 832 | // then we want to check orientationAngle < 315 - GAP / 2. |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 833 | if (rotation == currentRotation |
| 834 | || rotation == (currentRotation + 3) % 4) { |
| 835 | int upperBound = rotation * 90 + 45 |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 836 | - ADJACENT_ORIENTATION_ANGLE_GAP / 2; |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 837 | if (rotation == 0) { |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 838 | if (orientationAngle <= 45 && orientationAngle > upperBound) { |
| 839 | return false; |
| 840 | } |
| 841 | } else { |
| 842 | if (orientationAngle > upperBound) { |
| 843 | return false; |
| 844 | } |
| 845 | } |
| 846 | } |
| 847 | } |
| 848 | return true; |
| 849 | } |
| 850 | |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 851 | /** |
| 852 | * Returns true if the predicted rotation is ready to be advertised as a |
| 853 | * proposed rotation. |
| 854 | */ |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 855 | private boolean isPredictedRotationAcceptableLocked(long now) { |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 856 | // The predicted rotation must have settled long enough. |
| 857 | if (now < mPredictedRotationTimestampNanos + PROPOSAL_SETTLE_TIME_NANOS) { |
| 858 | return false; |
| 859 | } |
| 860 | |
| 861 | // The last flat state (time since picked up) must have been sufficiently long ago. |
| 862 | if (now < mFlatTimestampNanos + PROPOSAL_MIN_TIME_SINCE_FLAT_ENDED_NANOS) { |
| 863 | return false; |
| 864 | } |
| 865 | |
| 866 | // The last swing state (time since last movement to put down) must have been |
| 867 | // sufficiently long ago. |
| 868 | if (now < mSwingTimestampNanos + PROPOSAL_MIN_TIME_SINCE_SWING_ENDED_NANOS) { |
| 869 | return false; |
| 870 | } |
| 871 | |
Jeff Brown | daf5d89 | 2012-05-07 18:30:18 -0700 | [diff] [blame] | 872 | // The last acceleration state must have been sufficiently long ago. |
| 873 | if (now < mAccelerationTimestampNanos |
| 874 | + PROPOSAL_MIN_TIME_SINCE_ACCELERATION_ENDED_NANOS) { |
| 875 | return false; |
| 876 | } |
| 877 | |
Adrian Roos | 3595be4 | 2015-03-05 16:31:15 +0100 | [diff] [blame] | 878 | // The last touch must have ended sufficiently long ago. |
| 879 | if (mTouched || now < mTouchEndedTimestampNanos |
| 880 | + PROPOSAL_MIN_TIME_SINCE_TOUCH_END_NANOS) { |
| 881 | return false; |
| 882 | } |
| 883 | |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 884 | // Looks good! |
| 885 | return true; |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 886 | } |
| 887 | |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 888 | private void clearPredictedRotationLocked() { |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 889 | mPredictedRotation = -1; |
| 890 | mPredictedRotationTimestampNanos = Long.MIN_VALUE; |
| 891 | } |
| 892 | |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 893 | private void updatePredictedRotationLocked(long now, int rotation) { |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 894 | if (mPredictedRotation != rotation) { |
| 895 | mPredictedRotation = rotation; |
| 896 | mPredictedRotationTimestampNanos = now; |
Jeff Brown | c0347aa | 2011-09-23 17:26:09 -0700 | [diff] [blame] | 897 | } |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 898 | } |
| 899 | |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 900 | private boolean isAcceleratingLocked(float magnitude) { |
Jeff Brown | daf5d89 | 2012-05-07 18:30:18 -0700 | [diff] [blame] | 901 | return magnitude < MIN_ACCELERATION_MAGNITUDE |
| 902 | || magnitude > MAX_ACCELERATION_MAGNITUDE; |
| 903 | } |
| 904 | |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 905 | private void clearTiltHistoryLocked() { |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 906 | mTiltHistoryTimestampNanos[0] = Long.MIN_VALUE; |
| 907 | mTiltHistoryIndex = 1; |
| 908 | } |
| 909 | |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 910 | private void addTiltHistoryEntryLocked(long now, float tilt) { |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 911 | mTiltHistory[mTiltHistoryIndex] = tilt; |
| 912 | mTiltHistoryTimestampNanos[mTiltHistoryIndex] = now; |
| 913 | mTiltHistoryIndex = (mTiltHistoryIndex + 1) % TILT_HISTORY_SIZE; |
| 914 | mTiltHistoryTimestampNanos[mTiltHistoryIndex] = Long.MIN_VALUE; |
| 915 | } |
| 916 | |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 917 | private boolean isFlatLocked(long now) { |
| 918 | for (int i = mTiltHistoryIndex; (i = nextTiltHistoryIndexLocked(i)) >= 0; ) { |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 919 | if (mTiltHistory[i] < FLAT_ANGLE) { |
| 920 | break; |
| 921 | } |
| 922 | if (mTiltHistoryTimestampNanos[i] + FLAT_TIME_NANOS <= now) { |
| 923 | // Tilt has remained greater than FLAT_TILT_ANGLE for FLAT_TIME_NANOS. |
| 924 | return true; |
| 925 | } |
| 926 | } |
| 927 | return false; |
| 928 | } |
| 929 | |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 930 | private boolean isSwingingLocked(long now, float tilt) { |
| 931 | for (int i = mTiltHistoryIndex; (i = nextTiltHistoryIndexLocked(i)) >= 0; ) { |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 932 | if (mTiltHistoryTimestampNanos[i] + SWING_TIME_NANOS < now) { |
| 933 | break; |
| 934 | } |
| 935 | if (mTiltHistory[i] + SWING_AWAY_ANGLE_DELTA <= tilt) { |
| 936 | // Tilted away by SWING_AWAY_ANGLE_DELTA within SWING_TIME_NANOS. |
| 937 | return true; |
| 938 | } |
| 939 | } |
| 940 | return false; |
| 941 | } |
| 942 | |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 943 | private int nextTiltHistoryIndexLocked(int index) { |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 944 | index = (index == 0 ? TILT_HISTORY_SIZE : index) - 1; |
| 945 | return mTiltHistoryTimestampNanos[index] != Long.MIN_VALUE ? index : -1; |
| 946 | } |
| 947 | |
Jeff Brown | 600f003 | 2014-05-22 17:06:00 -0700 | [diff] [blame] | 948 | private float getLastTiltLocked() { |
| 949 | int index = nextTiltHistoryIndexLocked(mTiltHistoryIndex); |
| 950 | return index >= 0 ? mTiltHistory[index] : Float.NaN; |
| 951 | } |
| 952 | |
Craig Mautner | eee29c4 | 2013-01-17 14:44:34 -0800 | [diff] [blame] | 953 | private float remainingMS(long now, long until) { |
Jeff Brown | 5aa73ae | 2012-01-13 15:29:21 -0800 | [diff] [blame] | 954 | return now >= until ? 0 : (until - now) * 0.000001f; |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 955 | } |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 956 | } |
Adrian Roos | 3595be4 | 2015-03-05 16:31:15 +0100 | [diff] [blame] | 957 | |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 958 | final class OrientationSensorJudge extends OrientationJudge { |
| 959 | private boolean mTouching; |
| 960 | private long mTouchEndedTimestampNanos = Long.MIN_VALUE; |
| 961 | private int mProposedRotation = -1; |
| 962 | private int mDesiredRotation = -1; |
| 963 | private boolean mRotationEvaluationScheduled; |
| 964 | |
| 965 | @Override |
| 966 | public int getProposedRotationLocked() { |
| 967 | return mProposedRotation; |
Adrian Roos | 3595be4 | 2015-03-05 16:31:15 +0100 | [diff] [blame] | 968 | } |
| 969 | |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 970 | @Override |
| 971 | public void onTouchStartLocked() { |
| 972 | mTouching = true; |
| 973 | } |
| 974 | |
| 975 | @Override |
| 976 | public void onTouchEndLocked(long whenElapsedNanos) { |
| 977 | mTouching = false; |
Adrian Roos | 3595be4 | 2015-03-05 16:31:15 +0100 | [diff] [blame] | 978 | mTouchEndedTimestampNanos = whenElapsedNanos; |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 979 | if (mDesiredRotation != mProposedRotation) { |
| 980 | final long now = SystemClock.elapsedRealtimeNanos(); |
| 981 | scheduleRotationEvaluationIfNecessaryLocked(now); |
| 982 | } |
Adrian Roos | 3595be4 | 2015-03-05 16:31:15 +0100 | [diff] [blame] | 983 | } |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 984 | |
| 985 | |
| 986 | @Override |
| 987 | public void onSensorChanged(SensorEvent event) { |
Michael Wright | 74f6c79 | 2015-08-25 21:15:12 +0100 | [diff] [blame] | 988 | int newRotation; |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 989 | synchronized (mLock) { |
| 990 | mDesiredRotation = (int) event.values[0]; |
Michael Wright | 74f6c79 | 2015-08-25 21:15:12 +0100 | [diff] [blame] | 991 | newRotation = evaluateRotationChangeLocked(); |
| 992 | } |
| 993 | if (newRotation >=0) { |
| 994 | onProposedRotationChanged(newRotation); |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 995 | } |
| 996 | } |
| 997 | |
| 998 | @Override |
| 999 | public void onAccuracyChanged(Sensor sensor, int accuracy) { } |
| 1000 | |
| 1001 | @Override |
| 1002 | public void dumpLocked(PrintWriter pw, String prefix) { |
| 1003 | pw.println(prefix + "OrientationSensorJudge"); |
| 1004 | prefix += " "; |
| 1005 | pw.println(prefix + "mDesiredRotation=" + mDesiredRotation); |
| 1006 | pw.println(prefix + "mProposedRotation=" + mProposedRotation); |
| 1007 | pw.println(prefix + "mTouching=" + mTouching); |
| 1008 | pw.println(prefix + "mTouchEndedTimestampNanos=" + mTouchEndedTimestampNanos); |
| 1009 | } |
| 1010 | |
| 1011 | @Override |
| 1012 | public void resetLocked() { |
| 1013 | mProposedRotation = -1; |
| 1014 | mDesiredRotation = -1; |
| 1015 | mTouching = false; |
| 1016 | mTouchEndedTimestampNanos = Long.MIN_VALUE; |
| 1017 | unscheduleRotationEvaluationLocked(); |
| 1018 | } |
| 1019 | |
Michael Wright | 74f6c79 | 2015-08-25 21:15:12 +0100 | [diff] [blame] | 1020 | public int evaluateRotationChangeLocked() { |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 1021 | unscheduleRotationEvaluationLocked(); |
| 1022 | if (mDesiredRotation == mProposedRotation) { |
Michael Wright | 74f6c79 | 2015-08-25 21:15:12 +0100 | [diff] [blame] | 1023 | return -1; |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 1024 | } |
| 1025 | final long now = SystemClock.elapsedRealtimeNanos(); |
| 1026 | if (isDesiredRotationAcceptableLocked(now)) { |
| 1027 | mProposedRotation = mDesiredRotation; |
Michael Wright | 74f6c79 | 2015-08-25 21:15:12 +0100 | [diff] [blame] | 1028 | return mProposedRotation; |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 1029 | } else { |
| 1030 | scheduleRotationEvaluationIfNecessaryLocked(now); |
| 1031 | } |
Michael Wright | 74f6c79 | 2015-08-25 21:15:12 +0100 | [diff] [blame] | 1032 | return -1; |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | private boolean isDesiredRotationAcceptableLocked(long now) { |
| 1036 | if (mTouching) { |
| 1037 | return false; |
| 1038 | } |
| 1039 | if (now < mTouchEndedTimestampNanos + PROPOSAL_MIN_TIME_SINCE_TOUCH_END_NANOS) { |
| 1040 | return false; |
| 1041 | } |
| 1042 | return true; |
| 1043 | } |
| 1044 | |
| 1045 | private void scheduleRotationEvaluationIfNecessaryLocked(long now) { |
| 1046 | if (mRotationEvaluationScheduled || mDesiredRotation == mProposedRotation) { |
| 1047 | if (LOG) { |
| 1048 | Slog.d(TAG, "scheduleRotationEvaluationLocked: " + |
| 1049 | "ignoring, an evaluation is already scheduled or is unnecessary."); |
| 1050 | } |
| 1051 | return; |
| 1052 | } |
| 1053 | if (mTouching) { |
| 1054 | if (LOG) { |
| 1055 | Slog.d(TAG, "scheduleRotationEvaluationLocked: " + |
| 1056 | "ignoring, user is still touching the screen."); |
| 1057 | } |
| 1058 | return; |
| 1059 | } |
| 1060 | long timeOfNextPossibleRotationNanos = |
| 1061 | mTouchEndedTimestampNanos + PROPOSAL_MIN_TIME_SINCE_TOUCH_END_NANOS; |
| 1062 | if (now >= timeOfNextPossibleRotationNanos) { |
| 1063 | if (LOG) { |
| 1064 | Slog.d(TAG, "scheduleRotationEvaluationLocked: " + |
| 1065 | "ignoring, already past the next possible time of rotation."); |
| 1066 | } |
| 1067 | return; |
| 1068 | } |
| 1069 | // Use a delay instead of an absolute time since handlers are in uptime millis and we |
| 1070 | // use elapsed realtime. |
| 1071 | final long delayMs = |
| 1072 | (long) Math.ceil((timeOfNextPossibleRotationNanos - now) * MILLIS_PER_NANO); |
| 1073 | mHandler.postDelayed(mRotationEvaluator, delayMs); |
| 1074 | mRotationEvaluationScheduled = true; |
| 1075 | } |
| 1076 | |
| 1077 | private void unscheduleRotationEvaluationLocked() { |
| 1078 | if (!mRotationEvaluationScheduled) { |
| 1079 | return; |
| 1080 | } |
| 1081 | mHandler.removeCallbacks(mRotationEvaluator); |
| 1082 | mRotationEvaluationScheduled = false; |
| 1083 | } |
| 1084 | |
| 1085 | private Runnable mRotationEvaluator = new Runnable() { |
| 1086 | @Override |
| 1087 | public void run() { |
Michael Wright | 74f6c79 | 2015-08-25 21:15:12 +0100 | [diff] [blame] | 1088 | int newRotation; |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 1089 | synchronized (mLock) { |
| 1090 | mRotationEvaluationScheduled = false; |
Michael Wright | 74f6c79 | 2015-08-25 21:15:12 +0100 | [diff] [blame] | 1091 | newRotation = evaluateRotationChangeLocked(); |
| 1092 | } |
| 1093 | if (newRotation >= 0) { |
| 1094 | onProposedRotationChanged(newRotation); |
Michael Wright | 814de9b | 2015-08-17 23:31:55 +0100 | [diff] [blame] | 1095 | } |
| 1096 | } |
| 1097 | }; |
Jeff Brown | 4519f07 | 2011-01-23 13:16:01 -0800 | [diff] [blame] | 1098 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1099 | } |