Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.wm; |
| 18 | |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 19 | import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; |
| 20 | import static android.view.WindowManager.LayoutParams.ROTATION_ANIMATION_CROSSFADE; |
| 21 | import static android.view.WindowManager.LayoutParams.ROTATION_ANIMATION_JUMPCUT; |
| 22 | import static android.view.WindowManager.LayoutParams.ROTATION_ANIMATION_ROTATE; |
| 23 | import static android.view.WindowManager.LayoutParams.ROTATION_ANIMATION_SEAMLESS; |
| 24 | |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 25 | import static com.android.server.policy.WindowManagerPolicy.WindowManagerFuncs.LID_OPEN; |
Adrian Roos | b125e0b | 2019-10-02 14:55:14 +0200 | [diff] [blame] | 26 | import static com.android.server.wm.ProtoLogGroup.WM_DEBUG_ORIENTATION; |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 27 | import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ANIM; |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 28 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME; |
| 29 | import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM; |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 30 | import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE; |
| 31 | import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION; |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 32 | |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 33 | import android.annotation.AnimRes; |
Garfield Tan | 7fbca05 | 2019-02-19 10:45:35 -0800 | [diff] [blame] | 34 | import android.annotation.IntDef; |
Garfield Tan | ff36222 | 2018-11-14 17:52:32 -0800 | [diff] [blame] | 35 | import android.annotation.UserIdInt; |
Rajeev Kumar | b2ff8e8 | 2018-08-06 11:45:05 -0700 | [diff] [blame] | 36 | import android.app.ActivityManager; |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 37 | import android.content.ContentResolver; |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 38 | import android.content.Context; |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 39 | import android.content.Intent; |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 40 | import android.content.pm.ActivityInfo; |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 41 | import android.content.pm.ActivityInfo.ScreenOrientation; |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 42 | import android.content.pm.PackageManager; |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 43 | import android.content.res.Resources; |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 44 | import android.database.ContentObserver; |
| 45 | import android.hardware.power.V1_0.PowerHint; |
Garfield Tan | ff36222 | 2018-11-14 17:52:32 -0800 | [diff] [blame] | 46 | import android.net.Uri; |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 47 | import android.os.Handler; |
Evan Rosky | 69cace4 | 2019-09-20 16:28:13 -0700 | [diff] [blame] | 48 | import android.os.RemoteException; |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 49 | import android.os.SystemProperties; |
| 50 | import android.os.UserHandle; |
| 51 | import android.provider.Settings; |
| 52 | import android.util.Slog; |
| 53 | import android.util.SparseArray; |
Evan Rosky | 69cace4 | 2019-09-20 16:28:13 -0700 | [diff] [blame] | 54 | import android.view.IDisplayWindowRotationCallback; |
Garfield Tan | a3f1903 | 2019-11-19 18:04:50 -0800 | [diff] [blame] | 55 | import android.view.IWindowManager; |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 56 | import android.view.Surface; |
Evan Rosky | 69cace4 | 2019-09-20 16:28:13 -0700 | [diff] [blame] | 57 | import android.view.WindowContainerTransaction; |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 58 | |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 59 | import com.android.internal.R; |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 60 | import com.android.internal.annotations.VisibleForTesting; |
Evan Rosky | 69cace4 | 2019-09-20 16:28:13 -0700 | [diff] [blame] | 61 | import com.android.internal.util.function.pooled.PooledLambda; |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 62 | import com.android.server.LocalServices; |
| 63 | import com.android.server.UiThread; |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 64 | import com.android.server.policy.WindowManagerPolicy; |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 65 | import com.android.server.policy.WindowOrientationListener; |
Adrian Roos | b125e0b | 2019-10-02 14:55:14 +0200 | [diff] [blame] | 66 | import com.android.server.protolog.common.ProtoLog; |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 67 | import com.android.server.statusbar.StatusBarManagerInternal; |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 68 | |
| 69 | import java.io.PrintWriter; |
Garfield Tan | 7fbca05 | 2019-02-19 10:45:35 -0800 | [diff] [blame] | 70 | import java.lang.annotation.Retention; |
| 71 | import java.lang.annotation.RetentionPolicy; |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * Defines the mapping between orientation and rotation of a display. |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 75 | * Non-public methods are assumed to run inside WM lock. |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 76 | */ |
| 77 | public class DisplayRotation { |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 78 | private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayRotation" : TAG_WM; |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 79 | |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 80 | private static class RotationAnimationPair { |
| 81 | @AnimRes |
| 82 | int mEnter; |
| 83 | @AnimRes |
| 84 | int mExit; |
| 85 | } |
| 86 | |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 87 | private final WindowManagerService mService; |
Garfield Tan | 90c9005 | 2018-10-08 12:29:41 -0700 | [diff] [blame] | 88 | private final DisplayContent mDisplayContent; |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 89 | private final DisplayPolicy mDisplayPolicy; |
Garfield Tan | ff36222 | 2018-11-14 17:52:32 -0800 | [diff] [blame] | 90 | private final DisplayWindowSettings mDisplayWindowSettings; |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 91 | 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 Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 100 | private final RotationAnimationPair mTmpRotationAnim = new RotationAnimationPair(); |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 101 | |
| 102 | private OrientationListener mOrientationListener; |
| 103 | private StatusBarManagerInternal mStatusBarManagerInternal; |
| 104 | private SettingsObserver mSettingsObserver; |
| 105 | |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 106 | @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 Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 124 | |
| 125 | @VisibleForTesting |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 126 | int mLandscapeRotation; // default landscape |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 127 | @VisibleForTesting |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 128 | int mSeascapeRotation; // "other" landscape, 180 degrees from mLandscapeRotation |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 129 | @VisibleForTesting |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 130 | int mPortraitRotation; // default portrait |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 131 | @VisibleForTesting |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 132 | int mUpsideDownRotation; // "other" portrait |
| 133 | |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 134 | 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 Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 156 | private int mShowRotationSuggestions; |
| 157 | |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 158 | 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 Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 177 | private int mUserRotationMode = WindowManagerPolicy.USER_ROTATION_FREE; |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 178 | |
| 179 | @Surface.Rotation |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 180 | private int mUserRotation = Surface.ROTATION_0; |
| 181 | |
Garfield Tan | ff36222 | 2018-11-14 17:52:32 -0800 | [diff] [blame] | 182 | /** |
Garfield Tan | 7fbca05 | 2019-02-19 10:45:35 -0800 | [diff] [blame] | 183 | * Flag that indicates this is a display that may run better when fixed to user rotation. |
| 184 | */ |
| 185 | private boolean mDefaultFixedToUserRotation; |
| 186 | |
| 187 | /** |
Garfield Tan | ff36222 | 2018-11-14 17:52:32 -0800 | [diff] [blame] | 188 | * 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 Tan | a3f1903 | 2019-11-19 18:04:50 -0800 | [diff] [blame] | 192 | private int mFixedToUserRotation = IWindowManager.FIXED_TO_USER_ROTATION_DEFAULT; |
Garfield Tan | ff36222 | 2018-11-14 17:52:32 -0800 | [diff] [blame] | 193 | |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 194 | private int mDemoHdmiRotation; |
| 195 | private int mDemoRotation; |
| 196 | private boolean mDemoHdmiRotationLock; |
| 197 | private boolean mDemoRotationLock; |
| 198 | |
Evan Rosky | 69cace4 | 2019-09-20 16:28:13 -0700 | [diff] [blame] | 199 | 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 Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 224 | DisplayRotation(WindowManagerService service, DisplayContent displayContent) { |
| 225 | this(service, displayContent, displayContent.getDisplayPolicy(), |
Garfield Tan | ff36222 | 2018-11-14 17:52:32 -0800 | [diff] [blame] | 226 | service.mDisplayWindowSettings, service.mContext, service.getWindowManagerLock()); |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 227 | } |
| 228 | |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 229 | @VisibleForTesting |
| 230 | DisplayRotation(WindowManagerService service, DisplayContent displayContent, |
Garfield Tan | ff36222 | 2018-11-14 17:52:32 -0800 | [diff] [blame] | 231 | DisplayPolicy displayPolicy, DisplayWindowSettings displayWindowSettings, |
| 232 | Context context, Object lock) { |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 233 | mService = service; |
Garfield Tan | 90c9005 | 2018-10-08 12:29:41 -0700 | [diff] [blame] | 234 | mDisplayContent = displayContent; |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 235 | mDisplayPolicy = displayPolicy; |
Garfield Tan | ff36222 | 2018-11-14 17:52:32 -0800 | [diff] [blame] | 236 | mDisplayWindowSettings = displayWindowSettings; |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 237 | mContext = context; |
| 238 | mLock = lock; |
| 239 | isDefaultDisplay = displayContent.isDefaultDisplay; |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 240 | |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 241 | 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 Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 247 | |
| 248 | if (isDefaultDisplay) { |
| 249 | final Handler uiHandler = UiThread.getHandler(); |
| 250 | mOrientationListener = new OrientationListener(mContext, uiHandler); |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 251 | mOrientationListener.setCurrentRotation(mRotation); |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 252 | 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 Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 276 | /** |
| 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 Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 285 | void configure(int width, int height, int shortSizeDp, int longSizeDp) { |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 286 | final Resources res = mContext.getResources(); |
| 287 | if (width > height) { |
| 288 | mLandscapeRotation = Surface.ROTATION_0; |
| 289 | mSeascapeRotation = Surface.ROTATION_180; |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 290 | if (res.getBoolean(R.bool.config_reverseDefaultRotation)) { |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 291 | 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 Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 300 | if (res.getBoolean(R.bool.config_reverseDefaultRotation)) { |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 301 | 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 Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 309 | // 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 Tan | 12b12f7a | 2019-02-22 16:33:27 -0800 | [diff] [blame] | 327 | // It's physically impossible to rotate the car's screen. |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 328 | final boolean isCar = mContext.getPackageManager().hasSystemFeature( |
| 329 | PackageManager.FEATURE_AUTOMOTIVE); |
Garfield Tan | 12b12f7a | 2019-02-22 16:33:27 -0800 | [diff] [blame] | 330 | // It's also not likely to rotate a TV screen. |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 331 | final boolean isTv = mContext.getPackageManager().hasSystemFeature( |
| 332 | PackageManager.FEATURE_LEANBACK); |
Garfield Tan | 7fbca05 | 2019-02-19 10:45:35 -0800 | [diff] [blame] | 333 | final boolean forceDesktopMode = |
| 334 | mService.mForceDesktopModeOnExternalDisplays && !isDefaultDisplay; |
| 335 | mDefaultFixedToUserRotation = |
Tiger Huang | 86e6d07 | 2019-05-02 20:23:47 +0800 | [diff] [blame] | 336 | (isCar || isTv || mService.mIsPc || forceDesktopMode) |
Garfield Tan | 7fbca05 | 2019-02-19 10:45:35 -0800 | [diff] [blame] | 337 | // 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 Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 341 | } |
| 342 | |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 343 | void applyCurrentRotation(@Surface.Rotation int rotation) { |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 344 | if (mOrientationListener != null) { |
| 345 | mOrientationListener.setCurrentRotation(rotation); |
| 346 | } |
| 347 | } |
| 348 | |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 349 | @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 Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 369 | if (newOrientation != mCurrentAppOrientation) { |
| 370 | mCurrentAppOrientation = newOrientation; |
| 371 | if (isDefaultDisplay) { |
| 372 | updateOrientationListenerLw(); |
| 373 | } |
| 374 | } |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 375 | 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 Roos | b125e0b | 2019-10-02 14:55:14 +0200 | [diff] [blame] | 412 | ProtoLog.v(WM_DEBUG_ORIENTATION, "Deferring rotation, rotation is paused."); |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 413 | return false; |
| 414 | } |
| 415 | |
| 416 | final ScreenRotationAnimation screenRotationAnimation = |
Vadim Caen | b371583 | 2019-08-13 17:06:38 +0200 | [diff] [blame] | 417 | mDisplayContent.getRotationAnimation(); |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 418 | 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 Roos | b125e0b | 2019-10-02 14:55:14 +0200 | [diff] [blame] | 422 | ProtoLog.v(WM_DEBUG_ORIENTATION, "Deferring rotation, animation in progress."); |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 423 | 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 Roos | b125e0b | 2019-10-02 14:55:14 +0200 | [diff] [blame] | 429 | ProtoLog.v(WM_DEBUG_ORIENTATION, |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 430 | "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 Roos | b125e0b | 2019-10-02 14:55:14 +0200 | [diff] [blame] | 437 | ProtoLog.v(WM_DEBUG_ORIENTATION, "Deferring rotation, display is not enabled."); |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 438 | return false; |
| 439 | } |
| 440 | |
| 441 | final int oldRotation = mRotation; |
| 442 | final int lastOrientation = mLastOrientation; |
| 443 | final int rotation = rotationForOrientation(lastOrientation, oldRotation); |
Adrian Roos | b125e0b | 2019-10-02 14:55:14 +0200 | [diff] [blame] | 444 | ProtoLog.v(WM_DEBUG_ORIENTATION, |
Vadim Caen | 1096f11 | 2019-12-18 11:44:26 +0100 | [diff] [blame^] | 445 | "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 Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 451 | |
Adrian Roos | b125e0b | 2019-10-02 14:55:14 +0200 | [diff] [blame] | 452 | ProtoLog.v(WM_DEBUG_ORIENTATION, |
Vadim Caen | 1096f11 | 2019-12-18 11:44:26 +0100 | [diff] [blame^] | 453 | "Display id=%d selected orientation %s (%d), got rotation %s (%d)", displayId, |
| 454 | ActivityInfo.screenOrientationToString(lastOrientation), lastOrientation, |
| 455 | Surface.rotationToString(rotation), rotation); |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 456 | |
| 457 | if (oldRotation == rotation) { |
| 458 | // No change. |
| 459 | return false; |
| 460 | } |
| 461 | |
Adrian Roos | b125e0b | 2019-10-02 14:55:14 +0200 | [diff] [blame] | 462 | ProtoLog.v(WM_DEBUG_ORIENTATION, |
| 463 | "Display id=%d rotation changed to %d from %d, lastOrientation=%d", |
| 464 | displayId, rotation, oldRotation, lastOrientation); |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 465 | |
| 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 | |
Evan Rosky | 69cace4 | 2019-09-20 16:28:13 -0700 | [diff] [blame] | 487 | // The display is frozen now, give a remote handler (system ui) some time to reposition |
| 488 | // things. |
| 489 | startRemoteRotation(oldRotation, mRotation); |
| 490 | |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 491 | return true; |
| 492 | } |
| 493 | |
Evan Rosky | 69cace4 | 2019-09-20 16:28:13 -0700 | [diff] [blame] | 494 | /** |
| 495 | * A Remote rotation is when we are waiting for some registered (remote) |
| 496 | * {@link IDisplayWindowRotationController} to calculate and return some hierarchy operations |
| 497 | * to perform in sync with the rotation. |
| 498 | */ |
| 499 | boolean isWaitingForRemoteRotation() { |
| 500 | return mIsWaitingForRemoteRotation; |
| 501 | } |
| 502 | |
| 503 | private void startRemoteRotation(int fromRotation, int toRotation) { |
| 504 | if (mService.mDisplayRotationController == null) { |
| 505 | return; |
| 506 | } |
| 507 | mIsWaitingForRemoteRotation = true; |
| 508 | try { |
| 509 | mService.mDisplayRotationController.onRotateDisplay(mDisplayContent.getDisplayId(), |
| 510 | fromRotation, toRotation, mRemoteRotationCallback); |
| 511 | mService.mH.removeCallbacks(mDisplayRotationHandlerTimeout); |
| 512 | mService.mH.postDelayed(mDisplayRotationHandlerTimeout, REMOTE_ROTATION_TIMEOUT_MS); |
| 513 | } catch (RemoteException e) { |
| 514 | mIsWaitingForRemoteRotation = false; |
| 515 | return; |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | private void continueRotation(int targetRotation, WindowContainerTransaction t) { |
| 520 | synchronized (mService.mGlobalLock) { |
| 521 | if (targetRotation != mRotation || !mIsWaitingForRemoteRotation) { |
| 522 | // Drop it, this is either coming from an outdated remote rotation; or, we've |
| 523 | // already moved on. |
| 524 | return; |
| 525 | } |
| 526 | mService.mH.removeCallbacks(mDisplayRotationHandlerTimeout); |
| 527 | mIsWaitingForRemoteRotation = false; |
Evan Rosky | 69cace4 | 2019-09-20 16:28:13 -0700 | [diff] [blame] | 528 | mDisplayContent.sendNewConfiguration(); |
Evan Rosky | 7021370 | 2019-11-05 10:26:24 -0800 | [diff] [blame] | 529 | mService.mAtmService.applyContainerTransaction(t); |
Evan Rosky | 69cace4 | 2019-09-20 16:28:13 -0700 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 533 | void prepareNormalRotationAnimation() { |
| 534 | final RotationAnimationPair anim = selectRotationAnimation(); |
| 535 | mService.startFreezingDisplayLocked(anim.mExit, anim.mEnter, mDisplayContent); |
| 536 | } |
| 537 | |
| 538 | private void prepareSeamlessRotation() { |
| 539 | // We are careful to reset this in case a window was removed before it finished |
| 540 | // seamless rotation. |
| 541 | mSeamlessRotationCount = 0; |
| 542 | mRotatingSeamlessly = true; |
| 543 | } |
| 544 | |
| 545 | boolean isRotatingSeamlessly() { |
| 546 | return mRotatingSeamlessly; |
| 547 | } |
| 548 | |
| 549 | @VisibleForTesting |
| 550 | boolean shouldRotateSeamlessly(int oldRotation, int newRotation, boolean forceUpdate) { |
| 551 | final WindowState w = mDisplayPolicy.getTopFullscreenOpaqueWindow(); |
| 552 | if (w == null || w != mDisplayContent.mCurrentFocus) { |
| 553 | return false; |
| 554 | } |
| 555 | // We only enable seamless rotation if the top window has requested it and is in the |
| 556 | // fullscreen opaque state. Seamless rotation requires freezing various Surface states and |
| 557 | // won't work well with animations, so we disable it in the animation case for now. |
| 558 | if (w.getAttrs().rotationAnimation != ROTATION_ANIMATION_SEAMLESS || w.isAnimatingLw()) { |
| 559 | return false; |
| 560 | } |
| 561 | |
| 562 | // For the upside down rotation we don't rotate seamlessly as the navigation bar moves |
| 563 | // position. Note most apps (using orientation:sensor or user as opposed to fullSensor) |
| 564 | // will not enter the reverse portrait orientation, so actually the orientation won't change |
| 565 | // at all. |
| 566 | if (oldRotation == mUpsideDownRotation || newRotation == mUpsideDownRotation) { |
| 567 | return false; |
| 568 | } |
| 569 | |
| 570 | // If the navigation bar can't change sides, then it will jump when we change orientations |
| 571 | // and we don't rotate seamlessly - unless that is allowed, eg. with gesture navigation |
| 572 | // where the navbar is low-profile enough that this isn't very noticeable. |
| 573 | if (!mAllowSeamlessRotationDespiteNavBarMoving && !mDisplayPolicy.navigationBarCanMove()) { |
| 574 | return false; |
| 575 | } |
| 576 | |
| 577 | // If the bounds of activity window is different from its parent, then reject to be seamless |
| 578 | // because the window position may change after rotation that will look like a sudden jump. |
Garfield Tan | e8d84ab | 2019-10-11 09:49:40 -0700 | [diff] [blame] | 579 | if (w.mActivityRecord != null && !w.mActivityRecord.matchParentBounds()) { |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 580 | return false; |
| 581 | } |
| 582 | |
| 583 | // In the presence of the PINNED stack or System Alert windows we unfortunately can not |
| 584 | // seamlessly rotate. |
| 585 | if (mDisplayContent.hasPinnedStack() || mDisplayContent.hasAlertWindowSurfaces()) { |
| 586 | return false; |
| 587 | } |
| 588 | |
| 589 | // We can't rotate (seamlessly or not) while waiting for the last seamless rotation to |
| 590 | // complete (that is, waiting for windows to redraw). It's tempting to check |
| 591 | // mSeamlessRotationCount but that could be incorrect in the case of window-removal. |
| 592 | if (!forceUpdate && mDisplayContent.getWindow(win -> win.mSeamlesslyRotated) != null) { |
| 593 | return false; |
| 594 | } |
| 595 | |
| 596 | return true; |
| 597 | } |
| 598 | |
| 599 | void markForSeamlessRotation(WindowState w, boolean seamlesslyRotated) { |
| 600 | if (seamlesslyRotated == w.mSeamlesslyRotated || w.mForceSeamlesslyRotate) { |
| 601 | return; |
| 602 | } |
| 603 | |
| 604 | w.mSeamlesslyRotated = seamlesslyRotated; |
| 605 | if (seamlesslyRotated) { |
| 606 | mSeamlessRotationCount++; |
| 607 | } else { |
| 608 | mSeamlessRotationCount--; |
| 609 | } |
| 610 | if (mSeamlessRotationCount == 0) { |
Adrian Roos | b125e0b | 2019-10-02 14:55:14 +0200 | [diff] [blame] | 611 | ProtoLog.i(WM_DEBUG_ORIENTATION, |
| 612 | "Performing post-rotate rotation after seamless rotation"); |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 613 | // Finish seamless rotation. |
| 614 | mRotatingSeamlessly = false; |
| 615 | |
| 616 | updateRotationAndSendNewConfigIfChanged(); |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | void onSeamlessRotationTimeout() { |
| 621 | final boolean[] isLayoutNeeded = { false }; |
| 622 | |
| 623 | mDisplayContent.forAllWindows(w -> { |
| 624 | if (!w.mSeamlesslyRotated) { |
| 625 | return; |
| 626 | } |
| 627 | isLayoutNeeded[0] = true; |
| 628 | w.setDisplayLayoutNeeded(); |
| 629 | w.finishSeamlessRotation(true /* timeout */); |
| 630 | markForSeamlessRotation(w, false /* seamlesslyRotated */); |
| 631 | }, true /* traverseTopToBottom */); |
| 632 | |
| 633 | if (isLayoutNeeded[0]) { |
| 634 | mService.mWindowPlacerLocked.performSurfacePlacement(); |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | /** |
| 639 | * Returns the animation to run for a rotation transition based on the top fullscreen windows |
| 640 | * {@link android.view.WindowManager.LayoutParams#rotationAnimation} and whether it is currently |
| 641 | * fullscreen and frontmost. |
| 642 | */ |
| 643 | private RotationAnimationPair selectRotationAnimation() { |
| 644 | // If the screen is off or non-interactive, force a jumpcut. |
| 645 | final boolean forceJumpcut = !mDisplayPolicy.isScreenOnFully() |
| 646 | || !mService.mPolicy.okToAnimate(); |
| 647 | final WindowState topFullscreen = mDisplayPolicy.getTopFullscreenOpaqueWindow(); |
| 648 | if (DEBUG_ANIM) Slog.i(TAG, "selectRotationAnimation topFullscreen=" |
| 649 | + topFullscreen + " rotationAnimation=" |
| 650 | + (topFullscreen == null ? 0 : topFullscreen.getAttrs().rotationAnimation) |
| 651 | + " forceJumpcut=" + forceJumpcut); |
| 652 | if (forceJumpcut) { |
| 653 | mTmpRotationAnim.mExit = R.anim.rotation_animation_jump_exit; |
| 654 | mTmpRotationAnim.mEnter = R.anim.rotation_animation_enter; |
| 655 | return mTmpRotationAnim; |
| 656 | } |
| 657 | if (topFullscreen != null) { |
| 658 | int animationHint = topFullscreen.getRotationAnimationHint(); |
| 659 | if (animationHint < 0 && mDisplayPolicy.isTopLayoutFullscreen()) { |
| 660 | animationHint = topFullscreen.getAttrs().rotationAnimation; |
| 661 | } |
| 662 | switch (animationHint) { |
| 663 | case ROTATION_ANIMATION_CROSSFADE: |
| 664 | case ROTATION_ANIMATION_SEAMLESS: // Crossfade is fallback for seamless. |
| 665 | mTmpRotationAnim.mExit = R.anim.rotation_animation_xfade_exit; |
| 666 | mTmpRotationAnim.mEnter = R.anim.rotation_animation_enter; |
| 667 | break; |
| 668 | case ROTATION_ANIMATION_JUMPCUT: |
| 669 | mTmpRotationAnim.mExit = R.anim.rotation_animation_jump_exit; |
| 670 | mTmpRotationAnim.mEnter = R.anim.rotation_animation_enter; |
| 671 | break; |
| 672 | case ROTATION_ANIMATION_ROTATE: |
| 673 | default: |
| 674 | mTmpRotationAnim.mExit = mTmpRotationAnim.mEnter = 0; |
| 675 | break; |
| 676 | } |
| 677 | } else { |
| 678 | mTmpRotationAnim.mExit = mTmpRotationAnim.mEnter = 0; |
| 679 | } |
| 680 | return mTmpRotationAnim; |
| 681 | } |
| 682 | |
| 683 | /** |
| 684 | * Validate whether the current top fullscreen has specified the same |
| 685 | * {@link android.view.WindowManager.LayoutParams#rotationAnimation} value as that being passed |
| 686 | * in from the previous top fullscreen window. |
| 687 | * |
| 688 | * @param exitAnimId exiting resource id from the previous window. |
| 689 | * @param enterAnimId entering resource id from the previous window. |
| 690 | * @param forceDefault For rotation animations only, if true ignore the animation values and |
| 691 | * just return false. |
| 692 | * @return {@code true} if the previous values are still valid, false if they should be replaced |
| 693 | * with the default. |
| 694 | */ |
| 695 | boolean validateRotationAnimation(int exitAnimId, int enterAnimId, boolean forceDefault) { |
| 696 | switch (exitAnimId) { |
| 697 | case R.anim.rotation_animation_xfade_exit: |
| 698 | case R.anim.rotation_animation_jump_exit: |
| 699 | // These are the only cases that matter. |
| 700 | if (forceDefault) { |
| 701 | return false; |
| 702 | } |
| 703 | final RotationAnimationPair anim = selectRotationAnimation(); |
| 704 | return exitAnimId == anim.mExit && enterAnimId == anim.mEnter; |
| 705 | default: |
| 706 | return true; |
| 707 | } |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 708 | } |
| 709 | |
Garfield Tan | a3f1903 | 2019-11-19 18:04:50 -0800 | [diff] [blame] | 710 | void restoreSettings(int userRotationMode, int userRotation, int fixedToUserRotation) { |
Garfield Tan | ff36222 | 2018-11-14 17:52:32 -0800 | [diff] [blame] | 711 | mFixedToUserRotation = fixedToUserRotation; |
| 712 | |
| 713 | // We will retrieve user rotation and user rotation mode from settings for default display. |
| 714 | if (isDefaultDisplay) { |
| 715 | return; |
| 716 | } |
Garfield Tan | 90c9005 | 2018-10-08 12:29:41 -0700 | [diff] [blame] | 717 | if (userRotationMode != WindowManagerPolicy.USER_ROTATION_FREE |
| 718 | && userRotationMode != WindowManagerPolicy.USER_ROTATION_LOCKED) { |
| 719 | Slog.w(TAG, "Trying to restore an invalid user rotation mode " + userRotationMode |
| 720 | + " for " + mDisplayContent); |
| 721 | userRotationMode = WindowManagerPolicy.USER_ROTATION_FREE; |
| 722 | } |
| 723 | if (userRotation < Surface.ROTATION_0 || userRotation > Surface.ROTATION_270) { |
| 724 | Slog.w(TAG, "Trying to restore an invalid user rotation " + userRotation |
| 725 | + " for " + mDisplayContent); |
| 726 | userRotation = Surface.ROTATION_0; |
| 727 | } |
| 728 | mUserRotationMode = userRotationMode; |
| 729 | mUserRotation = userRotation; |
| 730 | } |
| 731 | |
Garfield Tan | a3f1903 | 2019-11-19 18:04:50 -0800 | [diff] [blame] | 732 | void setFixedToUserRotation(int fixedToUserRotation) { |
Garfield Tan | ff36222 | 2018-11-14 17:52:32 -0800 | [diff] [blame] | 733 | if (mFixedToUserRotation == fixedToUserRotation) { |
| 734 | return; |
| 735 | } |
| 736 | |
| 737 | mFixedToUserRotation = fixedToUserRotation; |
Garfield Tan | 7fbca05 | 2019-02-19 10:45:35 -0800 | [diff] [blame] | 738 | mDisplayWindowSettings.setFixedToUserRotation(mDisplayContent, fixedToUserRotation); |
Garfield Tan | ff36222 | 2018-11-14 17:52:32 -0800 | [diff] [blame] | 739 | mService.updateRotation(true /* alwaysSendConfiguration */, |
| 740 | false /* forceRelayout */); |
| 741 | } |
| 742 | |
Wale Ogunwale | 8a1860a | 2019-06-05 08:57:19 -0700 | [diff] [blame] | 743 | @VisibleForTesting |
| 744 | void setUserRotation(int userRotationMode, int userRotation) { |
Garfield Tan | 90c9005 | 2018-10-08 12:29:41 -0700 | [diff] [blame] | 745 | if (isDefaultDisplay) { |
| 746 | // We'll be notified via settings listener, so we don't need to update internal values. |
| 747 | final ContentResolver res = mContext.getContentResolver(); |
| 748 | final int accelerometerRotation = |
| 749 | userRotationMode == WindowManagerPolicy.USER_ROTATION_LOCKED ? 0 : 1; |
| 750 | Settings.System.putIntForUser(res, Settings.System.ACCELEROMETER_ROTATION, |
| 751 | accelerometerRotation, UserHandle.USER_CURRENT); |
| 752 | Settings.System.putIntForUser(res, Settings.System.USER_ROTATION, userRotation, |
| 753 | UserHandle.USER_CURRENT); |
| 754 | return; |
| 755 | } |
| 756 | |
| 757 | boolean changed = false; |
| 758 | if (mUserRotationMode != userRotationMode) { |
| 759 | mUserRotationMode = userRotationMode; |
| 760 | changed = true; |
| 761 | } |
| 762 | if (mUserRotation != userRotation) { |
| 763 | mUserRotation = userRotation; |
| 764 | changed = true; |
| 765 | } |
Garfield Tan | ff36222 | 2018-11-14 17:52:32 -0800 | [diff] [blame] | 766 | mDisplayWindowSettings.setUserRotation(mDisplayContent, userRotationMode, |
Chilun | 8753ad3 | 2018-10-09 15:56:45 +0800 | [diff] [blame] | 767 | userRotation); |
Garfield Tan | 90c9005 | 2018-10-08 12:29:41 -0700 | [diff] [blame] | 768 | if (changed) { |
| 769 | mService.updateRotation(true /* alwaysSendConfiguration */, |
| 770 | false /* forceRelayout */); |
Garfield Tan | 90c9005 | 2018-10-08 12:29:41 -0700 | [diff] [blame] | 771 | } |
| 772 | } |
| 773 | |
| 774 | void freezeRotation(int rotation) { |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 775 | rotation = (rotation == -1) ? mRotation : rotation; |
Garfield Tan | 90c9005 | 2018-10-08 12:29:41 -0700 | [diff] [blame] | 776 | setUserRotation(WindowManagerPolicy.USER_ROTATION_LOCKED, rotation); |
| 777 | } |
| 778 | |
| 779 | void thawRotation() { |
| 780 | setUserRotation(WindowManagerPolicy.USER_ROTATION_FREE, mUserRotation); |
| 781 | } |
| 782 | |
| 783 | boolean isRotationFrozen() { |
| 784 | if (!isDefaultDisplay) { |
| 785 | return mUserRotationMode == WindowManagerPolicy.USER_ROTATION_LOCKED; |
| 786 | } |
| 787 | |
| 788 | return Settings.System.getIntForUser(mContext.getContentResolver(), |
| 789 | Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT) == 0; |
| 790 | } |
| 791 | |
Garfield Tan | ff36222 | 2018-11-14 17:52:32 -0800 | [diff] [blame] | 792 | boolean isFixedToUserRotation() { |
Garfield Tan | 7fbca05 | 2019-02-19 10:45:35 -0800 | [diff] [blame] | 793 | switch (mFixedToUserRotation) { |
Garfield Tan | a3f1903 | 2019-11-19 18:04:50 -0800 | [diff] [blame] | 794 | case IWindowManager.FIXED_TO_USER_ROTATION_DISABLED: |
Garfield Tan | 7fbca05 | 2019-02-19 10:45:35 -0800 | [diff] [blame] | 795 | return false; |
Garfield Tan | a3f1903 | 2019-11-19 18:04:50 -0800 | [diff] [blame] | 796 | case IWindowManager.FIXED_TO_USER_ROTATION_ENABLED: |
Garfield Tan | 7fbca05 | 2019-02-19 10:45:35 -0800 | [diff] [blame] | 797 | return true; |
| 798 | default: |
| 799 | return mDefaultFixedToUserRotation; |
| 800 | } |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 801 | } |
| 802 | |
Garfield Tan | 49dae10 | 2019-02-04 09:51:59 -0800 | [diff] [blame] | 803 | /** |
| 804 | * Returns {@code true} if this display rotation takes app requested orientation into |
| 805 | * consideration; {@code false} otherwise. For the time being the only case where this is {@code |
| 806 | * false} is when {@link #isFixedToUserRotation()} is {@code true}. |
| 807 | */ |
| 808 | boolean respectAppRequestedOrientation() { |
Garfield Tan | 7fbca05 | 2019-02-19 10:45:35 -0800 | [diff] [blame] | 809 | return !isFixedToUserRotation(); |
Garfield Tan | 49dae10 | 2019-02-04 09:51:59 -0800 | [diff] [blame] | 810 | } |
| 811 | |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 812 | public int getLandscapeRotation() { |
| 813 | return mLandscapeRotation; |
| 814 | } |
| 815 | |
| 816 | public int getSeascapeRotation() { |
| 817 | return mSeascapeRotation; |
| 818 | } |
| 819 | |
| 820 | public int getPortraitRotation() { |
| 821 | return mPortraitRotation; |
| 822 | } |
| 823 | |
| 824 | public int getUpsideDownRotation() { |
| 825 | return mUpsideDownRotation; |
| 826 | } |
| 827 | |
| 828 | public int getCurrentAppOrientation() { |
| 829 | return mCurrentAppOrientation; |
| 830 | } |
| 831 | |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 832 | public DisplayPolicy getDisplayPolicy() { |
| 833 | return mDisplayPolicy; |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 834 | } |
| 835 | |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 836 | public WindowOrientationListener getOrientationListener() { |
| 837 | return mOrientationListener; |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 838 | } |
| 839 | |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 840 | public int getUserRotation() { |
| 841 | return mUserRotation; |
| 842 | } |
| 843 | |
| 844 | public int getUserRotationMode() { |
| 845 | return mUserRotationMode; |
| 846 | } |
| 847 | |
| 848 | public void updateOrientationListener() { |
| 849 | synchronized (mLock) { |
| 850 | updateOrientationListenerLw(); |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 851 | } |
| 852 | } |
| 853 | |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 854 | /** |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 855 | * Temporarily pauses rotation changes until resumed. |
| 856 | * <p> |
| 857 | * This can be used to prevent rotation changes from occurring while the user is performing |
| 858 | * certain operations, such as drag and drop. |
| 859 | * <p> |
| 860 | * This call nests and must be matched by an equal number of calls to {@link #resume}. |
| 861 | */ |
| 862 | void pause() { |
| 863 | mDeferredRotationPauseCount++; |
| 864 | } |
| 865 | |
| 866 | /** Resumes normal rotation changes after being paused. */ |
| 867 | void resume() { |
| 868 | if (mDeferredRotationPauseCount <= 0) { |
| 869 | return; |
| 870 | } |
| 871 | |
| 872 | mDeferredRotationPauseCount--; |
| 873 | if (mDeferredRotationPauseCount == 0) { |
| 874 | updateRotationAndSendNewConfigIfChanged(); |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | /** |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 879 | * Various use cases for invoking this function: |
| 880 | * <li>Screen turning off, should always disable listeners if already enabled.</li> |
| 881 | * <li>Screen turned on and current app has sensor based orientation, enable listeners |
| 882 | * if not already enabled.</li> |
| 883 | * <li>Screen turned on and current app does not have sensor orientation, disable listeners |
| 884 | * if already enabled.</li> |
| 885 | * <li>Screen turning on and current app has sensor based orientation, enable listeners |
| 886 | * if needed.</li> |
| 887 | * <li>screen turning on and current app has nosensor based orientation, do nothing.</li> |
| 888 | */ |
| 889 | private void updateOrientationListenerLw() { |
| 890 | if (mOrientationListener == null || !mOrientationListener.canDetectOrientation()) { |
| 891 | // If sensor is turned off or nonexistent for some reason. |
| 892 | return; |
| 893 | } |
| 894 | |
| 895 | final boolean screenOnEarly = mDisplayPolicy.isScreenOnEarly(); |
| 896 | final boolean awake = mDisplayPolicy.isAwake(); |
| 897 | final boolean keyguardDrawComplete = mDisplayPolicy.isKeyguardDrawComplete(); |
| 898 | final boolean windowManagerDrawComplete = mDisplayPolicy.isWindowManagerDrawComplete(); |
| 899 | |
| 900 | // Could have been invoked due to screen turning on or off or |
| 901 | // change of the currently visible window's orientation. |
Adrian Roos | b125e0b | 2019-10-02 14:55:14 +0200 | [diff] [blame] | 902 | ProtoLog.v(WM_DEBUG_ORIENTATION, |
| 903 | "screenOnEarly=%b, awake=%b, currentAppOrientation=%d, " |
| 904 | + "orientationSensorEnabled=%b, keyguardDrawComplete=%b, " |
| 905 | + "windowManagerDrawComplete=%b", |
| 906 | screenOnEarly, awake, mCurrentAppOrientation, mOrientationListener.mEnabled, |
| 907 | keyguardDrawComplete, windowManagerDrawComplete); |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 908 | |
| 909 | boolean disable = true; |
| 910 | // Note: We postpone the rotating of the screen until the keyguard as well as the |
| 911 | // window manager have reported a draw complete or the keyguard is going away in dismiss |
| 912 | // mode. |
| 913 | if (screenOnEarly && awake && ((keyguardDrawComplete && windowManagerDrawComplete))) { |
| 914 | if (needSensorRunning()) { |
| 915 | disable = false; |
| 916 | // Enable listener if not already enabled. |
| 917 | if (!mOrientationListener.mEnabled) { |
| 918 | // Don't clear the current sensor orientation if the keyguard is going away in |
| 919 | // dismiss mode. This allows window manager to use the last sensor reading to |
| 920 | // determine the orientation vs. falling back to the last known orientation if |
| 921 | // the sensor reading was cleared which can cause it to relaunch the app that |
| 922 | // will show in the wrong orientation first before correcting leading to app |
| 923 | // launch delays. |
| 924 | mOrientationListener.enable(true /* clearCurrentRotation */); |
| 925 | } |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 926 | } |
| 927 | } |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 928 | // Check if sensors need to be disabled. |
| 929 | if (disable && mOrientationListener.mEnabled) { |
| 930 | mOrientationListener.disable(); |
| 931 | } |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 932 | } |
| 933 | |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 934 | /** |
| 935 | * We always let the sensor be switched on by default except when |
| 936 | * the user has explicitly disabled sensor based rotation or when the |
| 937 | * screen is switched off. |
| 938 | */ |
| 939 | private boolean needSensorRunning() { |
Garfield Tan | 7fbca05 | 2019-02-19 10:45:35 -0800 | [diff] [blame] | 940 | if (isFixedToUserRotation()) { |
Garfield Tan | ff36222 | 2018-11-14 17:52:32 -0800 | [diff] [blame] | 941 | // We are sure we only respect user rotation settings, so we are sure we will not |
| 942 | // support sensor rotation. |
| 943 | return false; |
| 944 | } |
| 945 | |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 946 | if (mSupportAutoRotation) { |
| 947 | if (mCurrentAppOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR |
| 948 | || mCurrentAppOrientation == ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR |
| 949 | || mCurrentAppOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT |
| 950 | || mCurrentAppOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE) { |
| 951 | // If the application has explicitly requested to follow the |
| 952 | // orientation, then we need to turn the sensor on. |
| 953 | return true; |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | final int dockMode = mDisplayPolicy.getDockMode(); |
| 958 | if ((mDisplayPolicy.isCarDockEnablesAccelerometer() |
| 959 | && dockMode == Intent.EXTRA_DOCK_STATE_CAR) |
| 960 | || (mDisplayPolicy.isDeskDockEnablesAccelerometer() |
| 961 | && (dockMode == Intent.EXTRA_DOCK_STATE_DESK |
| 962 | || dockMode == Intent.EXTRA_DOCK_STATE_LE_DESK |
| 963 | || dockMode == Intent.EXTRA_DOCK_STATE_HE_DESK))) { |
| 964 | // Enable accelerometer if we are docked in a dock that enables accelerometer |
| 965 | // orientation management. |
| 966 | return true; |
| 967 | } |
| 968 | |
| 969 | if (mUserRotationMode == WindowManagerPolicy.USER_ROTATION_LOCKED) { |
| 970 | // If the setting for using the sensor by default is enabled, then |
| 971 | // we will always leave it on. Note that the user could go to |
| 972 | // a window that forces an orientation that does not use the |
| 973 | // sensor and in theory we could turn it off... however, when next |
| 974 | // turning it on we won't have a good value for the current |
| 975 | // orientation for a little bit, which can cause orientation |
| 976 | // changes to lag, so we'd like to keep it always on. (It will |
| 977 | // still be turned off when the screen is off.) |
| 978 | |
| 979 | // When locked we can provide rotation suggestions users can approve to change the |
| 980 | // current screen rotation. To do this the sensor needs to be running. |
| 981 | return mSupportAutoRotation && |
| 982 | mShowRotationSuggestions == Settings.Secure.SHOW_ROTATION_SUGGESTIONS_ENABLED; |
| 983 | } |
| 984 | return mSupportAutoRotation; |
| 985 | } |
| 986 | |
| 987 | /** |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 988 | * If this is true we have updated our desired orientation, but not yet changed the real |
| 989 | * orientation our applied our screen rotation animation. For example, because a previous |
| 990 | * screen rotation was in progress. |
| 991 | * |
| 992 | * @return {@code true} if the there is an ongoing rotation change. |
| 993 | */ |
| 994 | boolean needsUpdate() { |
| 995 | final int oldRotation = mRotation; |
| 996 | final int rotation = rotationForOrientation(mLastOrientation, oldRotation); |
| 997 | return oldRotation != rotation; |
| 998 | } |
| 999 | |
| 1000 | /** |
| 1001 | * Given an orientation constant, returns the appropriate surface rotation, taking into account |
| 1002 | * sensors, docking mode, rotation lock, and other factors. |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1003 | * |
| 1004 | * @param orientation An orientation constant, such as |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 1005 | * {@link ActivityInfo#SCREEN_ORIENTATION_LANDSCAPE}. |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1006 | * @param lastRotation The most recently used rotation. |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1007 | * @return The surface rotation to use. |
| 1008 | */ |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 1009 | @VisibleForTesting |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1010 | int rotationForOrientation(int orientation, int lastRotation) { |
Adrian Roos | b125e0b | 2019-10-02 14:55:14 +0200 | [diff] [blame] | 1011 | ProtoLog.v(WM_DEBUG_ORIENTATION, "rotationForOrientation(orient=%d, last=%d); user=%d %s", |
| 1012 | orientation, lastRotation, mUserRotation, |
| 1013 | mUserRotationMode == WindowManagerPolicy.USER_ROTATION_LOCKED |
| 1014 | ? "USER_ROTATION_LOCKED" : "" |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1015 | ); |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1016 | |
Garfield Tan | 7fbca05 | 2019-02-19 10:45:35 -0800 | [diff] [blame] | 1017 | if (isFixedToUserRotation()) { |
Garfield Tan | ff36222 | 2018-11-14 17:52:32 -0800 | [diff] [blame] | 1018 | return mUserRotation; |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | int sensorRotation = mOrientationListener != null |
| 1022 | ? mOrientationListener.getProposedRotation() // may be -1 |
| 1023 | : -1; |
| 1024 | if (sensorRotation < 0) { |
| 1025 | sensorRotation = lastRotation; |
| 1026 | } |
| 1027 | |
| 1028 | final int lidState = mDisplayPolicy.getLidState(); |
| 1029 | final int dockMode = mDisplayPolicy.getDockMode(); |
| 1030 | final boolean hdmiPlugged = mDisplayPolicy.isHdmiPlugged(); |
| 1031 | final boolean carDockEnablesAccelerometer = |
| 1032 | mDisplayPolicy.isCarDockEnablesAccelerometer(); |
| 1033 | final boolean deskDockEnablesAccelerometer = |
| 1034 | mDisplayPolicy.isDeskDockEnablesAccelerometer(); |
| 1035 | |
| 1036 | final int preferredRotation; |
| 1037 | if (!isDefaultDisplay) { |
| 1038 | // For secondary displays we ignore things like displays sensors, docking mode and |
Garfield Tan | 90c9005 | 2018-10-08 12:29:41 -0700 | [diff] [blame] | 1039 | // rotation lock, and always prefer user rotation. |
| 1040 | preferredRotation = mUserRotation; |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1041 | } else if (lidState == LID_OPEN && mLidOpenRotation >= 0) { |
| 1042 | // Ignore sensor when lid switch is open and rotation is forced. |
| 1043 | preferredRotation = mLidOpenRotation; |
| 1044 | } else if (dockMode == Intent.EXTRA_DOCK_STATE_CAR |
| 1045 | && (carDockEnablesAccelerometer || mCarDockRotation >= 0)) { |
| 1046 | // Ignore sensor when in car dock unless explicitly enabled. |
| 1047 | // This case can override the behavior of NOSENSOR, and can also |
| 1048 | // enable 180 degree rotation while docked. |
| 1049 | preferredRotation = carDockEnablesAccelerometer ? sensorRotation : mCarDockRotation; |
| 1050 | } else if ((dockMode == Intent.EXTRA_DOCK_STATE_DESK |
| 1051 | || dockMode == Intent.EXTRA_DOCK_STATE_LE_DESK |
| 1052 | || dockMode == Intent.EXTRA_DOCK_STATE_HE_DESK) |
| 1053 | && (deskDockEnablesAccelerometer || mDeskDockRotation >= 0)) { |
| 1054 | // Ignore sensor when in desk dock unless explicitly enabled. |
| 1055 | // This case can override the behavior of NOSENSOR, and can also |
| 1056 | // enable 180 degree rotation while docked. |
| 1057 | preferredRotation = deskDockEnablesAccelerometer ? sensorRotation : mDeskDockRotation; |
| 1058 | } else if (hdmiPlugged && mDemoHdmiRotationLock) { |
| 1059 | // Ignore sensor when plugged into HDMI when demo HDMI rotation lock enabled. |
| 1060 | // Note that the dock orientation overrides the HDMI orientation. |
| 1061 | preferredRotation = mDemoHdmiRotation; |
| 1062 | } else if (hdmiPlugged && dockMode == Intent.EXTRA_DOCK_STATE_UNDOCKED |
| 1063 | && mUndockedHdmiRotation >= 0) { |
| 1064 | // Ignore sensor when plugged into HDMI and an undocked orientation has |
| 1065 | // been specified in the configuration (only for legacy devices without |
| 1066 | // full multi-display support). |
| 1067 | // Note that the dock orientation overrides the HDMI orientation. |
| 1068 | preferredRotation = mUndockedHdmiRotation; |
| 1069 | } else if (mDemoRotationLock) { |
| 1070 | // Ignore sensor when demo rotation lock is enabled. |
| 1071 | // Note that the dock orientation and HDMI rotation lock override this. |
| 1072 | preferredRotation = mDemoRotation; |
| 1073 | } else if (mDisplayPolicy.isPersistentVrModeEnabled()) { |
| 1074 | // While in VR, apps always prefer a portrait rotation. This does not change |
| 1075 | // any apps that explicitly set landscape, but does cause sensors be ignored, |
| 1076 | // and ignored any orientation lock that the user has set (this conditional |
| 1077 | // should remain above the ORIENTATION_LOCKED conditional below). |
| 1078 | preferredRotation = mPortraitRotation; |
| 1079 | } else if (orientation == ActivityInfo.SCREEN_ORIENTATION_LOCKED) { |
| 1080 | // Application just wants to remain locked in the last rotation. |
| 1081 | preferredRotation = lastRotation; |
| 1082 | } else if (!mSupportAutoRotation) { |
| 1083 | // If we don't support auto-rotation then bail out here and ignore |
| 1084 | // the sensor and any rotation lock settings. |
| 1085 | preferredRotation = -1; |
| 1086 | } else if ((mUserRotationMode == WindowManagerPolicy.USER_ROTATION_FREE |
| 1087 | && (orientation == ActivityInfo.SCREEN_ORIENTATION_USER |
| 1088 | || orientation == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED |
| 1089 | || orientation == ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE |
| 1090 | || orientation == ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT |
| 1091 | || orientation == ActivityInfo.SCREEN_ORIENTATION_FULL_USER)) |
| 1092 | || orientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR |
| 1093 | || orientation == ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR |
| 1094 | || orientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE |
| 1095 | || orientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT) { |
| 1096 | // Otherwise, use sensor only if requested by the application or enabled |
| 1097 | // by default for USER or UNSPECIFIED modes. Does not apply to NOSENSOR. |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 1098 | if (mAllowAllRotations == ALLOW_ALL_ROTATIONS_UNDEFINED) { |
| 1099 | // Can't read this during init() because the context doesn't have display metrics at |
| 1100 | // that time so we cannot determine tablet vs. phone then. |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1101 | mAllowAllRotations = mContext.getResources().getBoolean( |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 1102 | R.bool.config_allowAllRotations) |
| 1103 | ? ALLOW_ALL_ROTATIONS_ENABLED |
| 1104 | : ALLOW_ALL_ROTATIONS_DISABLED; |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1105 | } |
| 1106 | if (sensorRotation != Surface.ROTATION_180 |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 1107 | || mAllowAllRotations == ALLOW_ALL_ROTATIONS_ENABLED |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1108 | || orientation == ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR |
| 1109 | || orientation == ActivityInfo.SCREEN_ORIENTATION_FULL_USER) { |
| 1110 | preferredRotation = sensorRotation; |
| 1111 | } else { |
| 1112 | preferredRotation = lastRotation; |
| 1113 | } |
| 1114 | } else if (mUserRotationMode == WindowManagerPolicy.USER_ROTATION_LOCKED |
Tetsutoki Shiozawa | 3e595fe | 2019-05-07 18:27:25 +0900 | [diff] [blame] | 1115 | && orientation != ActivityInfo.SCREEN_ORIENTATION_NOSENSOR |
| 1116 | && orientation != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE |
| 1117 | && orientation != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT |
| 1118 | && orientation != ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE |
| 1119 | && orientation != ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT) { |
| 1120 | // Apply rotation lock. Does not apply to NOSENSOR or specific rotations. |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1121 | // The idea is that the user rotation expresses a weak preference for the direction |
| 1122 | // of gravity and as NOSENSOR is never affected by gravity, then neither should |
| 1123 | // NOSENSOR be affected by rotation lock (although it will be affected by docks). |
Tetsutoki Shiozawa | 3e595fe | 2019-05-07 18:27:25 +0900 | [diff] [blame] | 1124 | // Also avoid setting user rotation when app has preference over one particular rotation |
| 1125 | // to avoid leaving the rotation to the reverse of it which has the compatible |
| 1126 | // orientation, but isn't what app wants, when the user rotation is the reverse of the |
| 1127 | // preferred rotation. |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1128 | preferredRotation = mUserRotation; |
| 1129 | } else { |
| 1130 | // No overriding preference. |
| 1131 | // We will do exactly what the application asked us to do. |
| 1132 | preferredRotation = -1; |
| 1133 | } |
| 1134 | |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1135 | switch (orientation) { |
| 1136 | case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT: |
| 1137 | // Return portrait unless overridden. |
| 1138 | if (isAnyPortrait(preferredRotation)) { |
| 1139 | return preferredRotation; |
| 1140 | } |
| 1141 | return mPortraitRotation; |
| 1142 | |
| 1143 | case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE: |
| 1144 | // Return landscape unless overridden. |
| 1145 | if (isLandscapeOrSeascape(preferredRotation)) { |
| 1146 | return preferredRotation; |
| 1147 | } |
| 1148 | return mLandscapeRotation; |
| 1149 | |
| 1150 | case ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT: |
| 1151 | // Return reverse portrait unless overridden. |
| 1152 | if (isAnyPortrait(preferredRotation)) { |
| 1153 | return preferredRotation; |
| 1154 | } |
| 1155 | return mUpsideDownRotation; |
| 1156 | |
| 1157 | case ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE: |
| 1158 | // Return seascape unless overridden. |
| 1159 | if (isLandscapeOrSeascape(preferredRotation)) { |
| 1160 | return preferredRotation; |
| 1161 | } |
| 1162 | return mSeascapeRotation; |
| 1163 | |
| 1164 | case ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE: |
| 1165 | case ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE: |
| 1166 | // Return either landscape rotation. |
| 1167 | if (isLandscapeOrSeascape(preferredRotation)) { |
| 1168 | return preferredRotation; |
| 1169 | } |
| 1170 | if (isLandscapeOrSeascape(lastRotation)) { |
| 1171 | return lastRotation; |
| 1172 | } |
| 1173 | return mLandscapeRotation; |
| 1174 | |
| 1175 | case ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT: |
| 1176 | case ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT: |
| 1177 | // Return either portrait rotation. |
| 1178 | if (isAnyPortrait(preferredRotation)) { |
| 1179 | return preferredRotation; |
| 1180 | } |
| 1181 | if (isAnyPortrait(lastRotation)) { |
| 1182 | return lastRotation; |
| 1183 | } |
| 1184 | return mPortraitRotation; |
| 1185 | |
| 1186 | default: |
| 1187 | // For USER, UNSPECIFIED, NOSENSOR, SENSOR and FULL_SENSOR, |
| 1188 | // just return the preferred orientation we already calculated. |
| 1189 | if (preferredRotation >= 0) { |
| 1190 | return preferredRotation; |
| 1191 | } |
| 1192 | return Surface.ROTATION_0; |
| 1193 | } |
| 1194 | } |
| 1195 | |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1196 | private boolean isLandscapeOrSeascape(int rotation) { |
| 1197 | return rotation == mLandscapeRotation || rotation == mSeascapeRotation; |
| 1198 | } |
| 1199 | |
| 1200 | private boolean isAnyPortrait(int rotation) { |
| 1201 | return rotation == mPortraitRotation || rotation == mUpsideDownRotation; |
| 1202 | } |
| 1203 | |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1204 | private boolean isValidRotationChoice(final int preferredRotation) { |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1205 | // Determine if the given app orientation is compatible with the provided rotation choice. |
| 1206 | switch (mCurrentAppOrientation) { |
| 1207 | case ActivityInfo.SCREEN_ORIENTATION_FULL_USER: |
| 1208 | // Works with any of the 4 rotations. |
| 1209 | return preferredRotation >= 0; |
| 1210 | |
| 1211 | case ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT: |
| 1212 | // It's possible for the user pref to be set at 180 because of FULL_USER. This would |
| 1213 | // make switching to USER_PORTRAIT appear at 180. Provide choice to back to portrait |
| 1214 | // but never to go to 180. |
| 1215 | return preferredRotation == mPortraitRotation; |
| 1216 | |
| 1217 | case ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE: |
| 1218 | // Works landscape or seascape. |
| 1219 | return isLandscapeOrSeascape(preferredRotation); |
| 1220 | |
| 1221 | case ActivityInfo.SCREEN_ORIENTATION_USER: |
| 1222 | case ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED: |
| 1223 | // Works with any rotation except upside down. |
| 1224 | return (preferredRotation >= 0) && (preferredRotation != mUpsideDownRotation); |
| 1225 | } |
| 1226 | |
| 1227 | return false; |
| 1228 | } |
| 1229 | |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1230 | private boolean isRotationChoicePossible(int orientation) { |
| 1231 | // Rotation choice is only shown when the user is in locked mode. |
| 1232 | if (mUserRotationMode != WindowManagerPolicy.USER_ROTATION_LOCKED) return false; |
| 1233 | |
| 1234 | // We should only enable rotation choice if the rotation isn't forced by the lid, dock, |
| 1235 | // demo, hdmi, vr, etc mode. |
| 1236 | |
| 1237 | // Determine if the rotation is currently forced. |
Garfield Tan | 7fbca05 | 2019-02-19 10:45:35 -0800 | [diff] [blame] | 1238 | if (isFixedToUserRotation()) { |
Garfield Tan | ff36222 | 2018-11-14 17:52:32 -0800 | [diff] [blame] | 1239 | return false; // Rotation is forced to user settings. |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1240 | } |
| 1241 | |
| 1242 | final int lidState = mDisplayPolicy.getLidState(); |
| 1243 | if (lidState == LID_OPEN && mLidOpenRotation >= 0) { |
| 1244 | return false; // Rotation is forced mLidOpenRotation. |
| 1245 | } |
| 1246 | |
| 1247 | final int dockMode = mDisplayPolicy.getDockMode(); |
| 1248 | final boolean carDockEnablesAccelerometer = false; |
| 1249 | if (dockMode == Intent.EXTRA_DOCK_STATE_CAR && !carDockEnablesAccelerometer) { |
| 1250 | return false; // Rotation forced to mCarDockRotation. |
| 1251 | } |
| 1252 | |
| 1253 | final boolean deskDockEnablesAccelerometer = |
| 1254 | mDisplayPolicy.isDeskDockEnablesAccelerometer(); |
| 1255 | if ((dockMode == Intent.EXTRA_DOCK_STATE_DESK |
| 1256 | || dockMode == Intent.EXTRA_DOCK_STATE_LE_DESK |
| 1257 | || dockMode == Intent.EXTRA_DOCK_STATE_HE_DESK) |
| 1258 | && !deskDockEnablesAccelerometer) { |
| 1259 | return false; // Rotation forced to mDeskDockRotation. |
| 1260 | } |
| 1261 | |
| 1262 | final boolean hdmiPlugged = mDisplayPolicy.isHdmiPlugged(); |
| 1263 | if (hdmiPlugged && mDemoHdmiRotationLock) { |
| 1264 | return false; // Rotation forced to mDemoHdmiRotation. |
| 1265 | |
| 1266 | } else if (hdmiPlugged && dockMode == Intent.EXTRA_DOCK_STATE_UNDOCKED |
| 1267 | && mUndockedHdmiRotation >= 0) { |
| 1268 | return false; // Rotation forced to mUndockedHdmiRotation. |
| 1269 | |
| 1270 | } else if (mDemoRotationLock) { |
| 1271 | return false; // Rotation forced to mDemoRotation. |
| 1272 | |
| 1273 | } else if (mDisplayPolicy.isPersistentVrModeEnabled()) { |
| 1274 | return false; // Rotation forced to mPortraitRotation. |
| 1275 | |
| 1276 | } else if (!mSupportAutoRotation) { |
| 1277 | return false; |
| 1278 | } |
| 1279 | |
| 1280 | // Ensure that some rotation choice is possible for the given orientation. |
| 1281 | switch (orientation) { |
| 1282 | case ActivityInfo.SCREEN_ORIENTATION_FULL_USER: |
| 1283 | case ActivityInfo.SCREEN_ORIENTATION_USER: |
| 1284 | case ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED: |
| 1285 | case ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE: |
| 1286 | case ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT: |
| 1287 | // NOSENSOR description is ambiguous, in reality WM ignores user choice. |
| 1288 | return true; |
| 1289 | } |
| 1290 | |
| 1291 | // Rotation is forced, should be controlled by system. |
| 1292 | return false; |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1293 | } |
| 1294 | |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1295 | /** Notify the StatusBar that system rotation suggestion has changed. */ |
| 1296 | private void sendProposedRotationChangeToStatusBarInternal(int rotation, boolean isValid) { |
| 1297 | if (mStatusBarManagerInternal == null) { |
| 1298 | mStatusBarManagerInternal = LocalServices.getService(StatusBarManagerInternal.class); |
| 1299 | } |
| 1300 | if (mStatusBarManagerInternal != null) { |
| 1301 | mStatusBarManagerInternal.onProposedRotationChanged(rotation, isValid); |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | private static String allowAllRotationsToString(int allowAll) { |
| 1306 | switch (allowAll) { |
| 1307 | case -1: |
| 1308 | return "unknown"; |
| 1309 | case 0: |
| 1310 | return "false"; |
| 1311 | case 1: |
| 1312 | return "true"; |
| 1313 | default: |
| 1314 | return Integer.toString(allowAll); |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | public void onUserSwitch() { |
| 1319 | if (mSettingsObserver != null) { |
| 1320 | mSettingsObserver.onChange(false); |
| 1321 | } |
| 1322 | } |
| 1323 | |
| 1324 | /** Return whether the rotation settings has changed. */ |
| 1325 | private boolean updateSettings() { |
| 1326 | final ContentResolver resolver = mContext.getContentResolver(); |
| 1327 | boolean shouldUpdateRotation = false; |
| 1328 | |
| 1329 | synchronized (mLock) { |
| 1330 | boolean shouldUpdateOrientationListener = false; |
| 1331 | |
| 1332 | // Configure rotation suggestions. |
Rajeev Kumar | b2ff8e8 | 2018-08-06 11:45:05 -0700 | [diff] [blame] | 1333 | final int showRotationSuggestions = |
| 1334 | ActivityManager.isLowRamDeviceStatic() |
| 1335 | ? Settings.Secure.SHOW_ROTATION_SUGGESTIONS_DISABLED |
| 1336 | : Settings.Secure.getIntForUser(resolver, |
| 1337 | Settings.Secure.SHOW_ROTATION_SUGGESTIONS, |
| 1338 | Settings.Secure.SHOW_ROTATION_SUGGESTIONS_DEFAULT, |
| 1339 | UserHandle.USER_CURRENT); |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1340 | if (mShowRotationSuggestions != showRotationSuggestions) { |
| 1341 | mShowRotationSuggestions = showRotationSuggestions; |
| 1342 | shouldUpdateOrientationListener = true; |
| 1343 | } |
| 1344 | |
| 1345 | // Configure rotation lock. |
| 1346 | final int userRotation = Settings.System.getIntForUser(resolver, |
| 1347 | Settings.System.USER_ROTATION, Surface.ROTATION_0, |
| 1348 | UserHandle.USER_CURRENT); |
| 1349 | if (mUserRotation != userRotation) { |
| 1350 | mUserRotation = userRotation; |
| 1351 | shouldUpdateRotation = true; |
| 1352 | } |
| 1353 | |
| 1354 | final int userRotationMode = Settings.System.getIntForUser(resolver, |
| 1355 | Settings.System.ACCELEROMETER_ROTATION, 0, UserHandle.USER_CURRENT) != 0 |
| 1356 | ? WindowManagerPolicy.USER_ROTATION_FREE |
| 1357 | : WindowManagerPolicy.USER_ROTATION_LOCKED; |
| 1358 | if (mUserRotationMode != userRotationMode) { |
| 1359 | mUserRotationMode = userRotationMode; |
| 1360 | shouldUpdateOrientationListener = true; |
| 1361 | shouldUpdateRotation = true; |
| 1362 | } |
| 1363 | |
| 1364 | if (shouldUpdateOrientationListener) { |
| 1365 | updateOrientationListenerLw(); // Enable or disable the orientation listener. |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | return shouldUpdateRotation; |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | void dump(String prefix, PrintWriter pw) { |
| 1373 | pw.println(prefix + "DisplayRotation"); |
| 1374 | pw.println(prefix + " mCurrentAppOrientation=" |
| 1375 | + ActivityInfo.screenOrientationToString(mCurrentAppOrientation)); |
Riddle Hsu | ccf0940 | 2019-08-13 00:33:06 +0800 | [diff] [blame] | 1376 | pw.println(prefix + " mLastOrientation=" + mLastOrientation); |
| 1377 | pw.print(prefix + " mRotation=" + mRotation); |
| 1378 | pw.println(" mDeferredRotationPauseCount=" + mDeferredRotationPauseCount); |
| 1379 | |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1380 | pw.print(prefix + " mLandscapeRotation=" + Surface.rotationToString(mLandscapeRotation)); |
| 1381 | pw.println(" mSeascapeRotation=" + Surface.rotationToString(mSeascapeRotation)); |
| 1382 | pw.print(prefix + " mPortraitRotation=" + Surface.rotationToString(mPortraitRotation)); |
| 1383 | pw.println(" mUpsideDownRotation=" + Surface.rotationToString(mUpsideDownRotation)); |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1384 | |
Tiger Huang | 7c610aa | 2018-10-27 00:01:01 +0800 | [diff] [blame] | 1385 | pw.println(prefix + " mSupportAutoRotation=" + mSupportAutoRotation); |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1386 | if (mOrientationListener != null) { |
Tiger Huang | 7c610aa | 2018-10-27 00:01:01 +0800 | [diff] [blame] | 1387 | mOrientationListener.dump(pw, prefix + " "); |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1388 | } |
| 1389 | pw.println(); |
| 1390 | |
| 1391 | pw.print(prefix + " mCarDockRotation=" + Surface.rotationToString(mCarDockRotation)); |
| 1392 | pw.println(" mDeskDockRotation=" + Surface.rotationToString(mDeskDockRotation)); |
| 1393 | pw.print(prefix + " mUserRotationMode=" |
| 1394 | + WindowManagerPolicy.userRotationModeToString(mUserRotationMode)); |
| 1395 | pw.print(" mUserRotation=" + Surface.rotationToString(mUserRotation)); |
| 1396 | pw.println(" mAllowAllRotations=" + allowAllRotationsToString(mAllowAllRotations)); |
| 1397 | |
| 1398 | pw.print(prefix + " mDemoHdmiRotation=" + Surface.rotationToString(mDemoHdmiRotation)); |
| 1399 | pw.print(" mDemoHdmiRotationLock=" + mDemoHdmiRotationLock); |
| 1400 | pw.println(" mUndockedHdmiRotation=" + Surface.rotationToString(mUndockedHdmiRotation)); |
| 1401 | pw.println(prefix + " mLidOpenRotation=" + Surface.rotationToString(mLidOpenRotation)); |
Garfield Tan | 7fbca05 | 2019-02-19 10:45:35 -0800 | [diff] [blame] | 1402 | pw.println(prefix + " mFixedToUserRotation=" + isFixedToUserRotation()); |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | private class OrientationListener extends WindowOrientationListener { |
| 1406 | final SparseArray<Runnable> mRunnableCache = new SparseArray<>(5); |
| 1407 | boolean mEnabled; |
| 1408 | |
| 1409 | OrientationListener(Context context, Handler handler) { |
| 1410 | super(context, handler); |
| 1411 | } |
| 1412 | |
| 1413 | private class UpdateRunnable implements Runnable { |
| 1414 | final int mRotation; |
| 1415 | |
| 1416 | UpdateRunnable(int rotation) { |
| 1417 | mRotation = rotation; |
| 1418 | } |
| 1419 | |
| 1420 | @Override |
| 1421 | public void run() { |
| 1422 | // Send interaction hint to improve redraw performance. |
| 1423 | mService.mPowerManagerInternal.powerHint(PowerHint.INTERACTION, 0); |
| 1424 | if (isRotationChoicePossible(mCurrentAppOrientation)) { |
| 1425 | final boolean isValid = isValidRotationChoice(mRotation); |
| 1426 | sendProposedRotationChangeToStatusBarInternal(mRotation, isValid); |
| 1427 | } else { |
| 1428 | mService.updateRotation(false /* alwaysSendConfiguration */, |
| 1429 | false /* forceRelayout */); |
| 1430 | } |
| 1431 | } |
| 1432 | } |
| 1433 | |
| 1434 | @Override |
| 1435 | public void onProposedRotationChanged(int rotation) { |
Adrian Roos | b125e0b | 2019-10-02 14:55:14 +0200 | [diff] [blame] | 1436 | ProtoLog.v(WM_DEBUG_ORIENTATION, "onProposedRotationChanged, rotation=%d", rotation); |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1437 | Runnable r = mRunnableCache.get(rotation, null); |
| 1438 | if (r == null) { |
| 1439 | r = new UpdateRunnable(rotation); |
| 1440 | mRunnableCache.put(rotation, r); |
| 1441 | } |
| 1442 | getHandler().post(r); |
| 1443 | } |
| 1444 | |
| 1445 | @Override |
| 1446 | public void enable(boolean clearCurrentRotation) { |
| 1447 | super.enable(clearCurrentRotation); |
| 1448 | mEnabled = true; |
Adrian Roos | b125e0b | 2019-10-02 14:55:14 +0200 | [diff] [blame] | 1449 | ProtoLog.v(WM_DEBUG_ORIENTATION, "Enabling listeners"); |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1450 | } |
| 1451 | |
| 1452 | @Override |
| 1453 | public void disable() { |
| 1454 | super.disable(); |
| 1455 | mEnabled = false; |
Adrian Roos | b125e0b | 2019-10-02 14:55:14 +0200 | [diff] [blame] | 1456 | ProtoLog.v(WM_DEBUG_ORIENTATION, "Disabling listeners"); |
Riddle Hsu | 5ce4bb3 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1457 | } |
| 1458 | } |
| 1459 | |
| 1460 | private class SettingsObserver extends ContentObserver { |
| 1461 | SettingsObserver(Handler handler) { |
| 1462 | super(handler); |
| 1463 | } |
| 1464 | |
| 1465 | void observe() { |
| 1466 | final ContentResolver resolver = mContext.getContentResolver(); |
| 1467 | resolver.registerContentObserver(Settings.Secure.getUriFor( |
| 1468 | Settings.Secure.SHOW_ROTATION_SUGGESTIONS), false, this, |
| 1469 | UserHandle.USER_ALL); |
| 1470 | resolver.registerContentObserver(Settings.System.getUriFor( |
| 1471 | Settings.System.ACCELEROMETER_ROTATION), false, this, |
| 1472 | UserHandle.USER_ALL); |
| 1473 | resolver.registerContentObserver(Settings.System.getUriFor( |
| 1474 | Settings.System.USER_ROTATION), false, this, |
| 1475 | UserHandle.USER_ALL); |
| 1476 | updateSettings(); |
| 1477 | } |
| 1478 | |
| 1479 | @Override |
| 1480 | public void onChange(boolean selfChange) { |
| 1481 | if (updateSettings()) { |
| 1482 | mService.updateRotation(true /* alwaysSendConfiguration */, |
| 1483 | false /* forceRelayout */); |
| 1484 | } |
| 1485 | } |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1486 | } |
Garfield Tan | ff36222 | 2018-11-14 17:52:32 -0800 | [diff] [blame] | 1487 | |
| 1488 | @VisibleForTesting |
| 1489 | interface ContentObserverRegister { |
| 1490 | void registerContentObserver(Uri uri, boolean notifyForDescendants, |
| 1491 | ContentObserver observer, @UserIdInt int userHandle); |
| 1492 | } |
Riddle Hsu | ad256a1 | 2018-07-18 16:11:30 +0800 | [diff] [blame] | 1493 | } |