blob: 6dd87f10a5ff28b46d581fbd51e72ce71c452003 [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;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700153import com.android.internal.view.IInputMethodClient;
Adrian Roose99bc052017-11-20 17:55:31 +0100154import com.android.server.policy.WindowManagerPolicy;
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100155import com.android.server.wm.utils.RotationCache;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100156import com.android.server.wm.utils.WmDisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700157
158import java.io.PrintWriter;
159import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700160import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700161import java.util.HashMap;
162import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700163import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700164import java.util.List;
Adrian Roos1cf585052018-01-03 18:43:27 +0100165import java.util.Objects;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800166import java.util.function.Consumer;
167import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700168
Craig Mautner59c00972012-07-30 12:10:24 -0700169/**
170 * Utility class for keeping track of the WindowStates and other pertinent contents of a
171 * particular Display.
172 *
173 * IMPORTANT: No method from this class should ever be used without holding
174 * WindowManagerService.mWindowMap.
175 */
Riddle Hsuad256a12018-07-18 16:11:30 +0800176class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer>
177 implements WindowManagerPolicy.DisplayContentInfo {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700178 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700179
180 /** Unique identifier of this stack. */
181 private final int mDisplayId;
182
Wale Ogunwale3a931692016-11-02 16:49:48 -0700183 /** The containers below are the only child containers the display can have. */
184 // Contains all window containers that are related to apps (Activities)
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100185 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers(mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700186 // Contains all non-app window containers that should be displayed above the app containers
187 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800188 private final AboveAppWindowContainers mAboveAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100189 new AboveAppWindowContainers("mAboveAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700190 // Contains all non-app window containers that should be displayed below the app containers
191 // (e.g. Wallpaper).
192 private final NonAppWindowContainers mBelowAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100193 new NonAppWindowContainers("mBelowAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700194 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
195 // 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 -0800196 // window containers together and move them in-sync if/when needed. We use a subclass of
197 // WindowContainer which is omitted from screen magnification, as the IME is never magnified.
198 private final NonMagnifiableWindowContainers mImeWindowsContainers =
199 new NonMagnifiableWindowContainers("mImeWindowsContainers", mService);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700200
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000201 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800202 private WindowState mTmpWindow2;
203 private WindowAnimator mTmpWindowAnimator;
204 private boolean mTmpRecoveringMemory;
205 private boolean mUpdateImeTarget;
206 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700207 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700208
Wale Ogunwale02319a62016-09-26 15:21:22 -0700209 // Mapping from a token IBinder to a WindowToken object on this display.
210 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
211
Andrii Kuliancd097992017-03-23 18:31:59 -0700212 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700213 int mInitialDisplayWidth = 0;
214 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700215 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700216
Adrian Roos1cf585052018-01-03 18:43:27 +0100217 DisplayCutout mInitialDisplayCutout;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100218 private final RotationCache<DisplayCutout, WmDisplayCutout> mDisplayCutoutCache
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100219 = new RotationCache<>(this::calculateDisplayCutoutForRotationUncached);
Adrian Roos1cf585052018-01-03 18:43:27 +0100220
Andrii Kuliancd097992017-03-23 18:31:59 -0700221 /**
222 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
223 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
224 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
225 */
Craig Mautner59c00972012-07-30 12:10:24 -0700226 int mBaseDisplayWidth = 0;
227 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700228 /**
229 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
230 * but can be set from Settings or via shell command "adb shell wm density".
231 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
232 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700233 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700234 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700235 private final DisplayInfo mDisplayInfo = new DisplayInfo();
236 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700237 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800238 private final DisplayPolicy mDisplayPolicy;
239 private DisplayRotation mDisplayRotation;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000240 DisplayFrames mDisplayFrames;
241
Andrii Kulian06d07d62017-03-14 11:11:47 -0700242 /**
243 * For default display it contains real metrics, empty for others.
244 * @see WindowManagerService#createWatermarkInTransaction()
245 */
246 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
247 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
248 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700249
Andrii Kulian06d07d62017-03-14 11:11:47 -0700250 /**
251 * Compat metrics computed based on {@link #mDisplayMetrics}.
252 * @see #updateDisplayAndOrientation(int)
253 */
254 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
255
256 /** The desired scaling factor for compatible apps. */
257 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700258
Andrii Kulian8ee72852017-03-10 10:36:45 -0800259 /**
260 * Current rotation of the display.
261 * Constants as per {@link android.view.Surface.Rotation}.
262 *
Robert Carrae606b42018-02-15 15:36:23 -0800263 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800264 */
265 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700266
Andrii Kulian8ee72852017-03-10 10:36:45 -0800267 /**
268 * Last applied orientation of the display.
269 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
270 *
271 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
272 */
273 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700274
Andrii Kulian8ee72852017-03-10 10:36:45 -0800275 /**
276 * Flag indicating that the application is receiving an orientation that has different metrics
277 * than it expected. E.g. Portrait instead of Landscape.
278 *
Robert Carrae606b42018-02-15 15:36:23 -0800279 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800280 */
281 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700282
Andrii Kulian8ee72852017-03-10 10:36:45 -0800283 /**
284 * Orientation forced by some window. If there is no visible window that specifies orientation
285 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
286 *
287 * @see NonAppWindowContainers#getOrientation()
288 */
289 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700290
Andrii Kulian8ee72852017-03-10 10:36:45 -0800291 /**
292 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
293 * occluded.
294 *
295 * @see NonAppWindowContainers#getOrientation()
296 */
297 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
298
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700299 /**
300 * Keep track of wallpaper visibility to notify changes.
301 */
302 private boolean mLastWallpaperVisible = false;
303
Andrii Kulian06d07d62017-03-14 11:11:47 -0700304 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700305
Craig Mautner39834192012-09-02 07:47:24 -0700306 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700307 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700308 int pendingLayoutChanges;
Riddle Hsu654a6f92018-07-13 22:59:36 +0800309 int mDeferredRotationPauseCount;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800310
Andrii Kulian839def92016-11-02 10:58:58 -0700311 // TODO(multi-display): remove some of the usages.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800312 @VisibleForTesting
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800313 boolean isDefaultDisplay;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800314
Andrii Kulianf0379de2018-03-14 16:24:07 -0700315 /**
316 * Flag indicating whether WindowManager should override info for this display in
317 * DisplayManager.
318 */
319 boolean mShouldOverrideDisplayConfiguration = true;
Craig Mautner39834192012-09-02 07:47:24 -0700320
Craig Mautnerdc548482014-02-05 13:35:24 -0800321 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700322 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800323
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700324 /** Detect user tapping outside of current focused task bounds .*/
325 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700326
Craig Mautner6601b7b2013-04-29 10:29:11 -0700327 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700328 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700329
Craig Mautner6601b7b2013-04-29 10:29:11 -0700330 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800331 private final Rect mTmpRect = new Rect();
332 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700333 private final RectF mTmpRectF = new RectF();
334 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800335 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700336
Bryce Leef3c6a472017-11-14 14:53:06 -0800337 /** Used for handing back size of display */
338 private final Rect mTmpBounds = new Rect();
339
Craig Mautner95da1082014-02-24 17:54:35 -0800340 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700341 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800342
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700343 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700344 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700345
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800346 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800347 /** A collection of windows that provide tap exclude regions inside of them. */
348 final ArraySet<WindowState> mTapExcludeProvidingWindows = new ArraySet<>();
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800349
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000350 private boolean mHaveBootMsg = false;
351 private boolean mHaveApp = false;
352 private boolean mHaveWallpaper = false;
353 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700354
355 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
356
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700357 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
358 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000359 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
360 new ApplySurfaceChangesTransactionState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700361
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700362 // True if this display is in the process of being removed. Used to determine if the removal of
363 // the display's direct children should be allowed.
364 private boolean mRemovingDisplay = false;
365
Bryce Leed1871262017-06-12 14:12:29 -0700366 // {@code false} if this display is in the processing of being created.
367 private boolean mDisplayReady = false;
368
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800369 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700370
Robert Carrb1579c82017-09-05 14:54:47 -0700371 private final SurfaceSession mSession = new SurfaceSession();
372
373 /**
374 * We organize all top-level Surfaces in to the following layers.
375 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800376 * and omitted from Screen-Magnification, for example the strict mode flash or
377 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700378 * {@link #mWindowingLayer} contains everything else.
379 */
380 private SurfaceControl mOverlayLayer;
381
382 /**
383 * See {@link #mOverlayLayer}
384 */
385 private SurfaceControl mWindowingLayer;
386
387 /**
388 * Specifies the size of the surfaces in {@link #mOverlayLayer} and {@link #mWindowingLayer}.
389 * <p>
390 * For these surfaces currently we use a surface based on the larger of width or height so we
391 * don't have to resize when rotating the display.
392 */
393 private int mSurfaceSize;
394
Adrian Roos5251b1d2018-03-23 18:57:43 +0100395 /**
396 * Sequence number for the current layout pass.
397 */
398 int mLayoutSeq = 0;
399
Chavi Weingarten3a748552018-05-14 17:32:42 +0000400 /**
401 * Specifies the count to determine whether to defer updating the IME target until ready.
402 */
403 private int mDeferUpdateImeTargetCount;
404
Jorim Jaggi9af095b2017-12-12 17:18:57 +0100405 /** Temporary float array to retrieve 3x3 matrix values. */
406 private final float[] mTmpFloats = new float[9];
407
Robert Carr24be9ab2018-04-30 17:54:53 -0700408 private MagnificationSpec mMagnificationSpec;
409
Arthur Hung95b38a92018-07-20 18:56:12 +0800410 private InputMonitor mInputMonitor;
411
lumark90120a82018-08-15 00:33:03 +0800412 /**
413 * The input method window for this display.
414 */
415 WindowState mInputMethodWindow;
416
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800417 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
418 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800419 final AppWindowToken atoken = w.mAppToken;
420 if (winAnimator.mDrawState == READY_TO_SHOW) {
Jorim Jaggiaf0d6d22018-06-08 15:25:35 +0200421 if (atoken == null || atoken.canShowWindows()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800422 if (w.performShowLocked()) {
423 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
424 if (DEBUG_LAYOUT_REPEATS) {
425 mService.mWindowPlacerLocked.debugLayoutRepeats(
426 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
427 }
428 }
429 }
430 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800431 };
432
433 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
434 final WindowStateAnimator winAnimator = w.mWinAnimator;
435 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
436 return;
437 }
438
439 final int flags = w.mAttrs.flags;
440
441 // If this window is animating, make a note that we have an animating window and take
442 // care of a request to run a detached wallpaper animation.
Jorim Jaggia5e10572017-11-15 14:36:26 +0100443 if (winAnimator.isAnimationSet()) {
444 final AnimationAdapter anim = w.getAnimation();
445 if (anim != null) {
446 if ((flags & FLAG_SHOW_WALLPAPER) != 0 && anim.getDetachWallpaper()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800447 mTmpWindow = w;
448 }
Jorim Jaggia5e10572017-11-15 14:36:26 +0100449 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800450 if (color != 0) {
451 final TaskStack stack = w.getStack();
452 if (stack != null) {
453 stack.setAnimationBackground(winAnimator, color);
454 }
455 }
456 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800457 }
458
459 // If this window's app token is running a detached wallpaper animation, make a note so
460 // we can ensure the wallpaper is displayed behind it.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200461 final AppWindowToken atoken = winAnimator.mWin.mAppToken;
462 final AnimationAdapter animation = atoken != null ? atoken.getAnimation() : null;
463 if (animation != null) {
464 if ((flags & FLAG_SHOW_WALLPAPER) != 0 && animation.getDetachWallpaper()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800465 mTmpWindow = w;
466 }
467
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200468 final int color = animation.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800469 if (color != 0) {
470 final TaskStack stack = w.getStack();
471 if (stack != null) {
472 stack.setAnimationBackground(winAnimator, color);
473 }
474 }
475 }
476 };
477
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800478 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
479 final int lostFocusUid = mTmpWindow.mOwnerUid;
480 final Handler handler = mService.mH;
481 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
482 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
483 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
484 w.mAttrs.hideTimeoutMilliseconds);
485 }
486 }
487 };
488
489 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
490 final AppWindowToken focusedApp = mService.mFocusedApp;
491 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
492 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
493
494 if (!w.canReceiveKeys()) {
495 return false;
496 }
497
498 final AppWindowToken wtoken = w.mAppToken;
499
500 // If this window's application has been removed, just skip it.
501 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
502 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
503 + (wtoken.removed ? "removed" : "sendingToBottom"));
504 return false;
505 }
506
507 if (focusedApp == null) {
508 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
509 + " using new focus @ " + w);
510 mTmpWindow = w;
511 return true;
512 }
513
514 if (!focusedApp.windowsAreFocusable()) {
515 // Current focused app windows aren't focusable...
516 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
517 + " focusable using new focus @ " + w);
518 mTmpWindow = w;
519 return true;
520 }
521
522 // Descend through all of the app tokens and find the first that either matches
523 // win.mAppToken (return win) or mFocusedApp (return null).
524 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
525 if (focusedApp.compareTo(wtoken) > 0) {
526 // App stack below focused app stack. No focus for you!!!
527 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
528 "findFocusedWindow: Reached focused app=" + focusedApp);
529 mTmpWindow = null;
530 return true;
531 }
532 }
533
534 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
535 mTmpWindow = w;
536 return true;
537 };
538
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800539 private final Consumer<WindowState> mPerformLayout = w -> {
540 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
541 // wasting time and funky changes while a window is animating away.
542 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
543 || w.isGoneForLayoutLw();
544
545 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
546 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
547 + " mLayoutAttached=" + w.mLayoutAttached
548 + " screen changed=" + w.isConfigChanged());
549 final AppWindowToken atoken = w.mAppToken;
550 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200551 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800552 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
553 + " parentHidden=" + w.isParentWindowHidden());
554 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200555 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800556 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
557 + " parentHidden=" + w.isParentWindowHidden());
558 }
559
560 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
561 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
562 // since that means "perform layout as normal, just don't display").
563 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
564 || ((w.isConfigChanged() || w.setReportResizeHints())
565 && !w.isGoneForLayoutLw() &&
566 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
567 (w.mHasSurface && w.mAppToken != null &&
568 w.mAppToken.layoutConfigChanges)))) {
569 if (!w.mLayoutAttached) {
570 if (mTmpInitial) {
571 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700572 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800573 }
574 if (w.mAttrs.type == TYPE_DREAM) {
575 // Don't layout windows behind a dream, so that if it does stuff like hide
576 // the status bar we won't get a bad transition when it goes away.
577 mTmpWindow = w;
578 }
579 w.mLayoutNeeded = false;
580 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200581 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000582 mService.mPolicy.layoutWindowLw(w, null, mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100583 w.mLayoutSeq = mLayoutSeq;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800584
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200585 // If this is the first layout, we need to initialize the last inset values as
586 // otherwise we'd immediately cause an unnecessary resize.
587 if (firstLayout) {
588 w.updateLastInsetValues();
589 }
590
Adrian Roos23df3a32018-03-15 15:41:13 +0100591 if (w.mAppToken != null) {
592 w.mAppToken.layoutLetterbox(w);
593 }
594
chaviw492139a2018-07-16 16:07:35 -0700595 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700596 + " mContainingFrame=" + w.getContainingFrame()
597 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800598 }
599 }
600 };
601
602 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
603 if (w.mLayoutAttached) {
604 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
605 + " mViewVisibility=" + w.mViewVisibility
606 + " mRelayoutCalled=" + w.mRelayoutCalled);
607 // If this view is GONE, then skip it -- keep the current frame, and let the caller
608 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
609 // windows, since that means "perform layout as normal, just don't display").
610 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
611 return;
612 }
613 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
614 || w.mLayoutNeeded) {
615 if (mTmpInitial) {
616 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700617 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800618 }
619 w.mLayoutNeeded = false;
620 w.prelayout();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000621 mService.mPolicy.layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100622 w.mLayoutSeq = mLayoutSeq;
chaviw492139a2018-07-16 16:07:35 -0700623 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700624 + " mContainingFrame=" + w.getContainingFrame()
625 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800626 }
627 } else if (w.mAttrs.type == TYPE_DREAM) {
628 // Don't layout windows behind a dream, so that if it does stuff like hide the
629 // status bar we won't get a bad transition when it goes away.
630 mTmpWindow = mTmpWindow2;
631 }
632 };
633
634 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
635 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
636 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
637 return w.canBeImeTarget();
638 };
639
640 private final Consumer<WindowState> mApplyPostLayoutPolicy =
641 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
642 mService.mInputMethodTarget);
643
644 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
645 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
646 final boolean obscuredChanged = w.mObscured !=
647 mTmpApplySurfaceChangesTransactionState.obscured;
648 final RootWindowContainer root = mService.mRoot;
649 // Only used if default window
650 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
651
652 // Update effect.
653 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
654 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
655 final boolean isDisplayed = w.isDisplayedLw();
656
657 if (isDisplayed && w.isObscuringDisplay()) {
658 // This window completely covers everything behind it, so we want to leave all
659 // of them as undimmed (for performance reasons).
660 root.mObscuringWindow = w;
661 mTmpApplySurfaceChangesTransactionState.obscured = true;
662 }
663
664 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
665 root.handleNotObscuredLocked(w,
666 mTmpApplySurfaceChangesTransactionState.obscured,
667 mTmpApplySurfaceChangesTransactionState.syswin);
668
669 if (w.mHasSurface && isDisplayed) {
670 final int type = w.mAttrs.type;
671 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
672 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
673 mTmpApplySurfaceChangesTransactionState.syswin = true;
674 }
675 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
676 && w.mAttrs.preferredRefreshRate != 0) {
677 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
678 = w.mAttrs.preferredRefreshRate;
679 }
680 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
681 && w.mAttrs.preferredDisplayModeId != 0) {
682 mTmpApplySurfaceChangesTransactionState.preferredModeId
683 = w.mAttrs.preferredDisplayModeId;
684 }
685 }
686 }
687
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800688 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
689 && mWallpaperController.isWallpaperTarget(w)) {
690 // This is the wallpaper target and its obscured state changed... make sure the
691 // current wallpaper's visibility has been updated accordingly.
692 mWallpaperController.updateWallpaperVisibility();
693 }
694
chaviw161ea3e2018-01-31 12:01:12 -0800695 w.handleWindowMovedIfNeeded();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800696
697 final WindowStateAnimator winAnimator = w.mWinAnimator;
698
699 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
chaviw1454b392018-08-06 09:54:04 -0700700 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800701
702 // Moved from updateWindowsAndWallpaperLocked().
703 if (w.mHasSurface) {
704 // Take care of the window being ready to display.
705 final boolean committed = winAnimator.commitFinishDrawingLocked();
706 if (isDefaultDisplay && committed) {
707 if (w.mAttrs.type == TYPE_DREAM) {
708 // HACK: When a dream is shown, it may at that point hide the lock screen.
709 // So we need to redo the layout to let the phone window manager make this
710 // happen.
711 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
712 if (DEBUG_LAYOUT_REPEATS) {
713 surfacePlacer.debugLayoutRepeats(
714 "dream and commitFinishDrawingLocked true",
715 pendingLayoutChanges);
716 }
717 }
718 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
719 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
720 "First draw done in potential wallpaper target " + w);
721 root.mWallpaperMayChange = true;
722 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
723 if (DEBUG_LAYOUT_REPEATS) {
724 surfacePlacer.debugLayoutRepeats(
725 "wallpaper and commitFinishDrawingLocked true",
726 pendingLayoutChanges);
727 }
728 }
729 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800730 }
731
732 final AppWindowToken atoken = w.mAppToken;
733 if (atoken != null) {
Adrian Roos23df3a32018-03-15 15:41:13 +0100734 atoken.updateLetterboxSurface(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800735 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
736 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
737 mTmpUpdateAllDrawn.add(atoken);
738 }
739 }
740
741 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
742 && w.isDisplayedLw()) {
743 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
744 }
745
746 w.updateResizingWindowIfNeeded();
747 };
748
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800749 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800750 * Create new {@link DisplayContent} instance, add itself to the root window container and
751 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700752 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800753 * @param service You know.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700754 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
755 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700756 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700757 DisplayContent(Display display, WindowManagerService service,
Bryce Leef19cbe22018-02-02 15:09:21 -0800758 WallpaperController wallpaperController, DisplayWindowController controller) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100759 super(service);
Bryce Leef19cbe22018-02-02 15:09:21 -0800760 setController(controller);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800761 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
762 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
763 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
764 + " new=" + display);
765 }
766
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700767 mDisplay = display;
768 mDisplayId = display.getDisplayId();
Wale Ogunwale0303c572016-10-20 10:16:29 -0700769 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700770 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700771 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700772 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100773 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo,
774 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700775 initializeDisplayBaseInfo();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800776 mDisplayPolicy = new DisplayPolicy(service);
777 mDisplayRotation = new DisplayRotation(service, this);
778 if (isDefaultDisplay) {
779 // The policy may be invoked right after here, so it requires the necessary default
780 // fields of this display content.
781 mService.mPolicy.setDefaultDisplay(this);
782 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800783 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700784 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700785
Robert Carr74a66a22018-02-23 12:17:51 -0800786 // We use this as our arbitrary surface size for buffer-less parents
787 // that don't impose cropping on their children. It may need to be larger
788 // than the display size because fullscreen windows can be shifted offscreen
789 // due to surfaceInsets. 2 times the largest display dimension feels like an
790 // appropriately arbitrary number. Eventually we would like to give SurfaceFlinger
791 // layers the ability to match their parent sizes and be able to skip
792 // such arbitrary size settings.
793 mSurfaceSize = Math.max(mBaseDisplayHeight, mBaseDisplayWidth) * 2;
Robert Carrb1579c82017-09-05 14:54:47 -0700794
795 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession)
796 .setSize(mSurfaceSize, mSurfaceSize)
797 .setOpaque(true);
798 mWindowingLayer = b.setName("Display Root").build();
799 mOverlayLayer = b.setName("Display Overlays").build();
800
Robert Carrf59b8dd2017-10-02 18:58:36 -0700801 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700802 .setLayerStack(mWindowingLayer, mDisplayId)
803 .show(mWindowingLayer)
804 .setLayer(mOverlayLayer, 1)
805 .setLayerStack(mOverlayLayer, mDisplayId)
806 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700807 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700808
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700809 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700810 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700811 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700812 super.addChild(mAboveAppWindowsContainers, null);
813 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800814
815 // Add itself as a child to the root container.
816 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700817
818 // TODO(b/62541591): evaluate whether this is the best spot to declare the
819 // {@link DisplayContent} ready for use.
820 mDisplayReady = true;
Arthur Hung95b38a92018-07-20 18:56:12 +0800821
Arthur Hung39134b22018-08-14 11:58:28 +0800822 mInputMonitor = new InputMonitor(service, mDisplayId);
Bryce Leed1871262017-06-12 14:12:29 -0700823 }
824
825 boolean isReady() {
826 // The display is ready when the system and the individual display are both ready.
827 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700828 }
829
830 int getDisplayId() {
831 return mDisplayId;
832 }
833
Wale Ogunwale02319a62016-09-26 15:21:22 -0700834 WindowToken getWindowToken(IBinder binder) {
835 return mTokenMap.get(binder);
836 }
837
838 AppWindowToken getAppWindowToken(IBinder binder) {
839 final WindowToken token = getWindowToken(binder);
840 if (token == null) {
841 return null;
842 }
843 return token.asAppWindowToken();
844 }
845
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700846 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700847 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
848 if (dc != null) {
849 // We currently don't support adding a window token to the display if the display
850 // already has the binder mapped to another token. If there is a use case for supporting
851 // this moving forward we will either need to merge the WindowTokens some how or have
852 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700853 throw new IllegalArgumentException("Can't map token=" + token + " to display="
854 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700855 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700856 if (binder == null) {
857 throw new IllegalArgumentException("Can't map token=" + token + " to display="
858 + getName() + " binder is null");
859 }
860 if (token == null) {
861 throw new IllegalArgumentException("Can't map null token to display="
862 + getName() + " binder=" + binder);
863 }
864
Wale Ogunwale02319a62016-09-26 15:21:22 -0700865 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700866
867 if (token.asAppWindowToken() == null) {
868 // Add non-app token to container hierarchy on the display. App tokens are added through
869 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700870 switch (token.windowType) {
871 case TYPE_WALLPAPER:
872 mBelowAppWindowsContainers.addChild(token);
873 break;
874 case TYPE_INPUT_METHOD:
875 case TYPE_INPUT_METHOD_DIALOG:
876 mImeWindowsContainers.addChild(token);
877 break;
878 default:
879 mAboveAppWindowsContainers.addChild(token);
880 break;
881 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700882 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700883 }
884
885 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700886 final WindowToken token = mTokenMap.remove(binder);
887 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800888 token.setExiting();
889 }
890 return token;
891 }
892
893 /** Changes the display the input window token is housed on to this one. */
894 void reParentWindowToken(WindowToken token) {
895 final DisplayContent prevDc = token.getDisplayContent();
896 if (prevDc == this) {
897 return;
898 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800899 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
900 && token.asAppWindowToken() == null) {
901 // Removed the token from the map, but made sure it's not an app token before removing
902 // from parent.
903 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700904 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800905
906 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700907 }
908
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700909 void removeAppToken(IBinder binder) {
910 final WindowToken token = removeWindowToken(binder);
911 if (token == null) {
912 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
913 return;
914 }
915
916 final AppWindowToken appToken = token.asAppWindowToken();
917
918 if (appToken == null) {
919 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
920 return;
921 }
922
923 appToken.onRemovedFromDisplay();
924 }
925
Riddle Hsuad256a12018-07-18 16:11:30 +0800926 @Override
Riddle Hsu86cb7de2018-08-13 23:29:58 +0800927 DisplayWindowController getController() {
928 return (DisplayWindowController) super.getController();
929 }
930
931 @Override
Riddle Hsuad256a12018-07-18 16:11:30 +0800932 public Display getDisplay() {
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700933 return mDisplay;
934 }
935
Craig Mautner59c00972012-07-30 12:10:24 -0700936 DisplayInfo getDisplayInfo() {
937 return mDisplayInfo;
938 }
939
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700940 DisplayMetrics getDisplayMetrics() {
941 return mDisplayMetrics;
942 }
943
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800944 DisplayPolicy getDisplayPolicy() {
945 return mDisplayPolicy;
946 }
947
Riddle Hsuad256a12018-07-18 16:11:30 +0800948 @Override
949 public DisplayRotation getDisplayRotation() {
950 return mDisplayRotation;
951 }
952
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800953 @VisibleForTesting
954 void setDisplayRotation(DisplayRotation displayRotation) {
955 mDisplayRotation = displayRotation;
956 }
957
Andrii Kulian8ee72852017-03-10 10:36:45 -0800958 int getRotation() {
959 return mRotation;
960 }
961
Andrii Kulianef5ce1c2018-03-20 19:27:24 -0700962 @VisibleForTesting
Andrii Kulian8ee72852017-03-10 10:36:45 -0800963 void setRotation(int newRotation) {
964 mRotation = newRotation;
Riddle Hsuad256a12018-07-18 16:11:30 +0800965 mDisplayRotation.setRotation(newRotation);
Andrii Kulian8ee72852017-03-10 10:36:45 -0800966 }
967
968 int getLastOrientation() {
969 return mLastOrientation;
970 }
971
972 void setLastOrientation(int orientation) {
973 mLastOrientation = orientation;
974 }
975
976 boolean getAltOrientation() {
977 return mAltOrientation;
978 }
979
980 void setAltOrientation(boolean altOrientation) {
981 mAltOrientation = altOrientation;
982 }
983
984 int getLastWindowForcedOrientation() {
985 return mLastWindowForcedOrientation;
986 }
987
Andrii Kulian06d07d62017-03-14 11:11:47 -0700988 /**
Riddle Hsu654a6f92018-07-13 22:59:36 +0800989 * Temporarily pauses rotation changes until resumed.
990 *
991 * This can be used to prevent rotation changes from occurring while the user is
992 * performing certain operations, such as drag and drop.
993 *
994 * This call nests and must be matched by an equal number of calls to
995 * {@link #resumeRotationLocked}.
996 */
997 void pauseRotationLocked() {
998 mDeferredRotationPauseCount++;
999 }
1000
1001 /**
1002 * Resumes normal rotation changes after being paused.
1003 */
1004 void resumeRotationLocked() {
1005 if (mDeferredRotationPauseCount <= 0) {
1006 return;
1007 }
1008
1009 mDeferredRotationPauseCount--;
1010 if (mDeferredRotationPauseCount == 0) {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001011 updateRotationAndSendNewConfigIfNeeded();
Riddle Hsu654a6f92018-07-13 22:59:36 +08001012 }
1013 }
1014
1015 /**
Riddle Hsuad256a12018-07-18 16:11:30 +08001016 * If this is true we have updated our desired orientation, but not yet changed the real
1017 * orientation our applied our screen rotation animation. For example, because a previous
1018 * screen rotation was in progress.
1019 *
1020 * @return {@code true} if the there is an ongoing rotation change.
1021 */
1022 boolean rotationNeedsUpdate() {
1023 final int lastOrientation = getLastOrientation();
1024 final int oldRotation = getRotation();
1025 final boolean oldAltOrientation = getAltOrientation();
1026
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001027 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Riddle Hsuad256a12018-07-18 16:11:30 +08001028 final boolean altOrientation = !mDisplayRotation.rotationHasCompatibleMetrics(
1029 lastOrientation, rotation);
1030 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1031 return false;
1032 }
1033 return true;
1034 }
1035
1036 /**
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001037 * Update rotation of the display and send configuration if the rotation is changed.
1038 *
1039 * @return {@code true} if the rotation has been changed and the new config is sent.
1040 */
1041 boolean updateRotationAndSendNewConfigIfNeeded() {
1042 final boolean changed = updateRotationUnchecked(false /* forceUpdate */);
1043 if (changed) {
1044 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
1045 }
1046 return changed;
1047 }
1048
1049 /**
Andrii Kulian06d07d62017-03-14 11:11:47 -07001050 * Update rotation of the display.
1051 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001052 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
1053 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001054 */
Robert Carrae606b42018-02-15 15:36:23 -08001055 boolean updateRotationUnchecked() {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001056 return updateRotationUnchecked(false /* forceUpdate */);
1057 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001058
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001059 /**
1060 * Update rotation of the display with an option to force the update.
1061 * @param forceUpdate Force the rotation update. Sometimes in WM we might skip updating
1062 * orientation because we're waiting for some rotation to finish or display
1063 * to unfreeze, which results in configuration of the previously visible
1064 * activity being applied to a newly visible one. Forcing the rotation
1065 * update allows to workaround this issue.
1066 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
1067 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
1068 */
1069 boolean updateRotationUnchecked(boolean forceUpdate) {
1070 ScreenRotationAnimation screenRotationAnimation;
1071 if (!forceUpdate) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001072 if (mDeferredRotationPauseCount > 0) {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001073 // Rotation updates have been paused temporarily. Defer the update until
1074 // updates have been resumed.
1075 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
1076 return false;
1077 }
1078
1079 screenRotationAnimation =
1080 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
1081 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
1082 // Rotation updates cannot be performed while the previous rotation change
1083 // animation is still in progress. Skip this update. We will try updating
1084 // again after the animation is finished and the display is unfrozen.
1085 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
1086 return false;
1087 }
1088 if (mService.mDisplayFrozen) {
1089 // Even if the screen rotation animation has finished (e.g. isAnimating
1090 // returns false), there is still some time where we haven't yet unfrozen
1091 // the display. We also need to abort rotation here.
1092 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
1093 "Deferring rotation, still finishing previous rotation");
1094 return false;
1095 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001096 }
1097
1098 if (!mService.mDisplayEnabled) {
1099 // No point choosing a rotation if the display is not enabled.
1100 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
1101 return false;
1102 }
1103
1104 final int oldRotation = mRotation;
1105 final int lastOrientation = mLastOrientation;
1106 final boolean oldAltOrientation = mAltOrientation;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001107 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001108 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Computed rotation=" + rotation + " for display id="
1109 + mDisplayId + " based on lastOrientation=" + lastOrientation
1110 + " and oldRotation=" + oldRotation);
Riddle Hsuad256a12018-07-18 16:11:30 +08001111 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(mDisplayRotation,
1112 oldRotation, rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001113
Robert Carr0e007272017-10-02 13:00:55 -07001114 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001115 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001116 if (seamlessRotated != null && !forceUpdate) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001117 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
1118 // to complete (that is, waiting for windows to redraw). It's tempting to check
1119 // w.mSeamlessRotationCount but that could be incorrect in the case of
1120 // window-removal.
1121 return false;
1122 }
Robert Carr0e007272017-10-02 13:00:55 -07001123
1124 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001125 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -07001126 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -07001127 mayRotateSeamlessly = false;
1128 }
1129 for (int i = 0; i < mService.mSessions.size(); i++) {
1130 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
1131 mayRotateSeamlessly = false;
1132 break;
1133 }
1134 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001135 }
Robert Carr0e007272017-10-02 13:00:55 -07001136 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001137
1138 // TODO: Implement forced rotation changes.
1139 // Set mAltOrientation to indicate that the application is receiving
1140 // an orientation that has different metrics than it expected.
1141 // eg. Portrait instead of Landscape.
1142
Riddle Hsuad256a12018-07-18 16:11:30 +08001143 final boolean altOrientation = !mDisplayRotation.rotationHasCompatibleMetrics(
Andrii Kulian06d07d62017-03-14 11:11:47 -07001144 lastOrientation, rotation);
1145
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001146 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1147 + " selected orientation " + lastOrientation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001148 + ", got rotation " + rotation + " which has "
1149 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1150
1151 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1152 // No change.
1153 return false;
1154 }
1155
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001156 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1157 + " rotation changed to " + rotation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001158 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1159 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1160
1161 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1162 mService.mWaitingForConfig = true;
1163 }
1164
Riddle Hsuad256a12018-07-18 16:11:30 +08001165 setRotation(rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001166 mAltOrientation = altOrientation;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001167
1168 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
Riddle Hsu654a6f92018-07-13 22:59:36 +08001169 mService.mH.sendNewMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1170 this, WINDOW_FREEZE_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001171
1172 setLayoutNeeded();
1173 final int[] anim = new int[2];
Robert Carrf59b8dd2017-10-02 18:58:36 -07001174 mService.mPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001175
1176 if (!rotateSeamlessly) {
Robert Carrae606b42018-02-15 15:36:23 -08001177 mService.startFreezingDisplayLocked(anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001178 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1179 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1180 mDisplayId);
1181 } else {
1182 // The screen rotation animation uses a screenshot to freeze the screen
1183 // while windows resize underneath.
1184 // When we are rotating seamlessly, we allow the elements to transition
1185 // to their rotated state independently and without a freeze required.
1186 screenRotationAnimation = null;
1187
Robert Carrefc75702018-02-16 11:46:16 -08001188 mService.startSeamlessRotation();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001189 }
1190
1191 // We need to update our screen size information to match the new rotation. If the rotation
1192 // has actually changed then this method will return true and, according to the comment at
1193 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1194 // By updating the Display info here it will be available to
1195 // #computeScreenConfiguration() later.
1196 updateDisplayAndOrientation(getConfiguration().uiMode);
1197
Robert Carrae606b42018-02-15 15:36:23 -08001198 // NOTE: We disable the rotation in the emulator because
1199 // it doesn't support hardware OpenGL emulation yet.
1200 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1201 && screenRotationAnimation.hasScreenshot()) {
1202 if (screenRotationAnimation.setRotation(getPendingTransaction(), rotation,
1203 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1204 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1205 mService.scheduleAnimationLocked();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001206 }
1207 }
1208
Vishnu Nair83537a72018-07-19 21:27:48 -07001209 forAllWindows(w -> {
Vishnu Nairddd80742018-08-21 14:12:46 -07001210 w.seamlesslyRotateIfAllowed(getPendingTransaction(), oldRotation, rotation,
1211 rotateSeamlessly);
Vishnu Nair83537a72018-07-19 21:27:48 -07001212 }, true /* traverseTopToBottom */);
1213
Robert Carrae606b42018-02-15 15:36:23 -08001214 mService.mDisplayManagerInternal.performTraversal(getPendingTransaction());
1215 scheduleAnimation();
1216
Andrii Kulian06d07d62017-03-14 11:11:47 -07001217 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001218 if (w.mHasSurface && !rotateSeamlessly) {
1219 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001220 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001221 mService.mRoot.mOrientationChangeComplete = false;
1222 w.mLastFreezeDuration = 0;
1223 }
1224 w.mReportOrientationChanged = true;
1225 }, true /* traverseTopToBottom */);
1226
1227 if (rotateSeamlessly) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001228 mService.mH.sendNewMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1229 this, SEAMLESS_ROTATION_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001230 }
1231
1232 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1233 final WindowManagerService.RotationWatcher rotationWatcher
1234 = mService.mRotationWatchers.get(i);
1235 if (rotationWatcher.mDisplayId == mDisplayId) {
1236 try {
1237 rotationWatcher.mWatcher.onRotationChanged(rotation);
1238 } catch (RemoteException e) {
1239 // Ignore
1240 }
1241 }
1242 }
1243
1244 // TODO (multi-display): Magnification is supported only for the default display.
1245 // Announce rotation only if we will not animate as we already have the
1246 // windows in final state. Otherwise, we make this call at the rotation end.
1247 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1248 && isDefaultDisplay) {
1249 mService.mAccessibilityController.onRotationChangedLocked(this);
1250 }
1251
1252 return true;
1253 }
1254
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001255 void configureDisplayPolicy() {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001256 final int width = mBaseDisplayWidth;
1257 final int height = mBaseDisplayHeight;
1258 final int shortSize;
1259 final int longSize;
1260 if (width > height) {
1261 shortSize = height;
1262 longSize = width;
1263 } else {
1264 shortSize = width;
1265 longSize = height;
1266 }
1267
1268 final int shortSizeDp = shortSize * DisplayMetrics.DENSITY_DEFAULT / mBaseDisplayDensity;
1269 final int longSizeDp = longSize * DisplayMetrics.DENSITY_DEFAULT / mBaseDisplayDensity;
1270
1271 mDisplayRotation.configure(width, height, shortSizeDp, longSizeDp);
1272 mDisplayPolicy.configure(width, height, shortSizeDp);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001273
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001274 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
1275 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001276 }
1277
Andrii Kulian06d07d62017-03-14 11:11:47 -07001278 /**
1279 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1280 * changed.
1281 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1282 */
1283 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1284 // Use the effective "visual" dimensions based on current rotation
1285 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1286 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1287 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1288 int dw = realdw;
1289 int dh = realdh;
1290
1291 if (mAltOrientation) {
1292 if (realdw > realdh) {
1293 // Turn landscape into portrait.
1294 int maxw = (int)(realdh/1.3f);
1295 if (maxw < realdw) {
1296 dw = maxw;
1297 }
1298 } else {
1299 // Turn portrait into landscape.
1300 int maxh = (int)(realdw/1.3f);
1301 if (maxh < realdh) {
1302 dh = maxh;
1303 }
1304 }
1305 }
1306
1307 // Update application display metrics.
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001308 final WmDisplayCutout wmDisplayCutout = calculateDisplayCutoutForRotation(mRotation);
1309 final DisplayCutout displayCutout = wmDisplayCutout.getDisplayCutout();
1310
Andrii Kulian06d07d62017-03-14 11:11:47 -07001311 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001312 mDisplayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001313 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001314 mDisplayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001315 mDisplayInfo.rotation = mRotation;
1316 mDisplayInfo.logicalWidth = dw;
1317 mDisplayInfo.logicalHeight = dh;
1318 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1319 mDisplayInfo.appWidth = appWidth;
1320 mDisplayInfo.appHeight = appHeight;
1321 if (isDefaultDisplay) {
1322 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1323 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1324 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001325 mDisplayInfo.displayCutout = displayCutout.isEmpty() ? null : displayCutout;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001326 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1327 if (mDisplayScalingDisabled) {
1328 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1329 } else {
1330 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1331 }
1332
Andrii Kulianf0379de2018-03-14 16:24:07 -07001333 // We usually set the override info in DisplayManager so that we get consistent display
1334 // metrics values when displays are changing and don't send out new values until WM is aware
1335 // of them. However, we don't do this for displays that serve as containers for ActivityView
1336 // because we don't want letter-/pillar-boxing during resize.
1337 final DisplayInfo overrideDisplayInfo = mShouldOverrideDisplayConfiguration
1338 ? mDisplayInfo : null;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001339 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
Andrii Kulianf0379de2018-03-14 16:24:07 -07001340 overrideDisplayInfo);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001341
1342 mBaseDisplayRect.set(0, 0, dw, dh);
1343
1344 if (isDefaultDisplay) {
1345 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1346 mCompatDisplayMetrics);
1347 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001348
1349 updateBounds();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001350 return mDisplayInfo;
1351 }
1352
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001353 WmDisplayCutout calculateDisplayCutoutForRotation(int rotation) {
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001354 return mDisplayCutoutCache.getOrCompute(mInitialDisplayCutout, rotation);
1355 }
1356
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001357 private WmDisplayCutout calculateDisplayCutoutForRotationUncached(
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001358 DisplayCutout cutout, int rotation) {
Adrian Roos24264212018-02-19 16:26:15 +01001359 if (cutout == null || cutout == DisplayCutout.NO_CUTOUT) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001360 return WmDisplayCutout.NO_CUTOUT;
Adrian Roos1cf585052018-01-03 18:43:27 +01001361 }
Adrian Roos11c25582018-02-19 18:06:36 +01001362 if (rotation == ROTATION_0) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001363 return WmDisplayCutout.computeSafeInsets(
1364 cutout, mInitialDisplayWidth, mInitialDisplayHeight);
Adrian Roos24264212018-02-19 16:26:15 +01001365 }
Adrian Roosbed538e2018-02-21 17:50:07 +01001366 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Jorim Jaggi60640512018-06-29 01:14:31 +02001367 final List<Rect> bounds = WmDisplayCutout.computeSafeInsets(
1368 cutout, mInitialDisplayWidth, mInitialDisplayHeight)
1369 .getDisplayCutout().getBoundingRects();
Adrian Roos11c25582018-02-19 18:06:36 +01001370 transformPhysicalToLogicalCoordinates(rotation, mInitialDisplayWidth, mInitialDisplayHeight,
1371 mTmpMatrix);
Jorim Jaggi60640512018-06-29 01:14:31 +02001372 final Region region = Region.obtain();
1373 for (int i = 0; i < bounds.size(); i++) {
1374 final Rect rect = bounds.get(i);
1375 final RectF rectF = new RectF(bounds.get(i));
1376 mTmpMatrix.mapRect(rectF);
1377 rectF.round(rect);
1378 region.op(rect, Op.UNION);
1379 }
1380
1381 return WmDisplayCutout.computeSafeInsets(DisplayCutout.fromBounds(region),
Adrian Roos24264212018-02-19 16:26:15 +01001382 rotated ? mInitialDisplayHeight : mInitialDisplayWidth,
1383 rotated ? mInitialDisplayWidth : mInitialDisplayHeight);
Adrian Roos1cf585052018-01-03 18:43:27 +01001384 }
1385
Andrii Kulian06d07d62017-03-14 11:11:47 -07001386 /**
1387 * Compute display configuration based on display properties and policy settings.
1388 * Do not call if mDisplayReady == false.
1389 */
1390 void computeScreenConfiguration(Configuration config) {
1391 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1392
1393 final int dw = displayInfo.logicalWidth;
1394 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001395 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
Garfield Tane0846042018-07-26 13:42:04 -07001396 config.windowConfiguration.setWindowingMode(getWindowingMode());
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001397
Adrian Roos11c25582018-02-19 18:06:36 +01001398 final float density = mDisplayMetrics.density;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001399 config.screenWidthDp =
1400 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001401 config.uiMode, mDisplayId, displayInfo.displayCutout) / density);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001402 config.screenHeightDp =
1403 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001404 config.uiMode, mDisplayId, displayInfo.displayCutout) / density);
Bryce Lee7566d762017-03-30 09:34:15 -07001405
Adrian Roos11c25582018-02-19 18:06:36 +01001406 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh,
1407 displayInfo.displayCutout, mTmpRect);
Bryce Lee7566d762017-03-30 09:34:15 -07001408 final int leftInset = mTmpRect.left;
1409 final int topInset = mTmpRect.top;
1410 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001411 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1412 leftInset + displayInfo.appWidth /* right */,
1413 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001414 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1415 || displayInfo.rotation == Surface.ROTATION_270);
1416
1417 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
Adrian Roos11c25582018-02-19 18:06:36 +01001418 density, config);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001419
1420 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1421 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1422 ? Configuration.SCREENLAYOUT_ROUND_YES
1423 : Configuration.SCREENLAYOUT_ROUND_NO);
1424
1425 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1426 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1427 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1428 dh, mDisplayId);
1429 config.densityDpi = displayInfo.logicalDensityDpi;
1430
1431 config.colorMode =
Chia-I Wuf739bad2018-05-09 12:36:57 -07001432 ((displayInfo.isHdr() && mService.hasHdrSupport())
Andrii Kulian06d07d62017-03-14 11:11:47 -07001433 ? Configuration.COLOR_MODE_HDR_YES
1434 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001435 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001436 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1437 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1438
1439 // Update the configuration based on available input devices, lid switch,
1440 // and platform configuration.
1441 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1442 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1443 config.navigation = Configuration.NAVIGATION_NONAV;
1444
1445 int keyboardPresence = 0;
1446 int navigationPresence = 0;
1447 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1448 final int len = devices != null ? devices.length : 0;
1449 for (int i = 0; i < len; i++) {
1450 InputDevice device = devices[i];
1451 if (!device.isVirtual()) {
1452 final int sources = device.getSources();
1453 final int presenceFlag = device.isExternal() ?
1454 WindowManagerPolicy.PRESENCE_EXTERNAL :
1455 WindowManagerPolicy.PRESENCE_INTERNAL;
1456
1457 // TODO(multi-display): Configure on per-display basis.
1458 if (mService.mIsTouchDevice) {
1459 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1460 InputDevice.SOURCE_TOUCHSCREEN) {
1461 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1462 }
1463 } else {
1464 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1465 }
1466
1467 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1468 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1469 navigationPresence |= presenceFlag;
1470 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1471 && config.navigation == Configuration.NAVIGATION_NONAV) {
1472 config.navigation = Configuration.NAVIGATION_DPAD;
1473 navigationPresence |= presenceFlag;
1474 }
1475
1476 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1477 config.keyboard = Configuration.KEYBOARD_QWERTY;
1478 keyboardPresence |= presenceFlag;
1479 }
1480 }
1481 }
1482
1483 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1484 config.navigation = Configuration.NAVIGATION_DPAD;
1485 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1486 }
1487
1488 // Determine whether a hard keyboard is available and enabled.
1489 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1490 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1491 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1492 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1493 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1494 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1495 }
1496
1497 // Let the policy update hidden states.
1498 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1499 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1500 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1501 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1502 }
1503
1504 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1505 int displayId) {
1506 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1507 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1508 final int unrotDw, unrotDh;
1509 if (rotated) {
1510 unrotDw = dh;
1511 unrotDh = dw;
1512 } else {
1513 unrotDw = dw;
1514 unrotDh = dh;
1515 }
1516 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1517 displayId);
1518 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1519 displayId);
1520 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1521 displayId);
1522 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1523 displayId);
1524 return sw;
1525 }
1526
1527 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1528 DisplayMetrics dm, int dw, int dh, int displayId) {
1529 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001530 displayId, mDisplayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001531 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001532 uiMode, displayId, mDisplayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001533 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1534 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1535 if (curSize == 0 || size < curSize) {
1536 curSize = size;
1537 }
1538 return curSize;
1539 }
1540
1541 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1542 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1543
1544 // We need to determine the smallest width that will occur under normal
1545 // operation. To this, start with the base screen size and compute the
1546 // width under the different possible rotations. We need to un-rotate
1547 // the current screen dimensions before doing this.
1548 int unrotDw, unrotDh;
1549 if (rotated) {
1550 unrotDw = dh;
1551 unrotDh = dw;
1552 } else {
1553 unrotDw = dw;
1554 unrotDh = dh;
1555 }
1556 displayInfo.smallestNominalAppWidth = 1<<30;
1557 displayInfo.smallestNominalAppHeight = 1<<30;
1558 displayInfo.largestNominalAppWidth = 0;
1559 displayInfo.largestNominalAppHeight = 0;
1560 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1561 unrotDh);
1562 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1563 unrotDw);
1564 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1565 unrotDh);
1566 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1567 unrotDw);
1568 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1569 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1570 displayId);
1571 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1572 displayId);
1573 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1574 displayId);
1575 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1576 displayId);
1577 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1578 outConfig.screenLayout = sl;
1579 }
1580
1581 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1582 int uiMode, int displayId) {
1583 // Get the app screen size at this rotation.
Adrian Roos11c25582018-02-19 18:06:36 +01001584 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId,
1585 mDisplayInfo.displayCutout);
1586 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId,
1587 mDisplayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001588
1589 // Compute the screen layout size class for this rotation.
1590 int longSize = w;
1591 int shortSize = h;
1592 if (longSize < shortSize) {
1593 int tmp = longSize;
1594 longSize = shortSize;
1595 shortSize = tmp;
1596 }
1597 longSize = (int)(longSize/density);
1598 shortSize = (int)(shortSize/density);
1599 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1600 }
1601
1602 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1603 int uiMode, int dw, int dh) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001604 final DisplayCutout displayCutout = calculateDisplayCutoutForRotation(
1605 rotation).getDisplayCutout();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001606 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001607 displayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001608 if (width < displayInfo.smallestNominalAppWidth) {
1609 displayInfo.smallestNominalAppWidth = width;
1610 }
1611 if (width > displayInfo.largestNominalAppWidth) {
1612 displayInfo.largestNominalAppWidth = width;
1613 }
1614 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001615 displayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001616 if (height < displayInfo.smallestNominalAppHeight) {
1617 displayInfo.smallestNominalAppHeight = height;
1618 }
1619 if (height > displayInfo.largestNominalAppHeight) {
1620 displayInfo.largestNominalAppHeight = height;
1621 }
1622 }
1623
Riddle Hsua4d6fa22018-08-11 00:50:39 +08001624 /**
1625 * Apps that use the compact menu panel (as controlled by the panelMenuIsCompact
1626 * theme attribute) on devices that feature a physical options menu key attempt to position
1627 * their menu panel window along the edge of the screen nearest the physical menu key.
1628 * This lowers the travel distance between invoking the menu panel and selecting
1629 * a menu option.
1630 *
1631 * This method helps control where that menu is placed. Its current implementation makes
1632 * assumptions about the menu key and its relationship to the screen based on whether
1633 * the device's natural orientation is portrait (width < height) or landscape.
1634 *
1635 * The menu key is assumed to be located along the bottom edge of natural-portrait
1636 * devices and along the right edge of natural-landscape devices. If these assumptions
1637 * do not hold for the target device, this method should be changed to reflect that.
1638 *
1639 * @return A {@link Gravity} value for placing the options menu window.
1640 */
1641 int getPreferredOptionsPanelGravity() {
1642 final int rotation = getRotation();
1643 if (mInitialDisplayWidth < mInitialDisplayHeight) {
1644 // On devices with a natural orientation of portrait.
1645 switch (rotation) {
1646 default:
1647 case Surface.ROTATION_0:
1648 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1649 case Surface.ROTATION_90:
1650 return Gravity.RIGHT | Gravity.BOTTOM;
1651 case Surface.ROTATION_180:
1652 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1653 case Surface.ROTATION_270:
1654 return Gravity.START | Gravity.BOTTOM;
1655 }
1656 }
1657
1658 // On devices with a natural orientation of landscape.
1659 switch (rotation) {
1660 default:
1661 case Surface.ROTATION_0:
1662 return Gravity.RIGHT | Gravity.BOTTOM;
1663 case Surface.ROTATION_90:
1664 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1665 case Surface.ROTATION_180:
1666 return Gravity.START | Gravity.BOTTOM;
1667 case Surface.ROTATION_270:
1668 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1669 }
1670 }
1671
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001672 DockedStackDividerController getDockedDividerController() {
1673 return mDividerControllerLocked;
1674 }
1675
Winson Chung655332c2016-10-31 13:14:28 -07001676 PinnedStackController getPinnedStackController() {
1677 return mPinnedStackControllerLocked;
1678 }
1679
Jeff Browna506a6e2013-06-04 00:02:38 -07001680 /**
1681 * Returns true if the specified UID has access to this display.
1682 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001683 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001684 return mDisplay.hasAccess(uid);
1685 }
1686
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001687 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001688 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001689 }
1690
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001691 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001692 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001693 }
1694
Wale Ogunwale61911492017-10-11 08:50:50 -07001695 /**
1696 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1697 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001698 TaskStack getSplitScreenPrimaryStack() {
1699 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001700 return (stack != null && stack.isVisible()) ? stack : null;
1701 }
1702
Robert Carr9785cf32018-04-25 15:06:07 -07001703 boolean hasSplitScreenPrimaryStack() {
1704 return getSplitScreenPrimaryStack() != null;
1705 }
1706
Wale Ogunwale61911492017-10-11 08:50:50 -07001707 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001708 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001709 * not visible.
1710 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001711 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1712 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001713 }
1714
1715 TaskStack getPinnedStack() {
1716 return mTaskStackContainers.getPinnedStack();
1717 }
1718
1719 private boolean hasPinnedStack() {
1720 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001721 }
1722
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001723 /**
1724 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1725 * Null is no compatible stack on the display.
1726 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001727 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001728 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1729 }
1730
1731 /**
1732 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1733 * activity type. Null is no compatible stack on the display.
1734 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001735 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001736 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001737 }
1738
Bryce Lee48f4b572017-04-10 10:54:15 -07001739 @VisibleForTesting
Kazuki Takise148d00a2018-05-31 15:32:19 +09001740 WindowList<TaskStack> getStacks() {
1741 return mTaskStackContainers.mChildren;
1742 }
1743
1744 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001745 TaskStack getTopStack() {
1746 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001747 }
1748
Winson Chunge2d72172018-01-25 17:46:20 +00001749 ArrayList<Task> getVisibleTasks() {
1750 return mTaskStackContainers.getVisibleTasks();
1751 }
1752
Wale Ogunwale61911492017-10-11 08:50:50 -07001753 void onStackWindowingModeChanged(TaskStack stack) {
1754 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001755 }
1756
Andrii Kulian441e4492016-09-29 15:25:00 -07001757 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001758 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001759 super.onConfigurationChanged(newParentConfig);
1760
Andrii Kulian3a507b52016-09-19 18:14:12 -07001761 // The display size information is heavily dependent on the resources in the current
1762 // configuration, so we need to reconfigure it every time the configuration changes.
1763 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1764 mService.reconfigureDisplayLocked(this);
1765
Bryce Leef3c6a472017-11-14 14:53:06 -08001766 final DockedStackDividerController dividerController = getDockedDividerController();
1767
1768 if (dividerController != null) {
1769 getDockedDividerController().onConfigurationChanged();
1770 }
1771
1772 final PinnedStackController pinnedStackController = getPinnedStackController();
1773
1774 if (pinnedStackController != null) {
1775 getPinnedStackController().onConfigurationChanged();
1776 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001777 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001778
Andrii Kulian441e4492016-09-29 15:25:00 -07001779 /**
1780 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1781 * bounds were updated.
1782 */
Bryce Leeaea60d22018-01-31 14:42:15 -08001783 void updateStackBoundsAfterConfigChange(@NonNull List<TaskStack> changedStackList) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001784 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1785 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001786 if (stack.updateBoundsAfterConfigChange()) {
Bryce Leeaea60d22018-01-31 14:42:15 -08001787 changedStackList.add(stack);
Andrii Kulian3a507b52016-09-19 18:14:12 -07001788 }
1789 }
Winson Chung32c566f2017-04-11 18:31:21 -07001790
1791 // If there was no pinned stack, we still need to notify the controller of the display info
1792 // update as a result of the config change. We do this here to consolidate the flow between
1793 // changes when there is and is not a stack.
Wale Ogunwale61911492017-10-11 08:50:50 -07001794 if (!hasPinnedStack()) {
Winson Chung32c566f2017-04-11 18:31:21 -07001795 mPinnedStackControllerLocked.onDisplayInfoChanged();
1796 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001797 }
1798
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001799 @Override
1800 boolean fillsParent() {
1801 return true;
1802 }
1803
1804 @Override
1805 boolean isVisible() {
1806 return true;
1807 }
1808
1809 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001810 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001811 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001812 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001813 }
1814
Robert Carr9785cf32018-04-25 15:06:07 -07001815 /**
1816 * In split-screen mode we process the IME containers above the docked divider
1817 * rather than directly above their target.
1818 */
1819 private boolean skipTraverseChild(WindowContainer child) {
1820 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null
1821 && !hasSplitScreenPrimaryStack()) {
1822 return true;
1823 }
1824 return false;
1825 }
1826
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001827 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001828 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1829 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1830 // target, or here in order if there isn't an IME target.
1831 if (traverseTopToBottom) {
1832 for (int i = mChildren.size() - 1; i >= 0; --i) {
1833 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001834 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001835 continue;
1836 }
Robert Carr9785cf32018-04-25 15:06:07 -07001837
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001838 if (child.forAllWindows(callback, traverseTopToBottom)) {
1839 return true;
1840 }
1841 }
1842 } else {
1843 final int count = mChildren.size();
1844 for (int i = 0; i < count; i++) {
1845 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001846 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001847 continue;
1848 }
Robert Carr9785cf32018-04-25 15:06:07 -07001849
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001850 if (child.forAllWindows(callback, traverseTopToBottom)) {
1851 return true;
1852 }
1853 }
1854 }
1855 return false;
1856 }
1857
1858 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1859 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1860 }
1861
Wale Ogunwale399c8692017-05-08 14:22:42 -07001862 @Override
1863 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001864 final WindowManagerPolicy policy = mService.mPolicy;
1865
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001866 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001867 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001868 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1869 + " is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001870 // If the display is frozen, some activities may be in the middle of restarting, and
1871 // thus have removed their old window. If the window has the flag to hide the lock
1872 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1873 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001874 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001875 } else if (policy.isKeyguardLocked()) {
1876 // Use the last orientation the while the display is frozen with the keyguard
1877 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1878 // window. We don't want to check the show when locked window directly though as
1879 // things aren't stable while the display is frozen, for example the window could be
1880 // momentarily unavailable due to activity relaunch.
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001881 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1882 + " is frozen while keyguard locked, return " + mLastOrientation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08001883 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001884 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001885 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001886 final int orientation = mAboveAppWindowsContainers.getOrientation();
1887 if (orientation != SCREEN_ORIENTATION_UNSET) {
1888 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001889 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001890 }
1891
Wale Ogunwale399c8692017-05-08 14:22:42 -07001892 // Top system windows are not requesting an orientation. Start searching from apps.
1893 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001894 }
1895
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001896 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001897 // Check if display metrics changed and update base values if needed.
1898 updateBaseDisplayMetricsIfNeeded();
1899
Craig Mautner722285e2012-09-07 13:55:58 -07001900 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001901 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001902
Wale Ogunwale61911492017-10-11 08:50:50 -07001903 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1904 mTaskStackContainers.getChildAt(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001905 }
Craig Mautner722285e2012-09-07 13:55:58 -07001906 }
1907
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001908 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001909 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1910 if (displayManagerInternal != null) {
1911 // Bootstrap the default logical display from the display manager.
1912 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1913 if (newDisplayInfo != null) {
1914 mDisplayInfo.copyFrom(newDisplayInfo);
1915 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001916 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001917
Andrii Kuliancd097992017-03-23 18:31:59 -07001918 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1919 mDisplayInfo.logicalDensityDpi);
1920 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1921 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1922 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01001923 mInitialDisplayCutout = mDisplayInfo.displayCutout;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001924 }
1925
Andrii Kuliancd097992017-03-23 18:31:59 -07001926 /**
1927 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1928 * values.
1929 */
1930 private void updateBaseDisplayMetricsIfNeeded() {
1931 // Get real display metrics without overrides from WM.
1932 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1933 final int orientation = mDisplayInfo.rotation;
1934 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1935 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1936 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1937 final int newDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01001938 final DisplayCutout newCutout = mDisplayInfo.displayCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07001939
1940 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1941 || mInitialDisplayHeight != newHeight
Adrian Roos1cf585052018-01-03 18:43:27 +01001942 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi
1943 || !Objects.equals(mInitialDisplayCutout, newCutout);
Andrii Kuliancd097992017-03-23 18:31:59 -07001944
1945 if (displayMetricsChanged) {
1946 // Check if display size or density is forced.
1947 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1948 || mBaseDisplayHeight != mInitialDisplayHeight;
1949 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1950
1951 // If there is an override set for base values - use it, otherwise use new values.
1952 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1953 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1954 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1955
1956 // Real display metrics changed, so we should also update initial values.
1957 mInitialDisplayWidth = newWidth;
1958 mInitialDisplayHeight = newHeight;
1959 mInitialDisplayDensity = newDensity;
Adrian Roos1cf585052018-01-03 18:43:27 +01001960 mInitialDisplayCutout = newCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07001961 mService.reconfigureDisplayLocked(this);
1962 }
1963 }
1964
Bryce Lee27cec322017-03-21 09:41:37 -07001965 /** Sets the maximum width the screen resolution can be */
1966 void setMaxUiWidth(int width) {
1967 if (DEBUG_DISPLAY) {
1968 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1969 }
1970
1971 mMaxUiWidth = width;
1972
1973 // Update existing metrics.
1974 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1975 }
1976
1977 /** Update base (override) display metrics. */
1978 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1979 mBaseDisplayWidth = baseWidth;
1980 mBaseDisplayHeight = baseHeight;
1981 mBaseDisplayDensity = baseDensity;
1982
1983 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1984 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1985 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1986 mBaseDisplayWidth = mMaxUiWidth;
1987
1988 if (DEBUG_DISPLAY) {
1989 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1990 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1991 + " on display:" + getDisplayId());
1992 }
1993 }
1994
1995 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08001996
1997 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07001998 }
1999
Wale Ogunwaledb506192017-12-08 10:57:32 -08002000 void getStableRect(Rect out) {
2001 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08002002 }
2003
Wale Ogunwale61911492017-10-11 08:50:50 -07002004 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002005 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
2006 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07002007
Wale Ogunwale61911492017-10-11 08:50:50 -07002008 final TaskStack stack = new TaskStack(mService, stackId, controller);
2009 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002010 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07002011 }
2012
Andrii Kulian51c1b672017-04-07 18:39:32 -07002013 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002014 final DisplayContent prevDc = stack.getDisplayContent();
2015 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07002016 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
2017 + " which is not currently attached to any display");
2018 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002019 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07002020 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
2021 + " to its current displayId=" + mDisplayId);
2022 }
2023
Wale Ogunwale61911492017-10-11 08:50:50 -07002024 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07002025 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08002026 }
2027
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002028 @Override
2029 protected void addChild(DisplayChildWindowContainer child,
2030 Comparator<DisplayChildWindowContainer> comparator) {
2031 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2032 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002033
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002034 @Override
2035 protected void addChild(DisplayChildWindowContainer child, int index) {
2036 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2037 }
2038
2039 @Override
2040 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002041 // Only allow removal of direct children from this display if the display is in the process
2042 // of been removed.
2043 if (mRemovingDisplay) {
2044 super.removeChild(child);
2045 return;
2046 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002047 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002048 }
2049
Andrii Kuliand2765632016-12-12 22:26:34 -08002050 @Override
2051 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
2052 // Children of the display are statically ordered, so the real intention here is to perform
2053 // the operation on the display and not the static direct children.
2054 getParent().positionChildAt(position, this, includingParents);
2055 }
2056
Riddle Hsu57831b52018-07-27 00:31:48 +08002057 void positionStackAt(int position, TaskStack child, boolean includingParents) {
2058 mTaskStackContainers.positionChildAt(position, child, includingParents);
Winson Chung59a47ded2018-01-25 17:46:06 +00002059 layoutAndAssignWindowLayersIfNeeded();
2060 }
2061
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002062 int taskIdFromPoint(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002063 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2064 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002065 final int taskId = stack.taskIdFromPoint(x, y);
2066 if (taskId != -1) {
2067 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07002068 }
2069 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002070 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07002071 }
2072
Chong Zhang8e89b312015-09-09 15:09:30 -07002073 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08002074 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07002075 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07002076 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07002077 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002078 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002079 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07002080 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2081 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002082 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002083 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002084 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002085
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002086 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
2087 if (mTmpTaskForResizePointSearchResult.searchDone) {
2088 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07002089 }
2090 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002091 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002092 }
2093
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002094 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08002095 // The provided task is the task on this display with focus, so if WindowManagerService's
2096 // focused app is not on this display, focusedTask will be null.
2097 if (focusedTask == null) {
2098 mTouchExcludeRegion.setEmpty();
2099 } else {
2100 mTouchExcludeRegion.set(mBaseDisplayRect);
2101 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
2102 mTmpRect2.setEmpty();
Wale Ogunwale61911492017-10-11 08:50:50 -07002103 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2104 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002105 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
2106 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08002107 }
2108 // If we removed the focused task above, add it back and only leave its
2109 // outside touch area in the exclusion. TapDectector is not interested in
2110 // any touch inside the focused task itself.
2111 if (!mTmpRect2.isEmpty()) {
2112 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
2113 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08002114 }
lumark90120a82018-08-15 00:33:03 +08002115 if (mInputMethodWindow != null && mInputMethodWindow.isVisibleLw()) {
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002116 // If the input method is visible and the user is typing, we don't want these touch
2117 // events to be intercepted and used to change focus. This would likely cause a
2118 // disappearance of the input method.
lumark90120a82018-08-15 00:33:03 +08002119 mInputMethodWindow.getTouchableRegion(mTmpRegion);
2120 if (mInputMethodWindow.getDisplayId() == mDisplayId) {
Andrii Kulian7a31b772017-05-02 13:22:42 -07002121 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
2122 } else {
2123 // IME is on a different display, so we need to update its tap detector.
lumark90120a82018-08-15 00:33:03 +08002124 setTouchExcludeRegion(null /* focusedTask */);
Andrii Kulian7a31b772017-05-02 13:22:42 -07002125 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002126 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002127 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002128 final WindowState win = mTapExcludedWindows.get(i);
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002129 win.getTouchableRegion(mTmpRegion);
2130 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
2131 }
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002132 for (int i = mTapExcludeProvidingWindows.size() - 1; i >= 0; i--) {
2133 final WindowState win = mTapExcludeProvidingWindows.valueAt(i);
2134 win.amendTapExcludeRegion(mTouchExcludeRegion);
2135 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08002136 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07002137 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002138 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07002139 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002140 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
2141 }
Craig Mautner1bef3892015-02-17 15:09:47 -08002142 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07002143 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08002144 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07002145 }
2146
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002147 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07002148 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002149 super.switchUser();
2150 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07002151 }
2152
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07002153 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002154 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2155 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07002156 }
2157 }
2158
Wale Ogunwale10124582016-09-15 20:25:50 -07002159 @Override
2160 void removeIfPossible() {
2161 if (isAnimating()) {
2162 mDeferredRemoval = true;
2163 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07002164 }
Wale Ogunwale10124582016-09-15 20:25:50 -07002165 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07002166 }
2167
Wale Ogunwale10124582016-09-15 20:25:50 -07002168 @Override
2169 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002170 mRemovingDisplay = true;
2171 try {
2172 super.removeImmediately();
2173 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -07002174 if (mService.canDispatchPointerEvents()) {
2175 if (mTapDetector != null) {
2176 mService.unregisterPointerEventListener(mTapDetector);
2177 }
2178 if (mDisplayId == DEFAULT_DISPLAY && mService.mMousePositionTracker != null) {
2179 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
2180 }
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002181 }
Jorim Jaggia3844032018-01-03 15:54:43 +01002182 mService.mAnimator.removeDisplayLocked(mDisplayId);
Louis Chang207f9112018-07-30 08:58:54 +08002183 mWindowingLayer.release();
2184 mOverlayLayer.release();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002185 } finally {
2186 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08002187 }
Bryce Leef19cbe22018-02-02 15:09:21 -08002188
Arthur Hung39134b22018-08-14 11:58:28 +08002189 mInputMonitor.onRemoved();
Bryce Leef19cbe22018-02-02 15:09:21 -08002190 mService.onDisplayRemoved(mDisplayId);
Craig Mautner95da1082014-02-24 17:54:35 -08002191 }
2192
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002193 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07002194 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002195 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002196 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
2197
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002198 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002199 removeImmediately();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002200 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08002201 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002202 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08002203 }
2204
Andrii Kulian0214ed92017-05-16 13:44:05 -07002205 /** @return 'true' if removal of this display content is deferred due to active animation. */
2206 boolean isRemovalDeferred() {
2207 return mDeferredRemoval;
2208 }
2209
Wale Ogunwale10124582016-09-15 20:25:50 -07002210 boolean animateForIme(float interpolatedValue, float animationTarget,
2211 float dividerAnimationTarget) {
2212 boolean updated = false;
2213
Wale Ogunwale61911492017-10-11 08:50:50 -07002214 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2215 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002216 if (stack == null || !stack.isAdjustedForIme()) {
2217 continue;
2218 }
2219
2220 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
2221 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2222 updated = true;
2223 } else {
2224 mDividerControllerLocked.mLastAnimationProgress =
2225 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
2226 mDividerControllerLocked.mLastDividerProgress =
2227 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
2228 updated |= stack.updateAdjustForIme(
2229 mDividerControllerLocked.mLastAnimationProgress,
2230 mDividerControllerLocked.mLastDividerProgress,
2231 false /* force */);
2232 }
2233 if (interpolatedValue >= 1f) {
2234 stack.endImeAdjustAnimation();
2235 }
2236 }
2237
2238 return updated;
2239 }
2240
2241 boolean clearImeAdjustAnimation() {
2242 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002243 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2244 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002245 if (stack != null && stack.isAdjustedForIme()) {
2246 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2247 changed = true;
2248 }
2249 }
2250 return changed;
2251 }
2252
2253 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002254 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2255 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002256 if (stack.isVisible() && stack.isAdjustedForIme()) {
2257 stack.beginImeAdjustAnimation();
2258 }
2259 }
2260 }
2261
2262 void adjustForImeIfNeeded() {
lumark90120a82018-08-15 00:33:03 +08002263 final WindowState imeWin = mInputMethodWindow;
Wale Ogunwale10124582016-09-15 20:25:50 -07002264 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2265 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002266 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002267 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2268 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2269 imeTargetStack.getDockSide() : DOCKED_INVALID;
2270 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2271 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2272 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002273 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002274 final boolean imeHeightChanged = imeVisible &&
2275 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2276
2277 // The divider could be adjusted for IME position, or be thinner than usual,
2278 // or both. There are three possible cases:
2279 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2280 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2281 // - If IME is not visible, divider is not moved and is normal width.
2282
2283 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002284 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2285 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002286 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002287 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2288 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002289 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2290 } else {
2291 stack.resetAdjustedForIme(false);
2292 }
2293 }
2294 mDividerControllerLocked.setAdjustedForIme(
2295 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2296 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002297 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2298 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002299 stack.resetAdjustedForIme(!dockVisible);
2300 }
2301 mDividerControllerLocked.setAdjustedForIme(
2302 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2303 }
Winson Chung655332c2016-10-31 13:14:28 -07002304 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002305 }
2306
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002307 /**
2308 * If a window that has an animation specifying a colored background and the current wallpaper
2309 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2310 * suddenly disappear.
2311 */
2312 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002313 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2314 w -> w.mIsWallpaper && w.isVisibleNow());
2315
2316 if (visibleWallpaper != null) {
2317 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002318 }
2319 return winAnimator.mAnimLayer;
2320 }
2321
Wale Ogunwale10124582016-09-15 20:25:50 -07002322 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002323 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2324 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002325 stack.prepareFreezingTaskBounds();
2326 }
2327 }
2328
Wale Ogunwale94744212015-09-21 19:01:47 -07002329 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002330 getBounds(mTmpRect, newRotation);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002331
2332 // Compute a transform matrix to undo the coordinate space transformation,
2333 // and present the window at the same physical position it previously occupied.
2334 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2335 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2336
2337 mTmpRectF.set(bounds);
2338 mTmpMatrix.mapRect(mTmpRectF);
2339 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002340 }
2341
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002342 static int deltaRotation(int oldRotation, int newRotation) {
2343 int delta = newRotation - oldRotation;
2344 if (delta < 0) delta += 4;
2345 return delta;
2346 }
2347
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002348 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002349 Matrix outMatrix) {
2350 // For rotations without Z-ordering we don't need the target rectangle's position.
2351 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2352 displayHeight, outMatrix);
2353 }
2354
2355 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2356 float displayWidth, float displayHeight, Matrix outMatrix) {
2357 switch (rotation) {
2358 case ROTATION_0:
2359 outMatrix.reset();
2360 break;
2361 case ROTATION_270:
2362 outMatrix.setRotate(270, 0, 0);
2363 outMatrix.postTranslate(0, displayHeight);
2364 outMatrix.postTranslate(rectTop, 0);
2365 break;
2366 case ROTATION_180:
2367 outMatrix.reset();
2368 break;
2369 case ROTATION_90:
2370 outMatrix.setRotate(90, 0, 0);
2371 outMatrix.postTranslate(displayWidth, 0);
2372 outMatrix.postTranslate(-rectTop, rectLeft);
2373 break;
2374 }
2375 }
2376
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002377 @CallSuper
2378 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002379 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002380 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002381 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002382 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002383 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2384 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002385 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002386 }
2387 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2388 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002389 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2390 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002391 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002392 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002393 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2394 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002395 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002396 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002397 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2398 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002399 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002400 }
2401 proto.write(DPI, mBaseDisplayDensity);
2402 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002403 proto.write(ROTATION, mRotation);
2404 final ScreenRotationAnimation screenRotationAnimation =
2405 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2406 if (screenRotationAnimation != null) {
2407 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2408 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002409 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002410 proto.end(token);
2411 }
2412
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002413 @Override
2414 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2415 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002416 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2417 final String subPrefix = " " + prefix;
2418 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2419 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2420 pw.print("dpi");
2421 if (mInitialDisplayWidth != mBaseDisplayWidth
2422 || mInitialDisplayHeight != mBaseDisplayHeight
2423 || mInitialDisplayDensity != mBaseDisplayDensity) {
2424 pw.print(" base=");
2425 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2426 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2427 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002428 if (mDisplayScalingDisabled) {
2429 pw.println(" noscale");
2430 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002431 pw.print(" cur=");
2432 pw.print(mDisplayInfo.logicalWidth);
2433 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2434 pw.print(" app=");
2435 pw.print(mDisplayInfo.appWidth);
2436 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2437 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2438 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2439 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2440 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002441 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002442 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002443 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002444
Craig Mautnerdc548482014-02-05 13:35:24 -08002445 pw.println();
Adrian Roos5251b1d2018-03-23 18:57:43 +01002446 pw.print(prefix); pw.print("mLayoutSeq="); pw.println(mLayoutSeq);
Riddle Hsu654a6f92018-07-13 22:59:36 +08002447 pw.print(prefix);
2448 pw.print("mDeferredRotationPauseCount="); pw.println(mDeferredRotationPauseCount);
Adrian Roos5251b1d2018-03-23 18:57:43 +01002449
2450 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002451 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002452 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2453 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002454 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002455 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002456
Craig Mautnerdc548482014-02-05 13:35:24 -08002457 pw.println();
2458 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002459 pw.println();
2460 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002461 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002462 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002463 pw.print(" Exiting #"); pw.print(i);
2464 pw.print(' '); pw.print(token);
2465 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002466 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002467 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002468 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002469
Jorim Jaggi31f71702016-05-04 16:43:04 -07002470 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002471
2472 // Dump stack references
2473 final TaskStack homeStack = getHomeStack();
2474 if (homeStack != null) {
2475 pw.println(prefix + "homeStack=" + homeStack.getName());
2476 }
2477 final TaskStack pinnedStack = getPinnedStack();
2478 if (pinnedStack != null) {
2479 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2480 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002481 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002482 if (splitScreenPrimaryStack != null) {
2483 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2484 }
2485
2486 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002487 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002488 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002489 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002490
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002491 pw.println();
2492 mDisplayFrames.dump(prefix, pw);
Arthur Hung95b38a92018-07-20 18:56:12 +08002493 pw.println();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08002494 mDisplayPolicy.dump(prefix, pw);
2495 pw.println();
Riddle Hsuad256a12018-07-18 16:11:30 +08002496 mDisplayRotation.dump(prefix, pw);
2497 pw.println();
Arthur Hung95b38a92018-07-20 18:56:12 +08002498 mInputMonitor.dump(pw, " ");
Craig Mautner59c00972012-07-30 12:10:24 -07002499 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002500
2501 @Override
2502 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002503 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002504 }
2505
2506 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002507 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002508 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002509
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002510 /** Returns true if the stack in the windowing mode is visible. */
2511 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002512 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002513 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002514 }
2515
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002516 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002517 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002518 final int x = (int) xf;
2519 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002520 final WindowState touchedWin = getWindow(w -> {
2521 final int flags = w.mAttrs.flags;
2522 if (!w.isVisibleLw()) {
2523 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002524 }
2525 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002526 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002527 }
2528
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002529 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002530 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002531 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002532 }
2533
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002534 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002535
2536 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002537 return mTmpRegion.contains(x, y) || touchFlags == 0;
2538 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002539
2540 return touchedWin;
2541 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002542
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002543 boolean canAddToastWindowForUid(int uid) {
2544 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002545 // Also if the app is focused adding more than one toast at
2546 // a time for better backwards compatibility.
2547 final WindowState focusedWindowForUid = getWindow(w ->
2548 w.mOwnerUid == uid && w.isFocused());
2549 if (focusedWindowForUid != null) {
2550 return true;
2551 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002552 final WindowState win = getWindow(w ->
2553 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2554 && !w.mWindowRemovalAllowed);
2555 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002556 }
2557
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002558 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002559 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2560 return;
2561 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002562
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002563 // Used to communicate the old focus to the callback method.
2564 mTmpWindow = oldFocus;
2565
2566 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002567 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002568
2569 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002570 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002571
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002572 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002573
2574 if (mTmpWindow == null) {
2575 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2576 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002577 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002578 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002579 }
2580
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002581 /** Updates the layer assignment of windows on this display. */
2582 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002583 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07002584 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002585 if (setLayoutNeeded) {
2586 setLayoutNeeded();
2587 }
Robert Carrb1579c82017-09-05 14:54:47 -07002588
Robert Carrf59b8dd2017-10-02 18:58:36 -07002589 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002590 // the application of this transaction until the animation pass triggers
2591 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2592 // the hiding and showing of surfaces.
2593 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002594 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002595 }
2596
Wale Ogunwale1666e312016-12-16 11:27:18 -08002597 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2598 // moving containers or resizing them. Need to investigate the best way to have it automatically
2599 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002600 void layoutAndAssignWindowLayersIfNeeded() {
2601 mService.mWindowsChanged = true;
2602 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002603
2604 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2605 false /*updateInputWindows*/)) {
2606 assignWindowLayers(false /* setLayoutNeeded */);
2607 }
2608
Arthur Hung95b38a92018-07-20 18:56:12 +08002609 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002610 mService.mWindowPlacerLocked.performSurfacePlacement();
Arthur Hung95b38a92018-07-20 18:56:12 +08002611 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002612 }
2613
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002614 /** Returns true if a leaked surface was destroyed */
2615 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002616 // Used to indicate that a surface was leaked.
2617 mTmpWindow = null;
2618 forAllWindows(w -> {
2619 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002620 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002621 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002622 }
2623 if (!mService.mSessions.contains(wsa.mSession)) {
2624 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002625 + w + " surface=" + wsa.mSurfaceController
2626 + " token=" + w.mToken
2627 + " pid=" + w.mSession.mPid
2628 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002629 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002630 mService.mForceRemoves.add(w);
2631 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002632 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002633 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002634 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002635 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002636 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002637 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002638 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002639 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002640 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002641
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002642 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002643 }
2644
2645 /**
lumark90120a82018-08-15 00:33:03 +08002646 * Set input method window for the display.
2647 * @param win Set when window added or Null when destroyed.
2648 */
2649 void setInputMethodWindowLocked(WindowState win) {
2650 mInputMethodWindow = win;
2651 computeImeTarget(true /* updateImeTarget */);
2652 }
2653
2654 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002655 * Determine and return the window that should be the IME target.
2656 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2657 * @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 +00002658 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002659 WindowState computeImeTarget(boolean updateImeTarget) {
lumark90120a82018-08-15 00:33:03 +08002660 if (mInputMethodWindow == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002661 // There isn't an IME so there shouldn't be a target...That was easy!
2662 if (updateImeTarget) {
2663 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2664 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002665 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002666 }
2667 return null;
2668 }
2669
Chavi Weingarten3a748552018-05-14 17:32:42 +00002670 final WindowState curTarget = mService.mInputMethodTarget;
2671 if (!canUpdateImeTarget()) {
2672 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Defer updating IME target");
2673 return curTarget;
2674 }
2675
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002676 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2677 // same display. Or even when the current IME/target are not on the same screen as the next
2678 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002679 mUpdateImeTarget = updateImeTarget;
2680 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002681
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002682
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002683 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2684 // to be on top of it, but it is not -really- where input will go. So look down below
2685 // for a real window to target...
2686 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2687 final AppWindowToken token = target.mAppToken;
2688 if (token != null) {
2689 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2690 if (betterTarget != null) {
2691 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002692 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002693 }
2694 }
2695
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002696 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2697 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002698
chaviw1c13ad32018-06-12 16:44:23 -07002699 // Now, a special case -- if the last target's window is in the process of exiting, but
2700 // not removed, and the new target is home, keep on the last target to avoid flicker.
2701 // Home is a special case since its above other stacks in the ordering list, but layed
2702 // out below the others.
2703 if (curTarget != null && !curTarget.mRemoved && curTarget.isDisplayedLw()
2704 && curTarget.isClosing() && (target == null || target.isActivityTypeHome())) {
chaviw4ad54912018-05-30 11:05:44 -07002705 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "New target is home while current target is "
Chavi Weingarten3a748552018-05-14 17:32:42 +00002706 + "closing, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002707 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002708 }
2709
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002710 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2711 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002712
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002713 if (target == null) {
2714 if (updateImeTarget) {
2715 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2716 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2717 + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002718 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002719 }
2720
2721 return null;
2722 }
2723
2724 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002725 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2726 if (token != null) {
2727
2728 // Now some fun for dealing with window animations that modify the Z order. We need
2729 // to look at all windows below the current target that are in this app, finding the
2730 // highest visible one in layering.
2731 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002732 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002733 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002734 }
2735
2736 if (highestTarget != null) {
2737 final AppTransition appTransition = mService.mAppTransition;
2738 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2739 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2740 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002741 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002742
2743 if (appTransition.isTransitionSet()) {
2744 // If we are currently setting up for an animation, hold everything until we
2745 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002746 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002747 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002748 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002749 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002750 // If the window we are currently targeting is involved with an animation,
2751 // and it is on top of the next target we will be over, then hold off on
2752 // moving until that is done.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002753 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002754 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002755 }
2756 }
2757 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002758
2759 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2760 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002761 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002762 }
2763
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002764 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002765 }
2766
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002767 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002768 if (target == mService.mInputMethodTarget
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002769 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002770 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002771 }
2772
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002773 mService.mInputMethodTarget = target;
2774 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002775 assignWindowLayers(false /* setLayoutNeeded */);
2776 }
2777
2778 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2779 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2780 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2781 }
2782
2783 // Used to indicate we have reached the first window in the range we are interested in.
2784 mTmpWindow = null;
2785
2786 // TODO: Figure-out a more efficient way to do this.
2787 final WindowState candidate = getWindow(w -> {
2788 if (w == top) {
2789 // Reached the first window in the range we are interested in.
2790 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002791 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002792 if (mTmpWindow == null) {
2793 return false;
2794 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002795
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002796 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2797 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002798 }
2799 // If we reached the bottom of the range of windows we are considering,
2800 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002801 if (w == bottom) {
2802 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002803 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002804 return false;
2805 });
2806
2807 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002808 }
2809
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002810 void setLayoutNeeded() {
2811 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2812 mLayoutNeeded = true;
2813 }
2814
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002815 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002816 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2817 mLayoutNeeded = false;
2818 }
2819
2820 boolean isLayoutNeeded() {
2821 return mLayoutNeeded;
2822 }
2823
Wale Ogunwale02319a62016-09-26 15:21:22 -07002824 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2825 if (mTokenMap.isEmpty()) {
2826 return;
2827 }
2828 pw.println(" Display #" + mDisplayId);
2829 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2830 while (it.hasNext()) {
2831 final WindowToken token = it.next();
2832 pw.print(" ");
2833 pw.print(token);
2834 if (dumpAll) {
2835 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002836 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07002837 } else {
2838 pw.println();
2839 }
2840 }
2841 }
2842
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002843 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002844 final int[] index = new int[1];
2845 forAllWindows(w -> {
2846 final WindowStateAnimator wAnim = w.mWinAnimator;
2847 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2848 index[0] = index[0] + 1;
2849 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002850 }
2851
Jorim Jaggife762342016-10-13 14:33:27 +02002852 /**
2853 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2854 */
2855 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2856 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002857 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002858 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2859 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01002860 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02002861 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002862 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002863 }
2864
Wale Ogunwale494009b82016-10-21 09:01:38 -07002865 boolean checkWaitingForWindows() {
2866
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002867 mHaveBootMsg = false;
2868 mHaveApp = false;
2869 mHaveWallpaper = false;
2870 mHaveKeyguard = true;
2871
2872 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002873 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2874 return true;
2875 }
2876 if (w.isDrawnLw()) {
2877 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002878 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002879 } else if (w.mAttrs.type == TYPE_APPLICATION
2880 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002881 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002882 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002883 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002884 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002885 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002886 }
2887 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002888 return false;
2889 });
2890
2891 if (visibleWindow != null) {
2892 // We have a visible window.
2893 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002894 }
2895
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002896 // if the wallpaper service is disabled on the device, we're never going to have
2897 // wallpaper, don't bother waiting for it
2898 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2899 com.android.internal.R.bool.config_enableWallpaperService)
Daichi Hironoc1432222018-03-29 13:06:07 +09002900 && mService.mContext.getResources().getBoolean(
2901 com.android.internal.R.bool.config_checkWallpaperAtBoot)
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002902 && !mService.mOnlyCore;
2903
Wale Ogunwale494009b82016-10-21 09:01:38 -07002904 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2905 "******** booted=" + mService.mSystemBooted
2906 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002907 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2908 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2909 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002910
2911 // If we are turning on the screen to show the boot message, don't do it until the boot
2912 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002913 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002914 return true;
2915 }
2916
2917 // If we are turning on the screen after the boot is completed normally, don't do so until
2918 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002919 if (mService.mSystemBooted
2920 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002921 return true;
2922 }
2923
2924 return false;
2925 }
2926
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002927 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002928 mTmpWindowAnimator = animator;
2929 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002930 }
2931
2932 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002933 resetAnimationBackgroundAnimator();
2934
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002935 // Used to indicate a detached wallpaper.
2936 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002937 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002938
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002939 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002940
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002941 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002942 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002943 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2944 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002945 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2946 }
2947 }
2948
Wale Ogunwale494009b82016-10-21 09:01:38 -07002949 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002950 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002951 if (imFocus == null) {
2952 return false;
2953 }
2954
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002955 if (DEBUG_INPUT_METHOD) {
2956 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2957 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2958 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002959 }
2960
Wale Ogunwale494009b82016-10-21 09:01:38 -07002961 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2962
2963 if (DEBUG_INPUT_METHOD) {
2964 Slog.i(TAG_WM, "IM target client: " + imeClient);
2965 if (imeClient != null) {
2966 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2967 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2968 }
2969 }
2970
2971 return imeClient != null && imeClient.asBinder() == client.asBinder();
2972 }
2973
2974 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002975 final WindowState win = getWindow(
2976 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2977 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002978 }
2979
2980 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002981 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002982 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002983 final int curValue = w.mSystemUiVisibility;
2984 final int diff = (curValue ^ visibility) & globalDiff;
2985 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002986 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002987 w.mSeq++;
2988 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002989 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002990 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2991 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002992 visibility, newValue, diff);
2993 }
2994 } catch (RemoteException e) {
2995 // so sorry
2996 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002997 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002998 }
2999
3000 void onWindowFreezeTimeout() {
3001 Slog.w(TAG_WM, "Window freeze timeout expired.");
3002 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003003
3004 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07003005 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003006 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003007 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07003008 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003009 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
3010 - mService.mDisplayFreezeTime);
3011 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003012 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003013 mService.mWindowPlacerLocked.performSurfacePlacement();
3014 }
3015
3016 void waitForAllWindowsDrawn() {
3017 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003018 forAllWindows(w -> {
3019 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
3020 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
3021 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003022 // Force add to mResizingWindows.
chaviw9c81e632018-07-31 11:17:52 -07003023 w.resetLastContentInsets();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003024 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003025 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003026 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003027 }
3028
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003029 // TODO: Super crazy long method that should be broken down...
3030 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
3031
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003032 final int dw = mDisplayInfo.logicalWidth;
3033 final int dh = mDisplayInfo.logicalHeight;
3034 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
3035
3036 mTmpUpdateAllDrawn.clear();
3037
3038 int repeats = 0;
3039 do {
3040 repeats++;
3041 if (repeats > 6) {
3042 Slog.w(TAG, "Animation repeat aborted after too many iterations");
3043 clearLayoutNeeded();
3044 break;
3045 }
3046
3047 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
3048 pendingLayoutChanges);
3049
Andrii Kulian839def92016-11-02 10:58:58 -07003050 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
3051 // the wallpaper window jumping across displays.
3052 // Remove check for default display when there will be support for multiple wallpaper
3053 // targets (on different displays).
3054 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003055 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003056 }
3057
Riddle Hsu654a6f92018-07-13 22:59:36 +08003058 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003059 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
Robert Carrae606b42018-02-15 15:36:23 -08003060 if (mService.updateOrientationFromAppTokensLocked(mDisplayId)) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003061 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07003062 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003063 }
3064 }
3065
3066 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
3067 setLayoutNeeded();
3068 }
3069
3070 // FIRST LOOP: Perform a layout, if needed.
3071 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
3072 performLayout(repeats == 1, false /* updateInputWindows */);
3073 } else {
3074 Slog.w(TAG, "Layout repeat skipped after too many iterations");
3075 }
3076
3077 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
3078 pendingLayoutChanges = 0;
3079
3080 if (isDefaultDisplay) {
3081 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003082 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003083 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
3084 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
3085 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
3086 }
3087 } while (pendingLayoutChanges != 0);
3088
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003089 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003090
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003091 mTmpRecoveringMemory = recoveringMemory;
3092 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01003093 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003094
3095 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003096 mTmpApplySurfaceChangesTransactionState.displayHasContent,
3097 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
3098 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003099 true /* inTraversal, must call performTraversalInTrans... below */);
3100
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003101 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
3102 if (wallpaperVisible != mLastWallpaperVisible) {
3103 mLastWallpaperVisible = wallpaperVisible;
3104 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
3105 }
3106
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003107 while (!mTmpUpdateAllDrawn.isEmpty()) {
3108 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
3109 // See if any windows have been drawn, so they (and others associated with them)
3110 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07003111 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003112 }
3113
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003114 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003115 }
3116
Bryce Leef3c6a472017-11-14 14:53:06 -08003117 private void updateBounds() {
3118 calculateBounds(mTmpBounds);
3119 setBounds(mTmpBounds);
3120 }
3121
3122 // Determines the current display bounds based on the current state
3123 private void calculateBounds(Rect out) {
3124 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
3125 final int orientation = mDisplayInfo.rotation;
3126 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
3127 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
3128 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
3129 int width = mDisplayInfo.logicalWidth;
3130 int left = (physWidth - width) / 2;
3131 int height = mDisplayInfo.logicalHeight;
3132 int top = (physHeight - height) / 2;
3133 out.set(left, top, left + width, top + height);
3134 }
3135
3136 @Override
3137 public void getBounds(Rect out) {
3138 calculateBounds(out);
3139 }
3140
3141 private void getBounds(Rect out, int orientation) {
3142 getBounds(out);
3143
3144 // Rotate the Rect if needed.
3145 final int currentRotation = mDisplayInfo.rotation;
3146 final int rotationDelta = deltaRotation(currentRotation, orientation);
3147 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
3148 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
3149 mTmpRectF.set(out);
3150 mTmpMatrix.mapRect(mTmpRectF);
3151 mTmpRectF.round(out);
3152 }
3153 }
3154
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003155 void performLayout(boolean initial, boolean updateInputWindows) {
3156 if (!isLayoutNeeded()) {
3157 return;
3158 }
3159 clearLayoutNeeded();
3160
3161 final int dw = mDisplayInfo.logicalWidth;
3162 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003163 if (DEBUG_LAYOUT) {
3164 Slog.v(TAG, "-------------------------------------");
3165 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
3166 }
3167
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01003168 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
3169 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00003170 // TODO: Not sure if we really need to set the rotation here since we are updating from the
3171 // display info above...
3172 mDisplayFrames.mRotation = mRotation;
3173 mService.mPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003174 if (isDefaultDisplay) {
3175 // Not needed on non-default displays.
3176 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
3177 mService.mScreenRect.set(0, 0, dw, dh);
3178 }
3179
Adrian Roos5251b1d2018-03-23 18:57:43 +01003180 int seq = mLayoutSeq + 1;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003181 if (seq < 0) seq = 0;
Adrian Roos5251b1d2018-03-23 18:57:43 +01003182 mLayoutSeq = seq;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003183
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003184 // Used to indicate that we have processed the dream window and all additional windows are
3185 // behind it.
3186 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003187 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003188
3189 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003190 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003191
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003192 // Used to indicate that we have processed the dream window and all additional attached
3193 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003194 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003195 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003196
3197 // Now perform layout of attached windows, which usually depend on the position of the
3198 // window they are attached to. XXX does not deal with windows that are attached to windows
3199 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003200 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003201
3202 // Window frames may have changed. Tell the input dispatcher about it.
Arthur Hung95b38a92018-07-20 18:56:12 +08003203 mInputMonitor.layoutInputConsumers(dw, dh);
3204 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003205 if (updateInputWindows) {
Arthur Hung95b38a92018-07-20 18:56:12 +08003206 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003207 }
3208
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003209 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
3210 }
3211
3212 /**
3213 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
3214 * In portrait mode, it grabs the full screenshot.
3215 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003216 * @param config of the output bitmap
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003217 */
chaviw0315a1a2018-03-05 15:28:35 -08003218 Bitmap screenshotDisplayLocked(Bitmap.Config config) {
3219 if (!mService.mPolicy.isScreenOn()) {
3220 if (DEBUG_SCREENSHOT) {
3221 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003222 }
chaviw0315a1a2018-03-05 15:28:35 -08003223 return null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003224 }
chaviwfbe47df2017-11-10 16:14:49 -08003225
chaviw0315a1a2018-03-05 15:28:35 -08003226 int dw = mDisplayInfo.logicalWidth;
3227 int dh = mDisplayInfo.logicalHeight;
chaviwfbe47df2017-11-10 16:14:49 -08003228
chaviw0315a1a2018-03-05 15:28:35 -08003229 if (dw <= 0 || dh <= 0) {
3230 return null;
3231 }
chaviwfbe47df2017-11-10 16:14:49 -08003232
chaviw0315a1a2018-03-05 15:28:35 -08003233 final Rect frame = new Rect(0, 0, dw, dh);
chaviwfbe47df2017-11-10 16:14:49 -08003234
chaviw0315a1a2018-03-05 15:28:35 -08003235 // The screenshot API does not apply the current screen rotation.
3236 int rot = mDisplay.getRotation();
chaviwfbe47df2017-11-10 16:14:49 -08003237
chaviw0315a1a2018-03-05 15:28:35 -08003238 if (rot == ROTATION_90 || rot == ROTATION_270) {
3239 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3240 }
chaviwfbe47df2017-11-10 16:14:49 -08003241
chaviw0315a1a2018-03-05 15:28:35 -08003242 // SurfaceFlinger is not aware of orientation, so convert our logical
3243 // crop to SurfaceFlinger's portrait orientation.
3244 convertCropForSurfaceFlinger(frame, rot, dw, dh);
3245
3246 final ScreenRotationAnimation screenRotationAnimation =
3247 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3248 final boolean inRotation = screenRotationAnimation != null &&
3249 screenRotationAnimation.isAnimating();
3250 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
3251
3252 // TODO(b/68392460): We should screenshot Task controls directly
3253 // but it's difficult at the moment as the Task doesn't have the
3254 // correct size set.
3255 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, 0, 1, inRotation, rot);
3256 if (bitmap == null) {
3257 Slog.w(TAG_WM, "Failed to take screenshot");
3258 return null;
3259 }
3260
3261 // Create a copy of the screenshot that is immutable and backed in ashmem.
3262 // This greatly reduces the overhead of passing the bitmap between processes.
3263 final Bitmap ret = bitmap.createAshmemBitmap(config);
3264 bitmap.recycle();
3265 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003266 }
3267
3268 // TODO: Can this use createRotationMatrix()?
3269 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3270 if (rot == Surface.ROTATION_90) {
3271 final int tmp = crop.top;
3272 crop.top = dw - crop.right;
3273 crop.right = crop.bottom;
3274 crop.bottom = dw - crop.left;
3275 crop.left = tmp;
3276 } else if (rot == Surface.ROTATION_180) {
3277 int tmp = crop.top;
3278 crop.top = dh - crop.bottom;
3279 crop.bottom = dh - tmp;
3280 tmp = crop.right;
3281 crop.right = dw - crop.left;
3282 crop.left = dw - tmp;
3283 } else if (rot == Surface.ROTATION_270) {
3284 final int tmp = crop.top;
3285 crop.top = crop.left;
3286 crop.left = dh - crop.bottom;
3287 crop.bottom = crop.right;
3288 crop.right = dh - tmp;
3289 }
3290 }
3291
3292 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003293 // Used to indicate the layout is needed.
3294 mTmpWindow = null;
3295
3296 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003297 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003298 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003299 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003300 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003301 w.setDisplayLayoutNeeded();
3302 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003303 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003304
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003305 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003306 mService.mWindowPlacerLocked.performSurfacePlacement();
3307 }
3308 }
3309
Wale Ogunwale1666e312016-12-16 11:27:18 -08003310 void setExitingTokensHasVisible(boolean hasVisible) {
3311 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3312 mExitingTokens.get(i).hasVisible = hasVisible;
3313 }
3314
3315 // Initialize state of exiting applications.
3316 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3317 }
3318
3319 void removeExistingTokensIfPossible() {
3320 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3321 final WindowToken token = mExitingTokens.get(i);
3322 if (!token.hasVisible) {
3323 mExitingTokens.remove(i);
3324 }
3325 }
3326
3327 // Time to remove any exiting applications?
3328 mTaskStackContainers.removeExistingAppTokensIfPossible();
3329 }
3330
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003331 @Override
3332 void onDescendantOverrideConfigurationChanged() {
3333 setLayoutNeeded();
3334 mService.requestTraversal();
3335 }
3336
David Stevens9440dc82017-03-16 19:00:20 -07003337 boolean okToDisplay() {
3338 if (mDisplayId == DEFAULT_DISPLAY) {
3339 return !mService.mDisplayFrozen
3340 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3341 }
3342 return mDisplayInfo.state == Display.STATE_ON;
3343 }
3344
3345 boolean okToAnimate() {
3346 return okToDisplay() &&
3347 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3348 }
3349
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003350 static final class TaskForResizePointSearchResult {
3351 boolean searchDone;
3352 Task taskForResize;
3353
3354 void reset() {
3355 searchDone = false;
3356 taskForResize = null;
3357 }
3358 }
Robert Carr3b716242016-08-16 16:02:21 -07003359
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003360 private static final class ApplySurfaceChangesTransactionState {
3361 boolean displayHasContent;
3362 boolean obscured;
3363 boolean syswin;
3364 boolean focusDisplayed;
3365 float preferredRefreshRate;
3366 int preferredModeId;
3367
3368 void reset() {
3369 displayHasContent = false;
3370 obscured = false;
3371 syswin = false;
3372 focusDisplayed = false;
3373 preferredRefreshRate = 0;
3374 preferredModeId = 0;
3375 }
3376 }
3377
3378 private static final class ScreenshotApplicationState {
3379 WindowState appWin;
3380 int maxLayer;
3381 int minLayer;
3382 boolean screenshotReady;
3383
3384 void reset(boolean screenshotReady) {
3385 appWin = null;
3386 maxLayer = 0;
3387 minLayer = 0;
3388 this.screenshotReady = screenshotReady;
3389 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3390 }
3391 }
3392
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003393 /**
3394 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3395 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3396 * homogeneous children type which is currently required by sub-classes of
3397 * {@link WindowContainer} class.
3398 */
3399 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3400
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003401 DisplayChildWindowContainer(WindowManagerService service) {
3402 super(service);
3403 }
3404
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003405 @Override
3406 boolean fillsParent() {
3407 return true;
3408 }
3409
3410 @Override
3411 boolean isVisible() {
3412 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003413 }
3414 }
3415
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003416 /**
3417 * Window container class that contains all containers on this display relating to Apps.
3418 * I.e Activities.
3419 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003420 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003421 /**
3422 * A control placed at the appropriate level for transitions to occur.
3423 */
chaviw23ee71c2017-12-18 11:29:41 -08003424 SurfaceControl mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02003425 SurfaceControl mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02003426 SurfaceControl mHomeAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003427
Robert Carrf7a7ca82017-12-06 13:17:07 -08003428 /**
3429 * Given that the split-screen divider does not have an AppWindowToken, it
3430 * will have to live inside of a "NonAppWindowContainer", in particular
3431 * {@link DisplayContent#mAboveAppWindowsContainers}. However, in visual Z order
3432 * it will need to be interleaved with some of our children, appearing on top of
3433 * both docked stacks but underneath any assistant stacks.
3434 *
3435 * To solve this problem we have this anchor control, which will always exist so
3436 * we can always assign it the correct value in our {@link #assignChildLayers}.
3437 * Likewise since it always exists, {@link AboveAppWindowContainers} can always
3438 * assign the divider a layer relative to it. This way we prevent linking lifecycle
3439 * events between the two containers.
3440 */
3441 SurfaceControl mSplitScreenDividerAnchor = null;
3442
Wale Ogunwale61911492017-10-11 08:50:50 -07003443 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3444 // through the list to find them.
3445 private TaskStack mHomeStack = null;
3446 private TaskStack mPinnedStack = null;
3447 private TaskStack mSplitScreenPrimaryStack = null;
3448
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003449 TaskStackContainers(WindowManagerService service) {
3450 super(service);
3451 }
3452
Wale Ogunwale61911492017-10-11 08:50:50 -07003453 /**
3454 * Returns the topmost stack on the display that is compatible with the input windowing mode
3455 * and activity type. Null is no compatible stack on the display.
3456 */
3457 TaskStack getStack(int windowingMode, int activityType) {
3458 if (activityType == ACTIVITY_TYPE_HOME) {
3459 return mHomeStack;
3460 }
3461 if (windowingMode == WINDOWING_MODE_PINNED) {
3462 return mPinnedStack;
3463 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3464 return mSplitScreenPrimaryStack;
3465 }
3466 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3467 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003468 if (activityType == ACTIVITY_TYPE_UNDEFINED
3469 && windowingMode == stack.getWindowingMode()) {
3470 // Passing in undefined type means we want to match the topmost stack with the
3471 // windowing mode.
3472 return stack;
3473 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003474 if (stack.isCompatible(windowingMode, activityType)) {
3475 return stack;
3476 }
3477 }
3478 return null;
3479 }
3480
3481 @VisibleForTesting
3482 TaskStack getTopStack() {
3483 return mTaskStackContainers.getChildCount() > 0
3484 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3485 }
3486
3487 TaskStack getHomeStack() {
3488 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3489 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3490 }
3491 return mHomeStack;
3492 }
3493
3494 TaskStack getPinnedStack() {
3495 return mPinnedStack;
3496 }
3497
Matthew Ng64e77cf2017-10-31 14:01:31 -07003498 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003499 return mSplitScreenPrimaryStack;
3500 }
3501
Winson Chunge2d72172018-01-25 17:46:20 +00003502 ArrayList<Task> getVisibleTasks() {
3503 final ArrayList<Task> visibleTasks = new ArrayList<>();
3504 forAllTasks(task -> {
3505 if (task.isVisible()) {
3506 visibleTasks.add(task);
3507 }
3508 });
3509 return visibleTasks;
3510 }
3511
Andrii Kulian839def92016-11-02 10:58:58 -07003512 /**
3513 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003514 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003515 */
3516 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003517 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003518 addChild(stack, onTop);
3519 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003520 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003521
Wale Ogunwale61911492017-10-11 08:50:50 -07003522 void onStackWindowingModeChanged(TaskStack stack) {
3523 removeStackReferenceIfNeeded(stack);
3524 addStackReferenceIfNeeded(stack);
3525 if (stack == mPinnedStack && getTopStack() != stack) {
3526 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3527 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3528 }
3529 }
3530
3531 private void addStackReferenceIfNeeded(TaskStack stack) {
3532 if (stack.isActivityTypeHome()) {
3533 if (mHomeStack != null) {
Louis Chang7d0037c2018-08-13 12:42:06 +08003534 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
Wale Ogunwale61911492017-10-11 08:50:50 -07003535 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
Louis Chang7d0037c2018-08-13 12:42:06 +08003536
Wale Ogunwale61911492017-10-11 08:50:50 -07003537 }
Louis Chang7d0037c2018-08-13 12:42:06 +08003538 mHomeStack = stack;
Wale Ogunwale61911492017-10-11 08:50:50 -07003539 }
3540 final int windowingMode = stack.getWindowingMode();
3541 if (windowingMode == WINDOWING_MODE_PINNED) {
3542 if (mPinnedStack != null) {
3543 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3544 + mPinnedStack + " already exist on display=" + this
3545 + " stack=" + stack);
3546 }
3547 mPinnedStack = stack;
3548 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3549 if (mSplitScreenPrimaryStack != null) {
3550 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3551 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3552 + " already exist on display=" + this + " stack=" + stack);
3553 }
3554 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003555 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003556 }
3557 }
3558
3559 private void removeStackReferenceIfNeeded(TaskStack stack) {
3560 if (stack == mHomeStack) {
3561 mHomeStack = null;
3562 } else if (stack == mPinnedStack) {
3563 mPinnedStack = null;
3564 } else if (stack == mSplitScreenPrimaryStack) {
3565 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003566 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3567 mService.setDockedStackCreateStateLocked(
3568 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3569 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003570 }
Andrii Kulian839def92016-11-02 10:58:58 -07003571 }
3572
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003573 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003574 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3575 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003576 addChild(stack, addIndex);
3577 setLayoutNeeded();
3578 }
3579
Wale Ogunwale61911492017-10-11 08:50:50 -07003580 @Override
3581 protected void removeChild(TaskStack stack) {
3582 super.removeChild(stack);
3583 removeStackReferenceIfNeeded(stack);
3584 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003585
3586 @Override
3587 boolean isOnTop() {
3588 // Considered always on top
3589 return true;
3590 }
3591
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003592 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003593 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003594 if (child.getWindowConfiguration().isAlwaysOnTop()
3595 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003596 // This stack is always-on-top, override the default behavior.
3597 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3598
3599 // Moving to its current position, as we must call super but we don't want to
3600 // perform any meaningful action.
3601 final int currentPosition = mChildren.indexOf(child);
3602 super.positionChildAt(currentPosition, child, false /* includingParents */);
3603 return;
3604 }
3605
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003606 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3607 super.positionChildAt(targetPosition, child, includingParents);
3608
Riddle Hsu2f6e1742018-08-23 22:44:36 +08003609 if (includingParents) {
3610 // We still want to move the display of this stack container to top because even the
3611 // target position is adjusted to non-top, the intention of the condition is to have
3612 // higher z-order to gain focus (e.g. moving a task of a fullscreen stack to front
3613 // in a non-top display which is using picture-in-picture mode).
3614 final int topChildPosition = getChildCount() - 1;
3615 if (targetPosition < topChildPosition && position >= topChildPosition) {
3616 getParent().positionChildAt(POSITION_TOP, this /* child */,
3617 true /* includingParents */);
3618 }
3619 }
3620
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003621 setLayoutNeeded();
3622 }
3623
3624 /**
3625 * When stack is added or repositioned, find a proper position for it.
3626 * This will make sure that pinned stack always stays on top.
3627 * @param requestedPosition Position requested by caller.
3628 * @param stack Stack to be added or positioned.
3629 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3630 * @return The proper position for the stack.
3631 */
3632 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09003633 if (stack.inPinnedWindowingMode()) {
Kazuki Takisef85197b2018-06-18 18:18:36 +09003634 return POSITION_TOP;
Kazuki Takise148d00a2018-05-31 15:32:19 +09003635 }
3636
Kazuki Takisef85197b2018-06-18 18:18:36 +09003637 final int topChildPosition = mChildren.size() - 1;
3638 int belowAlwaysOnTopPosition = POSITION_BOTTOM;
3639 for (int i = topChildPosition; i >= 0; --i) {
3640 if (getStacks().get(i) != stack && !getStacks().get(i).isAlwaysOnTop()) {
3641 belowAlwaysOnTopPosition = i;
3642 break;
3643 }
3644 }
3645
3646 // The max possible position we can insert the stack at.
3647 int maxPosition = POSITION_TOP;
3648 // The min possible position we can insert the stack at.
3649 int minPosition = POSITION_BOTTOM;
3650
3651 if (stack.isAlwaysOnTop()) {
3652 if (hasPinnedStack()) {
3653 // Always-on-top stacks go below the pinned stack.
3654 maxPosition = getStacks().indexOf(mPinnedStack) - 1;
3655 }
3656 // Always-on-top stacks need to be above all other stacks.
3657 minPosition = belowAlwaysOnTopPosition !=
3658 POSITION_BOTTOM ? belowAlwaysOnTopPosition : topChildPosition;
3659 } else {
3660 // Other stacks need to be below the always-on-top stacks.
3661 maxPosition = belowAlwaysOnTopPosition !=
Arthur Hung928d8dc2018-07-18 15:38:05 +08003662 POSITION_BOTTOM ? belowAlwaysOnTopPosition : 0;
Kazuki Takisef85197b2018-06-18 18:18:36 +09003663 }
3664
3665 int targetPosition = requestedPosition;
3666 targetPosition = Math.min(targetPosition, maxPosition);
3667 targetPosition = Math.max(targetPosition, minPosition);
3668
3669 int prevPosition = getStacks().indexOf(stack);
3670 // The positions we calculated above (maxPosition, minPosition) do not take into
3671 // consideration the following edge cases.
3672 // 1) We need to adjust the position depending on the value "adding".
3673 // 2) When we are moving a stack to another position, we also need to adjust the
3674 // position depending on whether the stack is moving to a higher or lower position.
3675 if ((targetPosition != requestedPosition) &&
3676 (adding || targetPosition < prevPosition)) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09003677 targetPosition++;
3678 }
3679
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003680 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003681 }
3682
3683 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003684 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3685 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003686 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003687 if (super.forAllWindows(callback, traverseTopToBottom)) {
3688 return true;
3689 }
3690 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3691 return true;
3692 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003693 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003694 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3695 return true;
3696 }
3697 if (super.forAllWindows(callback, traverseTopToBottom)) {
3698 return true;
3699 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003700 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003701 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003702 }
3703
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003704 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003705 boolean traverseTopToBottom) {
3706 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3707 // app tokens.
3708 // TODO: Investigate if we need to continue to do this or if we can just process them
3709 // in-order.
3710 if (traverseTopToBottom) {
3711 for (int i = mChildren.size() - 1; i >= 0; --i) {
3712 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3713 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003714 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3715 traverseTopToBottom)) {
3716 return true;
3717 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003718 }
3719 }
3720 } else {
3721 final int count = mChildren.size();
3722 for (int i = 0; i < count; ++i) {
3723 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3724 final int appTokensCount = appTokens.size();
3725 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003726 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3727 traverseTopToBottom)) {
3728 return true;
3729 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003730 }
3731 }
3732 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003733 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003734 }
3735
Wale Ogunwale1666e312016-12-16 11:27:18 -08003736 void setExitingTokensHasVisible(boolean hasVisible) {
3737 for (int i = mChildren.size() - 1; i >= 0; --i) {
3738 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3739 for (int j = appTokens.size() - 1; j >= 0; --j) {
3740 appTokens.get(j).hasVisible = hasVisible;
3741 }
3742 }
3743 }
3744
3745 void removeExistingAppTokensIfPossible() {
3746 for (int i = mChildren.size() - 1; i >= 0; --i) {
3747 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3748 for (int j = appTokens.size() - 1; j >= 0; --j) {
3749 final AppWindowToken token = appTokens.get(j);
3750 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3751 && (!token.mIsExiting || token.isEmpty())) {
3752 // Make sure there is no animation running on this token, so any windows
3753 // associated with it will be removed as soon as their animations are
3754 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003755 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003756 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3757 "performLayout: App token exiting now removed" + token);
3758 token.removeIfPossible();
3759 }
3760 }
3761 }
3762 }
3763
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003764 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003765 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003766 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
3767 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003768 // Apps and their containers are not allowed to specify an orientation while the
Wale Ogunwaled0c225c2018-06-15 14:47:46 -07003769 // docked or freeform stack is visible...except for the home stack if the docked
3770 // stack is minimized and it actually set something and the bounds is different from
3771 // the display.
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003772 if (mHomeStack != null && mHomeStack.isVisible()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07003773 && mDividerControllerLocked.isMinimizedDock()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07003774 && !(mDividerControllerLocked.isHomeStackResizable()
3775 && mHomeStack.matchParentBounds())) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003776 final int orientation = mHomeStack.getOrientation();
3777 if (orientation != SCREEN_ORIENTATION_UNSET) {
3778 return orientation;
3779 }
3780 }
3781 return SCREEN_ORIENTATION_UNSPECIFIED;
3782 }
3783
3784 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07003785 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
3786 PackageManager.FEATURE_AUTOMOTIVE);
3787 if (isCar) {
3788 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
3789 // allow anything but the default orientation.
3790 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07003791 "Forcing UNSPECIFIED orientation in car for display id=" + mDisplayId
3792 + ". Ignoring " + orientation);
Dean Harding3e5a1522017-10-06 09:19:01 -07003793 return SCREEN_ORIENTATION_UNSPECIFIED;
3794 }
3795
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003796 if (orientation != SCREEN_ORIENTATION_UNSET
3797 && orientation != SCREEN_ORIENTATION_BEHIND) {
3798 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07003799 "App is requesting an orientation, return " + orientation
3800 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003801 return orientation;
3802 }
3803
3804 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07003805 "No app is requesting an orientation, return " + mLastOrientation
3806 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003807 // The next app has not been requested to be visible, so we keep the current orientation
3808 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003809 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003810 }
Robert Carrb1579c82017-09-05 14:54:47 -07003811
3812 @Override
3813 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carr2f8aa392018-01-31 14:46:51 -08003814 assignStackOrdering(t);
Robert Carrf7a7ca82017-12-06 13:17:07 -08003815
Robert Carr2f8aa392018-01-31 14:46:51 -08003816 for (int i = 0; i < mChildren.size(); i++) {
3817 final TaskStack s = mChildren.get(i);
3818 s.assignChildLayers(t);
3819 }
3820 }
3821
3822 void assignStackOrdering(SurfaceControl.Transaction t) {
Jorim Jaggibe418292018-03-26 16:14:12 +02003823
Robert Carrf7a7ca82017-12-06 13:17:07 -08003824 final int HOME_STACK_STATE = 0;
3825 final int NORMAL_STACK_STATE = 1;
3826 final int ALWAYS_ON_TOP_STATE = 2;
3827
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003828 int layer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08003829 int layerForAnimationLayer = 0;
Jorim Jaggibe418292018-03-26 16:14:12 +02003830 int layerForBoostedAnimationLayer = 0;
Jorim Jaggi391790622018-04-18 15:30:44 +02003831 int layerForHomeAnimationLayer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08003832
Robert Carrf7a7ca82017-12-06 13:17:07 -08003833 for (int state = 0; state <= ALWAYS_ON_TOP_STATE; state++) {
3834 for (int i = 0; i < mChildren.size(); i++) {
3835 final TaskStack s = mChildren.get(i);
Robert Carr2f8aa392018-01-31 14:46:51 -08003836 if (state == HOME_STACK_STATE && !s.isActivityTypeHome()) {
3837 continue;
3838 } else if (state == NORMAL_STACK_STATE && (s.isActivityTypeHome()
3839 || s.isAlwaysOnTop())) {
3840 continue;
3841 } else if (state == ALWAYS_ON_TOP_STATE && !s.isAlwaysOnTop()) {
3842 continue;
3843 }
3844 s.assignLayer(t, layer++);
3845 if (s.inSplitScreenWindowingMode() && mSplitScreenDividerAnchor != null) {
3846 t.setLayer(mSplitScreenDividerAnchor, layer++);
3847 }
Winson Chungd41f71d2018-03-16 15:26:07 -07003848 if ((s.isTaskAnimating() || s.isAppAnimating())
3849 && state != ALWAYS_ON_TOP_STATE) {
Robert Carr2f8aa392018-01-31 14:46:51 -08003850 // Ensure the animation layer ends up above the
3851 // highest animating stack and no higher.
3852 layerForAnimationLayer = layer++;
Robert Carrf7a7ca82017-12-06 13:17:07 -08003853 }
Jorim Jaggibe418292018-03-26 16:14:12 +02003854 if (state != ALWAYS_ON_TOP_STATE) {
3855 layerForBoostedAnimationLayer = layer++;
3856 }
Robert Carrf7a7ca82017-12-06 13:17:07 -08003857 }
Jorim Jaggi391790622018-04-18 15:30:44 +02003858 if (state == HOME_STACK_STATE) {
3859 layerForHomeAnimationLayer = layer++;
3860 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003861 }
Robert Carr2f8aa392018-01-31 14:46:51 -08003862 if (mAppAnimationLayer != null) {
3863 t.setLayer(mAppAnimationLayer, layerForAnimationLayer);
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003864 }
Jorim Jaggibe418292018-03-26 16:14:12 +02003865 if (mBoostedAppAnimationLayer != null) {
3866 t.setLayer(mBoostedAppAnimationLayer, layerForBoostedAnimationLayer);
3867 }
Jorim Jaggi391790622018-04-18 15:30:44 +02003868 if (mHomeAppAnimationLayer != null) {
3869 t.setLayer(mHomeAppAnimationLayer, layerForHomeAnimationLayer);
3870 }
Robert Carrb1579c82017-09-05 14:54:47 -07003871 }
3872
3873 @Override
Jorim Jaggi391790622018-04-18 15:30:44 +02003874 SurfaceControl getAppAnimationLayer(@AnimationLayer int animationLayer) {
3875 switch (animationLayer) {
3876 case ANIMATION_LAYER_BOOSTED:
3877 return mBoostedAppAnimationLayer;
3878 case ANIMATION_LAYER_HOME:
3879 return mHomeAppAnimationLayer;
3880 case ANIMATION_LAYER_STANDARD:
3881 default:
3882 return mAppAnimationLayer;
3883 }
chaviw23ee71c2017-12-18 11:29:41 -08003884 }
3885
Robert Carrf7a7ca82017-12-06 13:17:07 -08003886 SurfaceControl getSplitScreenDividerAnchor() {
3887 return mSplitScreenDividerAnchor;
3888 }
3889
chaviw23ee71c2017-12-18 11:29:41 -08003890 @Override
Robert Carrb1579c82017-09-05 14:54:47 -07003891 void onParentSet() {
3892 super.onParentSet();
3893 if (getParent() != null) {
chaviw23ee71c2017-12-18 11:29:41 -08003894 mAppAnimationLayer = makeChildSurface(null)
3895 .setName("animationLayer")
3896 .build();
Jorim Jaggibe418292018-03-26 16:14:12 +02003897 mBoostedAppAnimationLayer = makeChildSurface(null)
3898 .setName("boostedAnimationLayer")
3899 .build();
Jorim Jaggi391790622018-04-18 15:30:44 +02003900 mHomeAppAnimationLayer = makeChildSurface(null)
3901 .setName("homeAnimationLayer")
3902 .build();
Robert Carrf7a7ca82017-12-06 13:17:07 -08003903 mSplitScreenDividerAnchor = makeChildSurface(null)
3904 .setName("splitScreenDividerAnchor")
3905 .build();
3906 getPendingTransaction()
3907 .show(mAppAnimationLayer)
Jorim Jaggibe418292018-03-26 16:14:12 +02003908 .show(mBoostedAppAnimationLayer)
Jorim Jaggi391790622018-04-18 15:30:44 +02003909 .show(mHomeAppAnimationLayer)
Robert Carrf7a7ca82017-12-06 13:17:07 -08003910 .show(mSplitScreenDividerAnchor);
chaviw23ee71c2017-12-18 11:29:41 -08003911 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07003912 } else {
chaviw23ee71c2017-12-18 11:29:41 -08003913 mAppAnimationLayer.destroy();
3914 mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02003915 mBoostedAppAnimationLayer.destroy();
3916 mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02003917 mHomeAppAnimationLayer.destroy();
3918 mHomeAppAnimationLayer = null;
Robert Carrf7a7ca82017-12-06 13:17:07 -08003919 mSplitScreenDividerAnchor.destroy();
3920 mSplitScreenDividerAnchor = null;
Robert Carrb1579c82017-09-05 14:54:47 -07003921 }
3922 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003923 }
3924
Robert Carree4d4b92017-11-22 12:21:46 -08003925 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003926 AboveAppWindowContainers(String name, WindowManagerService service) {
3927 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08003928 }
3929
Robert Carrb9506032018-02-13 13:54:00 -08003930 @Override
Vishnu Nair83537a72018-07-19 21:27:48 -07003931 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
3932 final SurfaceControl.Builder builder = super.makeChildSurface(child);
3933 if (child instanceof WindowToken && ((WindowToken) child).mRoundedCornerOverlay) {
3934 // To draw above the ColorFade layer during the screen off transition, the
3935 // rounded corner overlays need to be at the root of the surface hierarchy.
3936 // TODO: move the ColorLayer into the display overlay layer such that this is not
3937 // necessary anymore.
3938 builder.setParent(null);
3939 }
3940 return builder;
3941 }
3942
3943 @Override
Robert Carrb9506032018-02-13 13:54:00 -08003944 void assignChildLayers(SurfaceControl.Transaction t) {
3945 assignChildLayers(t, null /* imeContainer */);
3946 }
3947
Robert Carree4d4b92017-11-22 12:21:46 -08003948 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
3949 boolean needAssignIme = imeContainer != null
3950 && imeContainer.getSurfaceControl() != null;
3951 for (int j = 0; j < mChildren.size(); ++j) {
3952 final WindowToken wt = mChildren.get(j);
Robert Carrf7a7ca82017-12-06 13:17:07 -08003953
3954 // See {@link mSplitScreenDividerAnchor}
3955 if (wt.windowType == TYPE_DOCK_DIVIDER) {
3956 wt.assignRelativeLayer(t, mTaskStackContainers.getSplitScreenDividerAnchor(), 1);
3957 continue;
3958 }
Vishnu Nair83537a72018-07-19 21:27:48 -07003959 if (wt.mRoundedCornerOverlay) {
3960 wt.assignLayer(t, WindowManagerPolicy.COLOR_FADE_LAYER + 1);
3961 continue;
3962 }
Robert Carree4d4b92017-11-22 12:21:46 -08003963 wt.assignLayer(t, j);
3964 wt.assignChildLayers(t);
3965
3966 int layer = mService.mPolicy.getWindowLayerFromTypeLw(
3967 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08003968
3969 if (needAssignIme && layer >= mService.mPolicy.getWindowLayerFromTypeLw(
3970 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003971 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08003972 needAssignIme = false;
3973 }
3974 }
3975 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003976 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08003977 }
3978 }
3979 }
3980
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003981 /**
3982 * Window container class that contains all containers on this display that are not related to
3983 * Apps. E.g. status bar.
3984 */
Robert Carree4d4b92017-11-22 12:21:46 -08003985 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07003986 /**
3987 * Compares two child window tokens returns -1 if the first is lesser than the second in
3988 * terms of z-order and 1 otherwise.
3989 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003990 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003991 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003992 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3993 token1.mOwnerCanManageAppTokens)
3994 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3995 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003996
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003997 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3998 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3999 return false;
4000 }
4001 final int req = w.mAttrs.screenOrientation;
4002 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
4003 || req == SCREEN_ORIENTATION_UNSET) {
4004 return false;
4005 }
4006 return true;
4007 };
4008
Wale Ogunwale3a931692016-11-02 16:49:48 -07004009 private final String mName;
chaviwf29223e2018-01-31 13:23:51 -08004010 private final Dimmer mDimmer = new Dimmer(this);
4011 private final Rect mTmpDimBoundsRect = new Rect();
4012
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004013 NonAppWindowContainers(String name, WindowManagerService service) {
4014 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07004015 mName = name;
4016 }
4017
4018 void addChild(WindowToken token) {
4019 addChild(token, mWindowComparator);
4020 }
4021
4022 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004023 int getOrientation() {
4024 final WindowManagerPolicy policy = mService.mPolicy;
4025 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004026 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004027
4028 if (win != null) {
4029 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004030 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004031 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07004032 if (mService.mKeyguardGoingAway) {
4033 // Keyguard can't affect the orientation if it is going away...
4034 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
4035 return SCREEN_ORIENTATION_UNSET;
4036 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004037 }
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004038 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req
4039 + " for display id=" + mDisplayId);
Andrii Kulian8ee72852017-03-10 10:36:45 -08004040 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004041 }
4042
Andrii Kulian8ee72852017-03-10 10:36:45 -08004043 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004044
Jorim Jaggi1c530592018-04-06 15:11:47 +02004045 // Only allow force setting the orientation when all unknown visibilities have been
4046 // resolved, as otherwise we just may be starting another occluding activity.
4047 final boolean isUnoccluding =
4048 mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
4049 && mService.mUnknownAppVisibilityController.allResolved();
4050 if (policy.isKeyguardShowingAndNotOccluded() || isUnoccluding) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004051 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004052 }
4053
4054 return SCREEN_ORIENTATION_UNSET;
4055 }
4056
4057 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07004058 String getName() {
4059 return mName;
4060 }
chaviwf29223e2018-01-31 13:23:51 -08004061
4062 @Override
4063 Dimmer getDimmer() {
4064 return mDimmer;
4065 }
4066
4067 @Override
4068 void prepareSurfaces() {
4069 mDimmer.resetDimStates();
4070 super.prepareSurfaces();
4071 getBounds(mTmpDimBoundsRect);
4072
4073 if (mDimmer.updateDims(getPendingTransaction(), mTmpDimBoundsRect)) {
4074 scheduleAnimation();
4075 }
4076 }
Robert Carr3b716242016-08-16 16:02:21 -07004077 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01004078
Robert Carr9034d962018-01-04 18:27:42 -08004079 private class NonMagnifiableWindowContainers extends NonAppWindowContainers {
4080 NonMagnifiableWindowContainers(String name, WindowManagerService service) {
4081 super(name, service);
4082 }
4083
4084 @Override
4085 void applyMagnificationSpec(Transaction t, MagnificationSpec spec) {
4086 }
4087 };
4088
Robert Carrb1579c82017-09-05 14:54:47 -07004089 SurfaceControl.Builder makeSurface(SurfaceSession s) {
4090 return mService.makeSurfaceBuilder(s)
4091 .setParent(mWindowingLayer);
4092 }
4093
4094 @Override
4095 SurfaceSession getSession() {
4096 return mSession;
4097 }
4098
4099 @Override
4100 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07004101 SurfaceSession s = child != null ? child.getSession() : getSession();
4102 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrb1579c82017-09-05 14:54:47 -07004103 b.setSize(mSurfaceSize, mSurfaceSize);
Robert Carrf59b8dd2017-10-02 18:58:36 -07004104
4105 if (child == null) {
4106 return b;
4107 }
4108
Robert Carree4d4b92017-11-22 12:21:46 -08004109 return b.setName(child.getName())
4110 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07004111 }
4112
4113 /**
4114 * The makeSurface variants are for use by the window-container
4115 * hierarchy. makeOverlay here is a function for various non windowing
4116 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
4117 * and other potpourii.
4118 */
4119 SurfaceControl.Builder makeOverlay() {
4120 return mService.makeSurfaceBuilder(mSession)
4121 .setParent(mOverlayLayer);
4122 }
4123
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004124 /**
4125 * Reparents the given surface to mOverlayLayer.
4126 */
4127 void reparentToOverlay(Transaction transaction, SurfaceControl surface) {
4128 transaction.reparent(surface, mOverlayLayer.getHandle());
4129 }
4130
Robert Carrb1579c82017-09-05 14:54:47 -07004131 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carr24be9ab2018-04-30 17:54:53 -07004132 if (spec.scale != 1.0) {
4133 mMagnificationSpec = spec;
4134 } else {
4135 mMagnificationSpec = null;
4136 }
4137
Robert Carrf59b8dd2017-10-02 18:58:36 -07004138 applyMagnificationSpec(getPendingTransaction(), spec);
4139 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07004140 }
4141
Robert Carr24be9ab2018-04-30 17:54:53 -07004142 void reapplyMagnificationSpec() {
4143 if (mMagnificationSpec != null) {
4144 applyMagnificationSpec(getPendingTransaction(), mMagnificationSpec);
4145 }
4146 }
4147
Robert Carrb1579c82017-09-05 14:54:47 -07004148 @Override
4149 void onParentSet() {
4150 // Since we are the top of the SurfaceControl hierarchy here
4151 // we create the root surfaces explicitly rather than chaining
4152 // up as the default implementation in onParentSet does. So we
4153 // explicitly do NOT call super here.
4154 }
4155
4156 @Override
4157 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07004158
4159 // These are layers as children of "mWindowingLayer"
4160 mBelowAppWindowsContainers.assignLayer(t, 0);
4161 mTaskStackContainers.assignLayer(t, 1);
4162 mAboveAppWindowsContainers.assignLayer(t, 2);
4163
4164 WindowState imeTarget = mService.mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08004165 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08004166
Robert Carree4d4b92017-11-22 12:21:46 -08004167 // In the case where we have an IME target that is not in split-screen
4168 // mode IME assignment is easy. We just need the IME to go directly above
4169 // the target. This way children of the target will naturally go above the IME
4170 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08004171 //
Robert Carree4d4b92017-11-22 12:21:46 -08004172 // In the case of split-screen windowing mode, we need to elevate the IME above the
4173 // docked divider while keeping the app itself below the docked divider, so instead
4174 // we use relative layering of the IME targets child windows, and place the
4175 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08004176 //
Robert Carr234b6332018-03-20 13:38:16 -07004177 // In the case the IME target is animating, the animation Z order may be different
4178 // than the WindowContainer Z order, so it's difficult to be sure we have the correct
4179 // IME target. In this case we just layer the IME over all transitions by placing it in the
4180 // above applications layer.
4181 //
Robert Carree4d4b92017-11-22 12:21:46 -08004182 // In the case where we have no IME target we assign it where it's base layer would
4183 // place it in the AboveAppWindowContainers.
Robert Carr234b6332018-03-20 13:38:16 -07004184 if (imeTarget != null && !(imeTarget.inSplitScreenWindowingMode()
4185 || imeTarget.mToken.isAppAnimating())
Robert Carree4d4b92017-11-22 12:21:46 -08004186 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004187 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07004188 // TODO: We need to use an extra level on the app surface to ensure
4189 // this is always above SurfaceView but always below attached window.
4190 1);
Robert Carree4d4b92017-11-22 12:21:46 -08004191 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07004192 }
4193
4194 // Above we have assigned layers to our children, now we ask them to assign
4195 // layers to their children.
4196 mBelowAppWindowsContainers.assignChildLayers(t);
4197 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08004198 mAboveAppWindowsContainers.assignChildLayers(t,
4199 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07004200 mImeWindowsContainers.assignChildLayers(t);
4201 }
4202
4203 /**
4204 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
4205 * that the IME target is one of the docked applications. We'd like the docked divider to be
4206 * above both of the applications, and we'd like the IME to be above the docked divider.
4207 * However we need child windows of the applications to be above the IME (Text drag handles).
4208 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
4209 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
4210 * with {@link #WindowState#assignLayer}
4211 */
4212 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
Robert Carrbb5de662018-04-26 13:29:51 -07004213 child.assignRelativeLayer(t, mImeWindowsContainers.getSurfaceControl(), 1);
Robert Carrb1579c82017-09-05 14:54:47 -07004214 }
4215
4216 @Override
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004217 void prepareSurfaces() {
4218 final ScreenRotationAnimation screenRotationAnimation =
4219 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
4220 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
4221 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
4222 mPendingTransaction.setMatrix(mWindowingLayer,
4223 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
4224 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
4225 mPendingTransaction.setPosition(mWindowingLayer,
4226 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
4227 mPendingTransaction.setAlpha(mWindowingLayer,
4228 screenRotationAnimation.getEnterTransformation().getAlpha());
4229 }
Chavi Weingartenb736e322018-02-23 00:27:54 +00004230
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004231 super.prepareSurfaces();
4232 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004233
Jorim Jaggibe418292018-03-26 16:14:12 +02004234 void assignStackOrdering() {
4235 mTaskStackContainers.assignStackOrdering(getPendingTransaction());
Robert Carr2f8aa392018-01-31 14:46:51 -08004236 }
Chavi Weingarten3a748552018-05-14 17:32:42 +00004237
4238 /**
4239 * Increment the deferral count to determine whether to update the IME target.
4240 */
4241 void deferUpdateImeTarget() {
4242 mDeferUpdateImeTargetCount++;
4243 }
4244
4245 /**
4246 * Decrement the deferral count to determine whether to update the IME target. If the count
4247 * reaches 0, a new ime target will get computed.
4248 */
4249 void continueUpdateImeTarget() {
4250 if (mDeferUpdateImeTargetCount == 0) {
4251 return;
4252 }
4253
4254 mDeferUpdateImeTargetCount--;
4255 if (mDeferUpdateImeTargetCount == 0) {
4256 computeImeTarget(true /* updateImeTarget */);
4257 }
4258 }
4259
4260 /**
4261 * @return Whether a new IME target should be computed.
4262 */
4263 private boolean canUpdateImeTarget() {
4264 return mDeferUpdateImeTargetCount == 0;
4265 }
Arthur Hung95b38a92018-07-20 18:56:12 +08004266
4267 InputMonitor getInputMonitor() {
4268 return mInputMonitor;
4269 }
Craig Mautner59c00972012-07-30 12:10:24 -07004270}