blob: bef80f0a230a561297256052c53c5bcfa982d4ab [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 Hsuccf09402019-08-13 00:33:06 +080019import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
20import static android.view.WindowManager.LayoutParams.ROTATION_ANIMATION_CROSSFADE;
21import static android.view.WindowManager.LayoutParams.ROTATION_ANIMATION_JUMPCUT;
22import static android.view.WindowManager.LayoutParams.ROTATION_ANIMATION_ROTATE;
23import static android.view.WindowManager.LayoutParams.ROTATION_ANIMATION_SEAMLESS;
24
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080025import static com.android.server.policy.WindowManagerPolicy.WindowManagerFuncs.LID_OPEN;
Adrian Roosb125e0b2019-10-02 14:55:14 +020026import static com.android.server.wm.ProtoLogGroup.WM_DEBUG_ORIENTATION;
Riddle Hsuccf09402019-08-13 00:33:06 +080027import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ANIM;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080028import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
29import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Riddle Hsuccf09402019-08-13 00:33:06 +080030import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
31import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080032
Riddle Hsuccf09402019-08-13 00:33:06 +080033import android.annotation.AnimRes;
Garfield Tan7fbca052019-02-19 10:45:35 -080034import android.annotation.IntDef;
Garfield Tanff362222018-11-14 17:52:32 -080035import android.annotation.UserIdInt;
Rajeev Kumarb2ff8e82018-08-06 11:45:05 -070036import android.app.ActivityManager;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080037import android.content.ContentResolver;
Riddle Hsuad256a12018-07-18 16:11:30 +080038import android.content.Context;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080039import android.content.Intent;
Riddle Hsuad256a12018-07-18 16:11:30 +080040import android.content.pm.ActivityInfo;
Riddle Hsuccf09402019-08-13 00:33:06 +080041import android.content.pm.ActivityInfo.ScreenOrientation;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080042import android.content.pm.PackageManager;
Riddle Hsuad256a12018-07-18 16:11:30 +080043import android.content.res.Resources;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080044import android.database.ContentObserver;
45import android.hardware.power.V1_0.PowerHint;
Garfield Tanff362222018-11-14 17:52:32 -080046import android.net.Uri;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080047import android.os.Handler;
Evan Rosky69cace42019-09-20 16:28:13 -070048import android.os.RemoteException;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080049import android.os.SystemProperties;
50import android.os.UserHandle;
51import android.provider.Settings;
52import android.util.Slog;
53import android.util.SparseArray;
Evan Rosky69cace42019-09-20 16:28:13 -070054import android.view.IDisplayWindowRotationCallback;
Garfield Tana3f19032019-11-19 18:04:50 -080055import android.view.IWindowManager;
Riddle Hsuad256a12018-07-18 16:11:30 +080056import android.view.Surface;
Wale Ogunwale57946582020-03-21 14:29:07 -070057import android.window.WindowContainerTransaction;
Riddle Hsuad256a12018-07-18 16:11:30 +080058
Riddle Hsuccf09402019-08-13 00:33:06 +080059import com.android.internal.R;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080060import com.android.internal.annotations.VisibleForTesting;
Evan Rosky69cace42019-09-20 16:28:13 -070061import com.android.internal.util.function.pooled.PooledLambda;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080062import com.android.server.LocalServices;
63import com.android.server.UiThread;
Riddle Hsuad256a12018-07-18 16:11:30 +080064import com.android.server.policy.WindowManagerPolicy;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080065import com.android.server.policy.WindowOrientationListener;
Adrian Roosb125e0b2019-10-02 14:55:14 +020066import com.android.server.protolog.common.ProtoLog;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080067import com.android.server.statusbar.StatusBarManagerInternal;
Riddle Hsuad256a12018-07-18 16:11:30 +080068
69import java.io.PrintWriter;
Garfield Tan7fbca052019-02-19 10:45:35 -080070import java.lang.annotation.Retention;
71import java.lang.annotation.RetentionPolicy;
Riddle Hsuad256a12018-07-18 16:11:30 +080072
73/**
74 * Defines the mapping between orientation and rotation of a display.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080075 * Non-public methods are assumed to run inside WM lock.
Riddle Hsuad256a12018-07-18 16:11:30 +080076 */
77public class DisplayRotation {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080078 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayRotation" : TAG_WM;
Riddle Hsuad256a12018-07-18 16:11:30 +080079
Riddle Hsuccf09402019-08-13 00:33:06 +080080 private static class RotationAnimationPair {
81 @AnimRes
82 int mEnter;
83 @AnimRes
84 int mExit;
85 }
86
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080087 private final WindowManagerService mService;
Garfield Tan90c90052018-10-08 12:29:41 -070088 private final DisplayContent mDisplayContent;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080089 private final DisplayPolicy mDisplayPolicy;
Garfield Tanff362222018-11-14 17:52:32 -080090 private final DisplayWindowSettings mDisplayWindowSettings;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +080091 private final Context mContext;
92 private final Object mLock;
93
94 public final boolean isDefaultDisplay;
95 private final boolean mSupportAutoRotation;
96 private final int mLidOpenRotation;
97 private final int mCarDockRotation;
98 private final int mDeskDockRotation;
99 private final int mUndockedHdmiRotation;
Riddle Hsuccf09402019-08-13 00:33:06 +0800100 private final RotationAnimationPair mTmpRotationAnim = new RotationAnimationPair();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800101
102 private OrientationListener mOrientationListener;
103 private StatusBarManagerInternal mStatusBarManagerInternal;
104 private SettingsObserver mSettingsObserver;
105
Riddle Hsuccf09402019-08-13 00:33:06 +0800106 @ScreenOrientation
107 private int mCurrentAppOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
108
109 /**
110 * Last applied orientation of the display.
111 *
112 * @see #updateOrientationFromApp
113 */
114 @ScreenOrientation
115 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
116
117 /**
118 * Current rotation of the display.
119 *
120 * @see #updateRotationUnchecked
121 */
122 @Surface.Rotation
123 private int mRotation;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800124
125 @VisibleForTesting
Riddle Hsuad256a12018-07-18 16:11:30 +0800126 int mLandscapeRotation; // default landscape
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800127 @VisibleForTesting
Riddle Hsuad256a12018-07-18 16:11:30 +0800128 int mSeascapeRotation; // "other" landscape, 180 degrees from mLandscapeRotation
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800129 @VisibleForTesting
Riddle Hsuad256a12018-07-18 16:11:30 +0800130 int mPortraitRotation; // default portrait
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800131 @VisibleForTesting
Riddle Hsuad256a12018-07-18 16:11:30 +0800132 int mUpsideDownRotation; // "other" portrait
133
Riddle Hsuccf09402019-08-13 00:33:06 +0800134 private boolean mAllowSeamlessRotationDespiteNavBarMoving;
135
136 private int mDeferredRotationPauseCount;
137
138 /**
139 * A count of the windows which are 'seamlessly rotated', e.g. a surface at an old orientation
140 * is being transformed. We freeze orientation updates while any windows are seamlessly rotated,
141 * so we need to track when this hits zero so we can apply deferred orientation updates.
142 */
143 private int mSeamlessRotationCount;
144
145 /**
146 * True in the interval from starting seamless rotation until the last rotated window draws in
147 * the new orientation.
148 */
149 private boolean mRotatingSeamlessly;
150
151 /**
152 * Behavior of rotation suggestions.
153 *
154 * @see Settings.Secure#SHOW_ROTATION_SUGGESTIONS
155 */
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800156 private int mShowRotationSuggestions;
157
Riddle Hsuccf09402019-08-13 00:33:06 +0800158 private static final int ALLOW_ALL_ROTATIONS_UNDEFINED = -1;
159 private static final int ALLOW_ALL_ROTATIONS_DISABLED = 0;
160 private static final int ALLOW_ALL_ROTATIONS_ENABLED = 1;
161
162 @IntDef({ ALLOW_ALL_ROTATIONS_UNDEFINED, ALLOW_ALL_ROTATIONS_DISABLED,
163 ALLOW_ALL_ROTATIONS_ENABLED })
164 @Retention(RetentionPolicy.SOURCE)
165 private @interface AllowAllRotations {}
166
167 /**
168 * Whether to allow the screen to rotate to all rotations (including 180 degree) according to
169 * the sensor even when the current orientation is not
170 * {@link ActivityInfo#SCREEN_ORIENTATION_FULL_SENSOR} or
171 * {@link ActivityInfo#SCREEN_ORIENTATION_FULL_USER}.
172 */
173 @AllowAllRotations
174 private int mAllowAllRotations = ALLOW_ALL_ROTATIONS_UNDEFINED;
175
176 @WindowManagerPolicy.UserRotationMode
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800177 private int mUserRotationMode = WindowManagerPolicy.USER_ROTATION_FREE;
Riddle Hsuccf09402019-08-13 00:33:06 +0800178
179 @Surface.Rotation
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800180 private int mUserRotation = Surface.ROTATION_0;
181
Garfield Tanff362222018-11-14 17:52:32 -0800182 /**
Garfield Tan7fbca052019-02-19 10:45:35 -0800183 * Flag that indicates this is a display that may run better when fixed to user rotation.
184 */
185 private boolean mDefaultFixedToUserRotation;
186
187 /**
Garfield Tanff362222018-11-14 17:52:32 -0800188 * A flag to indicate if the display rotation should be fixed to user specified rotation
189 * regardless of all other states (including app requrested orientation). {@code true} the
190 * display rotation should be fixed to user specified rotation, {@code false} otherwise.
191 */
Garfield Tana3f19032019-11-19 18:04:50 -0800192 private int mFixedToUserRotation = IWindowManager.FIXED_TO_USER_ROTATION_DEFAULT;
Garfield Tanff362222018-11-14 17:52:32 -0800193
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800194 private int mDemoHdmiRotation;
195 private int mDemoRotation;
196 private boolean mDemoHdmiRotationLock;
197 private boolean mDemoRotationLock;
198
Evan Rosky69cace42019-09-20 16:28:13 -0700199 private static final int REMOTE_ROTATION_TIMEOUT_MS = 800;
200
201 private boolean mIsWaitingForRemoteRotation = false;
202
203 private final Runnable mDisplayRotationHandlerTimeout =
204 new Runnable() {
205 @Override
206 public void run() {
207 continueRotation(mRotation, null /* transaction */);
208 }
209 };
210
211 private final IDisplayWindowRotationCallback mRemoteRotationCallback =
212 new IDisplayWindowRotationCallback.Stub() {
213 @Override
214 public void continueRotateDisplay(int targetRotation,
215 WindowContainerTransaction t) {
216 synchronized (mService.getWindowManagerLock()) {
217 mService.mH.sendMessage(PooledLambda.obtainMessage(
218 DisplayRotation::continueRotation, DisplayRotation.this,
219 targetRotation, t));
220 }
221 }
222 };
223
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800224 DisplayRotation(WindowManagerService service, DisplayContent displayContent) {
225 this(service, displayContent, displayContent.getDisplayPolicy(),
Garfield Tanff362222018-11-14 17:52:32 -0800226 service.mDisplayWindowSettings, service.mContext, service.getWindowManagerLock());
Riddle Hsuad256a12018-07-18 16:11:30 +0800227 }
228
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800229 @VisibleForTesting
230 DisplayRotation(WindowManagerService service, DisplayContent displayContent,
Garfield Tanff362222018-11-14 17:52:32 -0800231 DisplayPolicy displayPolicy, DisplayWindowSettings displayWindowSettings,
232 Context context, Object lock) {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800233 mService = service;
Garfield Tan90c90052018-10-08 12:29:41 -0700234 mDisplayContent = displayContent;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800235 mDisplayPolicy = displayPolicy;
Garfield Tanff362222018-11-14 17:52:32 -0800236 mDisplayWindowSettings = displayWindowSettings;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800237 mContext = context;
238 mLock = lock;
239 isDefaultDisplay = displayContent.isDefaultDisplay;
Riddle Hsuad256a12018-07-18 16:11:30 +0800240
Riddle Hsuccf09402019-08-13 00:33:06 +0800241 mSupportAutoRotation =
242 mContext.getResources().getBoolean(R.bool.config_supportAutoRotation);
243 mLidOpenRotation = readRotation(R.integer.config_lidOpenRotation);
244 mCarDockRotation = readRotation(R.integer.config_carDockRotation);
245 mDeskDockRotation = readRotation(R.integer.config_deskDockRotation);
246 mUndockedHdmiRotation = readRotation(R.integer.config_undockedHdmiRotation);
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800247
248 if (isDefaultDisplay) {
249 final Handler uiHandler = UiThread.getHandler();
250 mOrientationListener = new OrientationListener(mContext, uiHandler);
Riddle Hsuccf09402019-08-13 00:33:06 +0800251 mOrientationListener.setCurrentRotation(mRotation);
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800252 mSettingsObserver = new SettingsObserver(uiHandler);
253 mSettingsObserver.observe();
254 }
255 }
256
257 private int readRotation(int resID) {
258 try {
259 final int rotation = mContext.getResources().getInteger(resID);
260 switch (rotation) {
261 case 0:
262 return Surface.ROTATION_0;
263 case 90:
264 return Surface.ROTATION_90;
265 case 180:
266 return Surface.ROTATION_180;
267 case 270:
268 return Surface.ROTATION_270;
269 }
270 } catch (Resources.NotFoundException e) {
271 // fall through
272 }
273 return -1;
274 }
275
Riddle Hsuccf09402019-08-13 00:33:06 +0800276 /**
277 * Updates the configuration which may have different values depending on current user, e.g.
278 * runtime resource overlay.
279 */
280 void updateUserDependentConfiguration(Resources currentUserRes) {
281 mAllowSeamlessRotationDespiteNavBarMoving =
282 currentUserRes.getBoolean(R.bool.config_allowSeamlessRotationDespiteNavBarMoving);
283 }
284
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800285 void configure(int width, int height, int shortSizeDp, int longSizeDp) {
Riddle Hsuad256a12018-07-18 16:11:30 +0800286 final Resources res = mContext.getResources();
287 if (width > height) {
288 mLandscapeRotation = Surface.ROTATION_0;
289 mSeascapeRotation = Surface.ROTATION_180;
Riddle Hsuccf09402019-08-13 00:33:06 +0800290 if (res.getBoolean(R.bool.config_reverseDefaultRotation)) {
Riddle Hsuad256a12018-07-18 16:11:30 +0800291 mPortraitRotation = Surface.ROTATION_90;
292 mUpsideDownRotation = Surface.ROTATION_270;
293 } else {
294 mPortraitRotation = Surface.ROTATION_270;
295 mUpsideDownRotation = Surface.ROTATION_90;
296 }
297 } else {
298 mPortraitRotation = Surface.ROTATION_0;
299 mUpsideDownRotation = Surface.ROTATION_180;
Riddle Hsuccf09402019-08-13 00:33:06 +0800300 if (res.getBoolean(R.bool.config_reverseDefaultRotation)) {
Riddle Hsuad256a12018-07-18 16:11:30 +0800301 mLandscapeRotation = Surface.ROTATION_270;
302 mSeascapeRotation = Surface.ROTATION_90;
303 } else {
304 mLandscapeRotation = Surface.ROTATION_90;
305 mSeascapeRotation = Surface.ROTATION_270;
306 }
307 }
308
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800309 // For demo purposes, allow the rotation of the HDMI display to be controlled.
310 // By default, HDMI locks rotation to landscape.
311 if ("portrait".equals(SystemProperties.get("persist.demo.hdmirotation"))) {
312 mDemoHdmiRotation = mPortraitRotation;
313 } else {
314 mDemoHdmiRotation = mLandscapeRotation;
315 }
316 mDemoHdmiRotationLock = SystemProperties.getBoolean("persist.demo.hdmirotationlock", false);
317
318 // For demo purposes, allow the rotation of the remote display to be controlled.
319 // By default, remote display locks rotation to landscape.
320 if ("portrait".equals(SystemProperties.get("persist.demo.remoterotation"))) {
321 mDemoRotation = mPortraitRotation;
322 } else {
323 mDemoRotation = mLandscapeRotation;
324 }
325 mDemoRotationLock = SystemProperties.getBoolean("persist.demo.rotationlock", false);
326
Garfield Tan12b12f7a2019-02-22 16:33:27 -0800327 // It's physically impossible to rotate the car's screen.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800328 final boolean isCar = mContext.getPackageManager().hasSystemFeature(
329 PackageManager.FEATURE_AUTOMOTIVE);
Garfield Tan12b12f7a2019-02-22 16:33:27 -0800330 // It's also not likely to rotate a TV screen.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800331 final boolean isTv = mContext.getPackageManager().hasSystemFeature(
332 PackageManager.FEATURE_LEANBACK);
Garfield Tan7fbca052019-02-19 10:45:35 -0800333 final boolean forceDesktopMode =
334 mService.mForceDesktopModeOnExternalDisplays && !isDefaultDisplay;
335 mDefaultFixedToUserRotation =
Tiger Huang86e6d072019-05-02 20:23:47 +0800336 (isCar || isTv || mService.mIsPc || forceDesktopMode)
Garfield Tan7fbca052019-02-19 10:45:35 -0800337 // For debug purposes the next line turns this feature off with:
338 // $ adb shell setprop config.override_forced_orient true
339 // $ adb shell wm size reset
340 && !"true".equals(SystemProperties.get("config.override_forced_orient"));
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800341 }
342
Riddle Hsuccf09402019-08-13 00:33:06 +0800343 void applyCurrentRotation(@Surface.Rotation int rotation) {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800344 if (mOrientationListener != null) {
345 mOrientationListener.setCurrentRotation(rotation);
346 }
347 }
348
Riddle Hsuccf09402019-08-13 00:33:06 +0800349 @VisibleForTesting
350 void setRotation(@Surface.Rotation int rotation) {
351 mRotation = rotation;
352 }
353
354 @Surface.Rotation
355 int getRotation() {
356 return mRotation;
357 }
358
359 @ScreenOrientation
360 int getLastOrientation() {
361 return mLastOrientation;
362 }
363
364 boolean updateOrientation(@ScreenOrientation int newOrientation, boolean forceUpdate) {
365 if (newOrientation == mLastOrientation && !forceUpdate) {
366 return false;
367 }
368 mLastOrientation = newOrientation;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800369 if (newOrientation != mCurrentAppOrientation) {
370 mCurrentAppOrientation = newOrientation;
371 if (isDefaultDisplay) {
372 updateOrientationListenerLw();
373 }
374 }
Riddle Hsuccf09402019-08-13 00:33:06 +0800375 return updateRotationUnchecked(forceUpdate);
376 }
377
378 /**
379 * Update rotation of the display and send configuration if the rotation is changed.
380 *
381 * @return {@code true} if the rotation has been changed and the new config is sent.
382 */
383 boolean updateRotationAndSendNewConfigIfChanged() {
384 final boolean changed = updateRotationUnchecked(false /* forceUpdate */);
385 if (changed) {
386 mDisplayContent.sendNewConfiguration();
387 }
388 return changed;
389 }
390
391 /**
392 * Update rotation with an option to force the update. This updates the container's perception
393 * of rotation and, depending on the top activities, will freeze the screen or start seamless
394 * rotation. The display itself gets rotated in {@link DisplayContent#applyRotationLocked}
395 * during {@link DisplayContent#sendNewConfiguration}.
396 *
397 * @param forceUpdate Force the rotation update. Sometimes in WM we might skip updating
398 * orientation because we're waiting for some rotation to finish or display
399 * to unfreeze, which results in configuration of the previously visible
400 * activity being applied to a newly visible one. Forcing the rotation
401 * update allows to workaround this issue.
402 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
403 * {@link DisplayContent#sendNewConfiguration} TO COMPLETE THE ROTATION AND UNFREEZE
404 * THE SCREEN.
405 */
406 boolean updateRotationUnchecked(boolean forceUpdate) {
407 final int displayId = mDisplayContent.getDisplayId();
408 if (!forceUpdate) {
409 if (mDeferredRotationPauseCount > 0) {
410 // Rotation updates have been paused temporarily. Defer the update until updates
411 // have been resumed.
Adrian Roosb125e0b2019-10-02 14:55:14 +0200412 ProtoLog.v(WM_DEBUG_ORIENTATION, "Deferring rotation, rotation is paused.");
Riddle Hsuccf09402019-08-13 00:33:06 +0800413 return false;
414 }
415
416 final ScreenRotationAnimation screenRotationAnimation =
Vadim Caenb3715832019-08-13 17:06:38 +0200417 mDisplayContent.getRotationAnimation();
Riddle Hsuccf09402019-08-13 00:33:06 +0800418 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
419 // Rotation updates cannot be performed while the previous rotation change animation
420 // is still in progress. Skip this update. We will try updating again after the
421 // animation is finished and the display is unfrozen.
Adrian Roosb125e0b2019-10-02 14:55:14 +0200422 ProtoLog.v(WM_DEBUG_ORIENTATION, "Deferring rotation, animation in progress.");
Riddle Hsuccf09402019-08-13 00:33:06 +0800423 return false;
424 }
425 if (mService.mDisplayFrozen) {
426 // Even if the screen rotation animation has finished (e.g. isAnimating returns
427 // false), there is still some time where we haven't yet unfrozen the display. We
428 // also need to abort rotation here.
Adrian Roosb125e0b2019-10-02 14:55:14 +0200429 ProtoLog.v(WM_DEBUG_ORIENTATION,
Riddle Hsuccf09402019-08-13 00:33:06 +0800430 "Deferring rotation, still finishing previous rotation");
431 return false;
432 }
433 }
434
435 if (!mService.mDisplayEnabled) {
436 // No point choosing a rotation if the display is not enabled.
Adrian Roosb125e0b2019-10-02 14:55:14 +0200437 ProtoLog.v(WM_DEBUG_ORIENTATION, "Deferring rotation, display is not enabled.");
Riddle Hsuccf09402019-08-13 00:33:06 +0800438 return false;
439 }
440
441 final int oldRotation = mRotation;
442 final int lastOrientation = mLastOrientation;
443 final int rotation = rotationForOrientation(lastOrientation, oldRotation);
Adrian Roosb125e0b2019-10-02 14:55:14 +0200444 ProtoLog.v(WM_DEBUG_ORIENTATION,
Vadim Caen1096f112019-12-18 11:44:26 +0100445 "Computed rotation=%s (%d) for display id=%d based on lastOrientation=%s (%d) and "
446 + "oldRotation=%s (%d)",
447 Surface.rotationToString(rotation), rotation,
448 displayId,
449 ActivityInfo.screenOrientationToString(lastOrientation), lastOrientation,
450 Surface.rotationToString(oldRotation), oldRotation);
Riddle Hsuccf09402019-08-13 00:33:06 +0800451
Adrian Roosb125e0b2019-10-02 14:55:14 +0200452 ProtoLog.v(WM_DEBUG_ORIENTATION,
Vadim Caen1096f112019-12-18 11:44:26 +0100453 "Display id=%d selected orientation %s (%d), got rotation %s (%d)", displayId,
454 ActivityInfo.screenOrientationToString(lastOrientation), lastOrientation,
455 Surface.rotationToString(rotation), rotation);
Riddle Hsuccf09402019-08-13 00:33:06 +0800456
457 if (oldRotation == rotation) {
458 // No change.
459 return false;
460 }
461
Adrian Roosb125e0b2019-10-02 14:55:14 +0200462 ProtoLog.v(WM_DEBUG_ORIENTATION,
463 "Display id=%d rotation changed to %d from %d, lastOrientation=%d",
464 displayId, rotation, oldRotation, lastOrientation);
Riddle Hsuccf09402019-08-13 00:33:06 +0800465
466 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
467 mDisplayContent.mWaitingForConfig = true;
468 }
469
470 mRotation = rotation;
471
472 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
473 mService.mH.sendNewMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
474 mDisplayContent, WINDOW_FREEZE_TIMEOUT_DURATION);
475
476 mDisplayContent.setLayoutNeeded();
477
478 if (shouldRotateSeamlessly(oldRotation, rotation, forceUpdate)) {
479 // The screen rotation animation uses a screenshot to freeze the screen while windows
480 // resize underneath. When we are rotating seamlessly, we allow the elements to
481 // transition to their rotated state independently and without a freeze required.
482 prepareSeamlessRotation();
483 } else {
484 prepareNormalRotationAnimation();
485 }
486
Riddle Hsu06aeb062020-03-24 23:38:40 +0800487 // Give a remote handler (system ui) some time to reposition things.
488 startRemoteRotation(oldRotation, mRotation);
Evan Rosky69cace42019-09-20 16:28:13 -0700489
Riddle Hsuccf09402019-08-13 00:33:06 +0800490 return true;
491 }
492
Evan Rosky69cace42019-09-20 16:28:13 -0700493 /**
494 * A Remote rotation is when we are waiting for some registered (remote)
495 * {@link IDisplayWindowRotationController} to calculate and return some hierarchy operations
496 * to perform in sync with the rotation.
497 */
498 boolean isWaitingForRemoteRotation() {
499 return mIsWaitingForRemoteRotation;
500 }
501
502 private void startRemoteRotation(int fromRotation, int toRotation) {
503 if (mService.mDisplayRotationController == null) {
504 return;
505 }
506 mIsWaitingForRemoteRotation = true;
507 try {
508 mService.mDisplayRotationController.onRotateDisplay(mDisplayContent.getDisplayId(),
509 fromRotation, toRotation, mRemoteRotationCallback);
510 mService.mH.removeCallbacks(mDisplayRotationHandlerTimeout);
511 mService.mH.postDelayed(mDisplayRotationHandlerTimeout, REMOTE_ROTATION_TIMEOUT_MS);
512 } catch (RemoteException e) {
513 mIsWaitingForRemoteRotation = false;
514 return;
515 }
516 }
517
518 private void continueRotation(int targetRotation, WindowContainerTransaction t) {
519 synchronized (mService.mGlobalLock) {
520 if (targetRotation != mRotation || !mIsWaitingForRemoteRotation) {
521 // Drop it, this is either coming from an outdated remote rotation; or, we've
522 // already moved on.
523 return;
524 }
525 mService.mH.removeCallbacks(mDisplayRotationHandlerTimeout);
526 mIsWaitingForRemoteRotation = false;
Evan Roskyaf9f27c2020-02-18 18:58:35 +0000527 mService.mAtmService.deferWindowLayout();
528 try {
529 mDisplayContent.sendNewConfiguration();
530 if (t != null) {
Wale Ogunwale568f9f412020-03-21 22:27:35 -0700531 mService.mAtmService.mWindowOrganizerController.applyTransaction(t);
Evan Roskyaf9f27c2020-02-18 18:58:35 +0000532 }
533 } finally {
534 mService.mAtmService.continueWindowLayout();
Robert Carre10ee3d2019-11-11 15:03:15 -0800535 }
Evan Rosky69cace42019-09-20 16:28:13 -0700536 }
537 }
538
Riddle Hsuccf09402019-08-13 00:33:06 +0800539 void prepareNormalRotationAnimation() {
540 final RotationAnimationPair anim = selectRotationAnimation();
541 mService.startFreezingDisplayLocked(anim.mExit, anim.mEnter, mDisplayContent);
542 }
543
544 private void prepareSeamlessRotation() {
545 // We are careful to reset this in case a window was removed before it finished
546 // seamless rotation.
547 mSeamlessRotationCount = 0;
548 mRotatingSeamlessly = true;
549 }
550
551 boolean isRotatingSeamlessly() {
552 return mRotatingSeamlessly;
553 }
554
555 @VisibleForTesting
556 boolean shouldRotateSeamlessly(int oldRotation, int newRotation, boolean forceUpdate) {
Riddle Hsu6f548e92020-01-13 13:34:09 +0800557 // Display doesn't need to be frozen because application has been started in correct
558 // rotation already, so the rest of the windows can use seamless rotation.
Riddle Hsu9239d7c62020-02-20 01:35:56 +0800559 if (mDisplayContent.mFixedRotationLaunchingApp != null) {
Riddle Hsu6f548e92020-01-13 13:34:09 +0800560 return true;
561 }
Riddle Hsu9239d7c62020-02-20 01:35:56 +0800562
563 final WindowState w = mDisplayPolicy.getTopFullscreenOpaqueWindow();
564 if (w == null || w != mDisplayContent.mCurrentFocus) {
Riddle Hsuccf09402019-08-13 00:33:06 +0800565 return false;
566 }
567 // We only enable seamless rotation if the top window has requested it and is in the
568 // fullscreen opaque state. Seamless rotation requires freezing various Surface states and
569 // won't work well with animations, so we disable it in the animation case for now.
570 if (w.getAttrs().rotationAnimation != ROTATION_ANIMATION_SEAMLESS || w.isAnimatingLw()) {
571 return false;
572 }
573
574 // For the upside down rotation we don't rotate seamlessly as the navigation bar moves
575 // position. Note most apps (using orientation:sensor or user as opposed to fullSensor)
576 // will not enter the reverse portrait orientation, so actually the orientation won't change
577 // at all.
578 if (oldRotation == mUpsideDownRotation || newRotation == mUpsideDownRotation) {
579 return false;
580 }
581
582 // If the navigation bar can't change sides, then it will jump when we change orientations
583 // and we don't rotate seamlessly - unless that is allowed, eg. with gesture navigation
584 // where the navbar is low-profile enough that this isn't very noticeable.
585 if (!mAllowSeamlessRotationDespiteNavBarMoving && !mDisplayPolicy.navigationBarCanMove()) {
586 return false;
587 }
588
589 // If the bounds of activity window is different from its parent, then reject to be seamless
590 // because the window position may change after rotation that will look like a sudden jump.
Garfield Tane8d84ab2019-10-11 09:49:40 -0700591 if (w.mActivityRecord != null && !w.mActivityRecord.matchParentBounds()) {
Riddle Hsuccf09402019-08-13 00:33:06 +0800592 return false;
593 }
594
595 // In the presence of the PINNED stack or System Alert windows we unfortunately can not
596 // seamlessly rotate.
Andrii Kulian4c0fd0d2020-03-29 13:32:14 -0700597 if (mDisplayContent.getDefaultTaskDisplayArea().hasPinnedTask()
598 || mDisplayContent.hasAlertWindowSurfaces()) {
Riddle Hsuccf09402019-08-13 00:33:06 +0800599 return false;
600 }
601
602 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation to
603 // complete (that is, waiting for windows to redraw). It's tempting to check
604 // mSeamlessRotationCount but that could be incorrect in the case of window-removal.
605 if (!forceUpdate && mDisplayContent.getWindow(win -> win.mSeamlesslyRotated) != null) {
606 return false;
607 }
608
609 return true;
610 }
611
612 void markForSeamlessRotation(WindowState w, boolean seamlesslyRotated) {
613 if (seamlesslyRotated == w.mSeamlesslyRotated || w.mForceSeamlesslyRotate) {
614 return;
615 }
616
617 w.mSeamlesslyRotated = seamlesslyRotated;
618 if (seamlesslyRotated) {
619 mSeamlessRotationCount++;
620 } else {
621 mSeamlessRotationCount--;
622 }
623 if (mSeamlessRotationCount == 0) {
Adrian Roosb125e0b2019-10-02 14:55:14 +0200624 ProtoLog.i(WM_DEBUG_ORIENTATION,
625 "Performing post-rotate rotation after seamless rotation");
Riddle Hsuccf09402019-08-13 00:33:06 +0800626 // Finish seamless rotation.
627 mRotatingSeamlessly = false;
628
629 updateRotationAndSendNewConfigIfChanged();
630 }
631 }
632
633 void onSeamlessRotationTimeout() {
634 final boolean[] isLayoutNeeded = { false };
635
636 mDisplayContent.forAllWindows(w -> {
637 if (!w.mSeamlesslyRotated) {
638 return;
639 }
640 isLayoutNeeded[0] = true;
641 w.setDisplayLayoutNeeded();
642 w.finishSeamlessRotation(true /* timeout */);
643 markForSeamlessRotation(w, false /* seamlesslyRotated */);
644 }, true /* traverseTopToBottom */);
645
646 if (isLayoutNeeded[0]) {
647 mService.mWindowPlacerLocked.performSurfacePlacement();
648 }
649 }
650
651 /**
652 * Returns the animation to run for a rotation transition based on the top fullscreen windows
653 * {@link android.view.WindowManager.LayoutParams#rotationAnimation} and whether it is currently
654 * fullscreen and frontmost.
655 */
656 private RotationAnimationPair selectRotationAnimation() {
657 // If the screen is off or non-interactive, force a jumpcut.
658 final boolean forceJumpcut = !mDisplayPolicy.isScreenOnFully()
659 || !mService.mPolicy.okToAnimate();
660 final WindowState topFullscreen = mDisplayPolicy.getTopFullscreenOpaqueWindow();
661 if (DEBUG_ANIM) Slog.i(TAG, "selectRotationAnimation topFullscreen="
662 + topFullscreen + " rotationAnimation="
663 + (topFullscreen == null ? 0 : topFullscreen.getAttrs().rotationAnimation)
664 + " forceJumpcut=" + forceJumpcut);
665 if (forceJumpcut) {
666 mTmpRotationAnim.mExit = R.anim.rotation_animation_jump_exit;
667 mTmpRotationAnim.mEnter = R.anim.rotation_animation_enter;
668 return mTmpRotationAnim;
669 }
670 if (topFullscreen != null) {
671 int animationHint = topFullscreen.getRotationAnimationHint();
672 if (animationHint < 0 && mDisplayPolicy.isTopLayoutFullscreen()) {
673 animationHint = topFullscreen.getAttrs().rotationAnimation;
674 }
675 switch (animationHint) {
676 case ROTATION_ANIMATION_CROSSFADE:
677 case ROTATION_ANIMATION_SEAMLESS: // Crossfade is fallback for seamless.
678 mTmpRotationAnim.mExit = R.anim.rotation_animation_xfade_exit;
679 mTmpRotationAnim.mEnter = R.anim.rotation_animation_enter;
680 break;
681 case ROTATION_ANIMATION_JUMPCUT:
682 mTmpRotationAnim.mExit = R.anim.rotation_animation_jump_exit;
683 mTmpRotationAnim.mEnter = R.anim.rotation_animation_enter;
684 break;
685 case ROTATION_ANIMATION_ROTATE:
686 default:
687 mTmpRotationAnim.mExit = mTmpRotationAnim.mEnter = 0;
688 break;
689 }
690 } else {
691 mTmpRotationAnim.mExit = mTmpRotationAnim.mEnter = 0;
692 }
693 return mTmpRotationAnim;
694 }
695
696 /**
697 * Validate whether the current top fullscreen has specified the same
698 * {@link android.view.WindowManager.LayoutParams#rotationAnimation} value as that being passed
699 * in from the previous top fullscreen window.
700 *
701 * @param exitAnimId exiting resource id from the previous window.
702 * @param enterAnimId entering resource id from the previous window.
703 * @param forceDefault For rotation animations only, if true ignore the animation values and
704 * just return false.
705 * @return {@code true} if the previous values are still valid, false if they should be replaced
706 * with the default.
707 */
708 boolean validateRotationAnimation(int exitAnimId, int enterAnimId, boolean forceDefault) {
709 switch (exitAnimId) {
710 case R.anim.rotation_animation_xfade_exit:
711 case R.anim.rotation_animation_jump_exit:
712 // These are the only cases that matter.
713 if (forceDefault) {
714 return false;
715 }
716 final RotationAnimationPair anim = selectRotationAnimation();
717 return exitAnimId == anim.mExit && enterAnimId == anim.mEnter;
718 default:
719 return true;
720 }
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800721 }
722
Garfield Tana3f19032019-11-19 18:04:50 -0800723 void restoreSettings(int userRotationMode, int userRotation, int fixedToUserRotation) {
Garfield Tanff362222018-11-14 17:52:32 -0800724 mFixedToUserRotation = fixedToUserRotation;
725
726 // We will retrieve user rotation and user rotation mode from settings for default display.
727 if (isDefaultDisplay) {
728 return;
729 }
Garfield Tan90c90052018-10-08 12:29:41 -0700730 if (userRotationMode != WindowManagerPolicy.USER_ROTATION_FREE
731 && userRotationMode != WindowManagerPolicy.USER_ROTATION_LOCKED) {
732 Slog.w(TAG, "Trying to restore an invalid user rotation mode " + userRotationMode
733 + " for " + mDisplayContent);
734 userRotationMode = WindowManagerPolicy.USER_ROTATION_FREE;
735 }
736 if (userRotation < Surface.ROTATION_0 || userRotation > Surface.ROTATION_270) {
737 Slog.w(TAG, "Trying to restore an invalid user rotation " + userRotation
738 + " for " + mDisplayContent);
739 userRotation = Surface.ROTATION_0;
740 }
741 mUserRotationMode = userRotationMode;
742 mUserRotation = userRotation;
743 }
744
Garfield Tana3f19032019-11-19 18:04:50 -0800745 void setFixedToUserRotation(int fixedToUserRotation) {
Garfield Tanff362222018-11-14 17:52:32 -0800746 if (mFixedToUserRotation == fixedToUserRotation) {
747 return;
748 }
749
750 mFixedToUserRotation = fixedToUserRotation;
Garfield Tan7fbca052019-02-19 10:45:35 -0800751 mDisplayWindowSettings.setFixedToUserRotation(mDisplayContent, fixedToUserRotation);
Garfield Tanff362222018-11-14 17:52:32 -0800752 mService.updateRotation(true /* alwaysSendConfiguration */,
753 false /* forceRelayout */);
754 }
755
Wale Ogunwale8a1860a2019-06-05 08:57:19 -0700756 @VisibleForTesting
757 void setUserRotation(int userRotationMode, int userRotation) {
Garfield Tan90c90052018-10-08 12:29:41 -0700758 if (isDefaultDisplay) {
759 // We'll be notified via settings listener, so we don't need to update internal values.
760 final ContentResolver res = mContext.getContentResolver();
761 final int accelerometerRotation =
762 userRotationMode == WindowManagerPolicy.USER_ROTATION_LOCKED ? 0 : 1;
763 Settings.System.putIntForUser(res, Settings.System.ACCELEROMETER_ROTATION,
764 accelerometerRotation, UserHandle.USER_CURRENT);
765 Settings.System.putIntForUser(res, Settings.System.USER_ROTATION, userRotation,
766 UserHandle.USER_CURRENT);
767 return;
768 }
769
770 boolean changed = false;
771 if (mUserRotationMode != userRotationMode) {
772 mUserRotationMode = userRotationMode;
773 changed = true;
774 }
775 if (mUserRotation != userRotation) {
776 mUserRotation = userRotation;
777 changed = true;
778 }
Garfield Tanff362222018-11-14 17:52:32 -0800779 mDisplayWindowSettings.setUserRotation(mDisplayContent, userRotationMode,
Chilun8753ad32018-10-09 15:56:45 +0800780 userRotation);
Garfield Tan90c90052018-10-08 12:29:41 -0700781 if (changed) {
782 mService.updateRotation(true /* alwaysSendConfiguration */,
783 false /* forceRelayout */);
Garfield Tan90c90052018-10-08 12:29:41 -0700784 }
785 }
786
787 void freezeRotation(int rotation) {
Riddle Hsuccf09402019-08-13 00:33:06 +0800788 rotation = (rotation == -1) ? mRotation : rotation;
Garfield Tan90c90052018-10-08 12:29:41 -0700789 setUserRotation(WindowManagerPolicy.USER_ROTATION_LOCKED, rotation);
790 }
791
792 void thawRotation() {
793 setUserRotation(WindowManagerPolicy.USER_ROTATION_FREE, mUserRotation);
794 }
795
796 boolean isRotationFrozen() {
797 if (!isDefaultDisplay) {
798 return mUserRotationMode == WindowManagerPolicy.USER_ROTATION_LOCKED;
799 }
800
801 return Settings.System.getIntForUser(mContext.getContentResolver(),
802 Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT) == 0;
803 }
804
Garfield Tanff362222018-11-14 17:52:32 -0800805 boolean isFixedToUserRotation() {
Garfield Tan7fbca052019-02-19 10:45:35 -0800806 switch (mFixedToUserRotation) {
Garfield Tana3f19032019-11-19 18:04:50 -0800807 case IWindowManager.FIXED_TO_USER_ROTATION_DISABLED:
Garfield Tan7fbca052019-02-19 10:45:35 -0800808 return false;
Garfield Tana3f19032019-11-19 18:04:50 -0800809 case IWindowManager.FIXED_TO_USER_ROTATION_ENABLED:
Garfield Tan7fbca052019-02-19 10:45:35 -0800810 return true;
811 default:
812 return mDefaultFixedToUserRotation;
813 }
Riddle Hsuad256a12018-07-18 16:11:30 +0800814 }
815
Garfield Tan49dae102019-02-04 09:51:59 -0800816 /**
817 * Returns {@code true} if this display rotation takes app requested orientation into
818 * consideration; {@code false} otherwise. For the time being the only case where this is {@code
819 * false} is when {@link #isFixedToUserRotation()} is {@code true}.
820 */
821 boolean respectAppRequestedOrientation() {
Garfield Tan7fbca052019-02-19 10:45:35 -0800822 return !isFixedToUserRotation();
Garfield Tan49dae102019-02-04 09:51:59 -0800823 }
824
Riddle Hsuad256a12018-07-18 16:11:30 +0800825 public int getLandscapeRotation() {
826 return mLandscapeRotation;
827 }
828
829 public int getSeascapeRotation() {
830 return mSeascapeRotation;
831 }
832
833 public int getPortraitRotation() {
834 return mPortraitRotation;
835 }
836
837 public int getUpsideDownRotation() {
838 return mUpsideDownRotation;
839 }
840
841 public int getCurrentAppOrientation() {
842 return mCurrentAppOrientation;
843 }
844
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800845 public DisplayPolicy getDisplayPolicy() {
846 return mDisplayPolicy;
Riddle Hsuad256a12018-07-18 16:11:30 +0800847 }
848
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800849 public WindowOrientationListener getOrientationListener() {
850 return mOrientationListener;
Riddle Hsuad256a12018-07-18 16:11:30 +0800851 }
852
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800853 public int getUserRotation() {
854 return mUserRotation;
855 }
856
857 public int getUserRotationMode() {
858 return mUserRotationMode;
859 }
860
861 public void updateOrientationListener() {
862 synchronized (mLock) {
863 updateOrientationListenerLw();
Riddle Hsuad256a12018-07-18 16:11:30 +0800864 }
865 }
866
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800867 /**
Riddle Hsuccf09402019-08-13 00:33:06 +0800868 * Temporarily pauses rotation changes until resumed.
869 * <p>
870 * This can be used to prevent rotation changes from occurring while the user is performing
871 * certain operations, such as drag and drop.
872 * <p>
873 * This call nests and must be matched by an equal number of calls to {@link #resume}.
874 */
875 void pause() {
876 mDeferredRotationPauseCount++;
877 }
878
879 /** Resumes normal rotation changes after being paused. */
880 void resume() {
881 if (mDeferredRotationPauseCount <= 0) {
882 return;
883 }
884
885 mDeferredRotationPauseCount--;
886 if (mDeferredRotationPauseCount == 0) {
887 updateRotationAndSendNewConfigIfChanged();
888 }
889 }
890
891 /**
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800892 * Various use cases for invoking this function:
893 * <li>Screen turning off, should always disable listeners if already enabled.</li>
894 * <li>Screen turned on and current app has sensor based orientation, enable listeners
895 * if not already enabled.</li>
896 * <li>Screen turned on and current app does not have sensor orientation, disable listeners
897 * if already enabled.</li>
898 * <li>Screen turning on and current app has sensor based orientation, enable listeners
899 * if needed.</li>
900 * <li>screen turning on and current app has nosensor based orientation, do nothing.</li>
901 */
902 private void updateOrientationListenerLw() {
903 if (mOrientationListener == null || !mOrientationListener.canDetectOrientation()) {
904 // If sensor is turned off or nonexistent for some reason.
905 return;
906 }
907
908 final boolean screenOnEarly = mDisplayPolicy.isScreenOnEarly();
909 final boolean awake = mDisplayPolicy.isAwake();
910 final boolean keyguardDrawComplete = mDisplayPolicy.isKeyguardDrawComplete();
911 final boolean windowManagerDrawComplete = mDisplayPolicy.isWindowManagerDrawComplete();
912
913 // Could have been invoked due to screen turning on or off or
914 // change of the currently visible window's orientation.
Adrian Roosb125e0b2019-10-02 14:55:14 +0200915 ProtoLog.v(WM_DEBUG_ORIENTATION,
916 "screenOnEarly=%b, awake=%b, currentAppOrientation=%d, "
917 + "orientationSensorEnabled=%b, keyguardDrawComplete=%b, "
918 + "windowManagerDrawComplete=%b",
919 screenOnEarly, awake, mCurrentAppOrientation, mOrientationListener.mEnabled,
920 keyguardDrawComplete, windowManagerDrawComplete);
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800921
922 boolean disable = true;
923 // Note: We postpone the rotating of the screen until the keyguard as well as the
924 // window manager have reported a draw complete or the keyguard is going away in dismiss
925 // mode.
926 if (screenOnEarly && awake && ((keyguardDrawComplete && windowManagerDrawComplete))) {
927 if (needSensorRunning()) {
928 disable = false;
929 // Enable listener if not already enabled.
930 if (!mOrientationListener.mEnabled) {
931 // Don't clear the current sensor orientation if the keyguard is going away in
932 // dismiss mode. This allows window manager to use the last sensor reading to
933 // determine the orientation vs. falling back to the last known orientation if
934 // the sensor reading was cleared which can cause it to relaunch the app that
935 // will show in the wrong orientation first before correcting leading to app
936 // launch delays.
937 mOrientationListener.enable(true /* clearCurrentRotation */);
938 }
Riddle Hsuad256a12018-07-18 16:11:30 +0800939 }
940 }
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800941 // Check if sensors need to be disabled.
942 if (disable && mOrientationListener.mEnabled) {
943 mOrientationListener.disable();
944 }
Riddle Hsuad256a12018-07-18 16:11:30 +0800945 }
946
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800947 /**
948 * We always let the sensor be switched on by default except when
949 * the user has explicitly disabled sensor based rotation or when the
950 * screen is switched off.
951 */
952 private boolean needSensorRunning() {
Garfield Tan7fbca052019-02-19 10:45:35 -0800953 if (isFixedToUserRotation()) {
Garfield Tanff362222018-11-14 17:52:32 -0800954 // We are sure we only respect user rotation settings, so we are sure we will not
955 // support sensor rotation.
956 return false;
957 }
958
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800959 if (mSupportAutoRotation) {
960 if (mCurrentAppOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR
961 || mCurrentAppOrientation == ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR
962 || mCurrentAppOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT
963 || mCurrentAppOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE) {
964 // If the application has explicitly requested to follow the
965 // orientation, then we need to turn the sensor on.
966 return true;
967 }
968 }
969
970 final int dockMode = mDisplayPolicy.getDockMode();
971 if ((mDisplayPolicy.isCarDockEnablesAccelerometer()
972 && dockMode == Intent.EXTRA_DOCK_STATE_CAR)
973 || (mDisplayPolicy.isDeskDockEnablesAccelerometer()
974 && (dockMode == Intent.EXTRA_DOCK_STATE_DESK
975 || dockMode == Intent.EXTRA_DOCK_STATE_LE_DESK
976 || dockMode == Intent.EXTRA_DOCK_STATE_HE_DESK))) {
977 // Enable accelerometer if we are docked in a dock that enables accelerometer
978 // orientation management.
979 return true;
980 }
981
982 if (mUserRotationMode == WindowManagerPolicy.USER_ROTATION_LOCKED) {
983 // If the setting for using the sensor by default is enabled, then
984 // we will always leave it on. Note that the user could go to
985 // a window that forces an orientation that does not use the
986 // sensor and in theory we could turn it off... however, when next
987 // turning it on we won't have a good value for the current
988 // orientation for a little bit, which can cause orientation
989 // changes to lag, so we'd like to keep it always on. (It will
990 // still be turned off when the screen is off.)
991
992 // When locked we can provide rotation suggestions users can approve to change the
993 // current screen rotation. To do this the sensor needs to be running.
994 return mSupportAutoRotation &&
995 mShowRotationSuggestions == Settings.Secure.SHOW_ROTATION_SUGGESTIONS_ENABLED;
996 }
997 return mSupportAutoRotation;
998 }
999
1000 /**
Riddle Hsuccf09402019-08-13 00:33:06 +08001001 * If this is true we have updated our desired orientation, but not yet changed the real
1002 * orientation our applied our screen rotation animation. For example, because a previous
1003 * screen rotation was in progress.
1004 *
1005 * @return {@code true} if the there is an ongoing rotation change.
1006 */
1007 boolean needsUpdate() {
1008 final int oldRotation = mRotation;
1009 final int rotation = rotationForOrientation(mLastOrientation, oldRotation);
1010 return oldRotation != rotation;
1011 }
1012
1013 /**
1014 * Given an orientation constant, returns the appropriate surface rotation, taking into account
1015 * sensors, docking mode, rotation lock, and other factors.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001016 *
Vadim Caenfc14c662020-01-20 16:00:31 +01001017 * @param orientation An orientation constant, such as
1018 * {@link ActivityInfo#SCREEN_ORIENTATION_LANDSCAPE}.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001019 * @param lastRotation The most recently used rotation.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001020 * @return The surface rotation to use.
1021 */
Riddle Hsuccf09402019-08-13 00:33:06 +08001022 @VisibleForTesting
Vadim Caenfc14c662020-01-20 16:00:31 +01001023 @Surface.Rotation
1024 int rotationForOrientation(@ScreenOrientation int orientation,
1025 @Surface.Rotation int lastRotation) {
1026 ProtoLog.v(WM_DEBUG_ORIENTATION,
1027 "rotationForOrientation(orient=%s (%d), last=%s (%d)); user=%s (%d) %s",
1028 ActivityInfo.screenOrientationToString(orientation), orientation,
1029 Surface.rotationToString(lastRotation), lastRotation,
1030 Surface.rotationToString(mUserRotation), mUserRotation,
1031 mUserRotationMode == WindowManagerPolicy.USER_ROTATION_LOCKED
1032 ? "USER_ROTATION_LOCKED" : "");
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001033
Garfield Tan7fbca052019-02-19 10:45:35 -08001034 if (isFixedToUserRotation()) {
Garfield Tanff362222018-11-14 17:52:32 -08001035 return mUserRotation;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001036 }
1037
1038 int sensorRotation = mOrientationListener != null
1039 ? mOrientationListener.getProposedRotation() // may be -1
1040 : -1;
1041 if (sensorRotation < 0) {
1042 sensorRotation = lastRotation;
1043 }
1044
1045 final int lidState = mDisplayPolicy.getLidState();
1046 final int dockMode = mDisplayPolicy.getDockMode();
1047 final boolean hdmiPlugged = mDisplayPolicy.isHdmiPlugged();
1048 final boolean carDockEnablesAccelerometer =
1049 mDisplayPolicy.isCarDockEnablesAccelerometer();
1050 final boolean deskDockEnablesAccelerometer =
1051 mDisplayPolicy.isDeskDockEnablesAccelerometer();
1052
1053 final int preferredRotation;
1054 if (!isDefaultDisplay) {
1055 // For secondary displays we ignore things like displays sensors, docking mode and
Garfield Tan90c90052018-10-08 12:29:41 -07001056 // rotation lock, and always prefer user rotation.
1057 preferredRotation = mUserRotation;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001058 } else if (lidState == LID_OPEN && mLidOpenRotation >= 0) {
1059 // Ignore sensor when lid switch is open and rotation is forced.
1060 preferredRotation = mLidOpenRotation;
1061 } else if (dockMode == Intent.EXTRA_DOCK_STATE_CAR
1062 && (carDockEnablesAccelerometer || mCarDockRotation >= 0)) {
1063 // Ignore sensor when in car dock unless explicitly enabled.
1064 // This case can override the behavior of NOSENSOR, and can also
1065 // enable 180 degree rotation while docked.
1066 preferredRotation = carDockEnablesAccelerometer ? sensorRotation : mCarDockRotation;
1067 } else if ((dockMode == Intent.EXTRA_DOCK_STATE_DESK
1068 || dockMode == Intent.EXTRA_DOCK_STATE_LE_DESK
1069 || dockMode == Intent.EXTRA_DOCK_STATE_HE_DESK)
1070 && (deskDockEnablesAccelerometer || mDeskDockRotation >= 0)) {
1071 // Ignore sensor when in desk dock unless explicitly enabled.
1072 // This case can override the behavior of NOSENSOR, and can also
1073 // enable 180 degree rotation while docked.
1074 preferredRotation = deskDockEnablesAccelerometer ? sensorRotation : mDeskDockRotation;
1075 } else if (hdmiPlugged && mDemoHdmiRotationLock) {
1076 // Ignore sensor when plugged into HDMI when demo HDMI rotation lock enabled.
1077 // Note that the dock orientation overrides the HDMI orientation.
1078 preferredRotation = mDemoHdmiRotation;
1079 } else if (hdmiPlugged && dockMode == Intent.EXTRA_DOCK_STATE_UNDOCKED
1080 && mUndockedHdmiRotation >= 0) {
1081 // Ignore sensor when plugged into HDMI and an undocked orientation has
1082 // been specified in the configuration (only for legacy devices without
1083 // full multi-display support).
1084 // Note that the dock orientation overrides the HDMI orientation.
1085 preferredRotation = mUndockedHdmiRotation;
1086 } else if (mDemoRotationLock) {
1087 // Ignore sensor when demo rotation lock is enabled.
1088 // Note that the dock orientation and HDMI rotation lock override this.
1089 preferredRotation = mDemoRotation;
1090 } else if (mDisplayPolicy.isPersistentVrModeEnabled()) {
1091 // While in VR, apps always prefer a portrait rotation. This does not change
1092 // any apps that explicitly set landscape, but does cause sensors be ignored,
1093 // and ignored any orientation lock that the user has set (this conditional
1094 // should remain above the ORIENTATION_LOCKED conditional below).
1095 preferredRotation = mPortraitRotation;
1096 } else if (orientation == ActivityInfo.SCREEN_ORIENTATION_LOCKED) {
1097 // Application just wants to remain locked in the last rotation.
1098 preferredRotation = lastRotation;
1099 } else if (!mSupportAutoRotation) {
1100 // If we don't support auto-rotation then bail out here and ignore
1101 // the sensor and any rotation lock settings.
1102 preferredRotation = -1;
1103 } else if ((mUserRotationMode == WindowManagerPolicy.USER_ROTATION_FREE
1104 && (orientation == ActivityInfo.SCREEN_ORIENTATION_USER
1105 || orientation == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
1106 || orientation == ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE
1107 || orientation == ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
1108 || orientation == ActivityInfo.SCREEN_ORIENTATION_FULL_USER))
1109 || orientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR
1110 || orientation == ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR
1111 || orientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
1112 || orientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT) {
1113 // Otherwise, use sensor only if requested by the application or enabled
1114 // by default for USER or UNSPECIFIED modes. Does not apply to NOSENSOR.
Riddle Hsuccf09402019-08-13 00:33:06 +08001115 if (mAllowAllRotations == ALLOW_ALL_ROTATIONS_UNDEFINED) {
1116 // Can't read this during init() because the context doesn't have display metrics at
1117 // that time so we cannot determine tablet vs. phone then.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001118 mAllowAllRotations = mContext.getResources().getBoolean(
Riddle Hsuccf09402019-08-13 00:33:06 +08001119 R.bool.config_allowAllRotations)
1120 ? ALLOW_ALL_ROTATIONS_ENABLED
1121 : ALLOW_ALL_ROTATIONS_DISABLED;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001122 }
1123 if (sensorRotation != Surface.ROTATION_180
Riddle Hsuccf09402019-08-13 00:33:06 +08001124 || mAllowAllRotations == ALLOW_ALL_ROTATIONS_ENABLED
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001125 || orientation == ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR
1126 || orientation == ActivityInfo.SCREEN_ORIENTATION_FULL_USER) {
1127 preferredRotation = sensorRotation;
1128 } else {
1129 preferredRotation = lastRotation;
1130 }
1131 } else if (mUserRotationMode == WindowManagerPolicy.USER_ROTATION_LOCKED
Tetsutoki Shiozawa3e595fe2019-05-07 18:27:25 +09001132 && orientation != ActivityInfo.SCREEN_ORIENTATION_NOSENSOR
1133 && orientation != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
1134 && orientation != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
1135 && orientation != ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
1136 && orientation != ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) {
1137 // Apply rotation lock. Does not apply to NOSENSOR or specific rotations.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001138 // The idea is that the user rotation expresses a weak preference for the direction
1139 // of gravity and as NOSENSOR is never affected by gravity, then neither should
1140 // NOSENSOR be affected by rotation lock (although it will be affected by docks).
Tetsutoki Shiozawa3e595fe2019-05-07 18:27:25 +09001141 // Also avoid setting user rotation when app has preference over one particular rotation
1142 // to avoid leaving the rotation to the reverse of it which has the compatible
1143 // orientation, but isn't what app wants, when the user rotation is the reverse of the
1144 // preferred rotation.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001145 preferredRotation = mUserRotation;
1146 } else {
1147 // No overriding preference.
1148 // We will do exactly what the application asked us to do.
1149 preferredRotation = -1;
1150 }
1151
Riddle Hsuad256a12018-07-18 16:11:30 +08001152 switch (orientation) {
1153 case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
1154 // Return portrait unless overridden.
1155 if (isAnyPortrait(preferredRotation)) {
1156 return preferredRotation;
1157 }
1158 return mPortraitRotation;
1159
1160 case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
1161 // Return landscape unless overridden.
1162 if (isLandscapeOrSeascape(preferredRotation)) {
1163 return preferredRotation;
1164 }
1165 return mLandscapeRotation;
1166
1167 case ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT:
1168 // Return reverse portrait unless overridden.
1169 if (isAnyPortrait(preferredRotation)) {
1170 return preferredRotation;
1171 }
1172 return mUpsideDownRotation;
1173
1174 case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE:
1175 // Return seascape unless overridden.
1176 if (isLandscapeOrSeascape(preferredRotation)) {
1177 return preferredRotation;
1178 }
1179 return mSeascapeRotation;
1180
1181 case ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE:
1182 case ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE:
1183 // Return either landscape rotation.
1184 if (isLandscapeOrSeascape(preferredRotation)) {
1185 return preferredRotation;
1186 }
1187 if (isLandscapeOrSeascape(lastRotation)) {
1188 return lastRotation;
1189 }
1190 return mLandscapeRotation;
1191
1192 case ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT:
1193 case ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT:
1194 // Return either portrait rotation.
1195 if (isAnyPortrait(preferredRotation)) {
1196 return preferredRotation;
1197 }
1198 if (isAnyPortrait(lastRotation)) {
1199 return lastRotation;
1200 }
1201 return mPortraitRotation;
1202
1203 default:
1204 // For USER, UNSPECIFIED, NOSENSOR, SENSOR and FULL_SENSOR,
1205 // just return the preferred orientation we already calculated.
1206 if (preferredRotation >= 0) {
1207 return preferredRotation;
1208 }
1209 return Surface.ROTATION_0;
1210 }
1211 }
1212
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001213 private boolean isLandscapeOrSeascape(int rotation) {
1214 return rotation == mLandscapeRotation || rotation == mSeascapeRotation;
1215 }
1216
1217 private boolean isAnyPortrait(int rotation) {
1218 return rotation == mPortraitRotation || rotation == mUpsideDownRotation;
1219 }
1220
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001221 private boolean isValidRotationChoice(final int preferredRotation) {
Riddle Hsuad256a12018-07-18 16:11:30 +08001222 // Determine if the given app orientation is compatible with the provided rotation choice.
1223 switch (mCurrentAppOrientation) {
1224 case ActivityInfo.SCREEN_ORIENTATION_FULL_USER:
1225 // Works with any of the 4 rotations.
1226 return preferredRotation >= 0;
1227
1228 case ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT:
1229 // It's possible for the user pref to be set at 180 because of FULL_USER. This would
1230 // make switching to USER_PORTRAIT appear at 180. Provide choice to back to portrait
1231 // but never to go to 180.
1232 return preferredRotation == mPortraitRotation;
1233
1234 case ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE:
1235 // Works landscape or seascape.
1236 return isLandscapeOrSeascape(preferredRotation);
1237
1238 case ActivityInfo.SCREEN_ORIENTATION_USER:
1239 case ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED:
1240 // Works with any rotation except upside down.
1241 return (preferredRotation >= 0) && (preferredRotation != mUpsideDownRotation);
1242 }
1243
1244 return false;
1245 }
1246
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001247 private boolean isRotationChoicePossible(int orientation) {
1248 // Rotation choice is only shown when the user is in locked mode.
1249 if (mUserRotationMode != WindowManagerPolicy.USER_ROTATION_LOCKED) return false;
1250
1251 // We should only enable rotation choice if the rotation isn't forced by the lid, dock,
1252 // demo, hdmi, vr, etc mode.
1253
1254 // Determine if the rotation is currently forced.
Garfield Tan7fbca052019-02-19 10:45:35 -08001255 if (isFixedToUserRotation()) {
Garfield Tanff362222018-11-14 17:52:32 -08001256 return false; // Rotation is forced to user settings.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001257 }
1258
1259 final int lidState = mDisplayPolicy.getLidState();
1260 if (lidState == LID_OPEN && mLidOpenRotation >= 0) {
1261 return false; // Rotation is forced mLidOpenRotation.
1262 }
1263
1264 final int dockMode = mDisplayPolicy.getDockMode();
1265 final boolean carDockEnablesAccelerometer = false;
1266 if (dockMode == Intent.EXTRA_DOCK_STATE_CAR && !carDockEnablesAccelerometer) {
1267 return false; // Rotation forced to mCarDockRotation.
1268 }
1269
1270 final boolean deskDockEnablesAccelerometer =
1271 mDisplayPolicy.isDeskDockEnablesAccelerometer();
1272 if ((dockMode == Intent.EXTRA_DOCK_STATE_DESK
1273 || dockMode == Intent.EXTRA_DOCK_STATE_LE_DESK
1274 || dockMode == Intent.EXTRA_DOCK_STATE_HE_DESK)
1275 && !deskDockEnablesAccelerometer) {
1276 return false; // Rotation forced to mDeskDockRotation.
1277 }
1278
1279 final boolean hdmiPlugged = mDisplayPolicy.isHdmiPlugged();
1280 if (hdmiPlugged && mDemoHdmiRotationLock) {
1281 return false; // Rotation forced to mDemoHdmiRotation.
1282
1283 } else if (hdmiPlugged && dockMode == Intent.EXTRA_DOCK_STATE_UNDOCKED
1284 && mUndockedHdmiRotation >= 0) {
1285 return false; // Rotation forced to mUndockedHdmiRotation.
1286
1287 } else if (mDemoRotationLock) {
1288 return false; // Rotation forced to mDemoRotation.
1289
1290 } else if (mDisplayPolicy.isPersistentVrModeEnabled()) {
1291 return false; // Rotation forced to mPortraitRotation.
1292
1293 } else if (!mSupportAutoRotation) {
1294 return false;
1295 }
1296
1297 // Ensure that some rotation choice is possible for the given orientation.
1298 switch (orientation) {
1299 case ActivityInfo.SCREEN_ORIENTATION_FULL_USER:
1300 case ActivityInfo.SCREEN_ORIENTATION_USER:
1301 case ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED:
1302 case ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE:
1303 case ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT:
1304 // NOSENSOR description is ambiguous, in reality WM ignores user choice.
1305 return true;
1306 }
1307
1308 // Rotation is forced, should be controlled by system.
1309 return false;
Riddle Hsuad256a12018-07-18 16:11:30 +08001310 }
1311
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001312 /** Notify the StatusBar that system rotation suggestion has changed. */
1313 private void sendProposedRotationChangeToStatusBarInternal(int rotation, boolean isValid) {
1314 if (mStatusBarManagerInternal == null) {
1315 mStatusBarManagerInternal = LocalServices.getService(StatusBarManagerInternal.class);
1316 }
1317 if (mStatusBarManagerInternal != null) {
1318 mStatusBarManagerInternal.onProposedRotationChanged(rotation, isValid);
1319 }
1320 }
1321
1322 private static String allowAllRotationsToString(int allowAll) {
1323 switch (allowAll) {
1324 case -1:
1325 return "unknown";
1326 case 0:
1327 return "false";
1328 case 1:
1329 return "true";
1330 default:
1331 return Integer.toString(allowAll);
1332 }
1333 }
1334
1335 public void onUserSwitch() {
1336 if (mSettingsObserver != null) {
1337 mSettingsObserver.onChange(false);
1338 }
1339 }
1340
1341 /** Return whether the rotation settings has changed. */
1342 private boolean updateSettings() {
1343 final ContentResolver resolver = mContext.getContentResolver();
1344 boolean shouldUpdateRotation = false;
1345
1346 synchronized (mLock) {
1347 boolean shouldUpdateOrientationListener = false;
1348
1349 // Configure rotation suggestions.
Rajeev Kumarb2ff8e82018-08-06 11:45:05 -07001350 final int showRotationSuggestions =
1351 ActivityManager.isLowRamDeviceStatic()
1352 ? Settings.Secure.SHOW_ROTATION_SUGGESTIONS_DISABLED
1353 : Settings.Secure.getIntForUser(resolver,
1354 Settings.Secure.SHOW_ROTATION_SUGGESTIONS,
1355 Settings.Secure.SHOW_ROTATION_SUGGESTIONS_DEFAULT,
1356 UserHandle.USER_CURRENT);
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001357 if (mShowRotationSuggestions != showRotationSuggestions) {
1358 mShowRotationSuggestions = showRotationSuggestions;
1359 shouldUpdateOrientationListener = true;
1360 }
1361
1362 // Configure rotation lock.
1363 final int userRotation = Settings.System.getIntForUser(resolver,
1364 Settings.System.USER_ROTATION, Surface.ROTATION_0,
1365 UserHandle.USER_CURRENT);
1366 if (mUserRotation != userRotation) {
1367 mUserRotation = userRotation;
1368 shouldUpdateRotation = true;
1369 }
1370
1371 final int userRotationMode = Settings.System.getIntForUser(resolver,
1372 Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT) != 0
1373 ? WindowManagerPolicy.USER_ROTATION_FREE
1374 : WindowManagerPolicy.USER_ROTATION_LOCKED;
1375 if (mUserRotationMode != userRotationMode) {
1376 mUserRotationMode = userRotationMode;
1377 shouldUpdateOrientationListener = true;
1378 shouldUpdateRotation = true;
1379 }
1380
1381 if (shouldUpdateOrientationListener) {
1382 updateOrientationListenerLw(); // Enable or disable the orientation listener.
1383 }
1384 }
1385
1386 return shouldUpdateRotation;
Riddle Hsuad256a12018-07-18 16:11:30 +08001387 }
1388
1389 void dump(String prefix, PrintWriter pw) {
1390 pw.println(prefix + "DisplayRotation");
1391 pw.println(prefix + " mCurrentAppOrientation="
1392 + ActivityInfo.screenOrientationToString(mCurrentAppOrientation));
Riddle Hsuccf09402019-08-13 00:33:06 +08001393 pw.println(prefix + " mLastOrientation=" + mLastOrientation);
1394 pw.print(prefix + " mRotation=" + mRotation);
1395 pw.println(" mDeferredRotationPauseCount=" + mDeferredRotationPauseCount);
1396
Riddle Hsuad256a12018-07-18 16:11:30 +08001397 pw.print(prefix + " mLandscapeRotation=" + Surface.rotationToString(mLandscapeRotation));
1398 pw.println(" mSeascapeRotation=" + Surface.rotationToString(mSeascapeRotation));
1399 pw.print(prefix + " mPortraitRotation=" + Surface.rotationToString(mPortraitRotation));
1400 pw.println(" mUpsideDownRotation=" + Surface.rotationToString(mUpsideDownRotation));
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001401
Tiger Huang7c610aa2018-10-27 00:01:01 +08001402 pw.println(prefix + " mSupportAutoRotation=" + mSupportAutoRotation);
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001403 if (mOrientationListener != null) {
Tiger Huang7c610aa2018-10-27 00:01:01 +08001404 mOrientationListener.dump(pw, prefix + " ");
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001405 }
1406 pw.println();
1407
1408 pw.print(prefix + " mCarDockRotation=" + Surface.rotationToString(mCarDockRotation));
1409 pw.println(" mDeskDockRotation=" + Surface.rotationToString(mDeskDockRotation));
1410 pw.print(prefix + " mUserRotationMode="
1411 + WindowManagerPolicy.userRotationModeToString(mUserRotationMode));
1412 pw.print(" mUserRotation=" + Surface.rotationToString(mUserRotation));
1413 pw.println(" mAllowAllRotations=" + allowAllRotationsToString(mAllowAllRotations));
1414
1415 pw.print(prefix + " mDemoHdmiRotation=" + Surface.rotationToString(mDemoHdmiRotation));
1416 pw.print(" mDemoHdmiRotationLock=" + mDemoHdmiRotationLock);
1417 pw.println(" mUndockedHdmiRotation=" + Surface.rotationToString(mUndockedHdmiRotation));
1418 pw.println(prefix + " mLidOpenRotation=" + Surface.rotationToString(mLidOpenRotation));
Garfield Tan7fbca052019-02-19 10:45:35 -08001419 pw.println(prefix + " mFixedToUserRotation=" + isFixedToUserRotation());
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001420 }
1421
1422 private class OrientationListener extends WindowOrientationListener {
1423 final SparseArray<Runnable> mRunnableCache = new SparseArray<>(5);
1424 boolean mEnabled;
1425
1426 OrientationListener(Context context, Handler handler) {
1427 super(context, handler);
1428 }
1429
1430 private class UpdateRunnable implements Runnable {
1431 final int mRotation;
1432
1433 UpdateRunnable(int rotation) {
1434 mRotation = rotation;
1435 }
1436
1437 @Override
1438 public void run() {
1439 // Send interaction hint to improve redraw performance.
1440 mService.mPowerManagerInternal.powerHint(PowerHint.INTERACTION, 0);
1441 if (isRotationChoicePossible(mCurrentAppOrientation)) {
1442 final boolean isValid = isValidRotationChoice(mRotation);
1443 sendProposedRotationChangeToStatusBarInternal(mRotation, isValid);
1444 } else {
1445 mService.updateRotation(false /* alwaysSendConfiguration */,
1446 false /* forceRelayout */);
1447 }
1448 }
1449 }
1450
1451 @Override
1452 public void onProposedRotationChanged(int rotation) {
Adrian Roosb125e0b2019-10-02 14:55:14 +02001453 ProtoLog.v(WM_DEBUG_ORIENTATION, "onProposedRotationChanged, rotation=%d", rotation);
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001454 Runnable r = mRunnableCache.get(rotation, null);
1455 if (r == null) {
1456 r = new UpdateRunnable(rotation);
1457 mRunnableCache.put(rotation, r);
1458 }
1459 getHandler().post(r);
1460 }
1461
1462 @Override
1463 public void enable(boolean clearCurrentRotation) {
1464 super.enable(clearCurrentRotation);
1465 mEnabled = true;
Adrian Roosb125e0b2019-10-02 14:55:14 +02001466 ProtoLog.v(WM_DEBUG_ORIENTATION, "Enabling listeners");
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001467 }
1468
1469 @Override
1470 public void disable() {
1471 super.disable();
1472 mEnabled = false;
Adrian Roosb125e0b2019-10-02 14:55:14 +02001473 ProtoLog.v(WM_DEBUG_ORIENTATION, "Disabling listeners");
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001474 }
1475 }
1476
1477 private class SettingsObserver extends ContentObserver {
1478 SettingsObserver(Handler handler) {
1479 super(handler);
1480 }
1481
1482 void observe() {
1483 final ContentResolver resolver = mContext.getContentResolver();
1484 resolver.registerContentObserver(Settings.Secure.getUriFor(
1485 Settings.Secure.SHOW_ROTATION_SUGGESTIONS), false, this,
1486 UserHandle.USER_ALL);
1487 resolver.registerContentObserver(Settings.System.getUriFor(
1488 Settings.System.ACCELEROMETER_ROTATION), false, this,
1489 UserHandle.USER_ALL);
1490 resolver.registerContentObserver(Settings.System.getUriFor(
1491 Settings.System.USER_ROTATION), false, this,
1492 UserHandle.USER_ALL);
1493 updateSettings();
1494 }
1495
1496 @Override
1497 public void onChange(boolean selfChange) {
1498 if (updateSettings()) {
1499 mService.updateRotation(true /* alwaysSendConfiguration */,
1500 false /* forceRelayout */);
1501 }
1502 }
Riddle Hsuad256a12018-07-18 16:11:30 +08001503 }
Garfield Tanff362222018-11-14 17:52:32 -08001504
1505 @VisibleForTesting
1506 interface ContentObserverRegister {
1507 void registerContentObserver(Uri uri, boolean notifyForDescendants,
1508 ContentObserver observer, @UserIdInt int userHandle);
1509 }
Riddle Hsuad256a12018-07-18 16:11:30 +08001510}