blob: d0d98fed04a64ca53c46cdaac5582388e4268610 [file] [log] [blame]
Craig Mautner59c00972012-07-30 12:10:24 -07001/*
2 * Copyright (C) 2012 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
Wale Ogunwale65ebd952018-04-25 15:41:44 -070019import static android.app.ActivityTaskManager.SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT;
Wale Ogunwale61911492017-10-11 08:50:50 -070020import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070021import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070022import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070023import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
24import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwalea77e1462016-09-28 10:09:46 -070025import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070026import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
27import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale687b4272017-07-27 02:56:23 -070028import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
29import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070030import static android.view.Display.DEFAULT_DISPLAY;
31import static android.view.Display.FLAG_PRIVATE;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070032import static android.view.Surface.ROTATION_0;
33import static android.view.Surface.ROTATION_180;
34import static android.view.Surface.ROTATION_270;
35import static android.view.Surface.ROTATION_90;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070036import static android.view.View.GONE;
Wale Ogunwale10124582016-09-15 20:25:50 -070037import static android.view.WindowManager.DOCKED_BOTTOM;
38import static android.view.WindowManager.DOCKED_INVALID;
39import static android.view.WindowManager.DOCKED_TOP;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080040import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
41import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
42import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale494009b82016-10-21 09:01:38 -070043import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070044import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070045import static android.view.WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE;
46import static android.view.WindowManager.LayoutParams.NEEDS_MENU_UNSET;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070047import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale494009b82016-10-21 09:01:38 -070048import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070049import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale494009b82016-10-21 09:01:38 -070050import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
Robert Carrf7a7ca82017-12-06 13:17:07 -080051import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
Wale Ogunwale494009b82016-10-21 09:01:38 -070052import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070053import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070054import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
55import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Wale Ogunwale494009b82016-10-21 09:01:38 -070056import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070057import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
58import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070059import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070060import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080061import static android.view.WindowManager.TRANSIT_KEYGUARD_UNOCCLUDE;
62
Adrian Roose99bc052017-11-20 17:55:31 +010063import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
64import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
65import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
66import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080067import static com.android.server.wm.DisplayContentProto.ABOVE_APP_WINDOWS;
68import static com.android.server.wm.DisplayContentProto.BELOW_APP_WINDOWS;
69import static com.android.server.wm.DisplayContentProto.DISPLAY_FRAMES;
70import static com.android.server.wm.DisplayContentProto.DISPLAY_INFO;
71import static com.android.server.wm.DisplayContentProto.DOCKED_STACK_DIVIDER_CONTROLLER;
72import static com.android.server.wm.DisplayContentProto.DPI;
73import static com.android.server.wm.DisplayContentProto.ID;
74import static com.android.server.wm.DisplayContentProto.IME_WINDOWS;
75import static com.android.server.wm.DisplayContentProto.PINNED_STACK_CONTROLLER;
76import static com.android.server.wm.DisplayContentProto.ROTATION;
77import static com.android.server.wm.DisplayContentProto.SCREEN_ROTATION_ANIMATION;
78import static com.android.server.wm.DisplayContentProto.STACKS;
79import static com.android.server.wm.DisplayContentProto.WINDOW_CONTAINER;
Wale Ogunwale1666e312016-12-16 11:27:18 -080080import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070081import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070082import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070083import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
84import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070085import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070086import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070087import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020088import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070089import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070090import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070091import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -080092import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070093import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070094import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070095import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070096import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070097import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080098import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -070099import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700100import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
101import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700102import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700103import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700104import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
105import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700106import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700107import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700108import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700109import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700110import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700111import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700112import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700113import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700114import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700115import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Riddle Hsua4d6fa22018-08-11 00:50:39 +0800116import static com.android.server.wm.utils.CoordinateTransforms.transformPhysicalToLogicalCoordinates;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700117
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700118import android.annotation.CallSuper;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700119import android.annotation.NonNull;
Dean Harding3e5a1522017-10-06 09:19:01 -0700120import android.content.pm.PackageManager;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700121import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700122import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700123import android.graphics.Bitmap;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700124import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800125import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700126import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700127import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100128import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700129import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700130import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700131import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700132import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700133import android.os.RemoteException;
134import android.os.SystemClock;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100135import android.os.Trace;
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800136import android.util.ArraySet;
Chong Zhang8e89b312015-09-09 15:09:30 -0700137import android.util.DisplayMetrics;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700138import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700139import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700140import android.view.Display;
Adrian Roos1cf585052018-01-03 18:43:27 +0100141import android.view.DisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700142import android.view.DisplayInfo;
Riddle Hsua4d6fa22018-08-11 00:50:39 +0800143import android.view.Gravity;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700144import android.view.InputDevice;
Robert Carrb1579c82017-09-05 14:54:47 -0700145import android.view.MagnificationSpec;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700146import android.view.Surface;
147import android.view.SurfaceControl;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100148import android.view.SurfaceControl.Transaction;
Robert Carrb1579c82017-09-05 14:54:47 -0700149import android.view.SurfaceSession;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700150
Bryce Lee48f4b572017-04-10 10:54:15 -0700151import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800152import com.android.internal.util.ToBooleanFunction;
Adrian Roose99bc052017-11-20 17:55:31 +0100153import com.android.server.policy.WindowManagerPolicy;
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100154import com.android.server.wm.utils.RotationCache;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100155import com.android.server.wm.utils.WmDisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700156
157import java.io.PrintWriter;
158import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700159import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700160import java.util.HashMap;
161import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700162import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700163import java.util.List;
Adrian Roos1cf585052018-01-03 18:43:27 +0100164import java.util.Objects;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800165import java.util.function.Consumer;
166import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700167
Craig Mautner59c00972012-07-30 12:10:24 -0700168/**
169 * Utility class for keeping track of the WindowStates and other pertinent contents of a
170 * particular Display.
171 *
172 * IMPORTANT: No method from this class should ever be used without holding
173 * WindowManagerService.mWindowMap.
174 */
Riddle Hsuad256a12018-07-18 16:11:30 +0800175class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer>
176 implements WindowManagerPolicy.DisplayContentInfo {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700177 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700178
179 /** Unique identifier of this stack. */
180 private final int mDisplayId;
181
Wale Ogunwale3a931692016-11-02 16:49:48 -0700182 /** The containers below are the only child containers the display can have. */
183 // Contains all window containers that are related to apps (Activities)
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100184 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers(mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700185 // Contains all non-app window containers that should be displayed above the app containers
186 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800187 private final AboveAppWindowContainers mAboveAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100188 new AboveAppWindowContainers("mAboveAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700189 // Contains all non-app window containers that should be displayed below the app containers
190 // (e.g. Wallpaper).
191 private final NonAppWindowContainers mBelowAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100192 new NonAppWindowContainers("mBelowAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700193 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
194 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
Robert Carr9034d962018-01-04 18:27:42 -0800195 // window containers together and move them in-sync if/when needed. We use a subclass of
196 // WindowContainer which is omitted from screen magnification, as the IME is never magnified.
197 private final NonMagnifiableWindowContainers mImeWindowsContainers =
198 new NonMagnifiableWindowContainers("mImeWindowsContainers", mService);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700199
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000200 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800201 private WindowState mTmpWindow2;
202 private WindowAnimator mTmpWindowAnimator;
203 private boolean mTmpRecoveringMemory;
204 private boolean mUpdateImeTarget;
205 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700206 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700207
Wale Ogunwale02319a62016-09-26 15:21:22 -0700208 // Mapping from a token IBinder to a WindowToken object on this display.
209 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
210
Andrii Kuliancd097992017-03-23 18:31:59 -0700211 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700212 int mInitialDisplayWidth = 0;
213 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700214 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700215
Adrian Roos1cf585052018-01-03 18:43:27 +0100216 DisplayCutout mInitialDisplayCutout;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100217 private final RotationCache<DisplayCutout, WmDisplayCutout> mDisplayCutoutCache
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100218 = new RotationCache<>(this::calculateDisplayCutoutForRotationUncached);
Adrian Roos1cf585052018-01-03 18:43:27 +0100219
Andrii Kuliancd097992017-03-23 18:31:59 -0700220 /**
221 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
222 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
223 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
224 */
Craig Mautner59c00972012-07-30 12:10:24 -0700225 int mBaseDisplayWidth = 0;
226 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700227 /**
228 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
229 * but can be set from Settings or via shell command "adb shell wm density".
230 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
231 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700232 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700233 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700234 private final DisplayInfo mDisplayInfo = new DisplayInfo();
235 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700236 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800237 private final DisplayPolicy mDisplayPolicy;
238 private DisplayRotation mDisplayRotation;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000239 DisplayFrames mDisplayFrames;
240
Andrii Kulian06d07d62017-03-14 11:11:47 -0700241 /**
242 * For default display it contains real metrics, empty for others.
243 * @see WindowManagerService#createWatermarkInTransaction()
244 */
245 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
246 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
247 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700248
Andrii Kulian06d07d62017-03-14 11:11:47 -0700249 /**
250 * Compat metrics computed based on {@link #mDisplayMetrics}.
251 * @see #updateDisplayAndOrientation(int)
252 */
253 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
254
255 /** The desired scaling factor for compatible apps. */
256 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700257
Andrii Kulian8ee72852017-03-10 10:36:45 -0800258 /**
259 * Current rotation of the display.
260 * Constants as per {@link android.view.Surface.Rotation}.
261 *
Robert Carrae606b42018-02-15 15:36:23 -0800262 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800263 */
264 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700265
Andrii Kulian8ee72852017-03-10 10:36:45 -0800266 /**
267 * Last applied orientation of the display.
268 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
269 *
270 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
271 */
272 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700273
Andrii Kulian8ee72852017-03-10 10:36:45 -0800274 /**
275 * Flag indicating that the application is receiving an orientation that has different metrics
276 * than it expected. E.g. Portrait instead of Landscape.
277 *
Robert Carrae606b42018-02-15 15:36:23 -0800278 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800279 */
280 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700281
Andrii Kulian8ee72852017-03-10 10:36:45 -0800282 /**
283 * Orientation forced by some window. If there is no visible window that specifies orientation
284 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
285 *
286 * @see NonAppWindowContainers#getOrientation()
287 */
288 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700289
Andrii Kulian8ee72852017-03-10 10:36:45 -0800290 /**
291 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
292 * occluded.
293 *
294 * @see NonAppWindowContainers#getOrientation()
295 */
296 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
297
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700298 /**
299 * Keep track of wallpaper visibility to notify changes.
300 */
301 private boolean mLastWallpaperVisible = false;
302
Andrii Kulian06d07d62017-03-14 11:11:47 -0700303 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700304
Craig Mautner39834192012-09-02 07:47:24 -0700305 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700306 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700307 int pendingLayoutChanges;
Riddle Hsu654a6f92018-07-13 22:59:36 +0800308 int mDeferredRotationPauseCount;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800309
Andrii Kulian839def92016-11-02 10:58:58 -0700310 // TODO(multi-display): remove some of the usages.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800311 @VisibleForTesting
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800312 boolean isDefaultDisplay;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800313
Andrii Kulianf0379de2018-03-14 16:24:07 -0700314 /**
315 * Flag indicating whether WindowManager should override info for this display in
316 * DisplayManager.
317 */
318 boolean mShouldOverrideDisplayConfiguration = true;
Craig Mautner39834192012-09-02 07:47:24 -0700319
Craig Mautnerdc548482014-02-05 13:35:24 -0800320 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700321 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800322
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700323 /** Detect user tapping outside of current focused task bounds .*/
324 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700325
Craig Mautner6601b7b2013-04-29 10:29:11 -0700326 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700327 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700328
Craig Mautner6601b7b2013-04-29 10:29:11 -0700329 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800330 private final Rect mTmpRect = new Rect();
331 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700332 private final RectF mTmpRectF = new RectF();
333 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800334 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700335
Bryce Leef3c6a472017-11-14 14:53:06 -0800336 /** Used for handing back size of display */
337 private final Rect mTmpBounds = new Rect();
338
Craig Mautner95da1082014-02-24 17:54:35 -0800339 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700340 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800341
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700342 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700343 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700344
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800345 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800346 /** A collection of windows that provide tap exclude regions inside of them. */
347 final ArraySet<WindowState> mTapExcludeProvidingWindows = new ArraySet<>();
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800348
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000349 private boolean mHaveBootMsg = false;
350 private boolean mHaveApp = false;
351 private boolean mHaveWallpaper = false;
352 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700353
354 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
355
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700356 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
357 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000358 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
359 new ApplySurfaceChangesTransactionState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700360
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700361 // True if this display is in the process of being removed. Used to determine if the removal of
362 // the display's direct children should be allowed.
363 private boolean mRemovingDisplay = false;
364
Bryce Leed1871262017-06-12 14:12:29 -0700365 // {@code false} if this display is in the processing of being created.
366 private boolean mDisplayReady = false;
367
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800368 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700369
Robert Carrb1579c82017-09-05 14:54:47 -0700370 private final SurfaceSession mSession = new SurfaceSession();
371
372 /**
373 * We organize all top-level Surfaces in to the following layers.
374 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800375 * and omitted from Screen-Magnification, for example the strict mode flash or
376 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700377 * {@link #mWindowingLayer} contains everything else.
378 */
379 private SurfaceControl mOverlayLayer;
380
381 /**
382 * See {@link #mOverlayLayer}
383 */
384 private SurfaceControl mWindowingLayer;
385
386 /**
387 * Specifies the size of the surfaces in {@link #mOverlayLayer} and {@link #mWindowingLayer}.
388 * <p>
389 * For these surfaces currently we use a surface based on the larger of width or height so we
390 * don't have to resize when rotating the display.
391 */
392 private int mSurfaceSize;
393
Adrian Roos5251b1d2018-03-23 18:57:43 +0100394 /**
395 * Sequence number for the current layout pass.
396 */
397 int mLayoutSeq = 0;
398
Chavi Weingarten3a748552018-05-14 17:32:42 +0000399 /**
400 * Specifies the count to determine whether to defer updating the IME target until ready.
401 */
402 private int mDeferUpdateImeTargetCount;
403
Jorim Jaggi9af095b2017-12-12 17:18:57 +0100404 /** Temporary float array to retrieve 3x3 matrix values. */
405 private final float[] mTmpFloats = new float[9];
406
Robert Carr24be9ab2018-04-30 17:54:53 -0700407 private MagnificationSpec mMagnificationSpec;
408
Arthur Hung95b38a92018-07-20 18:56:12 +0800409 private InputMonitor mInputMonitor;
410
lumark90120a82018-08-15 00:33:03 +0800411 /**
412 * The input method window for this display.
413 */
414 WindowState mInputMethodWindow;
415
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800416 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
417 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800418 final AppWindowToken atoken = w.mAppToken;
419 if (winAnimator.mDrawState == READY_TO_SHOW) {
Jorim Jaggiaf0d6d22018-06-08 15:25:35 +0200420 if (atoken == null || atoken.canShowWindows()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800421 if (w.performShowLocked()) {
422 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
423 if (DEBUG_LAYOUT_REPEATS) {
424 mService.mWindowPlacerLocked.debugLayoutRepeats(
425 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
426 }
427 }
428 }
429 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800430 };
431
432 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
433 final WindowStateAnimator winAnimator = w.mWinAnimator;
434 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
435 return;
436 }
437
438 final int flags = w.mAttrs.flags;
439
440 // If this window is animating, make a note that we have an animating window and take
441 // care of a request to run a detached wallpaper animation.
Jorim Jaggia5e10572017-11-15 14:36:26 +0100442 if (winAnimator.isAnimationSet()) {
443 final AnimationAdapter anim = w.getAnimation();
444 if (anim != null) {
445 if ((flags & FLAG_SHOW_WALLPAPER) != 0 && anim.getDetachWallpaper()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800446 mTmpWindow = w;
447 }
Jorim Jaggia5e10572017-11-15 14:36:26 +0100448 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800449 if (color != 0) {
450 final TaskStack stack = w.getStack();
451 if (stack != null) {
452 stack.setAnimationBackground(winAnimator, color);
453 }
454 }
455 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800456 }
457
458 // If this window's app token is running a detached wallpaper animation, make a note so
459 // we can ensure the wallpaper is displayed behind it.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200460 final AppWindowToken atoken = winAnimator.mWin.mAppToken;
461 final AnimationAdapter animation = atoken != null ? atoken.getAnimation() : null;
462 if (animation != null) {
463 if ((flags & FLAG_SHOW_WALLPAPER) != 0 && animation.getDetachWallpaper()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800464 mTmpWindow = w;
465 }
466
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200467 final int color = animation.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800468 if (color != 0) {
469 final TaskStack stack = w.getStack();
470 if (stack != null) {
471 stack.setAnimationBackground(winAnimator, color);
472 }
473 }
474 }
475 };
476
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800477 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
478 final int lostFocusUid = mTmpWindow.mOwnerUid;
479 final Handler handler = mService.mH;
480 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
481 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
482 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
483 w.mAttrs.hideTimeoutMilliseconds);
484 }
485 }
486 };
487
488 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
489 final AppWindowToken focusedApp = mService.mFocusedApp;
490 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
491 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
492
493 if (!w.canReceiveKeys()) {
494 return false;
495 }
496
497 final AppWindowToken wtoken = w.mAppToken;
498
499 // If this window's application has been removed, just skip it.
500 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
501 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
502 + (wtoken.removed ? "removed" : "sendingToBottom"));
503 return false;
504 }
505
506 if (focusedApp == null) {
507 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
508 + " using new focus @ " + w);
509 mTmpWindow = w;
510 return true;
511 }
512
513 if (!focusedApp.windowsAreFocusable()) {
514 // Current focused app windows aren't focusable...
515 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
516 + " focusable using new focus @ " + w);
517 mTmpWindow = w;
518 return true;
519 }
520
521 // Descend through all of the app tokens and find the first that either matches
522 // win.mAppToken (return win) or mFocusedApp (return null).
523 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
524 if (focusedApp.compareTo(wtoken) > 0) {
525 // App stack below focused app stack. No focus for you!!!
526 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
527 "findFocusedWindow: Reached focused app=" + focusedApp);
528 mTmpWindow = null;
529 return true;
530 }
531 }
532
533 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
534 mTmpWindow = w;
535 return true;
536 };
537
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800538 private final Consumer<WindowState> mPerformLayout = w -> {
539 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
540 // wasting time and funky changes while a window is animating away.
541 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
542 || w.isGoneForLayoutLw();
543
544 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
545 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
546 + " mLayoutAttached=" + w.mLayoutAttached
547 + " screen changed=" + w.isConfigChanged());
548 final AppWindowToken atoken = w.mAppToken;
549 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200550 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800551 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
552 + " parentHidden=" + w.isParentWindowHidden());
553 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200554 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800555 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
556 + " parentHidden=" + w.isParentWindowHidden());
557 }
558
559 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
560 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
561 // since that means "perform layout as normal, just don't display").
562 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
563 || ((w.isConfigChanged() || w.setReportResizeHints())
564 && !w.isGoneForLayoutLw() &&
565 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
566 (w.mHasSurface && w.mAppToken != null &&
567 w.mAppToken.layoutConfigChanges)))) {
568 if (!w.mLayoutAttached) {
569 if (mTmpInitial) {
570 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
571 w.mContentChanged = false;
572 }
573 if (w.mAttrs.type == TYPE_DREAM) {
574 // Don't layout windows behind a dream, so that if it does stuff like hide
575 // the status bar we won't get a bad transition when it goes away.
576 mTmpWindow = w;
577 }
578 w.mLayoutNeeded = false;
579 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200580 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000581 mService.mPolicy.layoutWindowLw(w, null, mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100582 w.mLayoutSeq = mLayoutSeq;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800583
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200584 // If this is the first layout, we need to initialize the last inset values as
585 // otherwise we'd immediately cause an unnecessary resize.
586 if (firstLayout) {
587 w.updateLastInsetValues();
588 }
589
Adrian Roos23df3a32018-03-15 15:41:13 +0100590 if (w.mAppToken != null) {
591 w.mAppToken.layoutLetterbox(w);
592 }
593
chaviw492139a2018-07-16 16:07:35 -0700594 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700595 + " mContainingFrame=" + w.getContainingFrame()
596 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800597 }
598 }
599 };
600
601 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
602 if (w.mLayoutAttached) {
603 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
604 + " mViewVisibility=" + w.mViewVisibility
605 + " mRelayoutCalled=" + w.mRelayoutCalled);
606 // If this view is GONE, then skip it -- keep the current frame, and let the caller
607 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
608 // windows, since that means "perform layout as normal, just don't display").
609 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
610 return;
611 }
612 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
613 || w.mLayoutNeeded) {
614 if (mTmpInitial) {
615 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
616 w.mContentChanged = false;
617 }
618 w.mLayoutNeeded = false;
619 w.prelayout();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000620 mService.mPolicy.layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100621 w.mLayoutSeq = mLayoutSeq;
chaviw492139a2018-07-16 16:07:35 -0700622 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700623 + " mContainingFrame=" + w.getContainingFrame()
624 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800625 }
626 } else if (w.mAttrs.type == TYPE_DREAM) {
627 // Don't layout windows behind a dream, so that if it does stuff like hide the
628 // status bar we won't get a bad transition when it goes away.
629 mTmpWindow = mTmpWindow2;
630 }
631 };
632
633 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
634 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
635 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
636 return w.canBeImeTarget();
637 };
638
639 private final Consumer<WindowState> mApplyPostLayoutPolicy =
640 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
641 mService.mInputMethodTarget);
642
643 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
644 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
645 final boolean obscuredChanged = w.mObscured !=
646 mTmpApplySurfaceChangesTransactionState.obscured;
647 final RootWindowContainer root = mService.mRoot;
648 // Only used if default window
649 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
650
651 // Update effect.
652 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
653 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
654 final boolean isDisplayed = w.isDisplayedLw();
655
656 if (isDisplayed && w.isObscuringDisplay()) {
657 // This window completely covers everything behind it, so we want to leave all
658 // of them as undimmed (for performance reasons).
659 root.mObscuringWindow = w;
660 mTmpApplySurfaceChangesTransactionState.obscured = true;
661 }
662
663 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
664 root.handleNotObscuredLocked(w,
665 mTmpApplySurfaceChangesTransactionState.obscured,
666 mTmpApplySurfaceChangesTransactionState.syswin);
667
668 if (w.mHasSurface && isDisplayed) {
669 final int type = w.mAttrs.type;
670 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
671 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
672 mTmpApplySurfaceChangesTransactionState.syswin = true;
673 }
674 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
675 && w.mAttrs.preferredRefreshRate != 0) {
676 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
677 = w.mAttrs.preferredRefreshRate;
678 }
679 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
680 && w.mAttrs.preferredDisplayModeId != 0) {
681 mTmpApplySurfaceChangesTransactionState.preferredModeId
682 = w.mAttrs.preferredDisplayModeId;
683 }
684 }
685 }
686
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800687 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
688 && mWallpaperController.isWallpaperTarget(w)) {
689 // This is the wallpaper target and its obscured state changed... make sure the
690 // current wallpaper's visibility has been updated accordingly.
691 mWallpaperController.updateWallpaperVisibility();
692 }
693
chaviw161ea3e2018-01-31 12:01:12 -0800694 w.handleWindowMovedIfNeeded();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800695
696 final WindowStateAnimator winAnimator = w.mWinAnimator;
697
698 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
699 w.mContentChanged = false;
700
701 // Moved from updateWindowsAndWallpaperLocked().
702 if (w.mHasSurface) {
703 // Take care of the window being ready to display.
704 final boolean committed = winAnimator.commitFinishDrawingLocked();
705 if (isDefaultDisplay && committed) {
706 if (w.mAttrs.type == TYPE_DREAM) {
707 // HACK: When a dream is shown, it may at that point hide the lock screen.
708 // So we need to redo the layout to let the phone window manager make this
709 // happen.
710 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
711 if (DEBUG_LAYOUT_REPEATS) {
712 surfacePlacer.debugLayoutRepeats(
713 "dream and commitFinishDrawingLocked true",
714 pendingLayoutChanges);
715 }
716 }
717 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
718 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
719 "First draw done in potential wallpaper target " + w);
720 root.mWallpaperMayChange = true;
721 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
722 if (DEBUG_LAYOUT_REPEATS) {
723 surfacePlacer.debugLayoutRepeats(
724 "wallpaper and commitFinishDrawingLocked true",
725 pendingLayoutChanges);
726 }
727 }
728 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800729 }
730
731 final AppWindowToken atoken = w.mAppToken;
732 if (atoken != null) {
Adrian Roos23df3a32018-03-15 15:41:13 +0100733 atoken.updateLetterboxSurface(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800734 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
735 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
736 mTmpUpdateAllDrawn.add(atoken);
737 }
738 }
739
740 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
741 && w.isDisplayedLw()) {
742 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
743 }
744
745 w.updateResizingWindowIfNeeded();
746 };
747
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800748 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800749 * Create new {@link DisplayContent} instance, add itself to the root window container and
750 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700751 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800752 * @param service You know.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700753 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
754 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700755 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700756 DisplayContent(Display display, WindowManagerService service,
Bryce Leef19cbe22018-02-02 15:09:21 -0800757 WallpaperController wallpaperController, DisplayWindowController controller) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100758 super(service);
Bryce Leef19cbe22018-02-02 15:09:21 -0800759 setController(controller);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800760 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
761 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
762 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
763 + " new=" + display);
764 }
765
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700766 mDisplay = display;
767 mDisplayId = display.getDisplayId();
Wale Ogunwale0303c572016-10-20 10:16:29 -0700768 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700769 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700770 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700771 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100772 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo,
773 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700774 initializeDisplayBaseInfo();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800775 mDisplayPolicy = new DisplayPolicy(service);
776 mDisplayRotation = new DisplayRotation(service, this);
777 if (isDefaultDisplay) {
778 // The policy may be invoked right after here, so it requires the necessary default
779 // fields of this display content.
780 mService.mPolicy.setDefaultDisplay(this);
781 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800782 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700783 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700784
Robert Carr74a66a22018-02-23 12:17:51 -0800785 // We use this as our arbitrary surface size for buffer-less parents
786 // that don't impose cropping on their children. It may need to be larger
787 // than the display size because fullscreen windows can be shifted offscreen
788 // due to surfaceInsets. 2 times the largest display dimension feels like an
789 // appropriately arbitrary number. Eventually we would like to give SurfaceFlinger
790 // layers the ability to match their parent sizes and be able to skip
791 // such arbitrary size settings.
792 mSurfaceSize = Math.max(mBaseDisplayHeight, mBaseDisplayWidth) * 2;
Robert Carrb1579c82017-09-05 14:54:47 -0700793
794 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession)
795 .setSize(mSurfaceSize, mSurfaceSize)
796 .setOpaque(true);
797 mWindowingLayer = b.setName("Display Root").build();
798 mOverlayLayer = b.setName("Display Overlays").build();
799
Robert Carrf59b8dd2017-10-02 18:58:36 -0700800 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700801 .setLayerStack(mWindowingLayer, mDisplayId)
802 .show(mWindowingLayer)
803 .setLayer(mOverlayLayer, 1)
804 .setLayerStack(mOverlayLayer, mDisplayId)
805 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700806 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700807
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700808 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700809 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700810 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700811 super.addChild(mAboveAppWindowsContainers, null);
812 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800813
814 // Add itself as a child to the root container.
815 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700816
817 // TODO(b/62541591): evaluate whether this is the best spot to declare the
818 // {@link DisplayContent} ready for use.
819 mDisplayReady = true;
Arthur Hung95b38a92018-07-20 18:56:12 +0800820
Arthur Hung39134b22018-08-14 11:58:28 +0800821 mInputMonitor = new InputMonitor(service, mDisplayId);
Bryce Leed1871262017-06-12 14:12:29 -0700822 }
823
824 boolean isReady() {
825 // The display is ready when the system and the individual display are both ready.
826 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700827 }
828
829 int getDisplayId() {
830 return mDisplayId;
831 }
832
Wale Ogunwale02319a62016-09-26 15:21:22 -0700833 WindowToken getWindowToken(IBinder binder) {
834 return mTokenMap.get(binder);
835 }
836
837 AppWindowToken getAppWindowToken(IBinder binder) {
838 final WindowToken token = getWindowToken(binder);
839 if (token == null) {
840 return null;
841 }
842 return token.asAppWindowToken();
843 }
844
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700845 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700846 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
847 if (dc != null) {
848 // We currently don't support adding a window token to the display if the display
849 // already has the binder mapped to another token. If there is a use case for supporting
850 // this moving forward we will either need to merge the WindowTokens some how or have
851 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700852 throw new IllegalArgumentException("Can't map token=" + token + " to display="
853 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700854 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700855 if (binder == null) {
856 throw new IllegalArgumentException("Can't map token=" + token + " to display="
857 + getName() + " binder is null");
858 }
859 if (token == null) {
860 throw new IllegalArgumentException("Can't map null token to display="
861 + getName() + " binder=" + binder);
862 }
863
Wale Ogunwale02319a62016-09-26 15:21:22 -0700864 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700865
866 if (token.asAppWindowToken() == null) {
867 // Add non-app token to container hierarchy on the display. App tokens are added through
868 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700869 switch (token.windowType) {
870 case TYPE_WALLPAPER:
871 mBelowAppWindowsContainers.addChild(token);
872 break;
873 case TYPE_INPUT_METHOD:
874 case TYPE_INPUT_METHOD_DIALOG:
875 mImeWindowsContainers.addChild(token);
876 break;
877 default:
878 mAboveAppWindowsContainers.addChild(token);
879 break;
880 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700881 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700882 }
883
884 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700885 final WindowToken token = mTokenMap.remove(binder);
886 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800887 token.setExiting();
888 }
889 return token;
890 }
891
892 /** Changes the display the input window token is housed on to this one. */
893 void reParentWindowToken(WindowToken token) {
894 final DisplayContent prevDc = token.getDisplayContent();
895 if (prevDc == this) {
896 return;
897 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800898 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
899 && token.asAppWindowToken() == null) {
900 // Removed the token from the map, but made sure it's not an app token before removing
901 // from parent.
902 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700903 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800904
905 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700906 }
907
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700908 void removeAppToken(IBinder binder) {
909 final WindowToken token = removeWindowToken(binder);
910 if (token == null) {
911 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
912 return;
913 }
914
915 final AppWindowToken appToken = token.asAppWindowToken();
916
917 if (appToken == null) {
918 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
919 return;
920 }
921
922 appToken.onRemovedFromDisplay();
923 }
924
Riddle Hsuad256a12018-07-18 16:11:30 +0800925 @Override
Riddle Hsu86cb7de2018-08-13 23:29:58 +0800926 DisplayWindowController getController() {
927 return (DisplayWindowController) super.getController();
928 }
929
930 @Override
Riddle Hsuad256a12018-07-18 16:11:30 +0800931 public Display getDisplay() {
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700932 return mDisplay;
933 }
934
Craig Mautner59c00972012-07-30 12:10:24 -0700935 DisplayInfo getDisplayInfo() {
936 return mDisplayInfo;
937 }
938
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700939 DisplayMetrics getDisplayMetrics() {
940 return mDisplayMetrics;
941 }
942
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800943 DisplayPolicy getDisplayPolicy() {
944 return mDisplayPolicy;
945 }
946
Riddle Hsuad256a12018-07-18 16:11:30 +0800947 @Override
948 public DisplayRotation getDisplayRotation() {
949 return mDisplayRotation;
950 }
951
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800952 @VisibleForTesting
953 void setDisplayRotation(DisplayRotation displayRotation) {
954 mDisplayRotation = displayRotation;
955 }
956
Andrii Kulian8ee72852017-03-10 10:36:45 -0800957 int getRotation() {
958 return mRotation;
959 }
960
Andrii Kulianef5ce1c2018-03-20 19:27:24 -0700961 @VisibleForTesting
Andrii Kulian8ee72852017-03-10 10:36:45 -0800962 void setRotation(int newRotation) {
963 mRotation = newRotation;
Riddle Hsuad256a12018-07-18 16:11:30 +0800964 mDisplayRotation.setRotation(newRotation);
Andrii Kulian8ee72852017-03-10 10:36:45 -0800965 }
966
967 int getLastOrientation() {
968 return mLastOrientation;
969 }
970
971 void setLastOrientation(int orientation) {
972 mLastOrientation = orientation;
973 }
974
975 boolean getAltOrientation() {
976 return mAltOrientation;
977 }
978
979 void setAltOrientation(boolean altOrientation) {
980 mAltOrientation = altOrientation;
981 }
982
983 int getLastWindowForcedOrientation() {
984 return mLastWindowForcedOrientation;
985 }
986
Andrii Kulian06d07d62017-03-14 11:11:47 -0700987 /**
Riddle Hsu654a6f92018-07-13 22:59:36 +0800988 * Temporarily pauses rotation changes until resumed.
989 *
990 * This can be used to prevent rotation changes from occurring while the user is
991 * performing certain operations, such as drag and drop.
992 *
993 * This call nests and must be matched by an equal number of calls to
994 * {@link #resumeRotationLocked}.
995 */
996 void pauseRotationLocked() {
997 mDeferredRotationPauseCount++;
998 }
999
1000 /**
1001 * Resumes normal rotation changes after being paused.
1002 */
1003 void resumeRotationLocked() {
1004 if (mDeferredRotationPauseCount <= 0) {
1005 return;
1006 }
1007
1008 mDeferredRotationPauseCount--;
1009 if (mDeferredRotationPauseCount == 0) {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001010 updateRotationAndSendNewConfigIfNeeded();
Riddle Hsu654a6f92018-07-13 22:59:36 +08001011 }
1012 }
1013
1014 /**
Riddle Hsuad256a12018-07-18 16:11:30 +08001015 * If this is true we have updated our desired orientation, but not yet changed the real
1016 * orientation our applied our screen rotation animation. For example, because a previous
1017 * screen rotation was in progress.
1018 *
1019 * @return {@code true} if the there is an ongoing rotation change.
1020 */
1021 boolean rotationNeedsUpdate() {
1022 final int lastOrientation = getLastOrientation();
1023 final int oldRotation = getRotation();
1024 final boolean oldAltOrientation = getAltOrientation();
1025
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001026 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Riddle Hsuad256a12018-07-18 16:11:30 +08001027 final boolean altOrientation = !mDisplayRotation.rotationHasCompatibleMetrics(
1028 lastOrientation, rotation);
1029 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1030 return false;
1031 }
1032 return true;
1033 }
1034
1035 /**
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001036 * Update rotation of the display and send configuration if the rotation is changed.
1037 *
1038 * @return {@code true} if the rotation has been changed and the new config is sent.
1039 */
1040 boolean updateRotationAndSendNewConfigIfNeeded() {
1041 final boolean changed = updateRotationUnchecked(false /* forceUpdate */);
1042 if (changed) {
1043 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
1044 }
1045 return changed;
1046 }
1047
1048 /**
Andrii Kulian06d07d62017-03-14 11:11:47 -07001049 * Update rotation of the display.
1050 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001051 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
1052 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001053 */
Robert Carrae606b42018-02-15 15:36:23 -08001054 boolean updateRotationUnchecked() {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001055 return updateRotationUnchecked(false /* forceUpdate */);
1056 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001057
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001058 /**
1059 * Update rotation of the display with an option to force the update.
1060 * @param forceUpdate Force the rotation update. Sometimes in WM we might skip updating
1061 * orientation because we're waiting for some rotation to finish or display
1062 * to unfreeze, which results in configuration of the previously visible
1063 * activity being applied to a newly visible one. Forcing the rotation
1064 * update allows to workaround this issue.
1065 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
1066 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
1067 */
1068 boolean updateRotationUnchecked(boolean forceUpdate) {
1069 ScreenRotationAnimation screenRotationAnimation;
1070 if (!forceUpdate) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001071 if (mDeferredRotationPauseCount > 0) {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001072 // Rotation updates have been paused temporarily. Defer the update until
1073 // updates have been resumed.
1074 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
1075 return false;
1076 }
1077
1078 screenRotationAnimation =
1079 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
1080 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
1081 // Rotation updates cannot be performed while the previous rotation change
1082 // animation is still in progress. Skip this update. We will try updating
1083 // again after the animation is finished and the display is unfrozen.
1084 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
1085 return false;
1086 }
1087 if (mService.mDisplayFrozen) {
1088 // Even if the screen rotation animation has finished (e.g. isAnimating
1089 // returns false), there is still some time where we haven't yet unfrozen
1090 // the display. We also need to abort rotation here.
1091 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
1092 "Deferring rotation, still finishing previous rotation");
1093 return false;
1094 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001095 }
1096
1097 if (!mService.mDisplayEnabled) {
1098 // No point choosing a rotation if the display is not enabled.
1099 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
1100 return false;
1101 }
1102
1103 final int oldRotation = mRotation;
1104 final int lastOrientation = mLastOrientation;
1105 final boolean oldAltOrientation = mAltOrientation;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001106 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001107 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Computed rotation=" + rotation + " for display id="
1108 + mDisplayId + " based on lastOrientation=" + lastOrientation
1109 + " and oldRotation=" + oldRotation);
Riddle Hsuad256a12018-07-18 16:11:30 +08001110 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(mDisplayRotation,
1111 oldRotation, rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001112
Robert Carr0e007272017-10-02 13:00:55 -07001113 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001114 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001115 if (seamlessRotated != null && !forceUpdate) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001116 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
1117 // to complete (that is, waiting for windows to redraw). It's tempting to check
1118 // w.mSeamlessRotationCount but that could be incorrect in the case of
1119 // window-removal.
1120 return false;
1121 }
Robert Carr0e007272017-10-02 13:00:55 -07001122
1123 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001124 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -07001125 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -07001126 mayRotateSeamlessly = false;
1127 }
1128 for (int i = 0; i < mService.mSessions.size(); i++) {
1129 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
1130 mayRotateSeamlessly = false;
1131 break;
1132 }
1133 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001134 }
Robert Carr0e007272017-10-02 13:00:55 -07001135 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001136
1137 // TODO: Implement forced rotation changes.
1138 // Set mAltOrientation to indicate that the application is receiving
1139 // an orientation that has different metrics than it expected.
1140 // eg. Portrait instead of Landscape.
1141
Riddle Hsuad256a12018-07-18 16:11:30 +08001142 final boolean altOrientation = !mDisplayRotation.rotationHasCompatibleMetrics(
Andrii Kulian06d07d62017-03-14 11:11:47 -07001143 lastOrientation, rotation);
1144
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001145 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1146 + " selected orientation " + lastOrientation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001147 + ", got rotation " + rotation + " which has "
1148 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1149
1150 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1151 // No change.
1152 return false;
1153 }
1154
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001155 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1156 + " rotation changed to " + rotation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001157 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1158 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1159
1160 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1161 mService.mWaitingForConfig = true;
1162 }
1163
Riddle Hsuad256a12018-07-18 16:11:30 +08001164 setRotation(rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001165 mAltOrientation = altOrientation;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001166
1167 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
Riddle Hsu654a6f92018-07-13 22:59:36 +08001168 mService.mH.sendNewMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1169 this, WINDOW_FREEZE_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001170
1171 setLayoutNeeded();
1172 final int[] anim = new int[2];
Robert Carrf59b8dd2017-10-02 18:58:36 -07001173 mService.mPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001174
1175 if (!rotateSeamlessly) {
Robert Carrae606b42018-02-15 15:36:23 -08001176 mService.startFreezingDisplayLocked(anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001177 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1178 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1179 mDisplayId);
1180 } else {
1181 // The screen rotation animation uses a screenshot to freeze the screen
1182 // while windows resize underneath.
1183 // When we are rotating seamlessly, we allow the elements to transition
1184 // to their rotated state independently and without a freeze required.
1185 screenRotationAnimation = null;
1186
Robert Carrefc75702018-02-16 11:46:16 -08001187 mService.startSeamlessRotation();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001188 }
1189
1190 // We need to update our screen size information to match the new rotation. If the rotation
1191 // has actually changed then this method will return true and, according to the comment at
1192 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1193 // By updating the Display info here it will be available to
1194 // #computeScreenConfiguration() later.
1195 updateDisplayAndOrientation(getConfiguration().uiMode);
1196
Robert Carrae606b42018-02-15 15:36:23 -08001197 // NOTE: We disable the rotation in the emulator because
1198 // it doesn't support hardware OpenGL emulation yet.
1199 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1200 && screenRotationAnimation.hasScreenshot()) {
1201 if (screenRotationAnimation.setRotation(getPendingTransaction(), rotation,
1202 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1203 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1204 mService.scheduleAnimationLocked();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001205 }
1206 }
1207
Vishnu Nair83537a72018-07-19 21:27:48 -07001208 forAllWindows(w -> {
Vishnu Nairddd80742018-08-21 14:12:46 -07001209 w.seamlesslyRotateIfAllowed(getPendingTransaction(), oldRotation, rotation,
1210 rotateSeamlessly);
Vishnu Nair83537a72018-07-19 21:27:48 -07001211 }, true /* traverseTopToBottom */);
1212
Robert Carrae606b42018-02-15 15:36:23 -08001213 mService.mDisplayManagerInternal.performTraversal(getPendingTransaction());
1214 scheduleAnimation();
1215
Andrii Kulian06d07d62017-03-14 11:11:47 -07001216 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001217 if (w.mHasSurface && !rotateSeamlessly) {
1218 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001219 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001220 mService.mRoot.mOrientationChangeComplete = false;
1221 w.mLastFreezeDuration = 0;
1222 }
1223 w.mReportOrientationChanged = true;
1224 }, true /* traverseTopToBottom */);
1225
1226 if (rotateSeamlessly) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001227 mService.mH.sendNewMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1228 this, SEAMLESS_ROTATION_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001229 }
1230
1231 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1232 final WindowManagerService.RotationWatcher rotationWatcher
1233 = mService.mRotationWatchers.get(i);
1234 if (rotationWatcher.mDisplayId == mDisplayId) {
1235 try {
1236 rotationWatcher.mWatcher.onRotationChanged(rotation);
1237 } catch (RemoteException e) {
1238 // Ignore
1239 }
1240 }
1241 }
1242
1243 // TODO (multi-display): Magnification is supported only for the default display.
1244 // Announce rotation only if we will not animate as we already have the
1245 // windows in final state. Otherwise, we make this call at the rotation end.
1246 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1247 && isDefaultDisplay) {
1248 mService.mAccessibilityController.onRotationChangedLocked(this);
1249 }
1250
1251 return true;
1252 }
1253
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001254 void configureDisplayPolicy() {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001255 final int width = mBaseDisplayWidth;
1256 final int height = mBaseDisplayHeight;
1257 final int shortSize;
1258 final int longSize;
1259 if (width > height) {
1260 shortSize = height;
1261 longSize = width;
1262 } else {
1263 shortSize = width;
1264 longSize = height;
1265 }
1266
1267 final int shortSizeDp = shortSize * DisplayMetrics.DENSITY_DEFAULT / mBaseDisplayDensity;
1268 final int longSizeDp = longSize * DisplayMetrics.DENSITY_DEFAULT / mBaseDisplayDensity;
1269
1270 mDisplayRotation.configure(width, height, shortSizeDp, longSizeDp);
1271 mDisplayPolicy.configure(width, height, shortSizeDp);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001272
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001273 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
1274 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001275 }
1276
Andrii Kulian06d07d62017-03-14 11:11:47 -07001277 /**
1278 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1279 * changed.
1280 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1281 */
1282 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1283 // Use the effective "visual" dimensions based on current rotation
1284 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1285 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1286 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1287 int dw = realdw;
1288 int dh = realdh;
1289
1290 if (mAltOrientation) {
1291 if (realdw > realdh) {
1292 // Turn landscape into portrait.
1293 int maxw = (int)(realdh/1.3f);
1294 if (maxw < realdw) {
1295 dw = maxw;
1296 }
1297 } else {
1298 // Turn portrait into landscape.
1299 int maxh = (int)(realdw/1.3f);
1300 if (maxh < realdh) {
1301 dh = maxh;
1302 }
1303 }
1304 }
1305
1306 // Update application display metrics.
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001307 final WmDisplayCutout wmDisplayCutout = calculateDisplayCutoutForRotation(mRotation);
1308 final DisplayCutout displayCutout = wmDisplayCutout.getDisplayCutout();
1309
Andrii Kulian06d07d62017-03-14 11:11:47 -07001310 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001311 mDisplayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001312 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001313 mDisplayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001314 mDisplayInfo.rotation = mRotation;
1315 mDisplayInfo.logicalWidth = dw;
1316 mDisplayInfo.logicalHeight = dh;
1317 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1318 mDisplayInfo.appWidth = appWidth;
1319 mDisplayInfo.appHeight = appHeight;
1320 if (isDefaultDisplay) {
1321 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1322 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1323 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001324 mDisplayInfo.displayCutout = displayCutout.isEmpty() ? null : displayCutout;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001325 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1326 if (mDisplayScalingDisabled) {
1327 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1328 } else {
1329 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1330 }
1331
Andrii Kulianf0379de2018-03-14 16:24:07 -07001332 // We usually set the override info in DisplayManager so that we get consistent display
1333 // metrics values when displays are changing and don't send out new values until WM is aware
1334 // of them. However, we don't do this for displays that serve as containers for ActivityView
1335 // because we don't want letter-/pillar-boxing during resize.
1336 final DisplayInfo overrideDisplayInfo = mShouldOverrideDisplayConfiguration
1337 ? mDisplayInfo : null;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001338 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
Andrii Kulianf0379de2018-03-14 16:24:07 -07001339 overrideDisplayInfo);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001340
1341 mBaseDisplayRect.set(0, 0, dw, dh);
1342
1343 if (isDefaultDisplay) {
1344 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1345 mCompatDisplayMetrics);
1346 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001347
1348 updateBounds();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001349 return mDisplayInfo;
1350 }
1351
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001352 WmDisplayCutout calculateDisplayCutoutForRotation(int rotation) {
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001353 return mDisplayCutoutCache.getOrCompute(mInitialDisplayCutout, rotation);
1354 }
1355
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001356 private WmDisplayCutout calculateDisplayCutoutForRotationUncached(
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001357 DisplayCutout cutout, int rotation) {
Adrian Roos24264212018-02-19 16:26:15 +01001358 if (cutout == null || cutout == DisplayCutout.NO_CUTOUT) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001359 return WmDisplayCutout.NO_CUTOUT;
Adrian Roos1cf585052018-01-03 18:43:27 +01001360 }
Adrian Roos11c25582018-02-19 18:06:36 +01001361 if (rotation == ROTATION_0) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001362 return WmDisplayCutout.computeSafeInsets(
1363 cutout, mInitialDisplayWidth, mInitialDisplayHeight);
Adrian Roos24264212018-02-19 16:26:15 +01001364 }
Adrian Roosbed538e2018-02-21 17:50:07 +01001365 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Jorim Jaggi60640512018-06-29 01:14:31 +02001366 final List<Rect> bounds = WmDisplayCutout.computeSafeInsets(
1367 cutout, mInitialDisplayWidth, mInitialDisplayHeight)
1368 .getDisplayCutout().getBoundingRects();
Adrian Roos11c25582018-02-19 18:06:36 +01001369 transformPhysicalToLogicalCoordinates(rotation, mInitialDisplayWidth, mInitialDisplayHeight,
1370 mTmpMatrix);
Jorim Jaggi60640512018-06-29 01:14:31 +02001371 final Region region = Region.obtain();
1372 for (int i = 0; i < bounds.size(); i++) {
1373 final Rect rect = bounds.get(i);
1374 final RectF rectF = new RectF(bounds.get(i));
1375 mTmpMatrix.mapRect(rectF);
1376 rectF.round(rect);
1377 region.op(rect, Op.UNION);
1378 }
1379
1380 return WmDisplayCutout.computeSafeInsets(DisplayCutout.fromBounds(region),
Adrian Roos24264212018-02-19 16:26:15 +01001381 rotated ? mInitialDisplayHeight : mInitialDisplayWidth,
1382 rotated ? mInitialDisplayWidth : mInitialDisplayHeight);
Adrian Roos1cf585052018-01-03 18:43:27 +01001383 }
1384
Andrii Kulian06d07d62017-03-14 11:11:47 -07001385 /**
1386 * Compute display configuration based on display properties and policy settings.
1387 * Do not call if mDisplayReady == false.
1388 */
1389 void computeScreenConfiguration(Configuration config) {
1390 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1391
1392 final int dw = displayInfo.logicalWidth;
1393 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001394 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
Garfield Tane0846042018-07-26 13:42:04 -07001395 config.windowConfiguration.setWindowingMode(getWindowingMode());
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001396
Adrian Roos11c25582018-02-19 18:06:36 +01001397 final float density = mDisplayMetrics.density;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001398 config.screenWidthDp =
1399 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001400 config.uiMode, mDisplayId, displayInfo.displayCutout) / density);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001401 config.screenHeightDp =
1402 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001403 config.uiMode, mDisplayId, displayInfo.displayCutout) / density);
Bryce Lee7566d762017-03-30 09:34:15 -07001404
Adrian Roos11c25582018-02-19 18:06:36 +01001405 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh,
1406 displayInfo.displayCutout, mTmpRect);
Bryce Lee7566d762017-03-30 09:34:15 -07001407 final int leftInset = mTmpRect.left;
1408 final int topInset = mTmpRect.top;
1409 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001410 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1411 leftInset + displayInfo.appWidth /* right */,
1412 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001413 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1414 || displayInfo.rotation == Surface.ROTATION_270);
1415
1416 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
Adrian Roos11c25582018-02-19 18:06:36 +01001417 density, config);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001418
1419 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1420 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1421 ? Configuration.SCREENLAYOUT_ROUND_YES
1422 : Configuration.SCREENLAYOUT_ROUND_NO);
1423
1424 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1425 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1426 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1427 dh, mDisplayId);
1428 config.densityDpi = displayInfo.logicalDensityDpi;
1429
1430 config.colorMode =
Chia-I Wuf739bad2018-05-09 12:36:57 -07001431 ((displayInfo.isHdr() && mService.hasHdrSupport())
Andrii Kulian06d07d62017-03-14 11:11:47 -07001432 ? Configuration.COLOR_MODE_HDR_YES
1433 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001434 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001435 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1436 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1437
1438 // Update the configuration based on available input devices, lid switch,
1439 // and platform configuration.
1440 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1441 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1442 config.navigation = Configuration.NAVIGATION_NONAV;
1443
1444 int keyboardPresence = 0;
1445 int navigationPresence = 0;
1446 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1447 final int len = devices != null ? devices.length : 0;
1448 for (int i = 0; i < len; i++) {
1449 InputDevice device = devices[i];
1450 if (!device.isVirtual()) {
1451 final int sources = device.getSources();
1452 final int presenceFlag = device.isExternal() ?
1453 WindowManagerPolicy.PRESENCE_EXTERNAL :
1454 WindowManagerPolicy.PRESENCE_INTERNAL;
1455
1456 // TODO(multi-display): Configure on per-display basis.
1457 if (mService.mIsTouchDevice) {
1458 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1459 InputDevice.SOURCE_TOUCHSCREEN) {
1460 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1461 }
1462 } else {
1463 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1464 }
1465
1466 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1467 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1468 navigationPresence |= presenceFlag;
1469 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1470 && config.navigation == Configuration.NAVIGATION_NONAV) {
1471 config.navigation = Configuration.NAVIGATION_DPAD;
1472 navigationPresence |= presenceFlag;
1473 }
1474
1475 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1476 config.keyboard = Configuration.KEYBOARD_QWERTY;
1477 keyboardPresence |= presenceFlag;
1478 }
1479 }
1480 }
1481
1482 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1483 config.navigation = Configuration.NAVIGATION_DPAD;
1484 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1485 }
1486
1487 // Determine whether a hard keyboard is available and enabled.
1488 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1489 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1490 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1491 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1492 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1493 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1494 }
1495
1496 // Let the policy update hidden states.
1497 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1498 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1499 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1500 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1501 }
1502
1503 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1504 int displayId) {
1505 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1506 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1507 final int unrotDw, unrotDh;
1508 if (rotated) {
1509 unrotDw = dh;
1510 unrotDh = dw;
1511 } else {
1512 unrotDw = dw;
1513 unrotDh = dh;
1514 }
1515 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1516 displayId);
1517 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1518 displayId);
1519 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1520 displayId);
1521 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1522 displayId);
1523 return sw;
1524 }
1525
1526 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1527 DisplayMetrics dm, int dw, int dh, int displayId) {
1528 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001529 displayId, mDisplayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001530 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001531 uiMode, displayId, mDisplayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001532 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1533 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1534 if (curSize == 0 || size < curSize) {
1535 curSize = size;
1536 }
1537 return curSize;
1538 }
1539
1540 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1541 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1542
1543 // We need to determine the smallest width that will occur under normal
1544 // operation. To this, start with the base screen size and compute the
1545 // width under the different possible rotations. We need to un-rotate
1546 // the current screen dimensions before doing this.
1547 int unrotDw, unrotDh;
1548 if (rotated) {
1549 unrotDw = dh;
1550 unrotDh = dw;
1551 } else {
1552 unrotDw = dw;
1553 unrotDh = dh;
1554 }
1555 displayInfo.smallestNominalAppWidth = 1<<30;
1556 displayInfo.smallestNominalAppHeight = 1<<30;
1557 displayInfo.largestNominalAppWidth = 0;
1558 displayInfo.largestNominalAppHeight = 0;
1559 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1560 unrotDh);
1561 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1562 unrotDw);
1563 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1564 unrotDh);
1565 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1566 unrotDw);
1567 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1568 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1569 displayId);
1570 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1571 displayId);
1572 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1573 displayId);
1574 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1575 displayId);
1576 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1577 outConfig.screenLayout = sl;
1578 }
1579
1580 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1581 int uiMode, int displayId) {
1582 // Get the app screen size at this rotation.
Adrian Roos11c25582018-02-19 18:06:36 +01001583 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId,
1584 mDisplayInfo.displayCutout);
1585 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId,
1586 mDisplayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001587
1588 // Compute the screen layout size class for this rotation.
1589 int longSize = w;
1590 int shortSize = h;
1591 if (longSize < shortSize) {
1592 int tmp = longSize;
1593 longSize = shortSize;
1594 shortSize = tmp;
1595 }
1596 longSize = (int)(longSize/density);
1597 shortSize = (int)(shortSize/density);
1598 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1599 }
1600
1601 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1602 int uiMode, int dw, int dh) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001603 final DisplayCutout displayCutout = calculateDisplayCutoutForRotation(
1604 rotation).getDisplayCutout();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001605 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001606 displayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001607 if (width < displayInfo.smallestNominalAppWidth) {
1608 displayInfo.smallestNominalAppWidth = width;
1609 }
1610 if (width > displayInfo.largestNominalAppWidth) {
1611 displayInfo.largestNominalAppWidth = width;
1612 }
1613 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001614 displayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001615 if (height < displayInfo.smallestNominalAppHeight) {
1616 displayInfo.smallestNominalAppHeight = height;
1617 }
1618 if (height > displayInfo.largestNominalAppHeight) {
1619 displayInfo.largestNominalAppHeight = height;
1620 }
1621 }
1622
Riddle Hsua4d6fa22018-08-11 00:50:39 +08001623 /**
1624 * Apps that use the compact menu panel (as controlled by the panelMenuIsCompact
1625 * theme attribute) on devices that feature a physical options menu key attempt to position
1626 * their menu panel window along the edge of the screen nearest the physical menu key.
1627 * This lowers the travel distance between invoking the menu panel and selecting
1628 * a menu option.
1629 *
1630 * This method helps control where that menu is placed. Its current implementation makes
1631 * assumptions about the menu key and its relationship to the screen based on whether
1632 * the device's natural orientation is portrait (width < height) or landscape.
1633 *
1634 * The menu key is assumed to be located along the bottom edge of natural-portrait
1635 * devices and along the right edge of natural-landscape devices. If these assumptions
1636 * do not hold for the target device, this method should be changed to reflect that.
1637 *
1638 * @return A {@link Gravity} value for placing the options menu window.
1639 */
1640 int getPreferredOptionsPanelGravity() {
1641 final int rotation = getRotation();
1642 if (mInitialDisplayWidth < mInitialDisplayHeight) {
1643 // On devices with a natural orientation of portrait.
1644 switch (rotation) {
1645 default:
1646 case Surface.ROTATION_0:
1647 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1648 case Surface.ROTATION_90:
1649 return Gravity.RIGHT | Gravity.BOTTOM;
1650 case Surface.ROTATION_180:
1651 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1652 case Surface.ROTATION_270:
1653 return Gravity.START | Gravity.BOTTOM;
1654 }
1655 }
1656
1657 // On devices with a natural orientation of landscape.
1658 switch (rotation) {
1659 default:
1660 case Surface.ROTATION_0:
1661 return Gravity.RIGHT | Gravity.BOTTOM;
1662 case Surface.ROTATION_90:
1663 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1664 case Surface.ROTATION_180:
1665 return Gravity.START | Gravity.BOTTOM;
1666 case Surface.ROTATION_270:
1667 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1668 }
1669 }
1670
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001671 DockedStackDividerController getDockedDividerController() {
1672 return mDividerControllerLocked;
1673 }
1674
Winson Chung655332c2016-10-31 13:14:28 -07001675 PinnedStackController getPinnedStackController() {
1676 return mPinnedStackControllerLocked;
1677 }
1678
Jeff Browna506a6e2013-06-04 00:02:38 -07001679 /**
1680 * Returns true if the specified UID has access to this display.
1681 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001682 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001683 return mDisplay.hasAccess(uid);
1684 }
1685
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001686 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001687 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001688 }
1689
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001690 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001691 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001692 }
1693
Wale Ogunwale61911492017-10-11 08:50:50 -07001694 /**
1695 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1696 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001697 TaskStack getSplitScreenPrimaryStack() {
1698 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001699 return (stack != null && stack.isVisible()) ? stack : null;
1700 }
1701
Robert Carr9785cf32018-04-25 15:06:07 -07001702 boolean hasSplitScreenPrimaryStack() {
1703 return getSplitScreenPrimaryStack() != null;
1704 }
1705
Wale Ogunwale61911492017-10-11 08:50:50 -07001706 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001707 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001708 * not visible.
1709 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001710 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1711 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001712 }
1713
1714 TaskStack getPinnedStack() {
1715 return mTaskStackContainers.getPinnedStack();
1716 }
1717
1718 private boolean hasPinnedStack() {
1719 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001720 }
1721
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001722 /**
1723 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1724 * Null is no compatible stack on the display.
1725 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001726 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001727 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1728 }
1729
1730 /**
1731 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1732 * activity type. Null is no compatible stack on the display.
1733 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001734 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001735 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001736 }
1737
Bryce Lee48f4b572017-04-10 10:54:15 -07001738 @VisibleForTesting
Kazuki Takise148d00a2018-05-31 15:32:19 +09001739 WindowList<TaskStack> getStacks() {
1740 return mTaskStackContainers.mChildren;
1741 }
1742
1743 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001744 TaskStack getTopStack() {
1745 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001746 }
1747
Winson Chunge2d72172018-01-25 17:46:20 +00001748 ArrayList<Task> getVisibleTasks() {
1749 return mTaskStackContainers.getVisibleTasks();
1750 }
1751
Wale Ogunwale61911492017-10-11 08:50:50 -07001752 void onStackWindowingModeChanged(TaskStack stack) {
1753 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001754 }
1755
Andrii Kulian441e4492016-09-29 15:25:00 -07001756 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001757 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001758 super.onConfigurationChanged(newParentConfig);
1759
Andrii Kulian3a507b52016-09-19 18:14:12 -07001760 // The display size information is heavily dependent on the resources in the current
1761 // configuration, so we need to reconfigure it every time the configuration changes.
1762 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1763 mService.reconfigureDisplayLocked(this);
1764
Bryce Leef3c6a472017-11-14 14:53:06 -08001765 final DockedStackDividerController dividerController = getDockedDividerController();
1766
1767 if (dividerController != null) {
1768 getDockedDividerController().onConfigurationChanged();
1769 }
1770
1771 final PinnedStackController pinnedStackController = getPinnedStackController();
1772
1773 if (pinnedStackController != null) {
1774 getPinnedStackController().onConfigurationChanged();
1775 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001776 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001777
Andrii Kulian441e4492016-09-29 15:25:00 -07001778 /**
1779 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1780 * bounds were updated.
1781 */
Bryce Leeaea60d22018-01-31 14:42:15 -08001782 void updateStackBoundsAfterConfigChange(@NonNull List<TaskStack> changedStackList) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001783 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1784 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001785 if (stack.updateBoundsAfterConfigChange()) {
Bryce Leeaea60d22018-01-31 14:42:15 -08001786 changedStackList.add(stack);
Andrii Kulian3a507b52016-09-19 18:14:12 -07001787 }
1788 }
Winson Chung32c566f2017-04-11 18:31:21 -07001789
1790 // If there was no pinned stack, we still need to notify the controller of the display info
1791 // update as a result of the config change. We do this here to consolidate the flow between
1792 // changes when there is and is not a stack.
Wale Ogunwale61911492017-10-11 08:50:50 -07001793 if (!hasPinnedStack()) {
Winson Chung32c566f2017-04-11 18:31:21 -07001794 mPinnedStackControllerLocked.onDisplayInfoChanged();
1795 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001796 }
1797
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001798 @Override
1799 boolean fillsParent() {
1800 return true;
1801 }
1802
1803 @Override
1804 boolean isVisible() {
1805 return true;
1806 }
1807
1808 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001809 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001810 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001811 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001812 }
1813
Robert Carr9785cf32018-04-25 15:06:07 -07001814 /**
1815 * In split-screen mode we process the IME containers above the docked divider
1816 * rather than directly above their target.
1817 */
1818 private boolean skipTraverseChild(WindowContainer child) {
1819 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null
1820 && !hasSplitScreenPrimaryStack()) {
1821 return true;
1822 }
1823 return false;
1824 }
1825
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001826 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001827 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1828 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1829 // target, or here in order if there isn't an IME target.
1830 if (traverseTopToBottom) {
1831 for (int i = mChildren.size() - 1; i >= 0; --i) {
1832 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001833 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001834 continue;
1835 }
Robert Carr9785cf32018-04-25 15:06:07 -07001836
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001837 if (child.forAllWindows(callback, traverseTopToBottom)) {
1838 return true;
1839 }
1840 }
1841 } else {
1842 final int count = mChildren.size();
1843 for (int i = 0; i < count; i++) {
1844 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001845 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001846 continue;
1847 }
Robert Carr9785cf32018-04-25 15:06:07 -07001848
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001849 if (child.forAllWindows(callback, traverseTopToBottom)) {
1850 return true;
1851 }
1852 }
1853 }
1854 return false;
1855 }
1856
1857 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1858 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1859 }
1860
Wale Ogunwale399c8692017-05-08 14:22:42 -07001861 @Override
1862 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001863 final WindowManagerPolicy policy = mService.mPolicy;
1864
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001865 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001866 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001867 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1868 + " is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001869 // If the display is frozen, some activities may be in the middle of restarting, and
1870 // thus have removed their old window. If the window has the flag to hide the lock
1871 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1872 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001873 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001874 } else if (policy.isKeyguardLocked()) {
1875 // Use the last orientation the while the display is frozen with the keyguard
1876 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1877 // window. We don't want to check the show when locked window directly though as
1878 // things aren't stable while the display is frozen, for example the window could be
1879 // momentarily unavailable due to activity relaunch.
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001880 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1881 + " is frozen while keyguard locked, return " + mLastOrientation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08001882 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001883 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001884 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001885 final int orientation = mAboveAppWindowsContainers.getOrientation();
1886 if (orientation != SCREEN_ORIENTATION_UNSET) {
1887 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001888 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001889 }
1890
Wale Ogunwale399c8692017-05-08 14:22:42 -07001891 // Top system windows are not requesting an orientation. Start searching from apps.
1892 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001893 }
1894
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001895 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001896 // Check if display metrics changed and update base values if needed.
1897 updateBaseDisplayMetricsIfNeeded();
1898
Craig Mautner722285e2012-09-07 13:55:58 -07001899 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001900 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001901
Wale Ogunwale61911492017-10-11 08:50:50 -07001902 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1903 mTaskStackContainers.getChildAt(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001904 }
Craig Mautner722285e2012-09-07 13:55:58 -07001905 }
1906
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001907 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001908 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1909 if (displayManagerInternal != null) {
1910 // Bootstrap the default logical display from the display manager.
1911 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1912 if (newDisplayInfo != null) {
1913 mDisplayInfo.copyFrom(newDisplayInfo);
1914 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001915 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001916
Andrii Kuliancd097992017-03-23 18:31:59 -07001917 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1918 mDisplayInfo.logicalDensityDpi);
1919 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1920 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1921 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01001922 mInitialDisplayCutout = mDisplayInfo.displayCutout;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001923 }
1924
Andrii Kuliancd097992017-03-23 18:31:59 -07001925 /**
1926 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1927 * values.
1928 */
1929 private void updateBaseDisplayMetricsIfNeeded() {
1930 // Get real display metrics without overrides from WM.
1931 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1932 final int orientation = mDisplayInfo.rotation;
1933 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1934 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1935 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1936 final int newDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01001937 final DisplayCutout newCutout = mDisplayInfo.displayCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07001938
1939 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1940 || mInitialDisplayHeight != newHeight
Adrian Roos1cf585052018-01-03 18:43:27 +01001941 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi
1942 || !Objects.equals(mInitialDisplayCutout, newCutout);
Andrii Kuliancd097992017-03-23 18:31:59 -07001943
1944 if (displayMetricsChanged) {
1945 // Check if display size or density is forced.
1946 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1947 || mBaseDisplayHeight != mInitialDisplayHeight;
1948 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1949
1950 // If there is an override set for base values - use it, otherwise use new values.
1951 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1952 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1953 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1954
1955 // Real display metrics changed, so we should also update initial values.
1956 mInitialDisplayWidth = newWidth;
1957 mInitialDisplayHeight = newHeight;
1958 mInitialDisplayDensity = newDensity;
Adrian Roos1cf585052018-01-03 18:43:27 +01001959 mInitialDisplayCutout = newCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07001960 mService.reconfigureDisplayLocked(this);
1961 }
1962 }
1963
Bryce Lee27cec322017-03-21 09:41:37 -07001964 /** Sets the maximum width the screen resolution can be */
1965 void setMaxUiWidth(int width) {
1966 if (DEBUG_DISPLAY) {
1967 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1968 }
1969
1970 mMaxUiWidth = width;
1971
1972 // Update existing metrics.
1973 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1974 }
1975
1976 /** Update base (override) display metrics. */
1977 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1978 mBaseDisplayWidth = baseWidth;
1979 mBaseDisplayHeight = baseHeight;
1980 mBaseDisplayDensity = baseDensity;
1981
1982 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1983 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1984 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1985 mBaseDisplayWidth = mMaxUiWidth;
1986
1987 if (DEBUG_DISPLAY) {
1988 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1989 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1990 + " on display:" + getDisplayId());
1991 }
1992 }
1993
1994 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08001995
1996 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07001997 }
1998
Wale Ogunwaledb506192017-12-08 10:57:32 -08001999 void getStableRect(Rect out) {
2000 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08002001 }
2002
Wale Ogunwale61911492017-10-11 08:50:50 -07002003 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002004 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
2005 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07002006
Wale Ogunwale61911492017-10-11 08:50:50 -07002007 final TaskStack stack = new TaskStack(mService, stackId, controller);
2008 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002009 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07002010 }
2011
Andrii Kulian51c1b672017-04-07 18:39:32 -07002012 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002013 final DisplayContent prevDc = stack.getDisplayContent();
2014 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07002015 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
2016 + " which is not currently attached to any display");
2017 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002018 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07002019 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
2020 + " to its current displayId=" + mDisplayId);
2021 }
2022
Wale Ogunwale61911492017-10-11 08:50:50 -07002023 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07002024 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08002025 }
2026
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002027 @Override
2028 protected void addChild(DisplayChildWindowContainer child,
2029 Comparator<DisplayChildWindowContainer> comparator) {
2030 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2031 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002032
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002033 @Override
2034 protected void addChild(DisplayChildWindowContainer child, int index) {
2035 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2036 }
2037
2038 @Override
2039 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002040 // Only allow removal of direct children from this display if the display is in the process
2041 // of been removed.
2042 if (mRemovingDisplay) {
2043 super.removeChild(child);
2044 return;
2045 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002046 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002047 }
2048
Andrii Kuliand2765632016-12-12 22:26:34 -08002049 @Override
2050 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
2051 // Children of the display are statically ordered, so the real intention here is to perform
2052 // the operation on the display and not the static direct children.
2053 getParent().positionChildAt(position, this, includingParents);
2054 }
2055
Riddle Hsu57831b52018-07-27 00:31:48 +08002056 void positionStackAt(int position, TaskStack child, boolean includingParents) {
2057 mTaskStackContainers.positionChildAt(position, child, includingParents);
Winson Chung59a47ded2018-01-25 17:46:06 +00002058 layoutAndAssignWindowLayersIfNeeded();
2059 }
2060
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002061 int taskIdFromPoint(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002062 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2063 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002064 final int taskId = stack.taskIdFromPoint(x, y);
2065 if (taskId != -1) {
2066 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07002067 }
2068 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002069 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07002070 }
2071
Chong Zhang8e89b312015-09-09 15:09:30 -07002072 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08002073 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07002074 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07002075 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07002076 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002077 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002078 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07002079 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2080 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002081 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002082 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002083 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002084
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002085 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
2086 if (mTmpTaskForResizePointSearchResult.searchDone) {
2087 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07002088 }
2089 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002090 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002091 }
2092
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002093 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08002094 // The provided task is the task on this display with focus, so if WindowManagerService's
2095 // focused app is not on this display, focusedTask will be null.
2096 if (focusedTask == null) {
2097 mTouchExcludeRegion.setEmpty();
2098 } else {
2099 mTouchExcludeRegion.set(mBaseDisplayRect);
2100 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
2101 mTmpRect2.setEmpty();
Wale Ogunwale61911492017-10-11 08:50:50 -07002102 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2103 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002104 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
2105 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08002106 }
2107 // If we removed the focused task above, add it back and only leave its
2108 // outside touch area in the exclusion. TapDectector is not interested in
2109 // any touch inside the focused task itself.
2110 if (!mTmpRect2.isEmpty()) {
2111 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
2112 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08002113 }
lumark90120a82018-08-15 00:33:03 +08002114 if (mInputMethodWindow != null && mInputMethodWindow.isVisibleLw()) {
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002115 // If the input method is visible and the user is typing, we don't want these touch
2116 // events to be intercepted and used to change focus. This would likely cause a
2117 // disappearance of the input method.
lumark90120a82018-08-15 00:33:03 +08002118 mInputMethodWindow.getTouchableRegion(mTmpRegion);
2119 if (mInputMethodWindow.getDisplayId() == mDisplayId) {
Andrii Kulian7a31b772017-05-02 13:22:42 -07002120 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
2121 } else {
2122 // IME is on a different display, so we need to update its tap detector.
lumark90120a82018-08-15 00:33:03 +08002123 setTouchExcludeRegion(null /* focusedTask */);
Andrii Kulian7a31b772017-05-02 13:22:42 -07002124 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002125 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002126 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002127 final WindowState win = mTapExcludedWindows.get(i);
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002128 win.getTouchableRegion(mTmpRegion);
2129 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
2130 }
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002131 for (int i = mTapExcludeProvidingWindows.size() - 1; i >= 0; i--) {
2132 final WindowState win = mTapExcludeProvidingWindows.valueAt(i);
2133 win.amendTapExcludeRegion(mTouchExcludeRegion);
2134 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08002135 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07002136 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002137 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07002138 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002139 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
2140 }
Craig Mautner1bef3892015-02-17 15:09:47 -08002141 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07002142 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08002143 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07002144 }
2145
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002146 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07002147 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002148 super.switchUser();
2149 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07002150 }
2151
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07002152 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002153 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2154 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07002155 }
2156 }
2157
Wale Ogunwale10124582016-09-15 20:25:50 -07002158 @Override
2159 void removeIfPossible() {
2160 if (isAnimating()) {
2161 mDeferredRemoval = true;
2162 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07002163 }
Wale Ogunwale10124582016-09-15 20:25:50 -07002164 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07002165 }
2166
Wale Ogunwale10124582016-09-15 20:25:50 -07002167 @Override
2168 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002169 mRemovingDisplay = true;
2170 try {
2171 super.removeImmediately();
2172 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -07002173 if (mService.canDispatchPointerEvents()) {
2174 if (mTapDetector != null) {
2175 mService.unregisterPointerEventListener(mTapDetector);
2176 }
2177 if (mDisplayId == DEFAULT_DISPLAY && mService.mMousePositionTracker != null) {
2178 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
2179 }
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002180 }
Jorim Jaggia3844032018-01-03 15:54:43 +01002181 mService.mAnimator.removeDisplayLocked(mDisplayId);
Louis Chang207f9112018-07-30 08:58:54 +08002182 mWindowingLayer.release();
2183 mOverlayLayer.release();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002184 } finally {
2185 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08002186 }
Bryce Leef19cbe22018-02-02 15:09:21 -08002187
Arthur Hung39134b22018-08-14 11:58:28 +08002188 mInputMonitor.onRemoved();
Bryce Leef19cbe22018-02-02 15:09:21 -08002189 mService.onDisplayRemoved(mDisplayId);
Craig Mautner95da1082014-02-24 17:54:35 -08002190 }
2191
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002192 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07002193 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002194 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002195 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
2196
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002197 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002198 removeImmediately();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002199 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08002200 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002201 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08002202 }
2203
Andrii Kulian0214ed92017-05-16 13:44:05 -07002204 /** @return 'true' if removal of this display content is deferred due to active animation. */
2205 boolean isRemovalDeferred() {
2206 return mDeferredRemoval;
2207 }
2208
Wale Ogunwale10124582016-09-15 20:25:50 -07002209 boolean animateForIme(float interpolatedValue, float animationTarget,
2210 float dividerAnimationTarget) {
2211 boolean updated = false;
2212
Wale Ogunwale61911492017-10-11 08:50:50 -07002213 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2214 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002215 if (stack == null || !stack.isAdjustedForIme()) {
2216 continue;
2217 }
2218
2219 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
2220 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2221 updated = true;
2222 } else {
2223 mDividerControllerLocked.mLastAnimationProgress =
2224 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
2225 mDividerControllerLocked.mLastDividerProgress =
2226 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
2227 updated |= stack.updateAdjustForIme(
2228 mDividerControllerLocked.mLastAnimationProgress,
2229 mDividerControllerLocked.mLastDividerProgress,
2230 false /* force */);
2231 }
2232 if (interpolatedValue >= 1f) {
2233 stack.endImeAdjustAnimation();
2234 }
2235 }
2236
2237 return updated;
2238 }
2239
2240 boolean clearImeAdjustAnimation() {
2241 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002242 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2243 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002244 if (stack != null && stack.isAdjustedForIme()) {
2245 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2246 changed = true;
2247 }
2248 }
2249 return changed;
2250 }
2251
2252 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002253 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2254 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002255 if (stack.isVisible() && stack.isAdjustedForIme()) {
2256 stack.beginImeAdjustAnimation();
2257 }
2258 }
2259 }
2260
2261 void adjustForImeIfNeeded() {
lumark90120a82018-08-15 00:33:03 +08002262 final WindowState imeWin = mInputMethodWindow;
Wale Ogunwale10124582016-09-15 20:25:50 -07002263 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2264 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002265 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002266 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2267 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2268 imeTargetStack.getDockSide() : DOCKED_INVALID;
2269 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2270 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2271 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002272 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002273 final boolean imeHeightChanged = imeVisible &&
2274 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2275
2276 // The divider could be adjusted for IME position, or be thinner than usual,
2277 // or both. There are three possible cases:
2278 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2279 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2280 // - If IME is not visible, divider is not moved and is normal width.
2281
2282 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002283 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2284 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002285 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002286 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2287 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002288 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2289 } else {
2290 stack.resetAdjustedForIme(false);
2291 }
2292 }
2293 mDividerControllerLocked.setAdjustedForIme(
2294 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2295 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002296 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2297 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002298 stack.resetAdjustedForIme(!dockVisible);
2299 }
2300 mDividerControllerLocked.setAdjustedForIme(
2301 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2302 }
Winson Chung655332c2016-10-31 13:14:28 -07002303 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002304 }
2305
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002306 /**
2307 * If a window that has an animation specifying a colored background and the current wallpaper
2308 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2309 * suddenly disappear.
2310 */
2311 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002312 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2313 w -> w.mIsWallpaper && w.isVisibleNow());
2314
2315 if (visibleWallpaper != null) {
2316 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002317 }
2318 return winAnimator.mAnimLayer;
2319 }
2320
Wale Ogunwale10124582016-09-15 20:25:50 -07002321 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002322 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2323 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002324 stack.prepareFreezingTaskBounds();
2325 }
2326 }
2327
Wale Ogunwale94744212015-09-21 19:01:47 -07002328 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002329 getBounds(mTmpRect, newRotation);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002330
2331 // Compute a transform matrix to undo the coordinate space transformation,
2332 // and present the window at the same physical position it previously occupied.
2333 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2334 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2335
2336 mTmpRectF.set(bounds);
2337 mTmpMatrix.mapRect(mTmpRectF);
2338 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002339 }
2340
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002341 static int deltaRotation(int oldRotation, int newRotation) {
2342 int delta = newRotation - oldRotation;
2343 if (delta < 0) delta += 4;
2344 return delta;
2345 }
2346
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002347 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002348 Matrix outMatrix) {
2349 // For rotations without Z-ordering we don't need the target rectangle's position.
2350 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2351 displayHeight, outMatrix);
2352 }
2353
2354 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2355 float displayWidth, float displayHeight, Matrix outMatrix) {
2356 switch (rotation) {
2357 case ROTATION_0:
2358 outMatrix.reset();
2359 break;
2360 case ROTATION_270:
2361 outMatrix.setRotate(270, 0, 0);
2362 outMatrix.postTranslate(0, displayHeight);
2363 outMatrix.postTranslate(rectTop, 0);
2364 break;
2365 case ROTATION_180:
2366 outMatrix.reset();
2367 break;
2368 case ROTATION_90:
2369 outMatrix.setRotate(90, 0, 0);
2370 outMatrix.postTranslate(displayWidth, 0);
2371 outMatrix.postTranslate(-rectTop, rectLeft);
2372 break;
2373 }
2374 }
2375
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002376 @CallSuper
2377 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002378 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002379 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002380 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002381 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002382 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2383 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002384 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002385 }
2386 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2387 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002388 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2389 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002390 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002391 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002392 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2393 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002394 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002395 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002396 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2397 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002398 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002399 }
2400 proto.write(DPI, mBaseDisplayDensity);
2401 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002402 proto.write(ROTATION, mRotation);
2403 final ScreenRotationAnimation screenRotationAnimation =
2404 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2405 if (screenRotationAnimation != null) {
2406 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2407 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002408 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002409 proto.end(token);
2410 }
2411
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002412 @Override
2413 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2414 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002415 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2416 final String subPrefix = " " + prefix;
2417 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2418 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2419 pw.print("dpi");
2420 if (mInitialDisplayWidth != mBaseDisplayWidth
2421 || mInitialDisplayHeight != mBaseDisplayHeight
2422 || mInitialDisplayDensity != mBaseDisplayDensity) {
2423 pw.print(" base=");
2424 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2425 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2426 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002427 if (mDisplayScalingDisabled) {
2428 pw.println(" noscale");
2429 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002430 pw.print(" cur=");
2431 pw.print(mDisplayInfo.logicalWidth);
2432 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2433 pw.print(" app=");
2434 pw.print(mDisplayInfo.appWidth);
2435 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2436 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2437 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2438 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2439 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002440 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002441 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002442 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002443
Craig Mautnerdc548482014-02-05 13:35:24 -08002444 pw.println();
Adrian Roos5251b1d2018-03-23 18:57:43 +01002445 pw.print(prefix); pw.print("mLayoutSeq="); pw.println(mLayoutSeq);
Riddle Hsu654a6f92018-07-13 22:59:36 +08002446 pw.print(prefix);
2447 pw.print("mDeferredRotationPauseCount="); pw.println(mDeferredRotationPauseCount);
Adrian Roos5251b1d2018-03-23 18:57:43 +01002448
2449 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002450 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002451 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2452 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002453 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002454 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002455
Craig Mautnerdc548482014-02-05 13:35:24 -08002456 pw.println();
2457 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002458 pw.println();
2459 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002460 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002461 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002462 pw.print(" Exiting #"); pw.print(i);
2463 pw.print(' '); pw.print(token);
2464 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002465 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002466 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002467 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002468
Jorim Jaggi31f71702016-05-04 16:43:04 -07002469 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002470
2471 // Dump stack references
2472 final TaskStack homeStack = getHomeStack();
2473 if (homeStack != null) {
2474 pw.println(prefix + "homeStack=" + homeStack.getName());
2475 }
2476 final TaskStack pinnedStack = getPinnedStack();
2477 if (pinnedStack != null) {
2478 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2479 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002480 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002481 if (splitScreenPrimaryStack != null) {
2482 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2483 }
2484
2485 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002486 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002487 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002488 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002489
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002490 pw.println();
2491 mDisplayFrames.dump(prefix, pw);
Arthur Hung95b38a92018-07-20 18:56:12 +08002492 pw.println();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08002493 mDisplayPolicy.dump(prefix, pw);
2494 pw.println();
Riddle Hsuad256a12018-07-18 16:11:30 +08002495 mDisplayRotation.dump(prefix, pw);
2496 pw.println();
Arthur Hung95b38a92018-07-20 18:56:12 +08002497 mInputMonitor.dump(pw, " ");
Craig Mautner59c00972012-07-30 12:10:24 -07002498 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002499
2500 @Override
2501 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002502 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002503 }
2504
2505 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002506 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002507 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002508
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002509 /** Returns true if the stack in the windowing mode is visible. */
2510 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002511 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002512 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002513 }
2514
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002515 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002516 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002517 final int x = (int) xf;
2518 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002519 final WindowState touchedWin = getWindow(w -> {
2520 final int flags = w.mAttrs.flags;
2521 if (!w.isVisibleLw()) {
2522 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002523 }
2524 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002525 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002526 }
2527
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002528 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002529 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002530 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002531 }
2532
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002533 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002534
2535 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002536 return mTmpRegion.contains(x, y) || touchFlags == 0;
2537 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002538
2539 return touchedWin;
2540 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002541
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002542 boolean canAddToastWindowForUid(int uid) {
2543 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002544 // Also if the app is focused adding more than one toast at
2545 // a time for better backwards compatibility.
2546 final WindowState focusedWindowForUid = getWindow(w ->
2547 w.mOwnerUid == uid && w.isFocused());
2548 if (focusedWindowForUid != null) {
2549 return true;
2550 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002551 final WindowState win = getWindow(w ->
2552 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2553 && !w.mWindowRemovalAllowed);
2554 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002555 }
2556
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002557 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002558 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2559 return;
2560 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002561
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002562 // Used to communicate the old focus to the callback method.
2563 mTmpWindow = oldFocus;
2564
2565 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002566 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002567
2568 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002569 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002570
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002571 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002572
2573 if (mTmpWindow == null) {
2574 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2575 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002576 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002577 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002578 }
2579
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002580 /** Updates the layer assignment of windows on this display. */
2581 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002582 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07002583 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002584 if (setLayoutNeeded) {
2585 setLayoutNeeded();
2586 }
Robert Carrb1579c82017-09-05 14:54:47 -07002587
Robert Carrf59b8dd2017-10-02 18:58:36 -07002588 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002589 // the application of this transaction until the animation pass triggers
2590 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2591 // the hiding and showing of surfaces.
2592 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002593 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002594 }
2595
Wale Ogunwale1666e312016-12-16 11:27:18 -08002596 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2597 // moving containers or resizing them. Need to investigate the best way to have it automatically
2598 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002599 void layoutAndAssignWindowLayersIfNeeded() {
2600 mService.mWindowsChanged = true;
2601 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002602
2603 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2604 false /*updateInputWindows*/)) {
2605 assignWindowLayers(false /* setLayoutNeeded */);
2606 }
2607
Arthur Hung95b38a92018-07-20 18:56:12 +08002608 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002609 mService.mWindowPlacerLocked.performSurfacePlacement();
Arthur Hung95b38a92018-07-20 18:56:12 +08002610 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002611 }
2612
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002613 /** Returns true if a leaked surface was destroyed */
2614 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002615 // Used to indicate that a surface was leaked.
2616 mTmpWindow = null;
2617 forAllWindows(w -> {
2618 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002619 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002620 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002621 }
2622 if (!mService.mSessions.contains(wsa.mSession)) {
2623 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002624 + w + " surface=" + wsa.mSurfaceController
2625 + " token=" + w.mToken
2626 + " pid=" + w.mSession.mPid
2627 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002628 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002629 mService.mForceRemoves.add(w);
2630 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002631 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002632 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002633 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002634 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002635 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002636 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002637 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002638 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002639 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002640
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002641 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002642 }
2643
2644 /**
lumark90120a82018-08-15 00:33:03 +08002645 * Set input method window for the display.
2646 * @param win Set when window added or Null when destroyed.
2647 */
2648 void setInputMethodWindowLocked(WindowState win) {
2649 mInputMethodWindow = win;
2650 computeImeTarget(true /* updateImeTarget */);
2651 }
2652
2653 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002654 * Determine and return the window that should be the IME target.
2655 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2656 * @return The window that should be used as the IME target or null if there isn't any.
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002657 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002658 WindowState computeImeTarget(boolean updateImeTarget) {
lumark90120a82018-08-15 00:33:03 +08002659 if (mInputMethodWindow == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002660 // There isn't an IME so there shouldn't be a target...That was easy!
2661 if (updateImeTarget) {
2662 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2663 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002664 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002665 }
2666 return null;
2667 }
2668
Chavi Weingarten3a748552018-05-14 17:32:42 +00002669 final WindowState curTarget = mService.mInputMethodTarget;
2670 if (!canUpdateImeTarget()) {
2671 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Defer updating IME target");
2672 return curTarget;
2673 }
2674
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002675 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2676 // same display. Or even when the current IME/target are not on the same screen as the next
2677 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002678 mUpdateImeTarget = updateImeTarget;
2679 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002680
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002681
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002682 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2683 // to be on top of it, but it is not -really- where input will go. So look down below
2684 // for a real window to target...
2685 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2686 final AppWindowToken token = target.mAppToken;
2687 if (token != null) {
2688 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2689 if (betterTarget != null) {
2690 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002691 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002692 }
2693 }
2694
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002695 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2696 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002697
chaviw1c13ad32018-06-12 16:44:23 -07002698 // Now, a special case -- if the last target's window is in the process of exiting, but
2699 // not removed, and the new target is home, keep on the last target to avoid flicker.
2700 // Home is a special case since its above other stacks in the ordering list, but layed
2701 // out below the others.
2702 if (curTarget != null && !curTarget.mRemoved && curTarget.isDisplayedLw()
2703 && curTarget.isClosing() && (target == null || target.isActivityTypeHome())) {
chaviw4ad54912018-05-30 11:05:44 -07002704 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "New target is home while current target is "
Chavi Weingarten3a748552018-05-14 17:32:42 +00002705 + "closing, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002706 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002707 }
2708
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002709 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2710 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002711
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002712 if (target == null) {
2713 if (updateImeTarget) {
2714 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2715 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2716 + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002717 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002718 }
2719
2720 return null;
2721 }
2722
2723 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002724 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2725 if (token != null) {
2726
2727 // Now some fun for dealing with window animations that modify the Z order. We need
2728 // to look at all windows below the current target that are in this app, finding the
2729 // highest visible one in layering.
2730 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002731 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002732 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002733 }
2734
2735 if (highestTarget != null) {
2736 final AppTransition appTransition = mService.mAppTransition;
2737 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2738 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2739 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002740 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002741
2742 if (appTransition.isTransitionSet()) {
2743 // If we are currently setting up for an animation, hold everything until we
2744 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002745 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002746 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002747 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002748 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002749 // If the window we are currently targeting is involved with an animation,
2750 // and it is on top of the next target we will be over, then hold off on
2751 // moving until that is done.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002752 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002753 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002754 }
2755 }
2756 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002757
2758 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2759 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002760 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002761 }
2762
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002763 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002764 }
2765
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002766 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002767 if (target == mService.mInputMethodTarget
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002768 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002769 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002770 }
2771
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002772 mService.mInputMethodTarget = target;
2773 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002774 assignWindowLayers(false /* setLayoutNeeded */);
2775 }
2776
2777 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2778 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2779 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2780 }
2781
2782 // Used to indicate we have reached the first window in the range we are interested in.
2783 mTmpWindow = null;
2784
2785 // TODO: Figure-out a more efficient way to do this.
2786 final WindowState candidate = getWindow(w -> {
2787 if (w == top) {
2788 // Reached the first window in the range we are interested in.
2789 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002790 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002791 if (mTmpWindow == null) {
2792 return false;
2793 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002794
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002795 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2796 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002797 }
2798 // If we reached the bottom of the range of windows we are considering,
2799 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002800 if (w == bottom) {
2801 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002802 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002803 return false;
2804 });
2805
2806 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002807 }
2808
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002809 void setLayoutNeeded() {
2810 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2811 mLayoutNeeded = true;
2812 }
2813
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002814 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002815 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2816 mLayoutNeeded = false;
2817 }
2818
2819 boolean isLayoutNeeded() {
2820 return mLayoutNeeded;
2821 }
2822
Wale Ogunwale02319a62016-09-26 15:21:22 -07002823 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2824 if (mTokenMap.isEmpty()) {
2825 return;
2826 }
2827 pw.println(" Display #" + mDisplayId);
2828 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2829 while (it.hasNext()) {
2830 final WindowToken token = it.next();
2831 pw.print(" ");
2832 pw.print(token);
2833 if (dumpAll) {
2834 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002835 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07002836 } else {
2837 pw.println();
2838 }
2839 }
2840 }
2841
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002842 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002843 final int[] index = new int[1];
2844 forAllWindows(w -> {
2845 final WindowStateAnimator wAnim = w.mWinAnimator;
2846 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2847 index[0] = index[0] + 1;
2848 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002849 }
2850
Jorim Jaggife762342016-10-13 14:33:27 +02002851 /**
2852 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2853 */
2854 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2855 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002856 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002857 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2858 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01002859 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02002860 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002861 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002862 }
2863
Wale Ogunwale494009b82016-10-21 09:01:38 -07002864 boolean checkWaitingForWindows() {
2865
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002866 mHaveBootMsg = false;
2867 mHaveApp = false;
2868 mHaveWallpaper = false;
2869 mHaveKeyguard = true;
2870
2871 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002872 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2873 return true;
2874 }
2875 if (w.isDrawnLw()) {
2876 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002877 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002878 } else if (w.mAttrs.type == TYPE_APPLICATION
2879 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002880 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002881 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002882 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002883 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002884 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002885 }
2886 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002887 return false;
2888 });
2889
2890 if (visibleWindow != null) {
2891 // We have a visible window.
2892 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002893 }
2894
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002895 // if the wallpaper service is disabled on the device, we're never going to have
2896 // wallpaper, don't bother waiting for it
2897 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2898 com.android.internal.R.bool.config_enableWallpaperService)
Daichi Hironoc1432222018-03-29 13:06:07 +09002899 && mService.mContext.getResources().getBoolean(
2900 com.android.internal.R.bool.config_checkWallpaperAtBoot)
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002901 && !mService.mOnlyCore;
2902
Wale Ogunwale494009b82016-10-21 09:01:38 -07002903 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2904 "******** booted=" + mService.mSystemBooted
2905 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002906 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2907 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2908 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002909
2910 // If we are turning on the screen to show the boot message, don't do it until the boot
2911 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002912 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002913 return true;
2914 }
2915
2916 // If we are turning on the screen after the boot is completed normally, don't do so until
2917 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002918 if (mService.mSystemBooted
2919 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002920 return true;
2921 }
2922
2923 return false;
2924 }
2925
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002926 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002927 mTmpWindowAnimator = animator;
2928 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002929 }
2930
2931 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002932 resetAnimationBackgroundAnimator();
2933
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002934 // Used to indicate a detached wallpaper.
2935 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002936 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002937
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002938 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002939
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002940 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002941 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002942 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2943 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002944 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2945 }
2946 }
2947
Yohei Yukawa41f89c32018-09-19 14:30:04 -07002948 boolean isInputMethodClientFocus(int uid, int pid) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002949 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002950 if (imFocus == null) {
2951 return false;
2952 }
2953
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002954 if (DEBUG_INPUT_METHOD) {
2955 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2956 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2957 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002958 }
2959
Wale Ogunwale494009b82016-10-21 09:01:38 -07002960 if (DEBUG_INPUT_METHOD) {
Yohei Yukawa41f89c32018-09-19 14:30:04 -07002961 Slog.i(TAG_WM, "IM target uid/pid: " + imFocus.mSession.mUid
2962 + "/" + imFocus.mSession.mPid);
2963 Slog.i(TAG_WM, "Requesting client uid/pid: " + uid + "/" + pid);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002964 }
2965
Yohei Yukawa41f89c32018-09-19 14:30:04 -07002966 return imFocus.mSession.mUid == uid && imFocus.mSession.mPid == pid;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002967 }
2968
2969 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002970 final WindowState win = getWindow(
2971 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2972 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002973 }
2974
2975 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002976 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002977 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002978 final int curValue = w.mSystemUiVisibility;
2979 final int diff = (curValue ^ visibility) & globalDiff;
2980 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002981 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002982 w.mSeq++;
2983 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002984 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002985 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2986 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002987 visibility, newValue, diff);
2988 }
2989 } catch (RemoteException e) {
2990 // so sorry
2991 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002992 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002993 }
2994
2995 void onWindowFreezeTimeout() {
2996 Slog.w(TAG_WM, "Window freeze timeout expired.");
2997 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002998
2999 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07003000 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003001 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003002 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07003003 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003004 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
3005 - mService.mDisplayFreezeTime);
3006 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003007 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003008 mService.mWindowPlacerLocked.performSurfacePlacement();
3009 }
3010
3011 void waitForAllWindowsDrawn() {
3012 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003013 forAllWindows(w -> {
3014 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
3015 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
3016 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003017 // Force add to mResizingWindows.
chaviw9c81e632018-07-31 11:17:52 -07003018 w.resetLastContentInsets();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003019 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003020 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003021 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003022 }
3023
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003024 // TODO: Super crazy long method that should be broken down...
3025 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
3026
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003027 final int dw = mDisplayInfo.logicalWidth;
3028 final int dh = mDisplayInfo.logicalHeight;
3029 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
3030
3031 mTmpUpdateAllDrawn.clear();
3032
3033 int repeats = 0;
3034 do {
3035 repeats++;
3036 if (repeats > 6) {
3037 Slog.w(TAG, "Animation repeat aborted after too many iterations");
3038 clearLayoutNeeded();
3039 break;
3040 }
3041
3042 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
3043 pendingLayoutChanges);
3044
Andrii Kulian839def92016-11-02 10:58:58 -07003045 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
3046 // the wallpaper window jumping across displays.
3047 // Remove check for default display when there will be support for multiple wallpaper
3048 // targets (on different displays).
3049 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003050 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003051 }
3052
Riddle Hsu654a6f92018-07-13 22:59:36 +08003053 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003054 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
Robert Carrae606b42018-02-15 15:36:23 -08003055 if (mService.updateOrientationFromAppTokensLocked(mDisplayId)) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003056 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07003057 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003058 }
3059 }
3060
3061 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
3062 setLayoutNeeded();
3063 }
3064
3065 // FIRST LOOP: Perform a layout, if needed.
3066 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
3067 performLayout(repeats == 1, false /* updateInputWindows */);
3068 } else {
3069 Slog.w(TAG, "Layout repeat skipped after too many iterations");
3070 }
3071
3072 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
3073 pendingLayoutChanges = 0;
3074
3075 if (isDefaultDisplay) {
3076 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003077 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003078 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
3079 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
3080 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
3081 }
3082 } while (pendingLayoutChanges != 0);
3083
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003084 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003085
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003086 mTmpRecoveringMemory = recoveringMemory;
3087 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01003088 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003089
3090 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003091 mTmpApplySurfaceChangesTransactionState.displayHasContent,
3092 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
3093 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003094 true /* inTraversal, must call performTraversalInTrans... below */);
3095
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003096 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
3097 if (wallpaperVisible != mLastWallpaperVisible) {
3098 mLastWallpaperVisible = wallpaperVisible;
3099 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
3100 }
3101
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003102 while (!mTmpUpdateAllDrawn.isEmpty()) {
3103 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
3104 // See if any windows have been drawn, so they (and others associated with them)
3105 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07003106 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003107 }
3108
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003109 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003110 }
3111
Bryce Leef3c6a472017-11-14 14:53:06 -08003112 private void updateBounds() {
3113 calculateBounds(mTmpBounds);
3114 setBounds(mTmpBounds);
3115 }
3116
3117 // Determines the current display bounds based on the current state
3118 private void calculateBounds(Rect out) {
3119 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
3120 final int orientation = mDisplayInfo.rotation;
3121 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
3122 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
3123 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
3124 int width = mDisplayInfo.logicalWidth;
3125 int left = (physWidth - width) / 2;
3126 int height = mDisplayInfo.logicalHeight;
3127 int top = (physHeight - height) / 2;
3128 out.set(left, top, left + width, top + height);
3129 }
3130
3131 @Override
3132 public void getBounds(Rect out) {
3133 calculateBounds(out);
3134 }
3135
3136 private void getBounds(Rect out, int orientation) {
3137 getBounds(out);
3138
3139 // Rotate the Rect if needed.
3140 final int currentRotation = mDisplayInfo.rotation;
3141 final int rotationDelta = deltaRotation(currentRotation, orientation);
3142 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
3143 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
3144 mTmpRectF.set(out);
3145 mTmpMatrix.mapRect(mTmpRectF);
3146 mTmpRectF.round(out);
3147 }
3148 }
3149
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003150 void performLayout(boolean initial, boolean updateInputWindows) {
3151 if (!isLayoutNeeded()) {
3152 return;
3153 }
3154 clearLayoutNeeded();
3155
3156 final int dw = mDisplayInfo.logicalWidth;
3157 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003158 if (DEBUG_LAYOUT) {
3159 Slog.v(TAG, "-------------------------------------");
3160 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
3161 }
3162
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01003163 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
3164 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00003165 // TODO: Not sure if we really need to set the rotation here since we are updating from the
3166 // display info above...
3167 mDisplayFrames.mRotation = mRotation;
3168 mService.mPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003169 if (isDefaultDisplay) {
3170 // Not needed on non-default displays.
3171 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
3172 mService.mScreenRect.set(0, 0, dw, dh);
3173 }
3174
Adrian Roos5251b1d2018-03-23 18:57:43 +01003175 int seq = mLayoutSeq + 1;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003176 if (seq < 0) seq = 0;
Adrian Roos5251b1d2018-03-23 18:57:43 +01003177 mLayoutSeq = seq;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003178
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003179 // Used to indicate that we have processed the dream window and all additional windows are
3180 // behind it.
3181 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003182 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003183
3184 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003185 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003186
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003187 // Used to indicate that we have processed the dream window and all additional attached
3188 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003189 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003190 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003191
3192 // Now perform layout of attached windows, which usually depend on the position of the
3193 // window they are attached to. XXX does not deal with windows that are attached to windows
3194 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003195 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003196
3197 // Window frames may have changed. Tell the input dispatcher about it.
Arthur Hung95b38a92018-07-20 18:56:12 +08003198 mInputMonitor.layoutInputConsumers(dw, dh);
3199 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003200 if (updateInputWindows) {
Arthur Hung95b38a92018-07-20 18:56:12 +08003201 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003202 }
3203
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003204 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
3205 }
3206
3207 /**
3208 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
3209 * In portrait mode, it grabs the full screenshot.
3210 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003211 * @param config of the output bitmap
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003212 */
chaviw0315a1a2018-03-05 15:28:35 -08003213 Bitmap screenshotDisplayLocked(Bitmap.Config config) {
3214 if (!mService.mPolicy.isScreenOn()) {
3215 if (DEBUG_SCREENSHOT) {
3216 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003217 }
chaviw0315a1a2018-03-05 15:28:35 -08003218 return null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003219 }
chaviwfbe47df2017-11-10 16:14:49 -08003220
chaviw0315a1a2018-03-05 15:28:35 -08003221 int dw = mDisplayInfo.logicalWidth;
3222 int dh = mDisplayInfo.logicalHeight;
chaviwfbe47df2017-11-10 16:14:49 -08003223
chaviw0315a1a2018-03-05 15:28:35 -08003224 if (dw <= 0 || dh <= 0) {
3225 return null;
3226 }
chaviwfbe47df2017-11-10 16:14:49 -08003227
chaviw0315a1a2018-03-05 15:28:35 -08003228 final Rect frame = new Rect(0, 0, dw, dh);
chaviwfbe47df2017-11-10 16:14:49 -08003229
chaviw0315a1a2018-03-05 15:28:35 -08003230 // The screenshot API does not apply the current screen rotation.
3231 int rot = mDisplay.getRotation();
chaviwfbe47df2017-11-10 16:14:49 -08003232
chaviw0315a1a2018-03-05 15:28:35 -08003233 if (rot == ROTATION_90 || rot == ROTATION_270) {
3234 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3235 }
chaviwfbe47df2017-11-10 16:14:49 -08003236
chaviw0315a1a2018-03-05 15:28:35 -08003237 // SurfaceFlinger is not aware of orientation, so convert our logical
3238 // crop to SurfaceFlinger's portrait orientation.
3239 convertCropForSurfaceFlinger(frame, rot, dw, dh);
3240
3241 final ScreenRotationAnimation screenRotationAnimation =
3242 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3243 final boolean inRotation = screenRotationAnimation != null &&
3244 screenRotationAnimation.isAnimating();
3245 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
3246
3247 // TODO(b/68392460): We should screenshot Task controls directly
3248 // but it's difficult at the moment as the Task doesn't have the
3249 // correct size set.
3250 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, 0, 1, inRotation, rot);
3251 if (bitmap == null) {
3252 Slog.w(TAG_WM, "Failed to take screenshot");
3253 return null;
3254 }
3255
3256 // Create a copy of the screenshot that is immutable and backed in ashmem.
3257 // This greatly reduces the overhead of passing the bitmap between processes.
3258 final Bitmap ret = bitmap.createAshmemBitmap(config);
3259 bitmap.recycle();
3260 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003261 }
3262
3263 // TODO: Can this use createRotationMatrix()?
3264 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3265 if (rot == Surface.ROTATION_90) {
3266 final int tmp = crop.top;
3267 crop.top = dw - crop.right;
3268 crop.right = crop.bottom;
3269 crop.bottom = dw - crop.left;
3270 crop.left = tmp;
3271 } else if (rot == Surface.ROTATION_180) {
3272 int tmp = crop.top;
3273 crop.top = dh - crop.bottom;
3274 crop.bottom = dh - tmp;
3275 tmp = crop.right;
3276 crop.right = dw - crop.left;
3277 crop.left = dw - tmp;
3278 } else if (rot == Surface.ROTATION_270) {
3279 final int tmp = crop.top;
3280 crop.top = crop.left;
3281 crop.left = dh - crop.bottom;
3282 crop.bottom = crop.right;
3283 crop.right = dh - tmp;
3284 }
3285 }
3286
3287 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003288 // Used to indicate the layout is needed.
3289 mTmpWindow = null;
3290
3291 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003292 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003293 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003294 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003295 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003296 w.setDisplayLayoutNeeded();
3297 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003298 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003299
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003300 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003301 mService.mWindowPlacerLocked.performSurfacePlacement();
3302 }
3303 }
3304
Wale Ogunwale1666e312016-12-16 11:27:18 -08003305 void setExitingTokensHasVisible(boolean hasVisible) {
3306 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3307 mExitingTokens.get(i).hasVisible = hasVisible;
3308 }
3309
3310 // Initialize state of exiting applications.
3311 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3312 }
3313
3314 void removeExistingTokensIfPossible() {
3315 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3316 final WindowToken token = mExitingTokens.get(i);
3317 if (!token.hasVisible) {
3318 mExitingTokens.remove(i);
3319 }
3320 }
3321
3322 // Time to remove any exiting applications?
3323 mTaskStackContainers.removeExistingAppTokensIfPossible();
3324 }
3325
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003326 @Override
3327 void onDescendantOverrideConfigurationChanged() {
3328 setLayoutNeeded();
3329 mService.requestTraversal();
3330 }
3331
David Stevens9440dc82017-03-16 19:00:20 -07003332 boolean okToDisplay() {
3333 if (mDisplayId == DEFAULT_DISPLAY) {
3334 return !mService.mDisplayFrozen
3335 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3336 }
3337 return mDisplayInfo.state == Display.STATE_ON;
3338 }
3339
3340 boolean okToAnimate() {
3341 return okToDisplay() &&
3342 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3343 }
3344
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003345 static final class TaskForResizePointSearchResult {
3346 boolean searchDone;
3347 Task taskForResize;
3348
3349 void reset() {
3350 searchDone = false;
3351 taskForResize = null;
3352 }
3353 }
Robert Carr3b716242016-08-16 16:02:21 -07003354
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003355 private static final class ApplySurfaceChangesTransactionState {
3356 boolean displayHasContent;
3357 boolean obscured;
3358 boolean syswin;
3359 boolean focusDisplayed;
3360 float preferredRefreshRate;
3361 int preferredModeId;
3362
3363 void reset() {
3364 displayHasContent = false;
3365 obscured = false;
3366 syswin = false;
3367 focusDisplayed = false;
3368 preferredRefreshRate = 0;
3369 preferredModeId = 0;
3370 }
3371 }
3372
3373 private static final class ScreenshotApplicationState {
3374 WindowState appWin;
3375 int maxLayer;
3376 int minLayer;
3377 boolean screenshotReady;
3378
3379 void reset(boolean screenshotReady) {
3380 appWin = null;
3381 maxLayer = 0;
3382 minLayer = 0;
3383 this.screenshotReady = screenshotReady;
3384 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3385 }
3386 }
3387
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003388 /**
3389 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3390 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3391 * homogeneous children type which is currently required by sub-classes of
3392 * {@link WindowContainer} class.
3393 */
3394 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3395
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003396 DisplayChildWindowContainer(WindowManagerService service) {
3397 super(service);
3398 }
3399
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003400 @Override
3401 boolean fillsParent() {
3402 return true;
3403 }
3404
3405 @Override
3406 boolean isVisible() {
3407 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003408 }
3409 }
3410
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003411 /**
3412 * Window container class that contains all containers on this display relating to Apps.
3413 * I.e Activities.
3414 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003415 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003416 /**
3417 * A control placed at the appropriate level for transitions to occur.
3418 */
chaviw23ee71c2017-12-18 11:29:41 -08003419 SurfaceControl mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02003420 SurfaceControl mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02003421 SurfaceControl mHomeAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003422
Robert Carrf7a7ca82017-12-06 13:17:07 -08003423 /**
3424 * Given that the split-screen divider does not have an AppWindowToken, it
3425 * will have to live inside of a "NonAppWindowContainer", in particular
3426 * {@link DisplayContent#mAboveAppWindowsContainers}. However, in visual Z order
3427 * it will need to be interleaved with some of our children, appearing on top of
3428 * both docked stacks but underneath any assistant stacks.
3429 *
3430 * To solve this problem we have this anchor control, which will always exist so
3431 * we can always assign it the correct value in our {@link #assignChildLayers}.
3432 * Likewise since it always exists, {@link AboveAppWindowContainers} can always
3433 * assign the divider a layer relative to it. This way we prevent linking lifecycle
3434 * events between the two containers.
3435 */
3436 SurfaceControl mSplitScreenDividerAnchor = null;
3437
Wale Ogunwale61911492017-10-11 08:50:50 -07003438 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3439 // through the list to find them.
3440 private TaskStack mHomeStack = null;
3441 private TaskStack mPinnedStack = null;
3442 private TaskStack mSplitScreenPrimaryStack = null;
3443
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003444 TaskStackContainers(WindowManagerService service) {
3445 super(service);
3446 }
3447
Wale Ogunwale61911492017-10-11 08:50:50 -07003448 /**
3449 * Returns the topmost stack on the display that is compatible with the input windowing mode
3450 * and activity type. Null is no compatible stack on the display.
3451 */
3452 TaskStack getStack(int windowingMode, int activityType) {
3453 if (activityType == ACTIVITY_TYPE_HOME) {
3454 return mHomeStack;
3455 }
3456 if (windowingMode == WINDOWING_MODE_PINNED) {
3457 return mPinnedStack;
3458 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3459 return mSplitScreenPrimaryStack;
3460 }
3461 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3462 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003463 if (activityType == ACTIVITY_TYPE_UNDEFINED
3464 && windowingMode == stack.getWindowingMode()) {
3465 // Passing in undefined type means we want to match the topmost stack with the
3466 // windowing mode.
3467 return stack;
3468 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003469 if (stack.isCompatible(windowingMode, activityType)) {
3470 return stack;
3471 }
3472 }
3473 return null;
3474 }
3475
3476 @VisibleForTesting
3477 TaskStack getTopStack() {
3478 return mTaskStackContainers.getChildCount() > 0
3479 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3480 }
3481
3482 TaskStack getHomeStack() {
3483 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3484 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3485 }
3486 return mHomeStack;
3487 }
3488
3489 TaskStack getPinnedStack() {
3490 return mPinnedStack;
3491 }
3492
Matthew Ng64e77cf2017-10-31 14:01:31 -07003493 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003494 return mSplitScreenPrimaryStack;
3495 }
3496
Winson Chunge2d72172018-01-25 17:46:20 +00003497 ArrayList<Task> getVisibleTasks() {
3498 final ArrayList<Task> visibleTasks = new ArrayList<>();
3499 forAllTasks(task -> {
3500 if (task.isVisible()) {
3501 visibleTasks.add(task);
3502 }
3503 });
3504 return visibleTasks;
3505 }
3506
Andrii Kulian839def92016-11-02 10:58:58 -07003507 /**
3508 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003509 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003510 */
3511 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003512 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003513 addChild(stack, onTop);
3514 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003515 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003516
Wale Ogunwale61911492017-10-11 08:50:50 -07003517 void onStackWindowingModeChanged(TaskStack stack) {
3518 removeStackReferenceIfNeeded(stack);
3519 addStackReferenceIfNeeded(stack);
3520 if (stack == mPinnedStack && getTopStack() != stack) {
3521 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3522 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3523 }
3524 }
3525
3526 private void addStackReferenceIfNeeded(TaskStack stack) {
3527 if (stack.isActivityTypeHome()) {
3528 if (mHomeStack != null) {
Louis Chang7d0037c2018-08-13 12:42:06 +08003529 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
Wale Ogunwale61911492017-10-11 08:50:50 -07003530 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
Louis Chang7d0037c2018-08-13 12:42:06 +08003531
Wale Ogunwale61911492017-10-11 08:50:50 -07003532 }
Louis Chang7d0037c2018-08-13 12:42:06 +08003533 mHomeStack = stack;
Wale Ogunwale61911492017-10-11 08:50:50 -07003534 }
3535 final int windowingMode = stack.getWindowingMode();
3536 if (windowingMode == WINDOWING_MODE_PINNED) {
3537 if (mPinnedStack != null) {
3538 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3539 + mPinnedStack + " already exist on display=" + this
3540 + " stack=" + stack);
3541 }
3542 mPinnedStack = stack;
3543 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3544 if (mSplitScreenPrimaryStack != null) {
3545 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3546 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3547 + " already exist on display=" + this + " stack=" + stack);
3548 }
3549 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003550 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003551 }
3552 }
3553
3554 private void removeStackReferenceIfNeeded(TaskStack stack) {
3555 if (stack == mHomeStack) {
3556 mHomeStack = null;
3557 } else if (stack == mPinnedStack) {
3558 mPinnedStack = null;
3559 } else if (stack == mSplitScreenPrimaryStack) {
3560 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003561 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3562 mService.setDockedStackCreateStateLocked(
3563 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3564 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003565 }
Andrii Kulian839def92016-11-02 10:58:58 -07003566 }
3567
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003568 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003569 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3570 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003571 addChild(stack, addIndex);
3572 setLayoutNeeded();
3573 }
3574
Wale Ogunwale61911492017-10-11 08:50:50 -07003575 @Override
3576 protected void removeChild(TaskStack stack) {
3577 super.removeChild(stack);
3578 removeStackReferenceIfNeeded(stack);
3579 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003580
3581 @Override
3582 boolean isOnTop() {
3583 // Considered always on top
3584 return true;
3585 }
3586
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003587 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003588 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003589 if (child.getWindowConfiguration().isAlwaysOnTop()
3590 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003591 // This stack is always-on-top, override the default behavior.
3592 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3593
3594 // Moving to its current position, as we must call super but we don't want to
3595 // perform any meaningful action.
3596 final int currentPosition = mChildren.indexOf(child);
3597 super.positionChildAt(currentPosition, child, false /* includingParents */);
3598 return;
3599 }
3600
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003601 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3602 super.positionChildAt(targetPosition, child, includingParents);
3603
Riddle Hsu2f6e1742018-08-23 22:44:36 +08003604 if (includingParents) {
3605 // We still want to move the display of this stack container to top because even the
3606 // target position is adjusted to non-top, the intention of the condition is to have
3607 // higher z-order to gain focus (e.g. moving a task of a fullscreen stack to front
3608 // in a non-top display which is using picture-in-picture mode).
3609 final int topChildPosition = getChildCount() - 1;
3610 if (targetPosition < topChildPosition && position >= topChildPosition) {
3611 getParent().positionChildAt(POSITION_TOP, this /* child */,
3612 true /* includingParents */);
3613 }
3614 }
3615
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003616 setLayoutNeeded();
3617 }
3618
3619 /**
3620 * When stack is added or repositioned, find a proper position for it.
3621 * This will make sure that pinned stack always stays on top.
3622 * @param requestedPosition Position requested by caller.
3623 * @param stack Stack to be added or positioned.
3624 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3625 * @return The proper position for the stack.
3626 */
3627 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09003628 if (stack.inPinnedWindowingMode()) {
Kazuki Takisef85197b2018-06-18 18:18:36 +09003629 return POSITION_TOP;
Kazuki Takise148d00a2018-05-31 15:32:19 +09003630 }
3631
Kazuki Takisef85197b2018-06-18 18:18:36 +09003632 final int topChildPosition = mChildren.size() - 1;
3633 int belowAlwaysOnTopPosition = POSITION_BOTTOM;
3634 for (int i = topChildPosition; i >= 0; --i) {
3635 if (getStacks().get(i) != stack && !getStacks().get(i).isAlwaysOnTop()) {
3636 belowAlwaysOnTopPosition = i;
3637 break;
3638 }
3639 }
3640
3641 // The max possible position we can insert the stack at.
3642 int maxPosition = POSITION_TOP;
3643 // The min possible position we can insert the stack at.
3644 int minPosition = POSITION_BOTTOM;
3645
3646 if (stack.isAlwaysOnTop()) {
3647 if (hasPinnedStack()) {
3648 // Always-on-top stacks go below the pinned stack.
3649 maxPosition = getStacks().indexOf(mPinnedStack) - 1;
3650 }
3651 // Always-on-top stacks need to be above all other stacks.
3652 minPosition = belowAlwaysOnTopPosition !=
3653 POSITION_BOTTOM ? belowAlwaysOnTopPosition : topChildPosition;
3654 } else {
3655 // Other stacks need to be below the always-on-top stacks.
3656 maxPosition = belowAlwaysOnTopPosition !=
Arthur Hung928d8dc2018-07-18 15:38:05 +08003657 POSITION_BOTTOM ? belowAlwaysOnTopPosition : 0;
Kazuki Takisef85197b2018-06-18 18:18:36 +09003658 }
3659
3660 int targetPosition = requestedPosition;
3661 targetPosition = Math.min(targetPosition, maxPosition);
3662 targetPosition = Math.max(targetPosition, minPosition);
3663
3664 int prevPosition = getStacks().indexOf(stack);
3665 // The positions we calculated above (maxPosition, minPosition) do not take into
3666 // consideration the following edge cases.
3667 // 1) We need to adjust the position depending on the value "adding".
3668 // 2) When we are moving a stack to another position, we also need to adjust the
3669 // position depending on whether the stack is moving to a higher or lower position.
3670 if ((targetPosition != requestedPosition) &&
3671 (adding || targetPosition < prevPosition)) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09003672 targetPosition++;
3673 }
3674
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003675 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003676 }
3677
3678 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003679 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3680 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003681 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003682 if (super.forAllWindows(callback, traverseTopToBottom)) {
3683 return true;
3684 }
3685 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3686 return true;
3687 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003688 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003689 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3690 return true;
3691 }
3692 if (super.forAllWindows(callback, traverseTopToBottom)) {
3693 return true;
3694 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003695 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003696 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003697 }
3698
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003699 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003700 boolean traverseTopToBottom) {
3701 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3702 // app tokens.
3703 // TODO: Investigate if we need to continue to do this or if we can just process them
3704 // in-order.
3705 if (traverseTopToBottom) {
3706 for (int i = mChildren.size() - 1; i >= 0; --i) {
3707 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3708 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003709 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3710 traverseTopToBottom)) {
3711 return true;
3712 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003713 }
3714 }
3715 } else {
3716 final int count = mChildren.size();
3717 for (int i = 0; i < count; ++i) {
3718 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3719 final int appTokensCount = appTokens.size();
3720 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003721 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3722 traverseTopToBottom)) {
3723 return true;
3724 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003725 }
3726 }
3727 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003728 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003729 }
3730
Wale Ogunwale1666e312016-12-16 11:27:18 -08003731 void setExitingTokensHasVisible(boolean hasVisible) {
3732 for (int i = mChildren.size() - 1; i >= 0; --i) {
3733 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3734 for (int j = appTokens.size() - 1; j >= 0; --j) {
3735 appTokens.get(j).hasVisible = hasVisible;
3736 }
3737 }
3738 }
3739
3740 void removeExistingAppTokensIfPossible() {
3741 for (int i = mChildren.size() - 1; i >= 0; --i) {
3742 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3743 for (int j = appTokens.size() - 1; j >= 0; --j) {
3744 final AppWindowToken token = appTokens.get(j);
3745 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3746 && (!token.mIsExiting || token.isEmpty())) {
3747 // Make sure there is no animation running on this token, so any windows
3748 // associated with it will be removed as soon as their animations are
3749 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003750 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003751 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3752 "performLayout: App token exiting now removed" + token);
3753 token.removeIfPossible();
3754 }
3755 }
3756 }
3757 }
3758
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003759 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003760 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003761 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
3762 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003763 // Apps and their containers are not allowed to specify an orientation while the
Wale Ogunwaled0c225c2018-06-15 14:47:46 -07003764 // docked or freeform stack is visible...except for the home stack if the docked
3765 // stack is minimized and it actually set something and the bounds is different from
3766 // the display.
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003767 if (mHomeStack != null && mHomeStack.isVisible()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07003768 && mDividerControllerLocked.isMinimizedDock()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07003769 && !(mDividerControllerLocked.isHomeStackResizable()
3770 && mHomeStack.matchParentBounds())) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003771 final int orientation = mHomeStack.getOrientation();
3772 if (orientation != SCREEN_ORIENTATION_UNSET) {
3773 return orientation;
3774 }
3775 }
3776 return SCREEN_ORIENTATION_UNSPECIFIED;
3777 }
3778
3779 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07003780 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
3781 PackageManager.FEATURE_AUTOMOTIVE);
3782 if (isCar) {
3783 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
3784 // allow anything but the default orientation.
3785 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07003786 "Forcing UNSPECIFIED orientation in car for display id=" + mDisplayId
3787 + ". Ignoring " + orientation);
Dean Harding3e5a1522017-10-06 09:19:01 -07003788 return SCREEN_ORIENTATION_UNSPECIFIED;
3789 }
3790
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003791 if (orientation != SCREEN_ORIENTATION_UNSET
3792 && orientation != SCREEN_ORIENTATION_BEHIND) {
3793 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07003794 "App is requesting an orientation, return " + orientation
3795 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003796 return orientation;
3797 }
3798
3799 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07003800 "No app is requesting an orientation, return " + mLastOrientation
3801 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003802 // The next app has not been requested to be visible, so we keep the current orientation
3803 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003804 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003805 }
Robert Carrb1579c82017-09-05 14:54:47 -07003806
3807 @Override
3808 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carr2f8aa392018-01-31 14:46:51 -08003809 assignStackOrdering(t);
Robert Carrf7a7ca82017-12-06 13:17:07 -08003810
Robert Carr2f8aa392018-01-31 14:46:51 -08003811 for (int i = 0; i < mChildren.size(); i++) {
3812 final TaskStack s = mChildren.get(i);
3813 s.assignChildLayers(t);
3814 }
3815 }
3816
3817 void assignStackOrdering(SurfaceControl.Transaction t) {
Jorim Jaggibe418292018-03-26 16:14:12 +02003818
Robert Carrf7a7ca82017-12-06 13:17:07 -08003819 final int HOME_STACK_STATE = 0;
3820 final int NORMAL_STACK_STATE = 1;
3821 final int ALWAYS_ON_TOP_STATE = 2;
3822
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003823 int layer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08003824 int layerForAnimationLayer = 0;
Jorim Jaggibe418292018-03-26 16:14:12 +02003825 int layerForBoostedAnimationLayer = 0;
Jorim Jaggi391790622018-04-18 15:30:44 +02003826 int layerForHomeAnimationLayer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08003827
Robert Carrf7a7ca82017-12-06 13:17:07 -08003828 for (int state = 0; state <= ALWAYS_ON_TOP_STATE; state++) {
3829 for (int i = 0; i < mChildren.size(); i++) {
3830 final TaskStack s = mChildren.get(i);
Robert Carr2f8aa392018-01-31 14:46:51 -08003831 if (state == HOME_STACK_STATE && !s.isActivityTypeHome()) {
3832 continue;
3833 } else if (state == NORMAL_STACK_STATE && (s.isActivityTypeHome()
3834 || s.isAlwaysOnTop())) {
3835 continue;
3836 } else if (state == ALWAYS_ON_TOP_STATE && !s.isAlwaysOnTop()) {
3837 continue;
3838 }
3839 s.assignLayer(t, layer++);
3840 if (s.inSplitScreenWindowingMode() && mSplitScreenDividerAnchor != null) {
3841 t.setLayer(mSplitScreenDividerAnchor, layer++);
3842 }
Winson Chungd41f71d2018-03-16 15:26:07 -07003843 if ((s.isTaskAnimating() || s.isAppAnimating())
3844 && state != ALWAYS_ON_TOP_STATE) {
Robert Carr2f8aa392018-01-31 14:46:51 -08003845 // Ensure the animation layer ends up above the
3846 // highest animating stack and no higher.
3847 layerForAnimationLayer = layer++;
Robert Carrf7a7ca82017-12-06 13:17:07 -08003848 }
Jorim Jaggibe418292018-03-26 16:14:12 +02003849 if (state != ALWAYS_ON_TOP_STATE) {
3850 layerForBoostedAnimationLayer = layer++;
3851 }
Robert Carrf7a7ca82017-12-06 13:17:07 -08003852 }
Jorim Jaggi391790622018-04-18 15:30:44 +02003853 if (state == HOME_STACK_STATE) {
3854 layerForHomeAnimationLayer = layer++;
3855 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003856 }
Robert Carr2f8aa392018-01-31 14:46:51 -08003857 if (mAppAnimationLayer != null) {
3858 t.setLayer(mAppAnimationLayer, layerForAnimationLayer);
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003859 }
Jorim Jaggibe418292018-03-26 16:14:12 +02003860 if (mBoostedAppAnimationLayer != null) {
3861 t.setLayer(mBoostedAppAnimationLayer, layerForBoostedAnimationLayer);
3862 }
Jorim Jaggi391790622018-04-18 15:30:44 +02003863 if (mHomeAppAnimationLayer != null) {
3864 t.setLayer(mHomeAppAnimationLayer, layerForHomeAnimationLayer);
3865 }
Robert Carrb1579c82017-09-05 14:54:47 -07003866 }
3867
3868 @Override
Jorim Jaggi391790622018-04-18 15:30:44 +02003869 SurfaceControl getAppAnimationLayer(@AnimationLayer int animationLayer) {
3870 switch (animationLayer) {
3871 case ANIMATION_LAYER_BOOSTED:
3872 return mBoostedAppAnimationLayer;
3873 case ANIMATION_LAYER_HOME:
3874 return mHomeAppAnimationLayer;
3875 case ANIMATION_LAYER_STANDARD:
3876 default:
3877 return mAppAnimationLayer;
3878 }
chaviw23ee71c2017-12-18 11:29:41 -08003879 }
3880
Robert Carrf7a7ca82017-12-06 13:17:07 -08003881 SurfaceControl getSplitScreenDividerAnchor() {
3882 return mSplitScreenDividerAnchor;
3883 }
3884
chaviw23ee71c2017-12-18 11:29:41 -08003885 @Override
Robert Carrb1579c82017-09-05 14:54:47 -07003886 void onParentSet() {
3887 super.onParentSet();
3888 if (getParent() != null) {
chaviw23ee71c2017-12-18 11:29:41 -08003889 mAppAnimationLayer = makeChildSurface(null)
3890 .setName("animationLayer")
3891 .build();
Jorim Jaggibe418292018-03-26 16:14:12 +02003892 mBoostedAppAnimationLayer = makeChildSurface(null)
3893 .setName("boostedAnimationLayer")
3894 .build();
Jorim Jaggi391790622018-04-18 15:30:44 +02003895 mHomeAppAnimationLayer = makeChildSurface(null)
3896 .setName("homeAnimationLayer")
3897 .build();
Robert Carrf7a7ca82017-12-06 13:17:07 -08003898 mSplitScreenDividerAnchor = makeChildSurface(null)
3899 .setName("splitScreenDividerAnchor")
3900 .build();
3901 getPendingTransaction()
3902 .show(mAppAnimationLayer)
Jorim Jaggibe418292018-03-26 16:14:12 +02003903 .show(mBoostedAppAnimationLayer)
Jorim Jaggi391790622018-04-18 15:30:44 +02003904 .show(mHomeAppAnimationLayer)
Robert Carrf7a7ca82017-12-06 13:17:07 -08003905 .show(mSplitScreenDividerAnchor);
chaviw23ee71c2017-12-18 11:29:41 -08003906 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07003907 } else {
chaviw23ee71c2017-12-18 11:29:41 -08003908 mAppAnimationLayer.destroy();
3909 mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02003910 mBoostedAppAnimationLayer.destroy();
3911 mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02003912 mHomeAppAnimationLayer.destroy();
3913 mHomeAppAnimationLayer = null;
Robert Carrf7a7ca82017-12-06 13:17:07 -08003914 mSplitScreenDividerAnchor.destroy();
3915 mSplitScreenDividerAnchor = null;
Robert Carrb1579c82017-09-05 14:54:47 -07003916 }
3917 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003918 }
3919
Robert Carree4d4b92017-11-22 12:21:46 -08003920 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003921 AboveAppWindowContainers(String name, WindowManagerService service) {
3922 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08003923 }
3924
Robert Carrb9506032018-02-13 13:54:00 -08003925 @Override
Vishnu Nair83537a72018-07-19 21:27:48 -07003926 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
3927 final SurfaceControl.Builder builder = super.makeChildSurface(child);
3928 if (child instanceof WindowToken && ((WindowToken) child).mRoundedCornerOverlay) {
3929 // To draw above the ColorFade layer during the screen off transition, the
3930 // rounded corner overlays need to be at the root of the surface hierarchy.
3931 // TODO: move the ColorLayer into the display overlay layer such that this is not
3932 // necessary anymore.
3933 builder.setParent(null);
3934 }
3935 return builder;
3936 }
3937
3938 @Override
Robert Carrb9506032018-02-13 13:54:00 -08003939 void assignChildLayers(SurfaceControl.Transaction t) {
3940 assignChildLayers(t, null /* imeContainer */);
3941 }
3942
Robert Carree4d4b92017-11-22 12:21:46 -08003943 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
3944 boolean needAssignIme = imeContainer != null
3945 && imeContainer.getSurfaceControl() != null;
3946 for (int j = 0; j < mChildren.size(); ++j) {
3947 final WindowToken wt = mChildren.get(j);
Robert Carrf7a7ca82017-12-06 13:17:07 -08003948
3949 // See {@link mSplitScreenDividerAnchor}
3950 if (wt.windowType == TYPE_DOCK_DIVIDER) {
3951 wt.assignRelativeLayer(t, mTaskStackContainers.getSplitScreenDividerAnchor(), 1);
3952 continue;
3953 }
Vishnu Nair83537a72018-07-19 21:27:48 -07003954 if (wt.mRoundedCornerOverlay) {
3955 wt.assignLayer(t, WindowManagerPolicy.COLOR_FADE_LAYER + 1);
3956 continue;
3957 }
Robert Carree4d4b92017-11-22 12:21:46 -08003958 wt.assignLayer(t, j);
3959 wt.assignChildLayers(t);
3960
3961 int layer = mService.mPolicy.getWindowLayerFromTypeLw(
3962 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08003963
3964 if (needAssignIme && layer >= mService.mPolicy.getWindowLayerFromTypeLw(
3965 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003966 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08003967 needAssignIme = false;
3968 }
3969 }
3970 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003971 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08003972 }
3973 }
3974 }
3975
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003976 /**
3977 * Window container class that contains all containers on this display that are not related to
3978 * Apps. E.g. status bar.
3979 */
Robert Carree4d4b92017-11-22 12:21:46 -08003980 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07003981 /**
3982 * Compares two child window tokens returns -1 if the first is lesser than the second in
3983 * terms of z-order and 1 otherwise.
3984 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003985 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003986 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003987 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3988 token1.mOwnerCanManageAppTokens)
3989 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3990 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003991
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003992 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3993 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3994 return false;
3995 }
3996 final int req = w.mAttrs.screenOrientation;
3997 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3998 || req == SCREEN_ORIENTATION_UNSET) {
3999 return false;
4000 }
4001 return true;
4002 };
4003
Wale Ogunwale3a931692016-11-02 16:49:48 -07004004 private final String mName;
chaviwf29223e2018-01-31 13:23:51 -08004005 private final Dimmer mDimmer = new Dimmer(this);
4006 private final Rect mTmpDimBoundsRect = new Rect();
4007
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004008 NonAppWindowContainers(String name, WindowManagerService service) {
4009 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07004010 mName = name;
4011 }
4012
4013 void addChild(WindowToken token) {
4014 addChild(token, mWindowComparator);
4015 }
4016
4017 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004018 int getOrientation() {
4019 final WindowManagerPolicy policy = mService.mPolicy;
4020 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004021 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004022
4023 if (win != null) {
4024 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004025 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004026 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07004027 if (mService.mKeyguardGoingAway) {
4028 // Keyguard can't affect the orientation if it is going away...
4029 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
4030 return SCREEN_ORIENTATION_UNSET;
4031 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004032 }
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004033 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req
4034 + " for display id=" + mDisplayId);
Andrii Kulian8ee72852017-03-10 10:36:45 -08004035 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004036 }
4037
Andrii Kulian8ee72852017-03-10 10:36:45 -08004038 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004039
Jorim Jaggi1c530592018-04-06 15:11:47 +02004040 // Only allow force setting the orientation when all unknown visibilities have been
4041 // resolved, as otherwise we just may be starting another occluding activity.
4042 final boolean isUnoccluding =
4043 mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
4044 && mService.mUnknownAppVisibilityController.allResolved();
4045 if (policy.isKeyguardShowingAndNotOccluded() || isUnoccluding) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004046 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004047 }
4048
4049 return SCREEN_ORIENTATION_UNSET;
4050 }
4051
4052 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07004053 String getName() {
4054 return mName;
4055 }
chaviwf29223e2018-01-31 13:23:51 -08004056
4057 @Override
4058 Dimmer getDimmer() {
4059 return mDimmer;
4060 }
4061
4062 @Override
4063 void prepareSurfaces() {
4064 mDimmer.resetDimStates();
4065 super.prepareSurfaces();
4066 getBounds(mTmpDimBoundsRect);
4067
4068 if (mDimmer.updateDims(getPendingTransaction(), mTmpDimBoundsRect)) {
4069 scheduleAnimation();
4070 }
4071 }
Robert Carr3b716242016-08-16 16:02:21 -07004072 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01004073
Robert Carr9034d962018-01-04 18:27:42 -08004074 private class NonMagnifiableWindowContainers extends NonAppWindowContainers {
4075 NonMagnifiableWindowContainers(String name, WindowManagerService service) {
4076 super(name, service);
4077 }
4078
4079 @Override
4080 void applyMagnificationSpec(Transaction t, MagnificationSpec spec) {
4081 }
4082 };
4083
Robert Carrb1579c82017-09-05 14:54:47 -07004084 SurfaceControl.Builder makeSurface(SurfaceSession s) {
4085 return mService.makeSurfaceBuilder(s)
4086 .setParent(mWindowingLayer);
4087 }
4088
4089 @Override
4090 SurfaceSession getSession() {
4091 return mSession;
4092 }
4093
4094 @Override
4095 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07004096 SurfaceSession s = child != null ? child.getSession() : getSession();
4097 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrb1579c82017-09-05 14:54:47 -07004098 b.setSize(mSurfaceSize, mSurfaceSize);
Robert Carrf59b8dd2017-10-02 18:58:36 -07004099
4100 if (child == null) {
4101 return b;
4102 }
4103
Robert Carree4d4b92017-11-22 12:21:46 -08004104 return b.setName(child.getName())
4105 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07004106 }
4107
4108 /**
4109 * The makeSurface variants are for use by the window-container
4110 * hierarchy. makeOverlay here is a function for various non windowing
4111 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
4112 * and other potpourii.
4113 */
4114 SurfaceControl.Builder makeOverlay() {
4115 return mService.makeSurfaceBuilder(mSession)
4116 .setParent(mOverlayLayer);
4117 }
4118
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004119 /**
4120 * Reparents the given surface to mOverlayLayer.
4121 */
4122 void reparentToOverlay(Transaction transaction, SurfaceControl surface) {
4123 transaction.reparent(surface, mOverlayLayer.getHandle());
4124 }
4125
Robert Carrb1579c82017-09-05 14:54:47 -07004126 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carr24be9ab2018-04-30 17:54:53 -07004127 if (spec.scale != 1.0) {
4128 mMagnificationSpec = spec;
4129 } else {
4130 mMagnificationSpec = null;
4131 }
4132
Robert Carrf59b8dd2017-10-02 18:58:36 -07004133 applyMagnificationSpec(getPendingTransaction(), spec);
4134 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07004135 }
4136
Robert Carr24be9ab2018-04-30 17:54:53 -07004137 void reapplyMagnificationSpec() {
4138 if (mMagnificationSpec != null) {
4139 applyMagnificationSpec(getPendingTransaction(), mMagnificationSpec);
4140 }
4141 }
4142
Robert Carrb1579c82017-09-05 14:54:47 -07004143 @Override
4144 void onParentSet() {
4145 // Since we are the top of the SurfaceControl hierarchy here
4146 // we create the root surfaces explicitly rather than chaining
4147 // up as the default implementation in onParentSet does. So we
4148 // explicitly do NOT call super here.
4149 }
4150
4151 @Override
4152 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07004153
4154 // These are layers as children of "mWindowingLayer"
4155 mBelowAppWindowsContainers.assignLayer(t, 0);
4156 mTaskStackContainers.assignLayer(t, 1);
4157 mAboveAppWindowsContainers.assignLayer(t, 2);
4158
4159 WindowState imeTarget = mService.mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08004160 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08004161
Robert Carree4d4b92017-11-22 12:21:46 -08004162 // In the case where we have an IME target that is not in split-screen
4163 // mode IME assignment is easy. We just need the IME to go directly above
4164 // the target. This way children of the target will naturally go above the IME
4165 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08004166 //
Robert Carree4d4b92017-11-22 12:21:46 -08004167 // In the case of split-screen windowing mode, we need to elevate the IME above the
4168 // docked divider while keeping the app itself below the docked divider, so instead
4169 // we use relative layering of the IME targets child windows, and place the
4170 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08004171 //
Robert Carr234b6332018-03-20 13:38:16 -07004172 // In the case the IME target is animating, the animation Z order may be different
4173 // than the WindowContainer Z order, so it's difficult to be sure we have the correct
4174 // IME target. In this case we just layer the IME over all transitions by placing it in the
4175 // above applications layer.
4176 //
Robert Carree4d4b92017-11-22 12:21:46 -08004177 // In the case where we have no IME target we assign it where it's base layer would
4178 // place it in the AboveAppWindowContainers.
Robert Carr234b6332018-03-20 13:38:16 -07004179 if (imeTarget != null && !(imeTarget.inSplitScreenWindowingMode()
4180 || imeTarget.mToken.isAppAnimating())
Robert Carree4d4b92017-11-22 12:21:46 -08004181 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004182 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07004183 // TODO: We need to use an extra level on the app surface to ensure
4184 // this is always above SurfaceView but always below attached window.
4185 1);
Robert Carree4d4b92017-11-22 12:21:46 -08004186 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07004187 }
4188
4189 // Above we have assigned layers to our children, now we ask them to assign
4190 // layers to their children.
4191 mBelowAppWindowsContainers.assignChildLayers(t);
4192 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08004193 mAboveAppWindowsContainers.assignChildLayers(t,
4194 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07004195 mImeWindowsContainers.assignChildLayers(t);
4196 }
4197
4198 /**
4199 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
4200 * that the IME target is one of the docked applications. We'd like the docked divider to be
4201 * above both of the applications, and we'd like the IME to be above the docked divider.
4202 * However we need child windows of the applications to be above the IME (Text drag handles).
4203 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
4204 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
4205 * with {@link #WindowState#assignLayer}
4206 */
4207 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
Robert Carrbb5de662018-04-26 13:29:51 -07004208 child.assignRelativeLayer(t, mImeWindowsContainers.getSurfaceControl(), 1);
Robert Carrb1579c82017-09-05 14:54:47 -07004209 }
4210
4211 @Override
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004212 void prepareSurfaces() {
4213 final ScreenRotationAnimation screenRotationAnimation =
4214 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
4215 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
4216 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
4217 mPendingTransaction.setMatrix(mWindowingLayer,
4218 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
4219 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
4220 mPendingTransaction.setPosition(mWindowingLayer,
4221 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
4222 mPendingTransaction.setAlpha(mWindowingLayer,
4223 screenRotationAnimation.getEnterTransformation().getAlpha());
4224 }
Chavi Weingartenb736e322018-02-23 00:27:54 +00004225
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004226 super.prepareSurfaces();
4227 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004228
Jorim Jaggibe418292018-03-26 16:14:12 +02004229 void assignStackOrdering() {
4230 mTaskStackContainers.assignStackOrdering(getPendingTransaction());
Robert Carr2f8aa392018-01-31 14:46:51 -08004231 }
Chavi Weingarten3a748552018-05-14 17:32:42 +00004232
4233 /**
4234 * Increment the deferral count to determine whether to update the IME target.
4235 */
4236 void deferUpdateImeTarget() {
4237 mDeferUpdateImeTargetCount++;
4238 }
4239
4240 /**
4241 * Decrement the deferral count to determine whether to update the IME target. If the count
4242 * reaches 0, a new ime target will get computed.
4243 */
4244 void continueUpdateImeTarget() {
4245 if (mDeferUpdateImeTargetCount == 0) {
4246 return;
4247 }
4248
4249 mDeferUpdateImeTargetCount--;
4250 if (mDeferUpdateImeTargetCount == 0) {
4251 computeImeTarget(true /* updateImeTarget */);
4252 }
4253 }
4254
4255 /**
4256 * @return Whether a new IME target should be computed.
4257 */
4258 private boolean canUpdateImeTarget() {
4259 return mDeferUpdateImeTargetCount == 0;
4260 }
Arthur Hung95b38a92018-07-20 18:56:12 +08004261
4262 InputMonitor getInputMonitor() {
4263 return mInputMonitor;
4264 }
Craig Mautner59c00972012-07-30 12:10:24 -07004265}