blob: c7c3f8ad54a207e617d956f23051726499c82c03 [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;
Evan Rosky69cace42019-09-20 16:28:13 -070057import android.view.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,
445 "Computed rotation=%d for display id=%d based on lastOrientation=%d and "
446 + "oldRotation=%d",
447 rotation, displayId, lastOrientation, oldRotation);
Riddle Hsuccf09402019-08-13 00:33:06 +0800448
Adrian Roosb125e0b2019-10-02 14:55:14 +0200449 ProtoLog.v(WM_DEBUG_ORIENTATION,
450 "Display id=%d selected orientation %d, got rotation %d", displayId,
451 lastOrientation, rotation);
Riddle Hsuccf09402019-08-13 00:33:06 +0800452
453 if (oldRotation == rotation) {
454 // No change.
455 return false;
456 }
457
Adrian Roosb125e0b2019-10-02 14:55:14 +0200458 ProtoLog.v(WM_DEBUG_ORIENTATION,
459 "Display id=%d rotation changed to %d from %d, lastOrientation=%d",
460 displayId, rotation, oldRotation, lastOrientation);
Riddle Hsuccf09402019-08-13 00:33:06 +0800461
462 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
463 mDisplayContent.mWaitingForConfig = true;
464 }
465
466 mRotation = rotation;
467
468 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
469 mService.mH.sendNewMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
470 mDisplayContent, WINDOW_FREEZE_TIMEOUT_DURATION);
471
472 mDisplayContent.setLayoutNeeded();
473
474 if (shouldRotateSeamlessly(oldRotation, rotation, forceUpdate)) {
475 // The screen rotation animation uses a screenshot to freeze the screen while windows
476 // resize underneath. When we are rotating seamlessly, we allow the elements to
477 // transition to their rotated state independently and without a freeze required.
478 prepareSeamlessRotation();
479 } else {
480 prepareNormalRotationAnimation();
481 }
482
Evan Rosky69cace42019-09-20 16:28:13 -0700483 // The display is frozen now, give a remote handler (system ui) some time to reposition
484 // things.
485 startRemoteRotation(oldRotation, mRotation);
486
Riddle Hsuccf09402019-08-13 00:33:06 +0800487 return true;
488 }
489
Evan Rosky69cace42019-09-20 16:28:13 -0700490 /**
491 * A Remote rotation is when we are waiting for some registered (remote)
492 * {@link IDisplayWindowRotationController} to calculate and return some hierarchy operations
493 * to perform in sync with the rotation.
494 */
495 boolean isWaitingForRemoteRotation() {
496 return mIsWaitingForRemoteRotation;
497 }
498
499 private void startRemoteRotation(int fromRotation, int toRotation) {
500 if (mService.mDisplayRotationController == null) {
501 return;
502 }
503 mIsWaitingForRemoteRotation = true;
504 try {
505 mService.mDisplayRotationController.onRotateDisplay(mDisplayContent.getDisplayId(),
506 fromRotation, toRotation, mRemoteRotationCallback);
507 mService.mH.removeCallbacks(mDisplayRotationHandlerTimeout);
508 mService.mH.postDelayed(mDisplayRotationHandlerTimeout, REMOTE_ROTATION_TIMEOUT_MS);
509 } catch (RemoteException e) {
510 mIsWaitingForRemoteRotation = false;
511 return;
512 }
513 }
514
515 private void continueRotation(int targetRotation, WindowContainerTransaction t) {
516 synchronized (mService.mGlobalLock) {
517 if (targetRotation != mRotation || !mIsWaitingForRemoteRotation) {
518 // Drop it, this is either coming from an outdated remote rotation; or, we've
519 // already moved on.
520 return;
521 }
522 mService.mH.removeCallbacks(mDisplayRotationHandlerTimeout);
523 mIsWaitingForRemoteRotation = false;
Evan Rosky69cace42019-09-20 16:28:13 -0700524 mDisplayContent.sendNewConfiguration();
Evan Rosky70213702019-11-05 10:26:24 -0800525 mService.mAtmService.applyContainerTransaction(t);
Evan Rosky69cace42019-09-20 16:28:13 -0700526 }
527 }
528
Riddle Hsuccf09402019-08-13 00:33:06 +0800529 void prepareNormalRotationAnimation() {
530 final RotationAnimationPair anim = selectRotationAnimation();
531 mService.startFreezingDisplayLocked(anim.mExit, anim.mEnter, mDisplayContent);
532 }
533
534 private void prepareSeamlessRotation() {
535 // We are careful to reset this in case a window was removed before it finished
536 // seamless rotation.
537 mSeamlessRotationCount = 0;
538 mRotatingSeamlessly = true;
539 }
540
541 boolean isRotatingSeamlessly() {
542 return mRotatingSeamlessly;
543 }
544
545 @VisibleForTesting
546 boolean shouldRotateSeamlessly(int oldRotation, int newRotation, boolean forceUpdate) {
547 final WindowState w = mDisplayPolicy.getTopFullscreenOpaqueWindow();
548 if (w == null || w != mDisplayContent.mCurrentFocus) {
549 return false;
550 }
551 // We only enable seamless rotation if the top window has requested it and is in the
552 // fullscreen opaque state. Seamless rotation requires freezing various Surface states and
553 // won't work well with animations, so we disable it in the animation case for now.
554 if (w.getAttrs().rotationAnimation != ROTATION_ANIMATION_SEAMLESS || w.isAnimatingLw()) {
555 return false;
556 }
557
558 // For the upside down rotation we don't rotate seamlessly as the navigation bar moves
559 // position. Note most apps (using orientation:sensor or user as opposed to fullSensor)
560 // will not enter the reverse portrait orientation, so actually the orientation won't change
561 // at all.
562 if (oldRotation == mUpsideDownRotation || newRotation == mUpsideDownRotation) {
563 return false;
564 }
565
566 // If the navigation bar can't change sides, then it will jump when we change orientations
567 // and we don't rotate seamlessly - unless that is allowed, eg. with gesture navigation
568 // where the navbar is low-profile enough that this isn't very noticeable.
569 if (!mAllowSeamlessRotationDespiteNavBarMoving && !mDisplayPolicy.navigationBarCanMove()) {
570 return false;
571 }
572
573 // If the bounds of activity window is different from its parent, then reject to be seamless
574 // because the window position may change after rotation that will look like a sudden jump.
Garfield Tane8d84ab2019-10-11 09:49:40 -0700575 if (w.mActivityRecord != null && !w.mActivityRecord.matchParentBounds()) {
Riddle Hsuccf09402019-08-13 00:33:06 +0800576 return false;
577 }
578
579 // In the presence of the PINNED stack or System Alert windows we unfortunately can not
580 // seamlessly rotate.
581 if (mDisplayContent.hasPinnedStack() || mDisplayContent.hasAlertWindowSurfaces()) {
582 return false;
583 }
584
585 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation to
586 // complete (that is, waiting for windows to redraw). It's tempting to check
587 // mSeamlessRotationCount but that could be incorrect in the case of window-removal.
588 if (!forceUpdate && mDisplayContent.getWindow(win -> win.mSeamlesslyRotated) != null) {
589 return false;
590 }
591
592 return true;
593 }
594
595 void markForSeamlessRotation(WindowState w, boolean seamlesslyRotated) {
596 if (seamlesslyRotated == w.mSeamlesslyRotated || w.mForceSeamlesslyRotate) {
597 return;
598 }
599
600 w.mSeamlesslyRotated = seamlesslyRotated;
601 if (seamlesslyRotated) {
602 mSeamlessRotationCount++;
603 } else {
604 mSeamlessRotationCount--;
605 }
606 if (mSeamlessRotationCount == 0) {
Adrian Roosb125e0b2019-10-02 14:55:14 +0200607 ProtoLog.i(WM_DEBUG_ORIENTATION,
608 "Performing post-rotate rotation after seamless rotation");
Riddle Hsuccf09402019-08-13 00:33:06 +0800609 // Finish seamless rotation.
610 mRotatingSeamlessly = false;
611
612 updateRotationAndSendNewConfigIfChanged();
613 }
614 }
615
616 void onSeamlessRotationTimeout() {
617 final boolean[] isLayoutNeeded = { false };
618
619 mDisplayContent.forAllWindows(w -> {
620 if (!w.mSeamlesslyRotated) {
621 return;
622 }
623 isLayoutNeeded[0] = true;
624 w.setDisplayLayoutNeeded();
625 w.finishSeamlessRotation(true /* timeout */);
626 markForSeamlessRotation(w, false /* seamlesslyRotated */);
627 }, true /* traverseTopToBottom */);
628
629 if (isLayoutNeeded[0]) {
630 mService.mWindowPlacerLocked.performSurfacePlacement();
631 }
632 }
633
634 /**
635 * Returns the animation to run for a rotation transition based on the top fullscreen windows
636 * {@link android.view.WindowManager.LayoutParams#rotationAnimation} and whether it is currently
637 * fullscreen and frontmost.
638 */
639 private RotationAnimationPair selectRotationAnimation() {
640 // If the screen is off or non-interactive, force a jumpcut.
641 final boolean forceJumpcut = !mDisplayPolicy.isScreenOnFully()
642 || !mService.mPolicy.okToAnimate();
643 final WindowState topFullscreen = mDisplayPolicy.getTopFullscreenOpaqueWindow();
644 if (DEBUG_ANIM) Slog.i(TAG, "selectRotationAnimation topFullscreen="
645 + topFullscreen + " rotationAnimation="
646 + (topFullscreen == null ? 0 : topFullscreen.getAttrs().rotationAnimation)
647 + " forceJumpcut=" + forceJumpcut);
648 if (forceJumpcut) {
649 mTmpRotationAnim.mExit = R.anim.rotation_animation_jump_exit;
650 mTmpRotationAnim.mEnter = R.anim.rotation_animation_enter;
651 return mTmpRotationAnim;
652 }
653 if (topFullscreen != null) {
654 int animationHint = topFullscreen.getRotationAnimationHint();
655 if (animationHint < 0 && mDisplayPolicy.isTopLayoutFullscreen()) {
656 animationHint = topFullscreen.getAttrs().rotationAnimation;
657 }
658 switch (animationHint) {
659 case ROTATION_ANIMATION_CROSSFADE:
660 case ROTATION_ANIMATION_SEAMLESS: // Crossfade is fallback for seamless.
661 mTmpRotationAnim.mExit = R.anim.rotation_animation_xfade_exit;
662 mTmpRotationAnim.mEnter = R.anim.rotation_animation_enter;
663 break;
664 case ROTATION_ANIMATION_JUMPCUT:
665 mTmpRotationAnim.mExit = R.anim.rotation_animation_jump_exit;
666 mTmpRotationAnim.mEnter = R.anim.rotation_animation_enter;
667 break;
668 case ROTATION_ANIMATION_ROTATE:
669 default:
670 mTmpRotationAnim.mExit = mTmpRotationAnim.mEnter = 0;
671 break;
672 }
673 } else {
674 mTmpRotationAnim.mExit = mTmpRotationAnim.mEnter = 0;
675 }
676 return mTmpRotationAnim;
677 }
678
679 /**
680 * Validate whether the current top fullscreen has specified the same
681 * {@link android.view.WindowManager.LayoutParams#rotationAnimation} value as that being passed
682 * in from the previous top fullscreen window.
683 *
684 * @param exitAnimId exiting resource id from the previous window.
685 * @param enterAnimId entering resource id from the previous window.
686 * @param forceDefault For rotation animations only, if true ignore the animation values and
687 * just return false.
688 * @return {@code true} if the previous values are still valid, false if they should be replaced
689 * with the default.
690 */
691 boolean validateRotationAnimation(int exitAnimId, int enterAnimId, boolean forceDefault) {
692 switch (exitAnimId) {
693 case R.anim.rotation_animation_xfade_exit:
694 case R.anim.rotation_animation_jump_exit:
695 // These are the only cases that matter.
696 if (forceDefault) {
697 return false;
698 }
699 final RotationAnimationPair anim = selectRotationAnimation();
700 return exitAnimId == anim.mExit && enterAnimId == anim.mEnter;
701 default:
702 return true;
703 }
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800704 }
705
Garfield Tana3f19032019-11-19 18:04:50 -0800706 void restoreSettings(int userRotationMode, int userRotation, int fixedToUserRotation) {
Garfield Tanff362222018-11-14 17:52:32 -0800707 mFixedToUserRotation = fixedToUserRotation;
708
709 // We will retrieve user rotation and user rotation mode from settings for default display.
710 if (isDefaultDisplay) {
711 return;
712 }
Garfield Tan90c90052018-10-08 12:29:41 -0700713 if (userRotationMode != WindowManagerPolicy.USER_ROTATION_FREE
714 && userRotationMode != WindowManagerPolicy.USER_ROTATION_LOCKED) {
715 Slog.w(TAG, "Trying to restore an invalid user rotation mode " + userRotationMode
716 + " for " + mDisplayContent);
717 userRotationMode = WindowManagerPolicy.USER_ROTATION_FREE;
718 }
719 if (userRotation < Surface.ROTATION_0 || userRotation > Surface.ROTATION_270) {
720 Slog.w(TAG, "Trying to restore an invalid user rotation " + userRotation
721 + " for " + mDisplayContent);
722 userRotation = Surface.ROTATION_0;
723 }
724 mUserRotationMode = userRotationMode;
725 mUserRotation = userRotation;
726 }
727
Garfield Tana3f19032019-11-19 18:04:50 -0800728 void setFixedToUserRotation(int fixedToUserRotation) {
Garfield Tanff362222018-11-14 17:52:32 -0800729 if (mFixedToUserRotation == fixedToUserRotation) {
730 return;
731 }
732
733 mFixedToUserRotation = fixedToUserRotation;
Garfield Tan7fbca052019-02-19 10:45:35 -0800734 mDisplayWindowSettings.setFixedToUserRotation(mDisplayContent, fixedToUserRotation);
Garfield Tanff362222018-11-14 17:52:32 -0800735 mService.updateRotation(true /* alwaysSendConfiguration */,
736 false /* forceRelayout */);
737 }
738
Wale Ogunwale8a1860a2019-06-05 08:57:19 -0700739 @VisibleForTesting
740 void setUserRotation(int userRotationMode, int userRotation) {
Garfield Tan90c90052018-10-08 12:29:41 -0700741 if (isDefaultDisplay) {
742 // We'll be notified via settings listener, so we don't need to update internal values.
743 final ContentResolver res = mContext.getContentResolver();
744 final int accelerometerRotation =
745 userRotationMode == WindowManagerPolicy.USER_ROTATION_LOCKED ? 0 : 1;
746 Settings.System.putIntForUser(res, Settings.System.ACCELEROMETER_ROTATION,
747 accelerometerRotation, UserHandle.USER_CURRENT);
748 Settings.System.putIntForUser(res, Settings.System.USER_ROTATION, userRotation,
749 UserHandle.USER_CURRENT);
750 return;
751 }
752
753 boolean changed = false;
754 if (mUserRotationMode != userRotationMode) {
755 mUserRotationMode = userRotationMode;
756 changed = true;
757 }
758 if (mUserRotation != userRotation) {
759 mUserRotation = userRotation;
760 changed = true;
761 }
Garfield Tanff362222018-11-14 17:52:32 -0800762 mDisplayWindowSettings.setUserRotation(mDisplayContent, userRotationMode,
Chilun8753ad32018-10-09 15:56:45 +0800763 userRotation);
Garfield Tan90c90052018-10-08 12:29:41 -0700764 if (changed) {
765 mService.updateRotation(true /* alwaysSendConfiguration */,
766 false /* forceRelayout */);
Garfield Tan90c90052018-10-08 12:29:41 -0700767 }
768 }
769
770 void freezeRotation(int rotation) {
Riddle Hsuccf09402019-08-13 00:33:06 +0800771 rotation = (rotation == -1) ? mRotation : rotation;
Garfield Tan90c90052018-10-08 12:29:41 -0700772 setUserRotation(WindowManagerPolicy.USER_ROTATION_LOCKED, rotation);
773 }
774
775 void thawRotation() {
776 setUserRotation(WindowManagerPolicy.USER_ROTATION_FREE, mUserRotation);
777 }
778
779 boolean isRotationFrozen() {
780 if (!isDefaultDisplay) {
781 return mUserRotationMode == WindowManagerPolicy.USER_ROTATION_LOCKED;
782 }
783
784 return Settings.System.getIntForUser(mContext.getContentResolver(),
785 Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT) == 0;
786 }
787
Garfield Tanff362222018-11-14 17:52:32 -0800788 boolean isFixedToUserRotation() {
Garfield Tan7fbca052019-02-19 10:45:35 -0800789 switch (mFixedToUserRotation) {
Garfield Tana3f19032019-11-19 18:04:50 -0800790 case IWindowManager.FIXED_TO_USER_ROTATION_DISABLED:
Garfield Tan7fbca052019-02-19 10:45:35 -0800791 return false;
Garfield Tana3f19032019-11-19 18:04:50 -0800792 case IWindowManager.FIXED_TO_USER_ROTATION_ENABLED:
Garfield Tan7fbca052019-02-19 10:45:35 -0800793 return true;
794 default:
795 return mDefaultFixedToUserRotation;
796 }
Riddle Hsuad256a12018-07-18 16:11:30 +0800797 }
798
Garfield Tan49dae102019-02-04 09:51:59 -0800799 /**
800 * Returns {@code true} if this display rotation takes app requested orientation into
801 * consideration; {@code false} otherwise. For the time being the only case where this is {@code
802 * false} is when {@link #isFixedToUserRotation()} is {@code true}.
803 */
804 boolean respectAppRequestedOrientation() {
Garfield Tan7fbca052019-02-19 10:45:35 -0800805 return !isFixedToUserRotation();
Garfield Tan49dae102019-02-04 09:51:59 -0800806 }
807
Riddle Hsuad256a12018-07-18 16:11:30 +0800808 public int getLandscapeRotation() {
809 return mLandscapeRotation;
810 }
811
812 public int getSeascapeRotation() {
813 return mSeascapeRotation;
814 }
815
816 public int getPortraitRotation() {
817 return mPortraitRotation;
818 }
819
820 public int getUpsideDownRotation() {
821 return mUpsideDownRotation;
822 }
823
824 public int getCurrentAppOrientation() {
825 return mCurrentAppOrientation;
826 }
827
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800828 public DisplayPolicy getDisplayPolicy() {
829 return mDisplayPolicy;
Riddle Hsuad256a12018-07-18 16:11:30 +0800830 }
831
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800832 public WindowOrientationListener getOrientationListener() {
833 return mOrientationListener;
Riddle Hsuad256a12018-07-18 16:11:30 +0800834 }
835
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800836 public int getUserRotation() {
837 return mUserRotation;
838 }
839
840 public int getUserRotationMode() {
841 return mUserRotationMode;
842 }
843
844 public void updateOrientationListener() {
845 synchronized (mLock) {
846 updateOrientationListenerLw();
Riddle Hsuad256a12018-07-18 16:11:30 +0800847 }
848 }
849
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800850 /**
Riddle Hsuccf09402019-08-13 00:33:06 +0800851 * Temporarily pauses rotation changes until resumed.
852 * <p>
853 * This can be used to prevent rotation changes from occurring while the user is performing
854 * certain operations, such as drag and drop.
855 * <p>
856 * This call nests and must be matched by an equal number of calls to {@link #resume}.
857 */
858 void pause() {
859 mDeferredRotationPauseCount++;
860 }
861
862 /** Resumes normal rotation changes after being paused. */
863 void resume() {
864 if (mDeferredRotationPauseCount <= 0) {
865 return;
866 }
867
868 mDeferredRotationPauseCount--;
869 if (mDeferredRotationPauseCount == 0) {
870 updateRotationAndSendNewConfigIfChanged();
871 }
872 }
873
874 /**
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800875 * Various use cases for invoking this function:
876 * <li>Screen turning off, should always disable listeners if already enabled.</li>
877 * <li>Screen turned on and current app has sensor based orientation, enable listeners
878 * if not already enabled.</li>
879 * <li>Screen turned on and current app does not have sensor orientation, disable listeners
880 * if already enabled.</li>
881 * <li>Screen turning on and current app has sensor based orientation, enable listeners
882 * if needed.</li>
883 * <li>screen turning on and current app has nosensor based orientation, do nothing.</li>
884 */
885 private void updateOrientationListenerLw() {
886 if (mOrientationListener == null || !mOrientationListener.canDetectOrientation()) {
887 // If sensor is turned off or nonexistent for some reason.
888 return;
889 }
890
891 final boolean screenOnEarly = mDisplayPolicy.isScreenOnEarly();
892 final boolean awake = mDisplayPolicy.isAwake();
893 final boolean keyguardDrawComplete = mDisplayPolicy.isKeyguardDrawComplete();
894 final boolean windowManagerDrawComplete = mDisplayPolicy.isWindowManagerDrawComplete();
895
896 // Could have been invoked due to screen turning on or off or
897 // change of the currently visible window's orientation.
Adrian Roosb125e0b2019-10-02 14:55:14 +0200898 ProtoLog.v(WM_DEBUG_ORIENTATION,
899 "screenOnEarly=%b, awake=%b, currentAppOrientation=%d, "
900 + "orientationSensorEnabled=%b, keyguardDrawComplete=%b, "
901 + "windowManagerDrawComplete=%b",
902 screenOnEarly, awake, mCurrentAppOrientation, mOrientationListener.mEnabled,
903 keyguardDrawComplete, windowManagerDrawComplete);
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800904
905 boolean disable = true;
906 // Note: We postpone the rotating of the screen until the keyguard as well as the
907 // window manager have reported a draw complete or the keyguard is going away in dismiss
908 // mode.
909 if (screenOnEarly && awake && ((keyguardDrawComplete && windowManagerDrawComplete))) {
910 if (needSensorRunning()) {
911 disable = false;
912 // Enable listener if not already enabled.
913 if (!mOrientationListener.mEnabled) {
914 // Don't clear the current sensor orientation if the keyguard is going away in
915 // dismiss mode. This allows window manager to use the last sensor reading to
916 // determine the orientation vs. falling back to the last known orientation if
917 // the sensor reading was cleared which can cause it to relaunch the app that
918 // will show in the wrong orientation first before correcting leading to app
919 // launch delays.
920 mOrientationListener.enable(true /* clearCurrentRotation */);
921 }
Riddle Hsuad256a12018-07-18 16:11:30 +0800922 }
923 }
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800924 // Check if sensors need to be disabled.
925 if (disable && mOrientationListener.mEnabled) {
926 mOrientationListener.disable();
927 }
Riddle Hsuad256a12018-07-18 16:11:30 +0800928 }
929
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800930 /**
931 * We always let the sensor be switched on by default except when
932 * the user has explicitly disabled sensor based rotation or when the
933 * screen is switched off.
934 */
935 private boolean needSensorRunning() {
Garfield Tan7fbca052019-02-19 10:45:35 -0800936 if (isFixedToUserRotation()) {
Garfield Tanff362222018-11-14 17:52:32 -0800937 // We are sure we only respect user rotation settings, so we are sure we will not
938 // support sensor rotation.
939 return false;
940 }
941
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800942 if (mSupportAutoRotation) {
943 if (mCurrentAppOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR
944 || mCurrentAppOrientation == ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR
945 || mCurrentAppOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT
946 || mCurrentAppOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE) {
947 // If the application has explicitly requested to follow the
948 // orientation, then we need to turn the sensor on.
949 return true;
950 }
951 }
952
953 final int dockMode = mDisplayPolicy.getDockMode();
954 if ((mDisplayPolicy.isCarDockEnablesAccelerometer()
955 && dockMode == Intent.EXTRA_DOCK_STATE_CAR)
956 || (mDisplayPolicy.isDeskDockEnablesAccelerometer()
957 && (dockMode == Intent.EXTRA_DOCK_STATE_DESK
958 || dockMode == Intent.EXTRA_DOCK_STATE_LE_DESK
959 || dockMode == Intent.EXTRA_DOCK_STATE_HE_DESK))) {
960 // Enable accelerometer if we are docked in a dock that enables accelerometer
961 // orientation management.
962 return true;
963 }
964
965 if (mUserRotationMode == WindowManagerPolicy.USER_ROTATION_LOCKED) {
966 // If the setting for using the sensor by default is enabled, then
967 // we will always leave it on. Note that the user could go to
968 // a window that forces an orientation that does not use the
969 // sensor and in theory we could turn it off... however, when next
970 // turning it on we won't have a good value for the current
971 // orientation for a little bit, which can cause orientation
972 // changes to lag, so we'd like to keep it always on. (It will
973 // still be turned off when the screen is off.)
974
975 // When locked we can provide rotation suggestions users can approve to change the
976 // current screen rotation. To do this the sensor needs to be running.
977 return mSupportAutoRotation &&
978 mShowRotationSuggestions == Settings.Secure.SHOW_ROTATION_SUGGESTIONS_ENABLED;
979 }
980 return mSupportAutoRotation;
981 }
982
983 /**
Riddle Hsuccf09402019-08-13 00:33:06 +0800984 * If this is true we have updated our desired orientation, but not yet changed the real
985 * orientation our applied our screen rotation animation. For example, because a previous
986 * screen rotation was in progress.
987 *
988 * @return {@code true} if the there is an ongoing rotation change.
989 */
990 boolean needsUpdate() {
991 final int oldRotation = mRotation;
992 final int rotation = rotationForOrientation(mLastOrientation, oldRotation);
993 return oldRotation != rotation;
994 }
995
996 /**
997 * Given an orientation constant, returns the appropriate surface rotation, taking into account
998 * sensors, docking mode, rotation lock, and other factors.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800999 *
1000 * @param orientation An orientation constant, such as
Riddle Hsuccf09402019-08-13 00:33:06 +08001001 * {@link ActivityInfo#SCREEN_ORIENTATION_LANDSCAPE}.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001002 * @param lastRotation The most recently used rotation.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001003 * @return The surface rotation to use.
1004 */
Riddle Hsuccf09402019-08-13 00:33:06 +08001005 @VisibleForTesting
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001006 int rotationForOrientation(int orientation, int lastRotation) {
Adrian Roosb125e0b2019-10-02 14:55:14 +02001007 ProtoLog.v(WM_DEBUG_ORIENTATION, "rotationForOrientation(orient=%d, last=%d); user=%d %s",
1008 orientation, lastRotation, mUserRotation,
1009 mUserRotationMode == WindowManagerPolicy.USER_ROTATION_LOCKED
1010 ? "USER_ROTATION_LOCKED" : ""
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001011 );
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001012
Garfield Tan7fbca052019-02-19 10:45:35 -08001013 if (isFixedToUserRotation()) {
Garfield Tanff362222018-11-14 17:52:32 -08001014 return mUserRotation;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001015 }
1016
1017 int sensorRotation = mOrientationListener != null
1018 ? mOrientationListener.getProposedRotation() // may be -1
1019 : -1;
1020 if (sensorRotation < 0) {
1021 sensorRotation = lastRotation;
1022 }
1023
1024 final int lidState = mDisplayPolicy.getLidState();
1025 final int dockMode = mDisplayPolicy.getDockMode();
1026 final boolean hdmiPlugged = mDisplayPolicy.isHdmiPlugged();
1027 final boolean carDockEnablesAccelerometer =
1028 mDisplayPolicy.isCarDockEnablesAccelerometer();
1029 final boolean deskDockEnablesAccelerometer =
1030 mDisplayPolicy.isDeskDockEnablesAccelerometer();
1031
1032 final int preferredRotation;
1033 if (!isDefaultDisplay) {
1034 // For secondary displays we ignore things like displays sensors, docking mode and
Garfield Tan90c90052018-10-08 12:29:41 -07001035 // rotation lock, and always prefer user rotation.
1036 preferredRotation = mUserRotation;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001037 } else if (lidState == LID_OPEN && mLidOpenRotation >= 0) {
1038 // Ignore sensor when lid switch is open and rotation is forced.
1039 preferredRotation = mLidOpenRotation;
1040 } else if (dockMode == Intent.EXTRA_DOCK_STATE_CAR
1041 && (carDockEnablesAccelerometer || mCarDockRotation >= 0)) {
1042 // Ignore sensor when in car dock unless explicitly enabled.
1043 // This case can override the behavior of NOSENSOR, and can also
1044 // enable 180 degree rotation while docked.
1045 preferredRotation = carDockEnablesAccelerometer ? sensorRotation : mCarDockRotation;
1046 } else if ((dockMode == Intent.EXTRA_DOCK_STATE_DESK
1047 || dockMode == Intent.EXTRA_DOCK_STATE_LE_DESK
1048 || dockMode == Intent.EXTRA_DOCK_STATE_HE_DESK)
1049 && (deskDockEnablesAccelerometer || mDeskDockRotation >= 0)) {
1050 // Ignore sensor when in desk dock unless explicitly enabled.
1051 // This case can override the behavior of NOSENSOR, and can also
1052 // enable 180 degree rotation while docked.
1053 preferredRotation = deskDockEnablesAccelerometer ? sensorRotation : mDeskDockRotation;
1054 } else if (hdmiPlugged && mDemoHdmiRotationLock) {
1055 // Ignore sensor when plugged into HDMI when demo HDMI rotation lock enabled.
1056 // Note that the dock orientation overrides the HDMI orientation.
1057 preferredRotation = mDemoHdmiRotation;
1058 } else if (hdmiPlugged && dockMode == Intent.EXTRA_DOCK_STATE_UNDOCKED
1059 && mUndockedHdmiRotation >= 0) {
1060 // Ignore sensor when plugged into HDMI and an undocked orientation has
1061 // been specified in the configuration (only for legacy devices without
1062 // full multi-display support).
1063 // Note that the dock orientation overrides the HDMI orientation.
1064 preferredRotation = mUndockedHdmiRotation;
1065 } else if (mDemoRotationLock) {
1066 // Ignore sensor when demo rotation lock is enabled.
1067 // Note that the dock orientation and HDMI rotation lock override this.
1068 preferredRotation = mDemoRotation;
1069 } else if (mDisplayPolicy.isPersistentVrModeEnabled()) {
1070 // While in VR, apps always prefer a portrait rotation. This does not change
1071 // any apps that explicitly set landscape, but does cause sensors be ignored,
1072 // and ignored any orientation lock that the user has set (this conditional
1073 // should remain above the ORIENTATION_LOCKED conditional below).
1074 preferredRotation = mPortraitRotation;
1075 } else if (orientation == ActivityInfo.SCREEN_ORIENTATION_LOCKED) {
1076 // Application just wants to remain locked in the last rotation.
1077 preferredRotation = lastRotation;
1078 } else if (!mSupportAutoRotation) {
1079 // If we don't support auto-rotation then bail out here and ignore
1080 // the sensor and any rotation lock settings.
1081 preferredRotation = -1;
1082 } else if ((mUserRotationMode == WindowManagerPolicy.USER_ROTATION_FREE
1083 && (orientation == ActivityInfo.SCREEN_ORIENTATION_USER
1084 || orientation == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
1085 || orientation == ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE
1086 || orientation == ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
1087 || orientation == ActivityInfo.SCREEN_ORIENTATION_FULL_USER))
1088 || orientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR
1089 || orientation == ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR
1090 || orientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
1091 || orientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT) {
1092 // Otherwise, use sensor only if requested by the application or enabled
1093 // by default for USER or UNSPECIFIED modes. Does not apply to NOSENSOR.
Riddle Hsuccf09402019-08-13 00:33:06 +08001094 if (mAllowAllRotations == ALLOW_ALL_ROTATIONS_UNDEFINED) {
1095 // Can't read this during init() because the context doesn't have display metrics at
1096 // that time so we cannot determine tablet vs. phone then.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001097 mAllowAllRotations = mContext.getResources().getBoolean(
Riddle Hsuccf09402019-08-13 00:33:06 +08001098 R.bool.config_allowAllRotations)
1099 ? ALLOW_ALL_ROTATIONS_ENABLED
1100 : ALLOW_ALL_ROTATIONS_DISABLED;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001101 }
1102 if (sensorRotation != Surface.ROTATION_180
Riddle Hsuccf09402019-08-13 00:33:06 +08001103 || mAllowAllRotations == ALLOW_ALL_ROTATIONS_ENABLED
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001104 || orientation == ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR
1105 || orientation == ActivityInfo.SCREEN_ORIENTATION_FULL_USER) {
1106 preferredRotation = sensorRotation;
1107 } else {
1108 preferredRotation = lastRotation;
1109 }
1110 } else if (mUserRotationMode == WindowManagerPolicy.USER_ROTATION_LOCKED
Tetsutoki Shiozawa3e595fe2019-05-07 18:27:25 +09001111 && orientation != ActivityInfo.SCREEN_ORIENTATION_NOSENSOR
1112 && orientation != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
1113 && orientation != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
1114 && orientation != ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
1115 && orientation != ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) {
1116 // Apply rotation lock. Does not apply to NOSENSOR or specific rotations.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001117 // The idea is that the user rotation expresses a weak preference for the direction
1118 // of gravity and as NOSENSOR is never affected by gravity, then neither should
1119 // NOSENSOR be affected by rotation lock (although it will be affected by docks).
Tetsutoki Shiozawa3e595fe2019-05-07 18:27:25 +09001120 // Also avoid setting user rotation when app has preference over one particular rotation
1121 // to avoid leaving the rotation to the reverse of it which has the compatible
1122 // orientation, but isn't what app wants, when the user rotation is the reverse of the
1123 // preferred rotation.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001124 preferredRotation = mUserRotation;
1125 } else {
1126 // No overriding preference.
1127 // We will do exactly what the application asked us to do.
1128 preferredRotation = -1;
1129 }
1130
Riddle Hsuad256a12018-07-18 16:11:30 +08001131 switch (orientation) {
1132 case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
1133 // Return portrait unless overridden.
1134 if (isAnyPortrait(preferredRotation)) {
1135 return preferredRotation;
1136 }
1137 return mPortraitRotation;
1138
1139 case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
1140 // Return landscape unless overridden.
1141 if (isLandscapeOrSeascape(preferredRotation)) {
1142 return preferredRotation;
1143 }
1144 return mLandscapeRotation;
1145
1146 case ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT:
1147 // Return reverse portrait unless overridden.
1148 if (isAnyPortrait(preferredRotation)) {
1149 return preferredRotation;
1150 }
1151 return mUpsideDownRotation;
1152
1153 case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE:
1154 // Return seascape unless overridden.
1155 if (isLandscapeOrSeascape(preferredRotation)) {
1156 return preferredRotation;
1157 }
1158 return mSeascapeRotation;
1159
1160 case ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE:
1161 case ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE:
1162 // Return either landscape rotation.
1163 if (isLandscapeOrSeascape(preferredRotation)) {
1164 return preferredRotation;
1165 }
1166 if (isLandscapeOrSeascape(lastRotation)) {
1167 return lastRotation;
1168 }
1169 return mLandscapeRotation;
1170
1171 case ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT:
1172 case ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT:
1173 // Return either portrait rotation.
1174 if (isAnyPortrait(preferredRotation)) {
1175 return preferredRotation;
1176 }
1177 if (isAnyPortrait(lastRotation)) {
1178 return lastRotation;
1179 }
1180 return mPortraitRotation;
1181
1182 default:
1183 // For USER, UNSPECIFIED, NOSENSOR, SENSOR and FULL_SENSOR,
1184 // just return the preferred orientation we already calculated.
1185 if (preferredRotation >= 0) {
1186 return preferredRotation;
1187 }
1188 return Surface.ROTATION_0;
1189 }
1190 }
1191
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001192 private boolean isLandscapeOrSeascape(int rotation) {
1193 return rotation == mLandscapeRotation || rotation == mSeascapeRotation;
1194 }
1195
1196 private boolean isAnyPortrait(int rotation) {
1197 return rotation == mPortraitRotation || rotation == mUpsideDownRotation;
1198 }
1199
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001200 private boolean isValidRotationChoice(final int preferredRotation) {
Riddle Hsuad256a12018-07-18 16:11:30 +08001201 // Determine if the given app orientation is compatible with the provided rotation choice.
1202 switch (mCurrentAppOrientation) {
1203 case ActivityInfo.SCREEN_ORIENTATION_FULL_USER:
1204 // Works with any of the 4 rotations.
1205 return preferredRotation >= 0;
1206
1207 case ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT:
1208 // It's possible for the user pref to be set at 180 because of FULL_USER. This would
1209 // make switching to USER_PORTRAIT appear at 180. Provide choice to back to portrait
1210 // but never to go to 180.
1211 return preferredRotation == mPortraitRotation;
1212
1213 case ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE:
1214 // Works landscape or seascape.
1215 return isLandscapeOrSeascape(preferredRotation);
1216
1217 case ActivityInfo.SCREEN_ORIENTATION_USER:
1218 case ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED:
1219 // Works with any rotation except upside down.
1220 return (preferredRotation >= 0) && (preferredRotation != mUpsideDownRotation);
1221 }
1222
1223 return false;
1224 }
1225
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001226 private boolean isRotationChoicePossible(int orientation) {
1227 // Rotation choice is only shown when the user is in locked mode.
1228 if (mUserRotationMode != WindowManagerPolicy.USER_ROTATION_LOCKED) return false;
1229
1230 // We should only enable rotation choice if the rotation isn't forced by the lid, dock,
1231 // demo, hdmi, vr, etc mode.
1232
1233 // Determine if the rotation is currently forced.
Garfield Tan7fbca052019-02-19 10:45:35 -08001234 if (isFixedToUserRotation()) {
Garfield Tanff362222018-11-14 17:52:32 -08001235 return false; // Rotation is forced to user settings.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001236 }
1237
1238 final int lidState = mDisplayPolicy.getLidState();
1239 if (lidState == LID_OPEN && mLidOpenRotation >= 0) {
1240 return false; // Rotation is forced mLidOpenRotation.
1241 }
1242
1243 final int dockMode = mDisplayPolicy.getDockMode();
1244 final boolean carDockEnablesAccelerometer = false;
1245 if (dockMode == Intent.EXTRA_DOCK_STATE_CAR && !carDockEnablesAccelerometer) {
1246 return false; // Rotation forced to mCarDockRotation.
1247 }
1248
1249 final boolean deskDockEnablesAccelerometer =
1250 mDisplayPolicy.isDeskDockEnablesAccelerometer();
1251 if ((dockMode == Intent.EXTRA_DOCK_STATE_DESK
1252 || dockMode == Intent.EXTRA_DOCK_STATE_LE_DESK
1253 || dockMode == Intent.EXTRA_DOCK_STATE_HE_DESK)
1254 && !deskDockEnablesAccelerometer) {
1255 return false; // Rotation forced to mDeskDockRotation.
1256 }
1257
1258 final boolean hdmiPlugged = mDisplayPolicy.isHdmiPlugged();
1259 if (hdmiPlugged && mDemoHdmiRotationLock) {
1260 return false; // Rotation forced to mDemoHdmiRotation.
1261
1262 } else if (hdmiPlugged && dockMode == Intent.EXTRA_DOCK_STATE_UNDOCKED
1263 && mUndockedHdmiRotation >= 0) {
1264 return false; // Rotation forced to mUndockedHdmiRotation.
1265
1266 } else if (mDemoRotationLock) {
1267 return false; // Rotation forced to mDemoRotation.
1268
1269 } else if (mDisplayPolicy.isPersistentVrModeEnabled()) {
1270 return false; // Rotation forced to mPortraitRotation.
1271
1272 } else if (!mSupportAutoRotation) {
1273 return false;
1274 }
1275
1276 // Ensure that some rotation choice is possible for the given orientation.
1277 switch (orientation) {
1278 case ActivityInfo.SCREEN_ORIENTATION_FULL_USER:
1279 case ActivityInfo.SCREEN_ORIENTATION_USER:
1280 case ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED:
1281 case ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE:
1282 case ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT:
1283 // NOSENSOR description is ambiguous, in reality WM ignores user choice.
1284 return true;
1285 }
1286
1287 // Rotation is forced, should be controlled by system.
1288 return false;
Riddle Hsuad256a12018-07-18 16:11:30 +08001289 }
1290
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001291 /** Notify the StatusBar that system rotation suggestion has changed. */
1292 private void sendProposedRotationChangeToStatusBarInternal(int rotation, boolean isValid) {
1293 if (mStatusBarManagerInternal == null) {
1294 mStatusBarManagerInternal = LocalServices.getService(StatusBarManagerInternal.class);
1295 }
1296 if (mStatusBarManagerInternal != null) {
1297 mStatusBarManagerInternal.onProposedRotationChanged(rotation, isValid);
1298 }
1299 }
1300
1301 private static String allowAllRotationsToString(int allowAll) {
1302 switch (allowAll) {
1303 case -1:
1304 return "unknown";
1305 case 0:
1306 return "false";
1307 case 1:
1308 return "true";
1309 default:
1310 return Integer.toString(allowAll);
1311 }
1312 }
1313
1314 public void onUserSwitch() {
1315 if (mSettingsObserver != null) {
1316 mSettingsObserver.onChange(false);
1317 }
1318 }
1319
1320 /** Return whether the rotation settings has changed. */
1321 private boolean updateSettings() {
1322 final ContentResolver resolver = mContext.getContentResolver();
1323 boolean shouldUpdateRotation = false;
1324
1325 synchronized (mLock) {
1326 boolean shouldUpdateOrientationListener = false;
1327
1328 // Configure rotation suggestions.
Rajeev Kumarb2ff8e82018-08-06 11:45:05 -07001329 final int showRotationSuggestions =
1330 ActivityManager.isLowRamDeviceStatic()
1331 ? Settings.Secure.SHOW_ROTATION_SUGGESTIONS_DISABLED
1332 : Settings.Secure.getIntForUser(resolver,
1333 Settings.Secure.SHOW_ROTATION_SUGGESTIONS,
1334 Settings.Secure.SHOW_ROTATION_SUGGESTIONS_DEFAULT,
1335 UserHandle.USER_CURRENT);
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001336 if (mShowRotationSuggestions != showRotationSuggestions) {
1337 mShowRotationSuggestions = showRotationSuggestions;
1338 shouldUpdateOrientationListener = true;
1339 }
1340
1341 // Configure rotation lock.
1342 final int userRotation = Settings.System.getIntForUser(resolver,
1343 Settings.System.USER_ROTATION, Surface.ROTATION_0,
1344 UserHandle.USER_CURRENT);
1345 if (mUserRotation != userRotation) {
1346 mUserRotation = userRotation;
1347 shouldUpdateRotation = true;
1348 }
1349
1350 final int userRotationMode = Settings.System.getIntForUser(resolver,
1351 Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT) != 0
1352 ? WindowManagerPolicy.USER_ROTATION_FREE
1353 : WindowManagerPolicy.USER_ROTATION_LOCKED;
1354 if (mUserRotationMode != userRotationMode) {
1355 mUserRotationMode = userRotationMode;
1356 shouldUpdateOrientationListener = true;
1357 shouldUpdateRotation = true;
1358 }
1359
1360 if (shouldUpdateOrientationListener) {
1361 updateOrientationListenerLw(); // Enable or disable the orientation listener.
1362 }
1363 }
1364
1365 return shouldUpdateRotation;
Riddle Hsuad256a12018-07-18 16:11:30 +08001366 }
1367
1368 void dump(String prefix, PrintWriter pw) {
1369 pw.println(prefix + "DisplayRotation");
1370 pw.println(prefix + " mCurrentAppOrientation="
1371 + ActivityInfo.screenOrientationToString(mCurrentAppOrientation));
Riddle Hsuccf09402019-08-13 00:33:06 +08001372 pw.println(prefix + " mLastOrientation=" + mLastOrientation);
1373 pw.print(prefix + " mRotation=" + mRotation);
1374 pw.println(" mDeferredRotationPauseCount=" + mDeferredRotationPauseCount);
1375
Riddle Hsuad256a12018-07-18 16:11:30 +08001376 pw.print(prefix + " mLandscapeRotation=" + Surface.rotationToString(mLandscapeRotation));
1377 pw.println(" mSeascapeRotation=" + Surface.rotationToString(mSeascapeRotation));
1378 pw.print(prefix + " mPortraitRotation=" + Surface.rotationToString(mPortraitRotation));
1379 pw.println(" mUpsideDownRotation=" + Surface.rotationToString(mUpsideDownRotation));
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001380
Tiger Huang7c610aa2018-10-27 00:01:01 +08001381 pw.println(prefix + " mSupportAutoRotation=" + mSupportAutoRotation);
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001382 if (mOrientationListener != null) {
Tiger Huang7c610aa2018-10-27 00:01:01 +08001383 mOrientationListener.dump(pw, prefix + " ");
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001384 }
1385 pw.println();
1386
1387 pw.print(prefix + " mCarDockRotation=" + Surface.rotationToString(mCarDockRotation));
1388 pw.println(" mDeskDockRotation=" + Surface.rotationToString(mDeskDockRotation));
1389 pw.print(prefix + " mUserRotationMode="
1390 + WindowManagerPolicy.userRotationModeToString(mUserRotationMode));
1391 pw.print(" mUserRotation=" + Surface.rotationToString(mUserRotation));
1392 pw.println(" mAllowAllRotations=" + allowAllRotationsToString(mAllowAllRotations));
1393
1394 pw.print(prefix + " mDemoHdmiRotation=" + Surface.rotationToString(mDemoHdmiRotation));
1395 pw.print(" mDemoHdmiRotationLock=" + mDemoHdmiRotationLock);
1396 pw.println(" mUndockedHdmiRotation=" + Surface.rotationToString(mUndockedHdmiRotation));
1397 pw.println(prefix + " mLidOpenRotation=" + Surface.rotationToString(mLidOpenRotation));
Garfield Tan7fbca052019-02-19 10:45:35 -08001398 pw.println(prefix + " mFixedToUserRotation=" + isFixedToUserRotation());
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001399 }
1400
1401 private class OrientationListener extends WindowOrientationListener {
1402 final SparseArray<Runnable> mRunnableCache = new SparseArray<>(5);
1403 boolean mEnabled;
1404
1405 OrientationListener(Context context, Handler handler) {
1406 super(context, handler);
1407 }
1408
1409 private class UpdateRunnable implements Runnable {
1410 final int mRotation;
1411
1412 UpdateRunnable(int rotation) {
1413 mRotation = rotation;
1414 }
1415
1416 @Override
1417 public void run() {
1418 // Send interaction hint to improve redraw performance.
1419 mService.mPowerManagerInternal.powerHint(PowerHint.INTERACTION, 0);
1420 if (isRotationChoicePossible(mCurrentAppOrientation)) {
1421 final boolean isValid = isValidRotationChoice(mRotation);
1422 sendProposedRotationChangeToStatusBarInternal(mRotation, isValid);
1423 } else {
1424 mService.updateRotation(false /* alwaysSendConfiguration */,
1425 false /* forceRelayout */);
1426 }
1427 }
1428 }
1429
1430 @Override
1431 public void onProposedRotationChanged(int rotation) {
Adrian Roosb125e0b2019-10-02 14:55:14 +02001432 ProtoLog.v(WM_DEBUG_ORIENTATION, "onProposedRotationChanged, rotation=%d", rotation);
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001433 Runnable r = mRunnableCache.get(rotation, null);
1434 if (r == null) {
1435 r = new UpdateRunnable(rotation);
1436 mRunnableCache.put(rotation, r);
1437 }
1438 getHandler().post(r);
1439 }
1440
1441 @Override
1442 public void enable(boolean clearCurrentRotation) {
1443 super.enable(clearCurrentRotation);
1444 mEnabled = true;
Adrian Roosb125e0b2019-10-02 14:55:14 +02001445 ProtoLog.v(WM_DEBUG_ORIENTATION, "Enabling listeners");
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001446 }
1447
1448 @Override
1449 public void disable() {
1450 super.disable();
1451 mEnabled = false;
Adrian Roosb125e0b2019-10-02 14:55:14 +02001452 ProtoLog.v(WM_DEBUG_ORIENTATION, "Disabling listeners");
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001453 }
1454 }
1455
1456 private class SettingsObserver extends ContentObserver {
1457 SettingsObserver(Handler handler) {
1458 super(handler);
1459 }
1460
1461 void observe() {
1462 final ContentResolver resolver = mContext.getContentResolver();
1463 resolver.registerContentObserver(Settings.Secure.getUriFor(
1464 Settings.Secure.SHOW_ROTATION_SUGGESTIONS), false, this,
1465 UserHandle.USER_ALL);
1466 resolver.registerContentObserver(Settings.System.getUriFor(
1467 Settings.System.ACCELEROMETER_ROTATION), false, this,
1468 UserHandle.USER_ALL);
1469 resolver.registerContentObserver(Settings.System.getUriFor(
1470 Settings.System.USER_ROTATION), false, this,
1471 UserHandle.USER_ALL);
1472 updateSettings();
1473 }
1474
1475 @Override
1476 public void onChange(boolean selfChange) {
1477 if (updateSettings()) {
1478 mService.updateRotation(true /* alwaysSendConfiguration */,
1479 false /* forceRelayout */);
1480 }
1481 }
Riddle Hsuad256a12018-07-18 16:11:30 +08001482 }
Garfield Tanff362222018-11-14 17:52:32 -08001483
1484 @VisibleForTesting
1485 interface ContentObserverRegister {
1486 void registerContentObserver(Uri uri, boolean notifyForDescendants,
1487 ContentObserver observer, @UserIdInt int userHandle);
1488 }
Riddle Hsuad256a12018-07-18 16:11:30 +08001489}