blob: 5f341ee8002c0f12e1fd465c224399886c452d69 [file] [log] [blame]
Riddle Hsuad256a12018-07-18 16:11:30 +08001/*
2 * Copyright (C) 2018 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
17package com.android.server.wm;
18
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080019import static com.android.server.policy.WindowManagerPolicy.WindowManagerFuncs.LID_OPEN;
20import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
21import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
22import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
23
Garfield Tanff362222018-11-14 17:52:32 -080024import android.annotation.UserIdInt;
Rajeev Kumarb2ff8e82018-08-06 11:45:05 -070025import android.app.ActivityManager;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080026import android.content.ContentResolver;
Riddle Hsuad256a12018-07-18 16:11:30 +080027import android.content.Context;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080028import android.content.Intent;
Riddle Hsuad256a12018-07-18 16:11:30 +080029import android.content.pm.ActivityInfo;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080030import android.content.pm.PackageManager;
Riddle Hsuad256a12018-07-18 16:11:30 +080031import android.content.res.Resources;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080032import android.database.ContentObserver;
33import android.hardware.power.V1_0.PowerHint;
Garfield Tanff362222018-11-14 17:52:32 -080034import android.net.Uri;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080035import android.os.Handler;
36import android.os.SystemProperties;
37import android.os.UserHandle;
38import android.provider.Settings;
39import android.util.Slog;
40import android.util.SparseArray;
Riddle Hsuad256a12018-07-18 16:11:30 +080041import android.view.Surface;
42
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080043import com.android.internal.annotations.VisibleForTesting;
44import com.android.server.LocalServices;
45import com.android.server.UiThread;
Riddle Hsuad256a12018-07-18 16:11:30 +080046import com.android.server.policy.WindowManagerPolicy;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080047import com.android.server.policy.WindowOrientationListener;
48import com.android.server.statusbar.StatusBarManagerInternal;
Riddle Hsuad256a12018-07-18 16:11:30 +080049
50import java.io.PrintWriter;
51
52/**
53 * Defines the mapping between orientation and rotation of a display.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080054 * Non-public methods are assumed to run inside WM lock.
Riddle Hsuad256a12018-07-18 16:11:30 +080055 */
56public class DisplayRotation {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080057 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayRotation" : TAG_WM;
Riddle Hsuad256a12018-07-18 16:11:30 +080058
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080059 private final WindowManagerService mService;
Garfield Tan90c90052018-10-08 12:29:41 -070060 private final DisplayContent mDisplayContent;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080061 private final DisplayPolicy mDisplayPolicy;
Garfield Tanff362222018-11-14 17:52:32 -080062 private final DisplayWindowSettings mDisplayWindowSettings;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080063 private final Context mContext;
64 private final Object mLock;
65
66 public final boolean isDefaultDisplay;
67 private final boolean mSupportAutoRotation;
68 private final int mLidOpenRotation;
69 private final int mCarDockRotation;
70 private final int mDeskDockRotation;
71 private final int mUndockedHdmiRotation;
72
73 private OrientationListener mOrientationListener;
74 private StatusBarManagerInternal mStatusBarManagerInternal;
75 private SettingsObserver mSettingsObserver;
76
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080077 private int mCurrentAppOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
78
79 @VisibleForTesting
Riddle Hsuad256a12018-07-18 16:11:30 +080080 int mLandscapeRotation; // default landscape
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080081 @VisibleForTesting
Riddle Hsuad256a12018-07-18 16:11:30 +080082 int mSeascapeRotation; // "other" landscape, 180 degrees from mLandscapeRotation
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080083 @VisibleForTesting
Riddle Hsuad256a12018-07-18 16:11:30 +080084 int mPortraitRotation; // default portrait
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080085 @VisibleForTesting
Riddle Hsuad256a12018-07-18 16:11:30 +080086 int mUpsideDownRotation; // "other" portrait
87
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080088 // Behavior of rotation suggestions. (See Settings.Secure.SHOW_ROTATION_SUGGESTION)
89 private int mShowRotationSuggestions;
90
91 private int mAllowAllRotations = -1;
92 private int mUserRotationMode = WindowManagerPolicy.USER_ROTATION_FREE;
93 private int mUserRotation = Surface.ROTATION_0;
94
Garfield Tanff362222018-11-14 17:52:32 -080095 /**
96 * A flag to indicate if the display rotation should be fixed to user specified rotation
97 * regardless of all other states (including app requrested orientation). {@code true} the
98 * display rotation should be fixed to user specified rotation, {@code false} otherwise.
99 */
100 private boolean mFixedToUserRotation;
101
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800102 private int mDemoHdmiRotation;
103 private int mDemoRotation;
104 private boolean mDemoHdmiRotationLock;
105 private boolean mDemoRotationLock;
106
107 DisplayRotation(WindowManagerService service, DisplayContent displayContent) {
108 this(service, displayContent, displayContent.getDisplayPolicy(),
Garfield Tanff362222018-11-14 17:52:32 -0800109 service.mDisplayWindowSettings, service.mContext, service.getWindowManagerLock());
Riddle Hsuad256a12018-07-18 16:11:30 +0800110 }
111
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800112 @VisibleForTesting
113 DisplayRotation(WindowManagerService service, DisplayContent displayContent,
Garfield Tanff362222018-11-14 17:52:32 -0800114 DisplayPolicy displayPolicy, DisplayWindowSettings displayWindowSettings,
115 Context context, Object lock) {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800116 mService = service;
Garfield Tan90c90052018-10-08 12:29:41 -0700117 mDisplayContent = displayContent;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800118 mDisplayPolicy = displayPolicy;
Garfield Tanff362222018-11-14 17:52:32 -0800119 mDisplayWindowSettings = displayWindowSettings;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800120 mContext = context;
121 mLock = lock;
122 isDefaultDisplay = displayContent.isDefaultDisplay;
Riddle Hsuad256a12018-07-18 16:11:30 +0800123
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800124 mSupportAutoRotation = mContext.getResources().getBoolean(
125 com.android.internal.R.bool.config_supportAutoRotation);
126 mLidOpenRotation = readRotation(
127 com.android.internal.R.integer.config_lidOpenRotation);
128 mCarDockRotation = readRotation(
129 com.android.internal.R.integer.config_carDockRotation);
130 mDeskDockRotation = readRotation(
131 com.android.internal.R.integer.config_deskDockRotation);
132 mUndockedHdmiRotation = readRotation(
133 com.android.internal.R.integer.config_undockedHdmiRotation);
134
135 if (isDefaultDisplay) {
136 final Handler uiHandler = UiThread.getHandler();
137 mOrientationListener = new OrientationListener(mContext, uiHandler);
138 mOrientationListener.setCurrentRotation(displayContent.getRotation());
139 mSettingsObserver = new SettingsObserver(uiHandler);
140 mSettingsObserver.observe();
141 }
142 }
143
144 private int readRotation(int resID) {
145 try {
146 final int rotation = mContext.getResources().getInteger(resID);
147 switch (rotation) {
148 case 0:
149 return Surface.ROTATION_0;
150 case 90:
151 return Surface.ROTATION_90;
152 case 180:
153 return Surface.ROTATION_180;
154 case 270:
155 return Surface.ROTATION_270;
156 }
157 } catch (Resources.NotFoundException e) {
158 // fall through
159 }
160 return -1;
161 }
162
163 void configure(int width, int height, int shortSizeDp, int longSizeDp) {
Riddle Hsuad256a12018-07-18 16:11:30 +0800164 final Resources res = mContext.getResources();
165 if (width > height) {
166 mLandscapeRotation = Surface.ROTATION_0;
167 mSeascapeRotation = Surface.ROTATION_180;
168 if (res.getBoolean(com.android.internal.R.bool.config_reverseDefaultRotation)) {
169 mPortraitRotation = Surface.ROTATION_90;
170 mUpsideDownRotation = Surface.ROTATION_270;
171 } else {
172 mPortraitRotation = Surface.ROTATION_270;
173 mUpsideDownRotation = Surface.ROTATION_90;
174 }
175 } else {
176 mPortraitRotation = Surface.ROTATION_0;
177 mUpsideDownRotation = Surface.ROTATION_180;
178 if (res.getBoolean(com.android.internal.R.bool.config_reverseDefaultRotation)) {
179 mLandscapeRotation = Surface.ROTATION_270;
180 mSeascapeRotation = Surface.ROTATION_90;
181 } else {
182 mLandscapeRotation = Surface.ROTATION_90;
183 mSeascapeRotation = Surface.ROTATION_270;
184 }
185 }
186
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800187 // For demo purposes, allow the rotation of the HDMI display to be controlled.
188 // By default, HDMI locks rotation to landscape.
189 if ("portrait".equals(SystemProperties.get("persist.demo.hdmirotation"))) {
190 mDemoHdmiRotation = mPortraitRotation;
191 } else {
192 mDemoHdmiRotation = mLandscapeRotation;
193 }
194 mDemoHdmiRotationLock = SystemProperties.getBoolean("persist.demo.hdmirotationlock", false);
195
196 // For demo purposes, allow the rotation of the remote display to be controlled.
197 // By default, remote display locks rotation to landscape.
198 if ("portrait".equals(SystemProperties.get("persist.demo.remoterotation"))) {
199 mDemoRotation = mPortraitRotation;
200 } else {
201 mDemoRotation = mLandscapeRotation;
202 }
203 mDemoRotationLock = SystemProperties.getBoolean("persist.demo.rotationlock", false);
204
205 // Only force the default orientation if the screen is xlarge, at least 960dp x 720dp, per
206 // http://developer.android.com/guide/practices/screens_support.html#range
207 // For car, ignore the dp limitation. It's physically impossible to rotate the car's screen
208 // so if the orientation is forced, we need to respect that no matter what.
209 final boolean isCar = mContext.getPackageManager().hasSystemFeature(
210 PackageManager.FEATURE_AUTOMOTIVE);
211 // For TV, it's usually 960dp x 540dp, ignore the size limitation.
212 // so if the orientation is forced, we need to respect that no matter what.
213 final boolean isTv = mContext.getPackageManager().hasSystemFeature(
214 PackageManager.FEATURE_LEANBACK);
Garfield Tanff362222018-11-14 17:52:32 -0800215 final boolean forceDefaultOrientationInRes =
216 res.getBoolean(com.android.internal.R.bool.config_forceDefaultOrientation);
217 final boolean forceDefaultOrienation =
218 ((longSizeDp >= 960 && shortSizeDp >= 720) || isCar || isTv)
219 && forceDefaultOrientationInRes
220 // For debug purposes the next line turns this feature off with:
221 // $ adb shell setprop config.override_forced_orient true
222 // $ adb shell wm size reset
223 && !"true".equals(SystemProperties.get("config.override_forced_orient"));
224 // Configuration says we force to use the default orientation. We can fall back to fix
225 // rotation to only user rotation. As long as OEM doesn't change user rotation then the
226 // rotation of this display is effectively stuck at 0 deg.
227 setFixedToUserRotation(forceDefaultOrienation);
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800228 }
229
230 void setRotation(int rotation) {
231 if (mOrientationListener != null) {
232 mOrientationListener.setCurrentRotation(rotation);
233 }
234 }
235
236 void setCurrentOrientation(int newOrientation) {
237 if (newOrientation != mCurrentAppOrientation) {
238 mCurrentAppOrientation = newOrientation;
239 if (isDefaultDisplay) {
240 updateOrientationListenerLw();
241 }
242 }
243 }
244
Garfield Tanff362222018-11-14 17:52:32 -0800245 void restoreSettings(int userRotationMode, int userRotation,
246 boolean fixedToUserRotation) {
247 mFixedToUserRotation = fixedToUserRotation;
248
249 // We will retrieve user rotation and user rotation mode from settings for default display.
250 if (isDefaultDisplay) {
251 return;
252 }
Garfield Tan90c90052018-10-08 12:29:41 -0700253 if (userRotationMode != WindowManagerPolicy.USER_ROTATION_FREE
254 && userRotationMode != WindowManagerPolicy.USER_ROTATION_LOCKED) {
255 Slog.w(TAG, "Trying to restore an invalid user rotation mode " + userRotationMode
256 + " for " + mDisplayContent);
257 userRotationMode = WindowManagerPolicy.USER_ROTATION_FREE;
258 }
259 if (userRotation < Surface.ROTATION_0 || userRotation > Surface.ROTATION_270) {
260 Slog.w(TAG, "Trying to restore an invalid user rotation " + userRotation
261 + " for " + mDisplayContent);
262 userRotation = Surface.ROTATION_0;
263 }
264 mUserRotationMode = userRotationMode;
265 mUserRotation = userRotation;
266 }
267
Garfield Tanff362222018-11-14 17:52:32 -0800268 void setFixedToUserRotation(boolean fixedToUserRotation) {
269 if (mFixedToUserRotation == fixedToUserRotation) {
270 return;
271 }
272
273 mFixedToUserRotation = fixedToUserRotation;
274 mDisplayWindowSettings.setFixedToUserRotation(mDisplayContent,
275 fixedToUserRotation);
276 mService.updateRotation(true /* alwaysSendConfiguration */,
277 false /* forceRelayout */);
278 }
279
Garfield Tan90c90052018-10-08 12:29:41 -0700280 private void setUserRotation(int userRotationMode, int userRotation) {
281 if (isDefaultDisplay) {
282 // We'll be notified via settings listener, so we don't need to update internal values.
283 final ContentResolver res = mContext.getContentResolver();
284 final int accelerometerRotation =
285 userRotationMode == WindowManagerPolicy.USER_ROTATION_LOCKED ? 0 : 1;
286 Settings.System.putIntForUser(res, Settings.System.ACCELEROMETER_ROTATION,
287 accelerometerRotation, UserHandle.USER_CURRENT);
288 Settings.System.putIntForUser(res, Settings.System.USER_ROTATION, userRotation,
289 UserHandle.USER_CURRENT);
290 return;
291 }
292
293 boolean changed = false;
294 if (mUserRotationMode != userRotationMode) {
295 mUserRotationMode = userRotationMode;
296 changed = true;
297 }
298 if (mUserRotation != userRotation) {
299 mUserRotation = userRotation;
300 changed = true;
301 }
Garfield Tanff362222018-11-14 17:52:32 -0800302 mDisplayWindowSettings.setUserRotation(mDisplayContent, userRotationMode,
Chilun8753ad32018-10-09 15:56:45 +0800303 userRotation);
Garfield Tan90c90052018-10-08 12:29:41 -0700304 if (changed) {
305 mService.updateRotation(true /* alwaysSendConfiguration */,
306 false /* forceRelayout */);
Garfield Tan90c90052018-10-08 12:29:41 -0700307 }
308 }
309
310 void freezeRotation(int rotation) {
311 rotation = (rotation == -1) ? mDisplayContent.getRotation() : rotation;
312 setUserRotation(WindowManagerPolicy.USER_ROTATION_LOCKED, rotation);
313 }
314
315 void thawRotation() {
316 setUserRotation(WindowManagerPolicy.USER_ROTATION_FREE, mUserRotation);
317 }
318
319 boolean isRotationFrozen() {
320 if (!isDefaultDisplay) {
321 return mUserRotationMode == WindowManagerPolicy.USER_ROTATION_LOCKED;
322 }
323
324 return Settings.System.getIntForUser(mContext.getContentResolver(),
325 Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT) == 0;
326 }
327
Garfield Tanff362222018-11-14 17:52:32 -0800328 boolean isFixedToUserRotation() {
329 return mFixedToUserRotation;
Riddle Hsuad256a12018-07-18 16:11:30 +0800330 }
331
Garfield Tan49dae102019-02-04 09:51:59 -0800332 /**
333 * Returns {@code true} if this display rotation takes app requested orientation into
334 * consideration; {@code false} otherwise. For the time being the only case where this is {@code
335 * false} is when {@link #isFixedToUserRotation()} is {@code true}.
336 */
337 boolean respectAppRequestedOrientation() {
338 return !mFixedToUserRotation;
339 }
340
Riddle Hsuad256a12018-07-18 16:11:30 +0800341 public int getLandscapeRotation() {
342 return mLandscapeRotation;
343 }
344
345 public int getSeascapeRotation() {
346 return mSeascapeRotation;
347 }
348
349 public int getPortraitRotation() {
350 return mPortraitRotation;
351 }
352
353 public int getUpsideDownRotation() {
354 return mUpsideDownRotation;
355 }
356
357 public int getCurrentAppOrientation() {
358 return mCurrentAppOrientation;
359 }
360
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800361 public DisplayPolicy getDisplayPolicy() {
362 return mDisplayPolicy;
Riddle Hsuad256a12018-07-18 16:11:30 +0800363 }
364
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800365 public WindowOrientationListener getOrientationListener() {
366 return mOrientationListener;
Riddle Hsuad256a12018-07-18 16:11:30 +0800367 }
368
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800369 public int getUserRotation() {
370 return mUserRotation;
371 }
372
373 public int getUserRotationMode() {
374 return mUserRotationMode;
375 }
376
377 public void updateOrientationListener() {
378 synchronized (mLock) {
379 updateOrientationListenerLw();
Riddle Hsuad256a12018-07-18 16:11:30 +0800380 }
381 }
382
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800383 /**
384 * Various use cases for invoking this function:
385 * <li>Screen turning off, should always disable listeners if already enabled.</li>
386 * <li>Screen turned on and current app has sensor based orientation, enable listeners
387 * if not already enabled.</li>
388 * <li>Screen turned on and current app does not have sensor orientation, disable listeners
389 * if already enabled.</li>
390 * <li>Screen turning on and current app has sensor based orientation, enable listeners
391 * if needed.</li>
392 * <li>screen turning on and current app has nosensor based orientation, do nothing.</li>
393 */
394 private void updateOrientationListenerLw() {
395 if (mOrientationListener == null || !mOrientationListener.canDetectOrientation()) {
396 // If sensor is turned off or nonexistent for some reason.
397 return;
398 }
399
400 final boolean screenOnEarly = mDisplayPolicy.isScreenOnEarly();
401 final boolean awake = mDisplayPolicy.isAwake();
402 final boolean keyguardDrawComplete = mDisplayPolicy.isKeyguardDrawComplete();
403 final boolean windowManagerDrawComplete = mDisplayPolicy.isWindowManagerDrawComplete();
404
405 // Could have been invoked due to screen turning on or off or
406 // change of the currently visible window's orientation.
407 if (DEBUG_ORIENTATION) Slog.v(TAG, "screenOnEarly=" + screenOnEarly
408 + ", awake=" + awake + ", currentAppOrientation=" + mCurrentAppOrientation
409 + ", orientationSensorEnabled=" + mOrientationListener.mEnabled
410 + ", keyguardDrawComplete=" + keyguardDrawComplete
411 + ", windowManagerDrawComplete=" + windowManagerDrawComplete);
412
413 boolean disable = true;
414 // Note: We postpone the rotating of the screen until the keyguard as well as the
415 // window manager have reported a draw complete or the keyguard is going away in dismiss
416 // mode.
417 if (screenOnEarly && awake && ((keyguardDrawComplete && windowManagerDrawComplete))) {
418 if (needSensorRunning()) {
419 disable = false;
420 // Enable listener if not already enabled.
421 if (!mOrientationListener.mEnabled) {
422 // Don't clear the current sensor orientation if the keyguard is going away in
423 // dismiss mode. This allows window manager to use the last sensor reading to
424 // determine the orientation vs. falling back to the last known orientation if
425 // the sensor reading was cleared which can cause it to relaunch the app that
426 // will show in the wrong orientation first before correcting leading to app
427 // launch delays.
428 mOrientationListener.enable(true /* clearCurrentRotation */);
429 }
Riddle Hsuad256a12018-07-18 16:11:30 +0800430 }
431 }
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800432 // Check if sensors need to be disabled.
433 if (disable && mOrientationListener.mEnabled) {
434 mOrientationListener.disable();
435 }
Riddle Hsuad256a12018-07-18 16:11:30 +0800436 }
437
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800438 /**
439 * We always let the sensor be switched on by default except when
440 * the user has explicitly disabled sensor based rotation or when the
441 * screen is switched off.
442 */
443 private boolean needSensorRunning() {
Garfield Tanff362222018-11-14 17:52:32 -0800444 if (mFixedToUserRotation) {
445 // We are sure we only respect user rotation settings, so we are sure we will not
446 // support sensor rotation.
447 return false;
448 }
449
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800450 if (mSupportAutoRotation) {
451 if (mCurrentAppOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR
452 || mCurrentAppOrientation == ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR
453 || mCurrentAppOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT
454 || mCurrentAppOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE) {
455 // If the application has explicitly requested to follow the
456 // orientation, then we need to turn the sensor on.
457 return true;
458 }
459 }
460
461 final int dockMode = mDisplayPolicy.getDockMode();
462 if ((mDisplayPolicy.isCarDockEnablesAccelerometer()
463 && dockMode == Intent.EXTRA_DOCK_STATE_CAR)
464 || (mDisplayPolicy.isDeskDockEnablesAccelerometer()
465 && (dockMode == Intent.EXTRA_DOCK_STATE_DESK
466 || dockMode == Intent.EXTRA_DOCK_STATE_LE_DESK
467 || dockMode == Intent.EXTRA_DOCK_STATE_HE_DESK))) {
468 // Enable accelerometer if we are docked in a dock that enables accelerometer
469 // orientation management.
470 return true;
471 }
472
473 if (mUserRotationMode == WindowManagerPolicy.USER_ROTATION_LOCKED) {
474 // If the setting for using the sensor by default is enabled, then
475 // we will always leave it on. Note that the user could go to
476 // a window that forces an orientation that does not use the
477 // sensor and in theory we could turn it off... however, when next
478 // turning it on we won't have a good value for the current
479 // orientation for a little bit, which can cause orientation
480 // changes to lag, so we'd like to keep it always on. (It will
481 // still be turned off when the screen is off.)
482
483 // When locked we can provide rotation suggestions users can approve to change the
484 // current screen rotation. To do this the sensor needs to be running.
485 return mSupportAutoRotation &&
486 mShowRotationSuggestions == Settings.Secure.SHOW_ROTATION_SUGGESTIONS_ENABLED;
487 }
488 return mSupportAutoRotation;
489 }
490
491 /**
492 * Given an orientation constant, returns the appropriate surface rotation,
493 * taking into account sensors, docking mode, rotation lock, and other factors.
494 *
495 * @param orientation An orientation constant, such as
496 * {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_LANDSCAPE}.
497 * @param lastRotation The most recently used rotation.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800498 * @return The surface rotation to use.
499 */
500 int rotationForOrientation(int orientation, int lastRotation) {
501 if (DEBUG_ORIENTATION) {
502 Slog.v(TAG, "rotationForOrientation(orient="
503 + orientation + ", last=" + lastRotation
504 + "); user=" + mUserRotation + " "
505 + (mUserRotationMode == WindowManagerPolicy.USER_ROTATION_LOCKED
506 ? "USER_ROTATION_LOCKED" : "")
507 );
508 }
509
Garfield Tanff362222018-11-14 17:52:32 -0800510 if (mFixedToUserRotation) {
511 return mUserRotation;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800512 }
513
514 int sensorRotation = mOrientationListener != null
515 ? mOrientationListener.getProposedRotation() // may be -1
516 : -1;
517 if (sensorRotation < 0) {
518 sensorRotation = lastRotation;
519 }
520
521 final int lidState = mDisplayPolicy.getLidState();
522 final int dockMode = mDisplayPolicy.getDockMode();
523 final boolean hdmiPlugged = mDisplayPolicy.isHdmiPlugged();
524 final boolean carDockEnablesAccelerometer =
525 mDisplayPolicy.isCarDockEnablesAccelerometer();
526 final boolean deskDockEnablesAccelerometer =
527 mDisplayPolicy.isDeskDockEnablesAccelerometer();
528
529 final int preferredRotation;
530 if (!isDefaultDisplay) {
531 // For secondary displays we ignore things like displays sensors, docking mode and
Garfield Tan90c90052018-10-08 12:29:41 -0700532 // rotation lock, and always prefer user rotation.
533 preferredRotation = mUserRotation;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800534 } else if (lidState == LID_OPEN && mLidOpenRotation >= 0) {
535 // Ignore sensor when lid switch is open and rotation is forced.
536 preferredRotation = mLidOpenRotation;
537 } else if (dockMode == Intent.EXTRA_DOCK_STATE_CAR
538 && (carDockEnablesAccelerometer || mCarDockRotation >= 0)) {
539 // Ignore sensor when in car dock unless explicitly enabled.
540 // This case can override the behavior of NOSENSOR, and can also
541 // enable 180 degree rotation while docked.
542 preferredRotation = carDockEnablesAccelerometer ? sensorRotation : mCarDockRotation;
543 } else if ((dockMode == Intent.EXTRA_DOCK_STATE_DESK
544 || dockMode == Intent.EXTRA_DOCK_STATE_LE_DESK
545 || dockMode == Intent.EXTRA_DOCK_STATE_HE_DESK)
546 && (deskDockEnablesAccelerometer || mDeskDockRotation >= 0)) {
547 // Ignore sensor when in desk dock unless explicitly enabled.
548 // This case can override the behavior of NOSENSOR, and can also
549 // enable 180 degree rotation while docked.
550 preferredRotation = deskDockEnablesAccelerometer ? sensorRotation : mDeskDockRotation;
551 } else if (hdmiPlugged && mDemoHdmiRotationLock) {
552 // Ignore sensor when plugged into HDMI when demo HDMI rotation lock enabled.
553 // Note that the dock orientation overrides the HDMI orientation.
554 preferredRotation = mDemoHdmiRotation;
555 } else if (hdmiPlugged && dockMode == Intent.EXTRA_DOCK_STATE_UNDOCKED
556 && mUndockedHdmiRotation >= 0) {
557 // Ignore sensor when plugged into HDMI and an undocked orientation has
558 // been specified in the configuration (only for legacy devices without
559 // full multi-display support).
560 // Note that the dock orientation overrides the HDMI orientation.
561 preferredRotation = mUndockedHdmiRotation;
562 } else if (mDemoRotationLock) {
563 // Ignore sensor when demo rotation lock is enabled.
564 // Note that the dock orientation and HDMI rotation lock override this.
565 preferredRotation = mDemoRotation;
566 } else if (mDisplayPolicy.isPersistentVrModeEnabled()) {
567 // While in VR, apps always prefer a portrait rotation. This does not change
568 // any apps that explicitly set landscape, but does cause sensors be ignored,
569 // and ignored any orientation lock that the user has set (this conditional
570 // should remain above the ORIENTATION_LOCKED conditional below).
571 preferredRotation = mPortraitRotation;
572 } else if (orientation == ActivityInfo.SCREEN_ORIENTATION_LOCKED) {
573 // Application just wants to remain locked in the last rotation.
574 preferredRotation = lastRotation;
575 } else if (!mSupportAutoRotation) {
576 // If we don't support auto-rotation then bail out here and ignore
577 // the sensor and any rotation lock settings.
578 preferredRotation = -1;
579 } else if ((mUserRotationMode == WindowManagerPolicy.USER_ROTATION_FREE
580 && (orientation == ActivityInfo.SCREEN_ORIENTATION_USER
581 || orientation == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
582 || orientation == ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE
583 || orientation == ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
584 || orientation == ActivityInfo.SCREEN_ORIENTATION_FULL_USER))
585 || orientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR
586 || orientation == ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR
587 || orientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
588 || orientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT) {
589 // Otherwise, use sensor only if requested by the application or enabled
590 // by default for USER or UNSPECIFIED modes. Does not apply to NOSENSOR.
591 if (mAllowAllRotations < 0) {
592 // Can't read this during init() because the context doesn't
593 // have display metrics at that time so we cannot determine
594 // tablet vs. phone then.
595 mAllowAllRotations = mContext.getResources().getBoolean(
596 com.android.internal.R.bool.config_allowAllRotations) ? 1 : 0;
597 }
598 if (sensorRotation != Surface.ROTATION_180
599 || mAllowAllRotations == 1
600 || orientation == ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR
601 || orientation == ActivityInfo.SCREEN_ORIENTATION_FULL_USER) {
602 preferredRotation = sensorRotation;
603 } else {
604 preferredRotation = lastRotation;
605 }
606 } else if (mUserRotationMode == WindowManagerPolicy.USER_ROTATION_LOCKED
607 && orientation != ActivityInfo.SCREEN_ORIENTATION_NOSENSOR) {
608 // Apply rotation lock. Does not apply to NOSENSOR.
609 // The idea is that the user rotation expresses a weak preference for the direction
610 // of gravity and as NOSENSOR is never affected by gravity, then neither should
611 // NOSENSOR be affected by rotation lock (although it will be affected by docks).
612 preferredRotation = mUserRotation;
613 } else {
614 // No overriding preference.
615 // We will do exactly what the application asked us to do.
616 preferredRotation = -1;
617 }
618
Riddle Hsuad256a12018-07-18 16:11:30 +0800619 switch (orientation) {
620 case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
621 // Return portrait unless overridden.
622 if (isAnyPortrait(preferredRotation)) {
623 return preferredRotation;
624 }
625 return mPortraitRotation;
626
627 case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
628 // Return landscape unless overridden.
629 if (isLandscapeOrSeascape(preferredRotation)) {
630 return preferredRotation;
631 }
632 return mLandscapeRotation;
633
634 case ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT:
635 // Return reverse portrait unless overridden.
636 if (isAnyPortrait(preferredRotation)) {
637 return preferredRotation;
638 }
639 return mUpsideDownRotation;
640
641 case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE:
642 // Return seascape unless overridden.
643 if (isLandscapeOrSeascape(preferredRotation)) {
644 return preferredRotation;
645 }
646 return mSeascapeRotation;
647
648 case ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE:
649 case ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE:
650 // Return either landscape rotation.
651 if (isLandscapeOrSeascape(preferredRotation)) {
652 return preferredRotation;
653 }
654 if (isLandscapeOrSeascape(lastRotation)) {
655 return lastRotation;
656 }
657 return mLandscapeRotation;
658
659 case ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT:
660 case ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT:
661 // Return either portrait rotation.
662 if (isAnyPortrait(preferredRotation)) {
663 return preferredRotation;
664 }
665 if (isAnyPortrait(lastRotation)) {
666 return lastRotation;
667 }
668 return mPortraitRotation;
669
670 default:
671 // For USER, UNSPECIFIED, NOSENSOR, SENSOR and FULL_SENSOR,
672 // just return the preferred orientation we already calculated.
673 if (preferredRotation >= 0) {
674 return preferredRotation;
675 }
676 return Surface.ROTATION_0;
677 }
678 }
679
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800680 private boolean isLandscapeOrSeascape(int rotation) {
681 return rotation == mLandscapeRotation || rotation == mSeascapeRotation;
682 }
683
684 private boolean isAnyPortrait(int rotation) {
685 return rotation == mPortraitRotation || rotation == mUpsideDownRotation;
686 }
687
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800688 private boolean isValidRotationChoice(final int preferredRotation) {
Riddle Hsuad256a12018-07-18 16:11:30 +0800689 // Determine if the given app orientation is compatible with the provided rotation choice.
690 switch (mCurrentAppOrientation) {
691 case ActivityInfo.SCREEN_ORIENTATION_FULL_USER:
692 // Works with any of the 4 rotations.
693 return preferredRotation >= 0;
694
695 case ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT:
696 // It's possible for the user pref to be set at 180 because of FULL_USER. This would
697 // make switching to USER_PORTRAIT appear at 180. Provide choice to back to portrait
698 // but never to go to 180.
699 return preferredRotation == mPortraitRotation;
700
701 case ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE:
702 // Works landscape or seascape.
703 return isLandscapeOrSeascape(preferredRotation);
704
705 case ActivityInfo.SCREEN_ORIENTATION_USER:
706 case ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED:
707 // Works with any rotation except upside down.
708 return (preferredRotation >= 0) && (preferredRotation != mUpsideDownRotation);
709 }
710
711 return false;
712 }
713
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800714 private boolean isRotationChoicePossible(int orientation) {
715 // Rotation choice is only shown when the user is in locked mode.
716 if (mUserRotationMode != WindowManagerPolicy.USER_ROTATION_LOCKED) return false;
717
718 // We should only enable rotation choice if the rotation isn't forced by the lid, dock,
719 // demo, hdmi, vr, etc mode.
720
721 // Determine if the rotation is currently forced.
Garfield Tanff362222018-11-14 17:52:32 -0800722 if (mFixedToUserRotation) {
723 return false; // Rotation is forced to user settings.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800724 }
725
726 final int lidState = mDisplayPolicy.getLidState();
727 if (lidState == LID_OPEN && mLidOpenRotation >= 0) {
728 return false; // Rotation is forced mLidOpenRotation.
729 }
730
731 final int dockMode = mDisplayPolicy.getDockMode();
732 final boolean carDockEnablesAccelerometer = false;
733 if (dockMode == Intent.EXTRA_DOCK_STATE_CAR && !carDockEnablesAccelerometer) {
734 return false; // Rotation forced to mCarDockRotation.
735 }
736
737 final boolean deskDockEnablesAccelerometer =
738 mDisplayPolicy.isDeskDockEnablesAccelerometer();
739 if ((dockMode == Intent.EXTRA_DOCK_STATE_DESK
740 || dockMode == Intent.EXTRA_DOCK_STATE_LE_DESK
741 || dockMode == Intent.EXTRA_DOCK_STATE_HE_DESK)
742 && !deskDockEnablesAccelerometer) {
743 return false; // Rotation forced to mDeskDockRotation.
744 }
745
746 final boolean hdmiPlugged = mDisplayPolicy.isHdmiPlugged();
747 if (hdmiPlugged && mDemoHdmiRotationLock) {
748 return false; // Rotation forced to mDemoHdmiRotation.
749
750 } else if (hdmiPlugged && dockMode == Intent.EXTRA_DOCK_STATE_UNDOCKED
751 && mUndockedHdmiRotation >= 0) {
752 return false; // Rotation forced to mUndockedHdmiRotation.
753
754 } else if (mDemoRotationLock) {
755 return false; // Rotation forced to mDemoRotation.
756
757 } else if (mDisplayPolicy.isPersistentVrModeEnabled()) {
758 return false; // Rotation forced to mPortraitRotation.
759
760 } else if (!mSupportAutoRotation) {
761 return false;
762 }
763
764 // Ensure that some rotation choice is possible for the given orientation.
765 switch (orientation) {
766 case ActivityInfo.SCREEN_ORIENTATION_FULL_USER:
767 case ActivityInfo.SCREEN_ORIENTATION_USER:
768 case ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED:
769 case ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE:
770 case ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT:
771 // NOSENSOR description is ambiguous, in reality WM ignores user choice.
772 return true;
773 }
774
775 // Rotation is forced, should be controlled by system.
776 return false;
Riddle Hsuad256a12018-07-18 16:11:30 +0800777 }
778
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800779 /** Notify the StatusBar that system rotation suggestion has changed. */
780 private void sendProposedRotationChangeToStatusBarInternal(int rotation, boolean isValid) {
781 if (mStatusBarManagerInternal == null) {
782 mStatusBarManagerInternal = LocalServices.getService(StatusBarManagerInternal.class);
783 }
784 if (mStatusBarManagerInternal != null) {
785 mStatusBarManagerInternal.onProposedRotationChanged(rotation, isValid);
786 }
787 }
788
789 private static String allowAllRotationsToString(int allowAll) {
790 switch (allowAll) {
791 case -1:
792 return "unknown";
793 case 0:
794 return "false";
795 case 1:
796 return "true";
797 default:
798 return Integer.toString(allowAll);
799 }
800 }
801
802 public void onUserSwitch() {
803 if (mSettingsObserver != null) {
804 mSettingsObserver.onChange(false);
805 }
806 }
807
808 /** Return whether the rotation settings has changed. */
809 private boolean updateSettings() {
810 final ContentResolver resolver = mContext.getContentResolver();
811 boolean shouldUpdateRotation = false;
812
813 synchronized (mLock) {
814 boolean shouldUpdateOrientationListener = false;
815
816 // Configure rotation suggestions.
Rajeev Kumarb2ff8e82018-08-06 11:45:05 -0700817 final int showRotationSuggestions =
818 ActivityManager.isLowRamDeviceStatic()
819 ? Settings.Secure.SHOW_ROTATION_SUGGESTIONS_DISABLED
820 : Settings.Secure.getIntForUser(resolver,
821 Settings.Secure.SHOW_ROTATION_SUGGESTIONS,
822 Settings.Secure.SHOW_ROTATION_SUGGESTIONS_DEFAULT,
823 UserHandle.USER_CURRENT);
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800824 if (mShowRotationSuggestions != showRotationSuggestions) {
825 mShowRotationSuggestions = showRotationSuggestions;
826 shouldUpdateOrientationListener = true;
827 }
828
829 // Configure rotation lock.
830 final int userRotation = Settings.System.getIntForUser(resolver,
831 Settings.System.USER_ROTATION, Surface.ROTATION_0,
832 UserHandle.USER_CURRENT);
833 if (mUserRotation != userRotation) {
834 mUserRotation = userRotation;
835 shouldUpdateRotation = true;
836 }
837
838 final int userRotationMode = Settings.System.getIntForUser(resolver,
839 Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT) != 0
840 ? WindowManagerPolicy.USER_ROTATION_FREE
841 : WindowManagerPolicy.USER_ROTATION_LOCKED;
842 if (mUserRotationMode != userRotationMode) {
843 mUserRotationMode = userRotationMode;
844 shouldUpdateOrientationListener = true;
845 shouldUpdateRotation = true;
846 }
847
848 if (shouldUpdateOrientationListener) {
849 updateOrientationListenerLw(); // Enable or disable the orientation listener.
850 }
851 }
852
853 return shouldUpdateRotation;
Riddle Hsuad256a12018-07-18 16:11:30 +0800854 }
855
856 void dump(String prefix, PrintWriter pw) {
857 pw.println(prefix + "DisplayRotation");
858 pw.println(prefix + " mCurrentAppOrientation="
859 + ActivityInfo.screenOrientationToString(mCurrentAppOrientation));
860 pw.print(prefix + " mLandscapeRotation=" + Surface.rotationToString(mLandscapeRotation));
861 pw.println(" mSeascapeRotation=" + Surface.rotationToString(mSeascapeRotation));
862 pw.print(prefix + " mPortraitRotation=" + Surface.rotationToString(mPortraitRotation));
863 pw.println(" mUpsideDownRotation=" + Surface.rotationToString(mUpsideDownRotation));
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800864
Tiger Huang7c610aa2018-10-27 00:01:01 +0800865 pw.println(prefix + " mSupportAutoRotation=" + mSupportAutoRotation);
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800866 if (mOrientationListener != null) {
Tiger Huang7c610aa2018-10-27 00:01:01 +0800867 mOrientationListener.dump(pw, prefix + " ");
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800868 }
869 pw.println();
870
871 pw.print(prefix + " mCarDockRotation=" + Surface.rotationToString(mCarDockRotation));
872 pw.println(" mDeskDockRotation=" + Surface.rotationToString(mDeskDockRotation));
873 pw.print(prefix + " mUserRotationMode="
874 + WindowManagerPolicy.userRotationModeToString(mUserRotationMode));
875 pw.print(" mUserRotation=" + Surface.rotationToString(mUserRotation));
876 pw.println(" mAllowAllRotations=" + allowAllRotationsToString(mAllowAllRotations));
877
878 pw.print(prefix + " mDemoHdmiRotation=" + Surface.rotationToString(mDemoHdmiRotation));
879 pw.print(" mDemoHdmiRotationLock=" + mDemoHdmiRotationLock);
880 pw.println(" mUndockedHdmiRotation=" + Surface.rotationToString(mUndockedHdmiRotation));
881 pw.println(prefix + " mLidOpenRotation=" + Surface.rotationToString(mLidOpenRotation));
Garfield Tanff362222018-11-14 17:52:32 -0800882 pw.println(prefix + " mFixedToUserRotation=" + mFixedToUserRotation);
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800883 }
884
885 private class OrientationListener extends WindowOrientationListener {
886 final SparseArray<Runnable> mRunnableCache = new SparseArray<>(5);
887 boolean mEnabled;
888
889 OrientationListener(Context context, Handler handler) {
890 super(context, handler);
891 }
892
893 private class UpdateRunnable implements Runnable {
894 final int mRotation;
895
896 UpdateRunnable(int rotation) {
897 mRotation = rotation;
898 }
899
900 @Override
901 public void run() {
902 // Send interaction hint to improve redraw performance.
903 mService.mPowerManagerInternal.powerHint(PowerHint.INTERACTION, 0);
904 if (isRotationChoicePossible(mCurrentAppOrientation)) {
905 final boolean isValid = isValidRotationChoice(mRotation);
906 sendProposedRotationChangeToStatusBarInternal(mRotation, isValid);
907 } else {
908 mService.updateRotation(false /* alwaysSendConfiguration */,
909 false /* forceRelayout */);
910 }
911 }
912 }
913
914 @Override
915 public void onProposedRotationChanged(int rotation) {
916 if (DEBUG_ORIENTATION) Slog.v(TAG, "onProposedRotationChanged, rotation=" + rotation);
917 Runnable r = mRunnableCache.get(rotation, null);
918 if (r == null) {
919 r = new UpdateRunnable(rotation);
920 mRunnableCache.put(rotation, r);
921 }
922 getHandler().post(r);
923 }
924
925 @Override
926 public void enable(boolean clearCurrentRotation) {
927 super.enable(clearCurrentRotation);
928 mEnabled = true;
929 if (DEBUG_ORIENTATION) Slog.v(TAG, "Enabling listeners");
930 }
931
932 @Override
933 public void disable() {
934 super.disable();
935 mEnabled = false;
936 if (DEBUG_ORIENTATION) Slog.v(TAG, "Disabling listeners");
937 }
938 }
939
940 private class SettingsObserver extends ContentObserver {
941 SettingsObserver(Handler handler) {
942 super(handler);
943 }
944
945 void observe() {
946 final ContentResolver resolver = mContext.getContentResolver();
947 resolver.registerContentObserver(Settings.Secure.getUriFor(
948 Settings.Secure.SHOW_ROTATION_SUGGESTIONS), false, this,
949 UserHandle.USER_ALL);
950 resolver.registerContentObserver(Settings.System.getUriFor(
951 Settings.System.ACCELEROMETER_ROTATION), false, this,
952 UserHandle.USER_ALL);
953 resolver.registerContentObserver(Settings.System.getUriFor(
954 Settings.System.USER_ROTATION), false, this,
955 UserHandle.USER_ALL);
956 updateSettings();
957 }
958
959 @Override
960 public void onChange(boolean selfChange) {
961 if (updateSettings()) {
962 mService.updateRotation(true /* alwaysSendConfiguration */,
963 false /* forceRelayout */);
964 }
965 }
Riddle Hsuad256a12018-07-18 16:11:30 +0800966 }
Garfield Tanff362222018-11-14 17:52:32 -0800967
968 @VisibleForTesting
969 interface ContentObserverRegister {
970 void registerContentObserver(Uri uri, boolean notifyForDescendants,
971 ContentObserver observer, @UserIdInt int userHandle);
972 }
Riddle Hsuad256a12018-07-18 16:11:30 +0800973}