blob: ca2336029ed92cc9f98625ff781b4b8dc7d73a66 [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;
Tiger Huang1e5b10a2018-07-30 20:19:51 +080073import static com.android.server.wm.DisplayContentProto.FOCUSED_APP;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080074import static com.android.server.wm.DisplayContentProto.ID;
75import static com.android.server.wm.DisplayContentProto.IME_WINDOWS;
76import static com.android.server.wm.DisplayContentProto.PINNED_STACK_CONTROLLER;
77import static com.android.server.wm.DisplayContentProto.ROTATION;
78import static com.android.server.wm.DisplayContentProto.SCREEN_ROTATION_ANIMATION;
79import static com.android.server.wm.DisplayContentProto.STACKS;
Louis Chang7501e332018-08-20 13:08:39 +080080import static com.android.server.wm.DisplayContentProto.SURFACE_SIZE;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080081import static com.android.server.wm.DisplayContentProto.WINDOW_CONTAINER;
Wale Ogunwale1666e312016-12-16 11:27:18 -080082import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070083import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070084import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070085import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
86import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070087import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070088import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070089import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020090import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070091import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070092import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070093import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -080094import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070095import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070096import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070097import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070098import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080099import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700100import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800101import static com.android.server.wm.WindowManagerService.H.REPORT_FOCUS_CHANGE;
102import static com.android.server.wm.WindowManagerService.H.REPORT_LOSING_FOCUS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700103import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
104import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700105import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700106import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700107import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
108import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800109import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_PLACING_SURFACES;
110import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_REMOVING_FOCUS;
111import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_ASSIGN_LAYERS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700112import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700113import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700114import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700115import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700116import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700117import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700118import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700119import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700120import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700121
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700122import android.annotation.CallSuper;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700123import android.annotation.NonNull;
Dean Harding3e5a1522017-10-06 09:19:01 -0700124import android.content.pm.PackageManager;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700125import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700126import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700127import android.graphics.Bitmap;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700128import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800129import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700130import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700131import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100132import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700133import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700134import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700135import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700136import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700137import android.os.RemoteException;
138import android.os.SystemClock;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100139import android.os.Trace;
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800140import android.util.ArraySet;
Chong Zhang8e89b312015-09-09 15:09:30 -0700141import android.util.DisplayMetrics;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700142import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700143import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700144import android.view.Display;
Adrian Roos1cf585052018-01-03 18:43:27 +0100145import android.view.DisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700146import android.view.DisplayInfo;
Riddle Hsua4d6fa22018-08-11 00:50:39 +0800147import android.view.Gravity;
Arthur Hungbe5ce212018-09-13 18:41:56 +0800148import android.view.InputChannel;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700149import android.view.InputDevice;
Robert Carrb1579c82017-09-05 14:54:47 -0700150import android.view.MagnificationSpec;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700151import android.view.Surface;
152import android.view.SurfaceControl;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100153import android.view.SurfaceControl.Transaction;
Robert Carrb1579c82017-09-05 14:54:47 -0700154import android.view.SurfaceSession;
Arthur Hungbe5ce212018-09-13 18:41:56 +0800155import android.view.WindowManagerPolicyConstants.PointerEventListener;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700156
Bryce Lee48f4b572017-04-10 10:54:15 -0700157import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800158import com.android.internal.util.ToBooleanFunction;
Adrian Roose99bc052017-11-20 17:55:31 +0100159import com.android.server.policy.WindowManagerPolicy;
Issei Suzuki43190bd2018-08-20 17:28:41 +0200160import com.android.server.wm.utils.DisplayRotationUtil;
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100161import com.android.server.wm.utils.RotationCache;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100162import com.android.server.wm.utils.WmDisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700163
164import java.io.PrintWriter;
165import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700166import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700167import java.util.HashMap;
168import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700169import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700170import java.util.List;
Adrian Roos1cf585052018-01-03 18:43:27 +0100171import java.util.Objects;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800172import java.util.function.Consumer;
173import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700174
Craig Mautner59c00972012-07-30 12:10:24 -0700175/**
176 * Utility class for keeping track of the WindowStates and other pertinent contents of a
177 * particular Display.
178 *
179 * IMPORTANT: No method from this class should ever be used without holding
180 * WindowManagerService.mWindowMap.
181 */
Riddle Hsuad256a12018-07-18 16:11:30 +0800182class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer>
183 implements WindowManagerPolicy.DisplayContentInfo {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700184 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700185
186 /** Unique identifier of this stack. */
187 private final int mDisplayId;
188
Wale Ogunwale3a931692016-11-02 16:49:48 -0700189 /** The containers below are the only child containers the display can have. */
190 // Contains all window containers that are related to apps (Activities)
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100191 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers(mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700192 // Contains all non-app window containers that should be displayed above the app containers
193 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800194 private final AboveAppWindowContainers mAboveAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100195 new AboveAppWindowContainers("mAboveAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700196 // Contains all non-app window containers that should be displayed below the app containers
197 // (e.g. Wallpaper).
198 private final NonAppWindowContainers mBelowAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100199 new NonAppWindowContainers("mBelowAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700200 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
201 // 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 -0800202 // window containers together and move them in-sync if/when needed. We use a subclass of
203 // WindowContainer which is omitted from screen magnification, as the IME is never magnified.
204 private final NonMagnifiableWindowContainers mImeWindowsContainers =
205 new NonMagnifiableWindowContainers("mImeWindowsContainers", mService);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700206
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000207 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800208 private WindowState mTmpWindow2;
209 private WindowAnimator mTmpWindowAnimator;
210 private boolean mTmpRecoveringMemory;
211 private boolean mUpdateImeTarget;
212 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700213 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700214
Wale Ogunwale02319a62016-09-26 15:21:22 -0700215 // Mapping from a token IBinder to a WindowToken object on this display.
216 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
217
Andrii Kuliancd097992017-03-23 18:31:59 -0700218 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700219 int mInitialDisplayWidth = 0;
220 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700221 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700222
Adrian Roos1cf585052018-01-03 18:43:27 +0100223 DisplayCutout mInitialDisplayCutout;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100224 private final RotationCache<DisplayCutout, WmDisplayCutout> mDisplayCutoutCache
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100225 = new RotationCache<>(this::calculateDisplayCutoutForRotationUncached);
Adrian Roos1cf585052018-01-03 18:43:27 +0100226
Andrii Kuliancd097992017-03-23 18:31:59 -0700227 /**
228 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
229 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
230 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
231 */
Craig Mautner59c00972012-07-30 12:10:24 -0700232 int mBaseDisplayWidth = 0;
233 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700234 /**
235 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
236 * but can be set from Settings or via shell command "adb shell wm density".
237 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
238 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700239 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700240 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700241 private final DisplayInfo mDisplayInfo = new DisplayInfo();
242 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700243 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800244 private final DisplayPolicy mDisplayPolicy;
245 private DisplayRotation mDisplayRotation;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000246 DisplayFrames mDisplayFrames;
247
Andrii Kulian06d07d62017-03-14 11:11:47 -0700248 /**
249 * For default display it contains real metrics, empty for others.
250 * @see WindowManagerService#createWatermarkInTransaction()
251 */
252 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
253 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
254 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700255
Andrii Kulian06d07d62017-03-14 11:11:47 -0700256 /**
257 * Compat metrics computed based on {@link #mDisplayMetrics}.
258 * @see #updateDisplayAndOrientation(int)
259 */
260 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
261
262 /** The desired scaling factor for compatible apps. */
263 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700264
Andrii Kulian8ee72852017-03-10 10:36:45 -0800265 /**
266 * Current rotation of the display.
267 * Constants as per {@link android.view.Surface.Rotation}.
268 *
Robert Carrae606b42018-02-15 15:36:23 -0800269 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800270 */
271 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700272
Andrii Kulian8ee72852017-03-10 10:36:45 -0800273 /**
274 * Last applied orientation of the display.
275 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
276 *
277 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
278 */
279 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700280
Andrii Kulian8ee72852017-03-10 10:36:45 -0800281 /**
282 * Flag indicating that the application is receiving an orientation that has different metrics
283 * than it expected. E.g. Portrait instead of Landscape.
284 *
Robert Carrae606b42018-02-15 15:36:23 -0800285 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800286 */
287 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700288
Andrii Kulian8ee72852017-03-10 10:36:45 -0800289 /**
290 * Orientation forced by some window. If there is no visible window that specifies orientation
291 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
292 *
293 * @see NonAppWindowContainers#getOrientation()
294 */
295 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700296
Andrii Kulian8ee72852017-03-10 10:36:45 -0800297 /**
298 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
299 * occluded.
300 *
301 * @see NonAppWindowContainers#getOrientation()
302 */
303 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
304
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700305 /**
306 * Keep track of wallpaper visibility to notify changes.
307 */
308 private boolean mLastWallpaperVisible = false;
309
Andrii Kulian06d07d62017-03-14 11:11:47 -0700310 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700311
Craig Mautner39834192012-09-02 07:47:24 -0700312 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700313 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700314 int pendingLayoutChanges;
Riddle Hsu654a6f92018-07-13 22:59:36 +0800315 int mDeferredRotationPauseCount;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800316
Andrii Kulian839def92016-11-02 10:58:58 -0700317 // TODO(multi-display): remove some of the usages.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800318 @VisibleForTesting
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800319 boolean isDefaultDisplay;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800320
Andrii Kulianf0379de2018-03-14 16:24:07 -0700321 /**
322 * Flag indicating whether WindowManager should override info for this display in
323 * DisplayManager.
324 */
325 boolean mShouldOverrideDisplayConfiguration = true;
Craig Mautner39834192012-09-02 07:47:24 -0700326
Craig Mautnerdc548482014-02-05 13:35:24 -0800327 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700328 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800329
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700330 /** Detect user tapping outside of current focused task bounds .*/
331 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700332
Craig Mautner6601b7b2013-04-29 10:29:11 -0700333 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700334 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700335
Craig Mautner6601b7b2013-04-29 10:29:11 -0700336 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800337 private final Rect mTmpRect = new Rect();
338 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700339 private final RectF mTmpRectF = new RectF();
340 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800341 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700342
Issei Suzuki43190bd2018-08-20 17:28:41 +0200343
Bryce Leef3c6a472017-11-14 14:53:06 -0800344 /** Used for handing back size of display */
345 private final Rect mTmpBounds = new Rect();
346
Craig Mautner95da1082014-02-24 17:54:35 -0800347 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700348 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800349
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700350 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700351 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700352
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800353 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800354 /** A collection of windows that provide tap exclude regions inside of them. */
355 final ArraySet<WindowState> mTapExcludeProvidingWindows = new ArraySet<>();
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800356
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000357 private boolean mHaveBootMsg = false;
358 private boolean mHaveApp = false;
359 private boolean mHaveWallpaper = false;
360 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700361
362 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
363
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700364 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
365 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000366 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
367 new ApplySurfaceChangesTransactionState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700368
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700369 // True if this display is in the process of being removed. Used to determine if the removal of
370 // the display's direct children should be allowed.
371 private boolean mRemovingDisplay = false;
372
Bryce Leed1871262017-06-12 14:12:29 -0700373 // {@code false} if this display is in the processing of being created.
374 private boolean mDisplayReady = false;
375
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800376 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700377
Robert Carrb1579c82017-09-05 14:54:47 -0700378 private final SurfaceSession mSession = new SurfaceSession();
379
380 /**
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800381 * Window that is currently interacting with the user. This window is responsible for receiving
382 * key events and pointer events from the user.
383 */
384 WindowState mCurrentFocus = null;
385
386 /**
387 * The last focused window that we've notified the client that the focus is changed.
388 */
389 WindowState mLastFocus = null;
390
391 /**
392 * Windows that have lost input focus and are waiting for the new focus window to be displayed
393 * before they are told about this.
394 */
395 ArrayList<WindowState> mLosingFocus = new ArrayList<>();
396
397 /**
398 * The foreground app of this display. Windows below this app cannot be the focused window. If
399 * the user taps on the area outside of the task of the focused app, we will notify AM about the
400 * new task the user wants to interact with.
401 */
402 AppWindowToken mFocusedApp = null;
403
404 /** Windows added since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
405 final ArrayList<WindowState> mWinAddedSinceNullFocus = new ArrayList<>();
406
407 /** Windows removed since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
408 final ArrayList<WindowState> mWinRemovedSinceNullFocus = new ArrayList<>();
409
410 /**
Robert Carrb1579c82017-09-05 14:54:47 -0700411 * We organize all top-level Surfaces in to the following layers.
412 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800413 * and omitted from Screen-Magnification, for example the strict mode flash or
414 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700415 * {@link #mWindowingLayer} contains everything else.
416 */
417 private SurfaceControl mOverlayLayer;
418
419 /**
420 * See {@link #mOverlayLayer}
421 */
422 private SurfaceControl mWindowingLayer;
423
424 /**
425 * Specifies the size of the surfaces in {@link #mOverlayLayer} and {@link #mWindowingLayer}.
426 * <p>
427 * For these surfaces currently we use a surface based on the larger of width or height so we
428 * don't have to resize when rotating the display.
429 */
430 private int mSurfaceSize;
431
Adrian Roos5251b1d2018-03-23 18:57:43 +0100432 /**
433 * Sequence number for the current layout pass.
434 */
435 int mLayoutSeq = 0;
436
Chavi Weingarten3a748552018-05-14 17:32:42 +0000437 /**
438 * Specifies the count to determine whether to defer updating the IME target until ready.
439 */
440 private int mDeferUpdateImeTargetCount;
441
Jorim Jaggi9af095b2017-12-12 17:18:57 +0100442 /** Temporary float array to retrieve 3x3 matrix values. */
443 private final float[] mTmpFloats = new float[9];
444
Robert Carr24be9ab2018-04-30 17:54:53 -0700445 private MagnificationSpec mMagnificationSpec;
446
Arthur Hung95b38a92018-07-20 18:56:12 +0800447 private InputMonitor mInputMonitor;
448
Jorim Jaggif1292892018-09-10 11:58:13 +0200449 /** Caches the value whether told display manager that we have content. */
450 private boolean mLastHasContent;
451
Issei Suzuki43190bd2018-08-20 17:28:41 +0200452 private DisplayRotationUtil mRotationUtil = new DisplayRotationUtil();
453
lumark90120a82018-08-15 00:33:03 +0800454 /**
455 * The input method window for this display.
456 */
457 WindowState mInputMethodWindow;
458
Arthur Hungbe5ce212018-09-13 18:41:56 +0800459 private final PointerEventDispatcher mPointerEventDispatcher;
460
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800461 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
462 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800463 final AppWindowToken atoken = w.mAppToken;
464 if (winAnimator.mDrawState == READY_TO_SHOW) {
Jorim Jaggiaf0d6d22018-06-08 15:25:35 +0200465 if (atoken == null || atoken.canShowWindows()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800466 if (w.performShowLocked()) {
467 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
468 if (DEBUG_LAYOUT_REPEATS) {
469 mService.mWindowPlacerLocked.debugLayoutRepeats(
470 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
471 }
472 }
473 }
474 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800475 };
476
477 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
478 final WindowStateAnimator winAnimator = w.mWinAnimator;
479 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
480 return;
481 }
482
Jorim Jaggi8f520872018-08-14 17:00:20 +0200483 // If this window is animating, ensure the animation background is set.
484 final AnimationAdapter anim = w.mAppToken != null
485 ? w.mAppToken.getAnimation()
486 : w.getAnimation();
487 if (anim != null) {
488 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800489 if (color != 0) {
490 final TaskStack stack = w.getStack();
491 if (stack != null) {
492 stack.setAnimationBackground(winAnimator, color);
493 }
494 }
495 }
496 };
497
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800498 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
499 final int lostFocusUid = mTmpWindow.mOwnerUid;
500 final Handler handler = mService.mH;
501 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
502 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
503 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
504 w.mAttrs.hideTimeoutMilliseconds);
505 }
506 }
507 };
508
509 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800510 final AppWindowToken focusedApp = mFocusedApp;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800511 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
512 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
513
514 if (!w.canReceiveKeys()) {
515 return false;
516 }
517
518 final AppWindowToken wtoken = w.mAppToken;
519
520 // If this window's application has been removed, just skip it.
521 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
522 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
523 + (wtoken.removed ? "removed" : "sendingToBottom"));
524 return false;
525 }
526
527 if (focusedApp == null) {
528 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
529 + " using new focus @ " + w);
530 mTmpWindow = w;
531 return true;
532 }
533
534 if (!focusedApp.windowsAreFocusable()) {
535 // Current focused app windows aren't focusable...
536 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
537 + " focusable using new focus @ " + w);
538 mTmpWindow = w;
539 return true;
540 }
541
542 // Descend through all of the app tokens and find the first that either matches
543 // win.mAppToken (return win) or mFocusedApp (return null).
544 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
545 if (focusedApp.compareTo(wtoken) > 0) {
546 // App stack below focused app stack. No focus for you!!!
547 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
548 "findFocusedWindow: Reached focused app=" + focusedApp);
549 mTmpWindow = null;
550 return true;
551 }
552 }
553
554 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
555 mTmpWindow = w;
556 return true;
557 };
558
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800559 private final Consumer<WindowState> mPerformLayout = w -> {
560 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
561 // wasting time and funky changes while a window is animating away.
562 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
563 || w.isGoneForLayoutLw();
564
565 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
566 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
567 + " mLayoutAttached=" + w.mLayoutAttached
568 + " screen changed=" + w.isConfigChanged());
569 final AppWindowToken atoken = w.mAppToken;
570 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200571 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800572 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
573 + " parentHidden=" + w.isParentWindowHidden());
574 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200575 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800576 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
577 + " parentHidden=" + w.isParentWindowHidden());
578 }
579
580 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
581 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
582 // since that means "perform layout as normal, just don't display").
583 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
584 || ((w.isConfigChanged() || w.setReportResizeHints())
585 && !w.isGoneForLayoutLw() &&
586 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
587 (w.mHasSurface && w.mAppToken != null &&
588 w.mAppToken.layoutConfigChanges)))) {
589 if (!w.mLayoutAttached) {
590 if (mTmpInitial) {
591 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700592 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800593 }
594 if (w.mAttrs.type == TYPE_DREAM) {
595 // Don't layout windows behind a dream, so that if it does stuff like hide
596 // the status bar we won't get a bad transition when it goes away.
597 mTmpWindow = w;
598 }
599 w.mLayoutNeeded = false;
600 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200601 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000602 mService.mPolicy.layoutWindowLw(w, null, mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100603 w.mLayoutSeq = mLayoutSeq;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800604
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200605 // If this is the first layout, we need to initialize the last inset values as
606 // otherwise we'd immediately cause an unnecessary resize.
607 if (firstLayout) {
608 w.updateLastInsetValues();
609 }
610
Adrian Roos23df3a32018-03-15 15:41:13 +0100611 if (w.mAppToken != null) {
612 w.mAppToken.layoutLetterbox(w);
613 }
614
chaviw492139a2018-07-16 16:07:35 -0700615 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700616 + " mContainingFrame=" + w.getContainingFrame()
617 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800618 }
619 }
620 };
621
622 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
623 if (w.mLayoutAttached) {
624 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
625 + " mViewVisibility=" + w.mViewVisibility
626 + " mRelayoutCalled=" + w.mRelayoutCalled);
627 // If this view is GONE, then skip it -- keep the current frame, and let the caller
628 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
629 // windows, since that means "perform layout as normal, just don't display").
630 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
631 return;
632 }
633 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
634 || w.mLayoutNeeded) {
635 if (mTmpInitial) {
636 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700637 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800638 }
639 w.mLayoutNeeded = false;
640 w.prelayout();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000641 mService.mPolicy.layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100642 w.mLayoutSeq = mLayoutSeq;
chaviw492139a2018-07-16 16:07:35 -0700643 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700644 + " mContainingFrame=" + w.getContainingFrame()
645 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800646 }
647 } else if (w.mAttrs.type == TYPE_DREAM) {
648 // Don't layout windows behind a dream, so that if it does stuff like hide the
649 // status bar we won't get a bad transition when it goes away.
650 mTmpWindow = mTmpWindow2;
651 }
652 };
653
654 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
655 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
656 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
657 return w.canBeImeTarget();
658 };
659
660 private final Consumer<WindowState> mApplyPostLayoutPolicy =
661 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
662 mService.mInputMethodTarget);
663
664 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
665 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
666 final boolean obscuredChanged = w.mObscured !=
667 mTmpApplySurfaceChangesTransactionState.obscured;
668 final RootWindowContainer root = mService.mRoot;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800669
670 // Update effect.
671 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
672 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
673 final boolean isDisplayed = w.isDisplayedLw();
674
675 if (isDisplayed && w.isObscuringDisplay()) {
676 // This window completely covers everything behind it, so we want to leave all
677 // of them as undimmed (for performance reasons).
678 root.mObscuringWindow = w;
679 mTmpApplySurfaceChangesTransactionState.obscured = true;
680 }
681
682 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
683 root.handleNotObscuredLocked(w,
684 mTmpApplySurfaceChangesTransactionState.obscured,
685 mTmpApplySurfaceChangesTransactionState.syswin);
686
687 if (w.mHasSurface && isDisplayed) {
688 final int type = w.mAttrs.type;
689 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
690 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
691 mTmpApplySurfaceChangesTransactionState.syswin = true;
692 }
693 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
694 && w.mAttrs.preferredRefreshRate != 0) {
695 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
696 = w.mAttrs.preferredRefreshRate;
697 }
698 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
699 && w.mAttrs.preferredDisplayModeId != 0) {
700 mTmpApplySurfaceChangesTransactionState.preferredModeId
701 = w.mAttrs.preferredDisplayModeId;
702 }
703 }
704 }
705
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800706 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
707 && mWallpaperController.isWallpaperTarget(w)) {
708 // This is the wallpaper target and its obscured state changed... make sure the
709 // current wallpaper's visibility has been updated accordingly.
710 mWallpaperController.updateWallpaperVisibility();
711 }
712
chaviw161ea3e2018-01-31 12:01:12 -0800713 w.handleWindowMovedIfNeeded();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800714
715 final WindowStateAnimator winAnimator = w.mWinAnimator;
716
717 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
chaviw1454b392018-08-06 09:54:04 -0700718 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800719
720 // Moved from updateWindowsAndWallpaperLocked().
721 if (w.mHasSurface) {
722 // Take care of the window being ready to display.
723 final boolean committed = winAnimator.commitFinishDrawingLocked();
724 if (isDefaultDisplay && committed) {
725 if (w.mAttrs.type == TYPE_DREAM) {
726 // HACK: When a dream is shown, it may at that point hide the lock screen.
727 // So we need to redo the layout to let the phone window manager make this
728 // happen.
729 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
730 if (DEBUG_LAYOUT_REPEATS) {
731 surfacePlacer.debugLayoutRepeats(
732 "dream and commitFinishDrawingLocked true",
733 pendingLayoutChanges);
734 }
735 }
736 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
737 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
738 "First draw done in potential wallpaper target " + w);
739 root.mWallpaperMayChange = true;
740 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
741 if (DEBUG_LAYOUT_REPEATS) {
742 surfacePlacer.debugLayoutRepeats(
743 "wallpaper and commitFinishDrawingLocked true",
744 pendingLayoutChanges);
745 }
746 }
747 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800748 }
749
750 final AppWindowToken atoken = w.mAppToken;
751 if (atoken != null) {
Adrian Roos23df3a32018-03-15 15:41:13 +0100752 atoken.updateLetterboxSurface(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800753 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
754 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
755 mTmpUpdateAllDrawn.add(atoken);
756 }
757 }
758
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800759 if (!mLosingFocus.isEmpty() && w.isFocused() && w.isDisplayedLw()) {
760 mService.mH.obtainMessage(REPORT_LOSING_FOCUS, this).sendToTarget();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800761 }
762
763 w.updateResizingWindowIfNeeded();
764 };
765
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800766 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800767 * Create new {@link DisplayContent} instance, add itself to the root window container and
768 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700769 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800770 * @param service You know.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700771 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
772 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700773 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700774 DisplayContent(Display display, WindowManagerService service,
Bryce Leef19cbe22018-02-02 15:09:21 -0800775 WallpaperController wallpaperController, DisplayWindowController controller) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100776 super(service);
Bryce Leef19cbe22018-02-02 15:09:21 -0800777 setController(controller);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800778 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
779 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
780 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
781 + " new=" + display);
782 }
783
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700784 mDisplay = display;
785 mDisplayId = display.getDisplayId();
Wale Ogunwale0303c572016-10-20 10:16:29 -0700786 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700787 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700788 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700789 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100790 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo,
791 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700792 initializeDisplayBaseInfo();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800793 mDisplayPolicy = new DisplayPolicy(service);
794 mDisplayRotation = new DisplayRotation(service, this);
795 if (isDefaultDisplay) {
796 // The policy may be invoked right after here, so it requires the necessary default
797 // fields of this display content.
798 mService.mPolicy.setDefaultDisplay(this);
799 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800800 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700801 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700802
Robert Carr74a66a22018-02-23 12:17:51 -0800803 // We use this as our arbitrary surface size for buffer-less parents
804 // that don't impose cropping on their children. It may need to be larger
805 // than the display size because fullscreen windows can be shifted offscreen
806 // due to surfaceInsets. 2 times the largest display dimension feels like an
807 // appropriately arbitrary number. Eventually we would like to give SurfaceFlinger
808 // layers the ability to match their parent sizes and be able to skip
809 // such arbitrary size settings.
810 mSurfaceSize = Math.max(mBaseDisplayHeight, mBaseDisplayWidth) * 2;
Robert Carrb1579c82017-09-05 14:54:47 -0700811
812 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession)
813 .setSize(mSurfaceSize, mSurfaceSize)
814 .setOpaque(true);
815 mWindowingLayer = b.setName("Display Root").build();
816 mOverlayLayer = b.setName("Display Overlays").build();
817
Robert Carrf59b8dd2017-10-02 18:58:36 -0700818 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700819 .setLayerStack(mWindowingLayer, mDisplayId)
820 .show(mWindowingLayer)
821 .setLayer(mOverlayLayer, 1)
822 .setLayerStack(mOverlayLayer, mDisplayId)
823 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700824 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700825
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700826 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700827 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700828 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700829 super.addChild(mAboveAppWindowsContainers, null);
830 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800831
832 // Add itself as a child to the root container.
833 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700834
835 // TODO(b/62541591): evaluate whether this is the best spot to declare the
836 // {@link DisplayContent} ready for use.
837 mDisplayReady = true;
Arthur Hung95b38a92018-07-20 18:56:12 +0800838
Arthur Hung39134b22018-08-14 11:58:28 +0800839 mInputMonitor = new InputMonitor(service, mDisplayId);
Arthur Hungbe5ce212018-09-13 18:41:56 +0800840
841 if (mService.mInputManager != null) {
842 final InputChannel inputChannel = mService.mInputManager.monitorInput("Display "
843 + mDisplayId, mDisplayId);
844 mPointerEventDispatcher = inputChannel != null
845 ? new PointerEventDispatcher(inputChannel) : null;
846 } else {
847 mPointerEventDispatcher = null;
848 }
Bryce Leed1871262017-06-12 14:12:29 -0700849 }
850
851 boolean isReady() {
852 // The display is ready when the system and the individual display are both ready.
853 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700854 }
855
856 int getDisplayId() {
857 return mDisplayId;
858 }
859
Wale Ogunwale02319a62016-09-26 15:21:22 -0700860 WindowToken getWindowToken(IBinder binder) {
861 return mTokenMap.get(binder);
862 }
863
864 AppWindowToken getAppWindowToken(IBinder binder) {
865 final WindowToken token = getWindowToken(binder);
866 if (token == null) {
867 return null;
868 }
869 return token.asAppWindowToken();
870 }
871
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700872 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700873 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
874 if (dc != null) {
875 // We currently don't support adding a window token to the display if the display
876 // already has the binder mapped to another token. If there is a use case for supporting
877 // this moving forward we will either need to merge the WindowTokens some how or have
878 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700879 throw new IllegalArgumentException("Can't map token=" + token + " to display="
880 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700881 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700882 if (binder == null) {
883 throw new IllegalArgumentException("Can't map token=" + token + " to display="
884 + getName() + " binder is null");
885 }
886 if (token == null) {
887 throw new IllegalArgumentException("Can't map null token to display="
888 + getName() + " binder=" + binder);
889 }
890
Wale Ogunwale02319a62016-09-26 15:21:22 -0700891 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700892
893 if (token.asAppWindowToken() == null) {
894 // Add non-app token to container hierarchy on the display. App tokens are added through
895 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700896 switch (token.windowType) {
897 case TYPE_WALLPAPER:
898 mBelowAppWindowsContainers.addChild(token);
899 break;
900 case TYPE_INPUT_METHOD:
901 case TYPE_INPUT_METHOD_DIALOG:
902 mImeWindowsContainers.addChild(token);
903 break;
904 default:
905 mAboveAppWindowsContainers.addChild(token);
906 break;
907 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700908 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700909 }
910
911 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700912 final WindowToken token = mTokenMap.remove(binder);
913 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800914 token.setExiting();
915 }
916 return token;
917 }
918
919 /** Changes the display the input window token is housed on to this one. */
920 void reParentWindowToken(WindowToken token) {
921 final DisplayContent prevDc = token.getDisplayContent();
922 if (prevDc == this) {
923 return;
924 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800925 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
926 && token.asAppWindowToken() == null) {
927 // Removed the token from the map, but made sure it's not an app token before removing
928 // from parent.
929 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700930 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800931
932 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700933 }
934
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700935 void removeAppToken(IBinder binder) {
936 final WindowToken token = removeWindowToken(binder);
937 if (token == null) {
938 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
939 return;
940 }
941
942 final AppWindowToken appToken = token.asAppWindowToken();
943
944 if (appToken == null) {
945 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
946 return;
947 }
948
949 appToken.onRemovedFromDisplay();
950 }
951
Riddle Hsuad256a12018-07-18 16:11:30 +0800952 @Override
Riddle Hsu86cb7de2018-08-13 23:29:58 +0800953 DisplayWindowController getController() {
954 return (DisplayWindowController) super.getController();
955 }
956
957 @Override
Riddle Hsuad256a12018-07-18 16:11:30 +0800958 public Display getDisplay() {
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700959 return mDisplay;
960 }
961
Craig Mautner59c00972012-07-30 12:10:24 -0700962 DisplayInfo getDisplayInfo() {
963 return mDisplayInfo;
964 }
965
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700966 DisplayMetrics getDisplayMetrics() {
967 return mDisplayMetrics;
968 }
969
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800970 DisplayPolicy getDisplayPolicy() {
971 return mDisplayPolicy;
972 }
973
Riddle Hsuad256a12018-07-18 16:11:30 +0800974 @Override
975 public DisplayRotation getDisplayRotation() {
976 return mDisplayRotation;
977 }
978
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800979 @VisibleForTesting
980 void setDisplayRotation(DisplayRotation displayRotation) {
981 mDisplayRotation = displayRotation;
982 }
983
Andrii Kulian8ee72852017-03-10 10:36:45 -0800984 int getRotation() {
985 return mRotation;
986 }
987
Andrii Kulianef5ce1c2018-03-20 19:27:24 -0700988 @VisibleForTesting
Andrii Kulian8ee72852017-03-10 10:36:45 -0800989 void setRotation(int newRotation) {
990 mRotation = newRotation;
Riddle Hsuad256a12018-07-18 16:11:30 +0800991 mDisplayRotation.setRotation(newRotation);
Andrii Kulian8ee72852017-03-10 10:36:45 -0800992 }
993
994 int getLastOrientation() {
995 return mLastOrientation;
996 }
997
998 void setLastOrientation(int orientation) {
999 mLastOrientation = orientation;
1000 }
1001
1002 boolean getAltOrientation() {
1003 return mAltOrientation;
1004 }
1005
1006 void setAltOrientation(boolean altOrientation) {
1007 mAltOrientation = altOrientation;
1008 }
1009
1010 int getLastWindowForcedOrientation() {
1011 return mLastWindowForcedOrientation;
1012 }
1013
Andrii Kulian06d07d62017-03-14 11:11:47 -07001014 /**
Riddle Hsu654a6f92018-07-13 22:59:36 +08001015 * Temporarily pauses rotation changes until resumed.
1016 *
1017 * This can be used to prevent rotation changes from occurring while the user is
1018 * performing certain operations, such as drag and drop.
1019 *
1020 * This call nests and must be matched by an equal number of calls to
1021 * {@link #resumeRotationLocked}.
1022 */
1023 void pauseRotationLocked() {
1024 mDeferredRotationPauseCount++;
1025 }
1026
1027 /**
1028 * Resumes normal rotation changes after being paused.
1029 */
1030 void resumeRotationLocked() {
1031 if (mDeferredRotationPauseCount <= 0) {
1032 return;
1033 }
1034
1035 mDeferredRotationPauseCount--;
1036 if (mDeferredRotationPauseCount == 0) {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001037 updateRotationAndSendNewConfigIfNeeded();
Riddle Hsu654a6f92018-07-13 22:59:36 +08001038 }
1039 }
1040
1041 /**
Riddle Hsuad256a12018-07-18 16:11:30 +08001042 * If this is true we have updated our desired orientation, but not yet changed the real
1043 * orientation our applied our screen rotation animation. For example, because a previous
1044 * screen rotation was in progress.
1045 *
1046 * @return {@code true} if the there is an ongoing rotation change.
1047 */
1048 boolean rotationNeedsUpdate() {
1049 final int lastOrientation = getLastOrientation();
1050 final int oldRotation = getRotation();
1051 final boolean oldAltOrientation = getAltOrientation();
1052
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001053 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Riddle Hsuad256a12018-07-18 16:11:30 +08001054 final boolean altOrientation = !mDisplayRotation.rotationHasCompatibleMetrics(
1055 lastOrientation, rotation);
1056 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1057 return false;
1058 }
1059 return true;
1060 }
1061
1062 /**
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001063 * Update rotation of the display and send configuration if the rotation is changed.
1064 *
1065 * @return {@code true} if the rotation has been changed and the new config is sent.
1066 */
1067 boolean updateRotationAndSendNewConfigIfNeeded() {
1068 final boolean changed = updateRotationUnchecked(false /* forceUpdate */);
1069 if (changed) {
1070 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
1071 }
1072 return changed;
1073 }
1074
1075 /**
Andrii Kulian06d07d62017-03-14 11:11:47 -07001076 * Update rotation of the display.
1077 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001078 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
1079 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001080 */
Robert Carrae606b42018-02-15 15:36:23 -08001081 boolean updateRotationUnchecked() {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001082 return updateRotationUnchecked(false /* forceUpdate */);
1083 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001084
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001085 /**
1086 * Update rotation of the display with an option to force the update.
1087 * @param forceUpdate Force the rotation update. Sometimes in WM we might skip updating
1088 * orientation because we're waiting for some rotation to finish or display
1089 * to unfreeze, which results in configuration of the previously visible
1090 * activity being applied to a newly visible one. Forcing the rotation
1091 * update allows to workaround this issue.
1092 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
1093 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
1094 */
1095 boolean updateRotationUnchecked(boolean forceUpdate) {
1096 ScreenRotationAnimation screenRotationAnimation;
1097 if (!forceUpdate) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001098 if (mDeferredRotationPauseCount > 0) {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001099 // Rotation updates have been paused temporarily. Defer the update until
1100 // updates have been resumed.
1101 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
1102 return false;
1103 }
1104
1105 screenRotationAnimation =
1106 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
1107 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
1108 // Rotation updates cannot be performed while the previous rotation change
1109 // animation is still in progress. Skip this update. We will try updating
1110 // again after the animation is finished and the display is unfrozen.
1111 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
1112 return false;
1113 }
1114 if (mService.mDisplayFrozen) {
1115 // Even if the screen rotation animation has finished (e.g. isAnimating
1116 // returns false), there is still some time where we haven't yet unfrozen
1117 // the display. We also need to abort rotation here.
1118 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
1119 "Deferring rotation, still finishing previous rotation");
1120 return false;
1121 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001122 }
1123
1124 if (!mService.mDisplayEnabled) {
1125 // No point choosing a rotation if the display is not enabled.
1126 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
1127 return false;
1128 }
1129
1130 final int oldRotation = mRotation;
1131 final int lastOrientation = mLastOrientation;
1132 final boolean oldAltOrientation = mAltOrientation;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001133 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001134 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Computed rotation=" + rotation + " for display id="
1135 + mDisplayId + " based on lastOrientation=" + lastOrientation
1136 + " and oldRotation=" + oldRotation);
Riddle Hsuad256a12018-07-18 16:11:30 +08001137 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(mDisplayRotation,
1138 oldRotation, rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001139
Robert Carr0e007272017-10-02 13:00:55 -07001140 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001141 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001142 if (seamlessRotated != null && !forceUpdate) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001143 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
1144 // to complete (that is, waiting for windows to redraw). It's tempting to check
1145 // w.mSeamlessRotationCount but that could be incorrect in the case of
1146 // window-removal.
1147 return false;
1148 }
Robert Carr0e007272017-10-02 13:00:55 -07001149
1150 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001151 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -07001152 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -07001153 mayRotateSeamlessly = false;
1154 }
1155 for (int i = 0; i < mService.mSessions.size(); i++) {
1156 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
1157 mayRotateSeamlessly = false;
1158 break;
1159 }
1160 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001161 }
Robert Carr0e007272017-10-02 13:00:55 -07001162 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001163
1164 // TODO: Implement forced rotation changes.
1165 // Set mAltOrientation to indicate that the application is receiving
1166 // an orientation that has different metrics than it expected.
1167 // eg. Portrait instead of Landscape.
1168
Riddle Hsuad256a12018-07-18 16:11:30 +08001169 final boolean altOrientation = !mDisplayRotation.rotationHasCompatibleMetrics(
Andrii Kulian06d07d62017-03-14 11:11:47 -07001170 lastOrientation, rotation);
1171
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001172 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1173 + " selected orientation " + lastOrientation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001174 + ", got rotation " + rotation + " which has "
1175 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1176
1177 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1178 // No change.
1179 return false;
1180 }
1181
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001182 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1183 + " rotation changed to " + rotation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001184 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1185 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1186
1187 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1188 mService.mWaitingForConfig = true;
1189 }
1190
Riddle Hsuad256a12018-07-18 16:11:30 +08001191 setRotation(rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001192 mAltOrientation = altOrientation;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001193
1194 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
Riddle Hsu654a6f92018-07-13 22:59:36 +08001195 mService.mH.sendNewMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1196 this, WINDOW_FREEZE_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001197
1198 setLayoutNeeded();
1199 final int[] anim = new int[2];
Robert Carrf59b8dd2017-10-02 18:58:36 -07001200 mService.mPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001201
1202 if (!rotateSeamlessly) {
Robert Carrae606b42018-02-15 15:36:23 -08001203 mService.startFreezingDisplayLocked(anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001204 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1205 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1206 mDisplayId);
1207 } else {
1208 // The screen rotation animation uses a screenshot to freeze the screen
1209 // while windows resize underneath.
1210 // When we are rotating seamlessly, we allow the elements to transition
1211 // to their rotated state independently and without a freeze required.
1212 screenRotationAnimation = null;
1213
Robert Carrefc75702018-02-16 11:46:16 -08001214 mService.startSeamlessRotation();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001215 }
1216
1217 // We need to update our screen size information to match the new rotation. If the rotation
1218 // has actually changed then this method will return true and, according to the comment at
1219 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1220 // By updating the Display info here it will be available to
1221 // #computeScreenConfiguration() later.
1222 updateDisplayAndOrientation(getConfiguration().uiMode);
1223
Robert Carrae606b42018-02-15 15:36:23 -08001224 // NOTE: We disable the rotation in the emulator because
1225 // it doesn't support hardware OpenGL emulation yet.
1226 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1227 && screenRotationAnimation.hasScreenshot()) {
1228 if (screenRotationAnimation.setRotation(getPendingTransaction(), rotation,
1229 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1230 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1231 mService.scheduleAnimationLocked();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001232 }
1233 }
1234
Vishnu Nair83537a72018-07-19 21:27:48 -07001235 forAllWindows(w -> {
Vishnu Nairddd80742018-08-21 14:12:46 -07001236 w.seamlesslyRotateIfAllowed(getPendingTransaction(), oldRotation, rotation,
1237 rotateSeamlessly);
Vishnu Nair83537a72018-07-19 21:27:48 -07001238 }, true /* traverseTopToBottom */);
1239
Robert Carrae606b42018-02-15 15:36:23 -08001240 mService.mDisplayManagerInternal.performTraversal(getPendingTransaction());
1241 scheduleAnimation();
1242
Andrii Kulian06d07d62017-03-14 11:11:47 -07001243 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001244 if (w.mHasSurface && !rotateSeamlessly) {
1245 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001246 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001247 mService.mRoot.mOrientationChangeComplete = false;
1248 w.mLastFreezeDuration = 0;
1249 }
1250 w.mReportOrientationChanged = true;
1251 }, true /* traverseTopToBottom */);
1252
1253 if (rotateSeamlessly) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001254 mService.mH.sendNewMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1255 this, SEAMLESS_ROTATION_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001256 }
1257
1258 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1259 final WindowManagerService.RotationWatcher rotationWatcher
1260 = mService.mRotationWatchers.get(i);
1261 if (rotationWatcher.mDisplayId == mDisplayId) {
1262 try {
1263 rotationWatcher.mWatcher.onRotationChanged(rotation);
1264 } catch (RemoteException e) {
1265 // Ignore
1266 }
1267 }
1268 }
1269
1270 // TODO (multi-display): Magnification is supported only for the default display.
1271 // Announce rotation only if we will not animate as we already have the
1272 // windows in final state. Otherwise, we make this call at the rotation end.
1273 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1274 && isDefaultDisplay) {
1275 mService.mAccessibilityController.onRotationChangedLocked(this);
1276 }
1277
1278 return true;
1279 }
1280
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001281 void configureDisplayPolicy() {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001282 final int width = mBaseDisplayWidth;
1283 final int height = mBaseDisplayHeight;
1284 final int shortSize;
1285 final int longSize;
1286 if (width > height) {
1287 shortSize = height;
1288 longSize = width;
1289 } else {
1290 shortSize = width;
1291 longSize = height;
1292 }
1293
1294 final int shortSizeDp = shortSize * DisplayMetrics.DENSITY_DEFAULT / mBaseDisplayDensity;
1295 final int longSizeDp = longSize * DisplayMetrics.DENSITY_DEFAULT / mBaseDisplayDensity;
1296
1297 mDisplayRotation.configure(width, height, shortSizeDp, longSizeDp);
1298 mDisplayPolicy.configure(width, height, shortSizeDp);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001299
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001300 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
1301 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Arthur Hungbe5ce212018-09-13 18:41:56 +08001302
1303 // Tap Listeners are supported for:
1304 // 1. All physical displays (multi-display).
1305 // 2. VirtualDisplays on VR, AA (and everything else).
1306 if (mPointerEventDispatcher != null && mTapDetector == null) {
1307 if (DEBUG_DISPLAY) {
1308 Slog.d(TAG,
1309 "Registering PointerEventListener for DisplayId: " + mDisplayId);
1310 }
1311 mTapDetector = new TaskTapPointerEventListener(mService, this);
1312 registerPointerEventListener(mTapDetector);
1313 registerPointerEventListener(mService.mMousePositionTracker);
1314 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001315 }
1316
Andrii Kulian06d07d62017-03-14 11:11:47 -07001317 /**
1318 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1319 * changed.
1320 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1321 */
1322 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1323 // Use the effective "visual" dimensions based on current rotation
1324 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1325 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1326 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1327 int dw = realdw;
1328 int dh = realdh;
1329
1330 if (mAltOrientation) {
1331 if (realdw > realdh) {
1332 // Turn landscape into portrait.
1333 int maxw = (int)(realdh/1.3f);
1334 if (maxw < realdw) {
1335 dw = maxw;
1336 }
1337 } else {
1338 // Turn portrait into landscape.
1339 int maxh = (int)(realdw/1.3f);
1340 if (maxh < realdh) {
1341 dh = maxh;
1342 }
1343 }
1344 }
1345
1346 // Update application display metrics.
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001347 final WmDisplayCutout wmDisplayCutout = calculateDisplayCutoutForRotation(mRotation);
1348 final DisplayCutout displayCutout = wmDisplayCutout.getDisplayCutout();
1349
Andrii Kulian06d07d62017-03-14 11:11:47 -07001350 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001351 mDisplayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001352 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001353 mDisplayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001354 mDisplayInfo.rotation = mRotation;
1355 mDisplayInfo.logicalWidth = dw;
1356 mDisplayInfo.logicalHeight = dh;
1357 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1358 mDisplayInfo.appWidth = appWidth;
1359 mDisplayInfo.appHeight = appHeight;
1360 if (isDefaultDisplay) {
1361 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1362 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1363 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001364 mDisplayInfo.displayCutout = displayCutout.isEmpty() ? null : displayCutout;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001365 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1366 if (mDisplayScalingDisabled) {
1367 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1368 } else {
1369 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1370 }
1371
Andrii Kulianf0379de2018-03-14 16:24:07 -07001372 // We usually set the override info in DisplayManager so that we get consistent display
1373 // metrics values when displays are changing and don't send out new values until WM is aware
1374 // of them. However, we don't do this for displays that serve as containers for ActivityView
1375 // because we don't want letter-/pillar-boxing during resize.
1376 final DisplayInfo overrideDisplayInfo = mShouldOverrideDisplayConfiguration
1377 ? mDisplayInfo : null;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001378 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
Andrii Kulianf0379de2018-03-14 16:24:07 -07001379 overrideDisplayInfo);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001380
1381 mBaseDisplayRect.set(0, 0, dw, dh);
1382
1383 if (isDefaultDisplay) {
1384 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1385 mCompatDisplayMetrics);
1386 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001387
1388 updateBounds();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001389 return mDisplayInfo;
1390 }
1391
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001392 WmDisplayCutout calculateDisplayCutoutForRotation(int rotation) {
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001393 return mDisplayCutoutCache.getOrCompute(mInitialDisplayCutout, rotation);
1394 }
1395
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001396 private WmDisplayCutout calculateDisplayCutoutForRotationUncached(
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001397 DisplayCutout cutout, int rotation) {
Adrian Roos24264212018-02-19 16:26:15 +01001398 if (cutout == null || cutout == DisplayCutout.NO_CUTOUT) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001399 return WmDisplayCutout.NO_CUTOUT;
Adrian Roos1cf585052018-01-03 18:43:27 +01001400 }
Adrian Roos11c25582018-02-19 18:06:36 +01001401 if (rotation == ROTATION_0) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001402 return WmDisplayCutout.computeSafeInsets(
1403 cutout, mInitialDisplayWidth, mInitialDisplayHeight);
Adrian Roos24264212018-02-19 16:26:15 +01001404 }
Adrian Roosbed538e2018-02-21 17:50:07 +01001405 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Issei Suzuki43190bd2018-08-20 17:28:41 +02001406 final Rect[] newBounds = mRotationUtil.getRotatedBounds(
1407 WmDisplayCutout.computeSafeInsets(
Jorim Jaggi60640512018-06-29 01:14:31 +02001408 cutout, mInitialDisplayWidth, mInitialDisplayHeight)
Issei Suzuki43190bd2018-08-20 17:28:41 +02001409 .getDisplayCutout().getBoundingRectsAll(),
1410 rotation, mInitialDisplayWidth, mInitialDisplayHeight);
1411 return WmDisplayCutout.computeSafeInsets(DisplayCutout.fromBounds(newBounds),
Adrian Roos24264212018-02-19 16:26:15 +01001412 rotated ? mInitialDisplayHeight : mInitialDisplayWidth,
1413 rotated ? mInitialDisplayWidth : mInitialDisplayHeight);
Adrian Roos1cf585052018-01-03 18:43:27 +01001414 }
1415
Andrii Kulian06d07d62017-03-14 11:11:47 -07001416 /**
1417 * Compute display configuration based on display properties and policy settings.
1418 * Do not call if mDisplayReady == false.
1419 */
1420 void computeScreenConfiguration(Configuration config) {
1421 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1422
1423 final int dw = displayInfo.logicalWidth;
1424 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001425 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
Garfield Tane0846042018-07-26 13:42:04 -07001426 config.windowConfiguration.setWindowingMode(getWindowingMode());
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001427
Adrian Roos11c25582018-02-19 18:06:36 +01001428 final float density = mDisplayMetrics.density;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001429 config.screenWidthDp =
1430 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001431 config.uiMode, mDisplayId, displayInfo.displayCutout) / density);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001432 config.screenHeightDp =
1433 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001434 config.uiMode, mDisplayId, displayInfo.displayCutout) / density);
Bryce Lee7566d762017-03-30 09:34:15 -07001435
Adrian Roos11c25582018-02-19 18:06:36 +01001436 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh,
1437 displayInfo.displayCutout, mTmpRect);
Bryce Lee7566d762017-03-30 09:34:15 -07001438 final int leftInset = mTmpRect.left;
1439 final int topInset = mTmpRect.top;
1440 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001441 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1442 leftInset + displayInfo.appWidth /* right */,
1443 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001444 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1445 || displayInfo.rotation == Surface.ROTATION_270);
1446
1447 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
Adrian Roos11c25582018-02-19 18:06:36 +01001448 density, config);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001449
1450 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1451 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1452 ? Configuration.SCREENLAYOUT_ROUND_YES
1453 : Configuration.SCREENLAYOUT_ROUND_NO);
1454
1455 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1456 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1457 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1458 dh, mDisplayId);
1459 config.densityDpi = displayInfo.logicalDensityDpi;
1460
1461 config.colorMode =
Chia-I Wuf739bad2018-05-09 12:36:57 -07001462 ((displayInfo.isHdr() && mService.hasHdrSupport())
Andrii Kulian06d07d62017-03-14 11:11:47 -07001463 ? Configuration.COLOR_MODE_HDR_YES
1464 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001465 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001466 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1467 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1468
1469 // Update the configuration based on available input devices, lid switch,
1470 // and platform configuration.
1471 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1472 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1473 config.navigation = Configuration.NAVIGATION_NONAV;
1474
1475 int keyboardPresence = 0;
1476 int navigationPresence = 0;
1477 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1478 final int len = devices != null ? devices.length : 0;
1479 for (int i = 0; i < len; i++) {
1480 InputDevice device = devices[i];
1481 if (!device.isVirtual()) {
1482 final int sources = device.getSources();
1483 final int presenceFlag = device.isExternal() ?
1484 WindowManagerPolicy.PRESENCE_EXTERNAL :
1485 WindowManagerPolicy.PRESENCE_INTERNAL;
1486
1487 // TODO(multi-display): Configure on per-display basis.
1488 if (mService.mIsTouchDevice) {
1489 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1490 InputDevice.SOURCE_TOUCHSCREEN) {
1491 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1492 }
1493 } else {
1494 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1495 }
1496
1497 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1498 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1499 navigationPresence |= presenceFlag;
1500 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1501 && config.navigation == Configuration.NAVIGATION_NONAV) {
1502 config.navigation = Configuration.NAVIGATION_DPAD;
1503 navigationPresence |= presenceFlag;
1504 }
1505
1506 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1507 config.keyboard = Configuration.KEYBOARD_QWERTY;
1508 keyboardPresence |= presenceFlag;
1509 }
1510 }
1511 }
1512
1513 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1514 config.navigation = Configuration.NAVIGATION_DPAD;
1515 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1516 }
1517
1518 // Determine whether a hard keyboard is available and enabled.
1519 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1520 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1521 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1522 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1523 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1524 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1525 }
1526
1527 // Let the policy update hidden states.
1528 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1529 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1530 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1531 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1532 }
1533
1534 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1535 int displayId) {
1536 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1537 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1538 final int unrotDw, unrotDh;
1539 if (rotated) {
1540 unrotDw = dh;
1541 unrotDh = dw;
1542 } else {
1543 unrotDw = dw;
1544 unrotDh = dh;
1545 }
1546 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1547 displayId);
1548 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1549 displayId);
1550 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1551 displayId);
1552 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1553 displayId);
1554 return sw;
1555 }
1556
1557 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1558 DisplayMetrics dm, int dw, int dh, int displayId) {
1559 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001560 displayId, mDisplayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001561 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001562 uiMode, displayId, mDisplayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001563 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1564 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1565 if (curSize == 0 || size < curSize) {
1566 curSize = size;
1567 }
1568 return curSize;
1569 }
1570
1571 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1572 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1573
1574 // We need to determine the smallest width that will occur under normal
1575 // operation. To this, start with the base screen size and compute the
1576 // width under the different possible rotations. We need to un-rotate
1577 // the current screen dimensions before doing this.
1578 int unrotDw, unrotDh;
1579 if (rotated) {
1580 unrotDw = dh;
1581 unrotDh = dw;
1582 } else {
1583 unrotDw = dw;
1584 unrotDh = dh;
1585 }
1586 displayInfo.smallestNominalAppWidth = 1<<30;
1587 displayInfo.smallestNominalAppHeight = 1<<30;
1588 displayInfo.largestNominalAppWidth = 0;
1589 displayInfo.largestNominalAppHeight = 0;
1590 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1591 unrotDh);
1592 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1593 unrotDw);
1594 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1595 unrotDh);
1596 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1597 unrotDw);
1598 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1599 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1600 displayId);
1601 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1602 displayId);
1603 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1604 displayId);
1605 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1606 displayId);
1607 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1608 outConfig.screenLayout = sl;
1609 }
1610
1611 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1612 int uiMode, int displayId) {
1613 // Get the app screen size at this rotation.
Adrian Roos11c25582018-02-19 18:06:36 +01001614 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId,
1615 mDisplayInfo.displayCutout);
1616 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId,
1617 mDisplayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001618
1619 // Compute the screen layout size class for this rotation.
1620 int longSize = w;
1621 int shortSize = h;
1622 if (longSize < shortSize) {
1623 int tmp = longSize;
1624 longSize = shortSize;
1625 shortSize = tmp;
1626 }
1627 longSize = (int)(longSize/density);
1628 shortSize = (int)(shortSize/density);
1629 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1630 }
1631
1632 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1633 int uiMode, int dw, int dh) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001634 final DisplayCutout displayCutout = calculateDisplayCutoutForRotation(
1635 rotation).getDisplayCutout();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001636 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001637 displayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001638 if (width < displayInfo.smallestNominalAppWidth) {
1639 displayInfo.smallestNominalAppWidth = width;
1640 }
1641 if (width > displayInfo.largestNominalAppWidth) {
1642 displayInfo.largestNominalAppWidth = width;
1643 }
1644 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001645 displayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001646 if (height < displayInfo.smallestNominalAppHeight) {
1647 displayInfo.smallestNominalAppHeight = height;
1648 }
1649 if (height > displayInfo.largestNominalAppHeight) {
1650 displayInfo.largestNominalAppHeight = height;
1651 }
1652 }
1653
Riddle Hsua4d6fa22018-08-11 00:50:39 +08001654 /**
1655 * Apps that use the compact menu panel (as controlled by the panelMenuIsCompact
1656 * theme attribute) on devices that feature a physical options menu key attempt to position
1657 * their menu panel window along the edge of the screen nearest the physical menu key.
1658 * This lowers the travel distance between invoking the menu panel and selecting
1659 * a menu option.
1660 *
1661 * This method helps control where that menu is placed. Its current implementation makes
1662 * assumptions about the menu key and its relationship to the screen based on whether
1663 * the device's natural orientation is portrait (width < height) or landscape.
1664 *
1665 * The menu key is assumed to be located along the bottom edge of natural-portrait
1666 * devices and along the right edge of natural-landscape devices. If these assumptions
1667 * do not hold for the target device, this method should be changed to reflect that.
1668 *
1669 * @return A {@link Gravity} value for placing the options menu window.
1670 */
1671 int getPreferredOptionsPanelGravity() {
1672 final int rotation = getRotation();
1673 if (mInitialDisplayWidth < mInitialDisplayHeight) {
1674 // On devices with a natural orientation of portrait.
1675 switch (rotation) {
1676 default:
1677 case Surface.ROTATION_0:
1678 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1679 case Surface.ROTATION_90:
1680 return Gravity.RIGHT | Gravity.BOTTOM;
1681 case Surface.ROTATION_180:
1682 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1683 case Surface.ROTATION_270:
1684 return Gravity.START | Gravity.BOTTOM;
1685 }
1686 }
1687
1688 // On devices with a natural orientation of landscape.
1689 switch (rotation) {
1690 default:
1691 case Surface.ROTATION_0:
1692 return Gravity.RIGHT | Gravity.BOTTOM;
1693 case Surface.ROTATION_90:
1694 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1695 case Surface.ROTATION_180:
1696 return Gravity.START | Gravity.BOTTOM;
1697 case Surface.ROTATION_270:
1698 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1699 }
1700 }
1701
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001702 DockedStackDividerController getDockedDividerController() {
1703 return mDividerControllerLocked;
1704 }
1705
Winson Chung655332c2016-10-31 13:14:28 -07001706 PinnedStackController getPinnedStackController() {
1707 return mPinnedStackControllerLocked;
1708 }
1709
Jeff Browna506a6e2013-06-04 00:02:38 -07001710 /**
1711 * Returns true if the specified UID has access to this display.
1712 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001713 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001714 return mDisplay.hasAccess(uid);
1715 }
1716
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001717 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001718 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001719 }
1720
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001721 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001722 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001723 }
1724
Wale Ogunwale61911492017-10-11 08:50:50 -07001725 /**
1726 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1727 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001728 TaskStack getSplitScreenPrimaryStack() {
1729 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001730 return (stack != null && stack.isVisible()) ? stack : null;
1731 }
1732
Robert Carr9785cf32018-04-25 15:06:07 -07001733 boolean hasSplitScreenPrimaryStack() {
1734 return getSplitScreenPrimaryStack() != null;
1735 }
1736
Wale Ogunwale61911492017-10-11 08:50:50 -07001737 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001738 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001739 * not visible.
1740 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001741 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1742 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001743 }
1744
1745 TaskStack getPinnedStack() {
1746 return mTaskStackContainers.getPinnedStack();
1747 }
1748
1749 private boolean hasPinnedStack() {
1750 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001751 }
1752
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001753 /**
1754 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1755 * Null is no compatible stack on the display.
1756 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001757 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001758 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1759 }
1760
1761 /**
1762 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1763 * activity type. Null is no compatible stack on the display.
1764 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001765 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001766 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001767 }
1768
Bryce Lee48f4b572017-04-10 10:54:15 -07001769 @VisibleForTesting
Kazuki Takise148d00a2018-05-31 15:32:19 +09001770 WindowList<TaskStack> getStacks() {
1771 return mTaskStackContainers.mChildren;
1772 }
1773
1774 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001775 TaskStack getTopStack() {
1776 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001777 }
1778
Winson Chunge2d72172018-01-25 17:46:20 +00001779 ArrayList<Task> getVisibleTasks() {
1780 return mTaskStackContainers.getVisibleTasks();
1781 }
1782
Wale Ogunwale61911492017-10-11 08:50:50 -07001783 void onStackWindowingModeChanged(TaskStack stack) {
1784 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001785 }
1786
Andrii Kulian441e4492016-09-29 15:25:00 -07001787 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001788 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001789 super.onConfigurationChanged(newParentConfig);
1790
Andrii Kulian3a507b52016-09-19 18:14:12 -07001791 // The display size information is heavily dependent on the resources in the current
1792 // configuration, so we need to reconfigure it every time the configuration changes.
1793 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1794 mService.reconfigureDisplayLocked(this);
1795
Bryce Leef3c6a472017-11-14 14:53:06 -08001796 final DockedStackDividerController dividerController = getDockedDividerController();
1797
1798 if (dividerController != null) {
1799 getDockedDividerController().onConfigurationChanged();
1800 }
1801
1802 final PinnedStackController pinnedStackController = getPinnedStackController();
1803
1804 if (pinnedStackController != null) {
1805 getPinnedStackController().onConfigurationChanged();
1806 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001807 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001808
Andrii Kulian441e4492016-09-29 15:25:00 -07001809 /**
1810 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1811 * bounds were updated.
1812 */
Bryce Leeaea60d22018-01-31 14:42:15 -08001813 void updateStackBoundsAfterConfigChange(@NonNull List<TaskStack> changedStackList) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001814 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1815 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001816 if (stack.updateBoundsAfterConfigChange()) {
Bryce Leeaea60d22018-01-31 14:42:15 -08001817 changedStackList.add(stack);
Andrii Kulian3a507b52016-09-19 18:14:12 -07001818 }
1819 }
Winson Chung32c566f2017-04-11 18:31:21 -07001820
1821 // If there was no pinned stack, we still need to notify the controller of the display info
1822 // update as a result of the config change. We do this here to consolidate the flow between
1823 // changes when there is and is not a stack.
Wale Ogunwale61911492017-10-11 08:50:50 -07001824 if (!hasPinnedStack()) {
Winson Chung32c566f2017-04-11 18:31:21 -07001825 mPinnedStackControllerLocked.onDisplayInfoChanged();
1826 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001827 }
1828
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001829 @Override
1830 boolean fillsParent() {
1831 return true;
1832 }
1833
1834 @Override
1835 boolean isVisible() {
1836 return true;
1837 }
1838
1839 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001840 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001841 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001842 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001843 }
1844
Robert Carr9785cf32018-04-25 15:06:07 -07001845 /**
1846 * In split-screen mode we process the IME containers above the docked divider
1847 * rather than directly above their target.
1848 */
1849 private boolean skipTraverseChild(WindowContainer child) {
1850 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null
1851 && !hasSplitScreenPrimaryStack()) {
1852 return true;
1853 }
1854 return false;
1855 }
1856
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001857 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001858 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1859 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1860 // target, or here in order if there isn't an IME target.
1861 if (traverseTopToBottom) {
1862 for (int i = mChildren.size() - 1; i >= 0; --i) {
1863 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001864 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001865 continue;
1866 }
Robert Carr9785cf32018-04-25 15:06:07 -07001867
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001868 if (child.forAllWindows(callback, traverseTopToBottom)) {
1869 return true;
1870 }
1871 }
1872 } else {
1873 final int count = mChildren.size();
1874 for (int i = 0; i < count; i++) {
1875 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001876 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001877 continue;
1878 }
Robert Carr9785cf32018-04-25 15:06:07 -07001879
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001880 if (child.forAllWindows(callback, traverseTopToBottom)) {
1881 return true;
1882 }
1883 }
1884 }
1885 return false;
1886 }
1887
1888 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1889 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1890 }
1891
Wale Ogunwale399c8692017-05-08 14:22:42 -07001892 @Override
1893 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001894 final WindowManagerPolicy policy = mService.mPolicy;
1895
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001896 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001897 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001898 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1899 + " is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001900 // If the display is frozen, some activities may be in the middle of restarting, and
1901 // thus have removed their old window. If the window has the flag to hide the lock
1902 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1903 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001904 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001905 } else if (policy.isKeyguardLocked()) {
1906 // Use the last orientation the while the display is frozen with the keyguard
1907 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1908 // window. We don't want to check the show when locked window directly though as
1909 // things aren't stable while the display is frozen, for example the window could be
1910 // momentarily unavailable due to activity relaunch.
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001911 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1912 + " is frozen while keyguard locked, return " + mLastOrientation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08001913 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001914 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001915 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001916 final int orientation = mAboveAppWindowsContainers.getOrientation();
1917 if (orientation != SCREEN_ORIENTATION_UNSET) {
1918 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001919 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001920 }
1921
Wale Ogunwale399c8692017-05-08 14:22:42 -07001922 // Top system windows are not requesting an orientation. Start searching from apps.
1923 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001924 }
1925
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001926 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001927 // Check if display metrics changed and update base values if needed.
1928 updateBaseDisplayMetricsIfNeeded();
1929
Craig Mautner722285e2012-09-07 13:55:58 -07001930 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001931 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001932
Wale Ogunwale61911492017-10-11 08:50:50 -07001933 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1934 mTaskStackContainers.getChildAt(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001935 }
Craig Mautner722285e2012-09-07 13:55:58 -07001936 }
1937
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001938 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001939 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1940 if (displayManagerInternal != null) {
1941 // Bootstrap the default logical display from the display manager.
1942 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1943 if (newDisplayInfo != null) {
1944 mDisplayInfo.copyFrom(newDisplayInfo);
1945 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001946 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001947
Andrii Kuliancd097992017-03-23 18:31:59 -07001948 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1949 mDisplayInfo.logicalDensityDpi);
1950 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1951 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1952 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01001953 mInitialDisplayCutout = mDisplayInfo.displayCutout;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001954 }
1955
Andrii Kuliancd097992017-03-23 18:31:59 -07001956 /**
1957 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1958 * values.
1959 */
1960 private void updateBaseDisplayMetricsIfNeeded() {
1961 // Get real display metrics without overrides from WM.
1962 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1963 final int orientation = mDisplayInfo.rotation;
1964 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1965 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1966 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1967 final int newDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01001968 final DisplayCutout newCutout = mDisplayInfo.displayCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07001969
1970 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1971 || mInitialDisplayHeight != newHeight
Adrian Roos1cf585052018-01-03 18:43:27 +01001972 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi
1973 || !Objects.equals(mInitialDisplayCutout, newCutout);
Andrii Kuliancd097992017-03-23 18:31:59 -07001974
1975 if (displayMetricsChanged) {
1976 // Check if display size or density is forced.
1977 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1978 || mBaseDisplayHeight != mInitialDisplayHeight;
1979 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1980
1981 // If there is an override set for base values - use it, otherwise use new values.
1982 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1983 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1984 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1985
1986 // Real display metrics changed, so we should also update initial values.
1987 mInitialDisplayWidth = newWidth;
1988 mInitialDisplayHeight = newHeight;
1989 mInitialDisplayDensity = newDensity;
Adrian Roos1cf585052018-01-03 18:43:27 +01001990 mInitialDisplayCutout = newCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07001991 mService.reconfigureDisplayLocked(this);
1992 }
1993 }
1994
Bryce Lee27cec322017-03-21 09:41:37 -07001995 /** Sets the maximum width the screen resolution can be */
1996 void setMaxUiWidth(int width) {
1997 if (DEBUG_DISPLAY) {
1998 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1999 }
2000
2001 mMaxUiWidth = width;
2002
2003 // Update existing metrics.
2004 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
2005 }
2006
2007 /** Update base (override) display metrics. */
2008 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
2009 mBaseDisplayWidth = baseWidth;
2010 mBaseDisplayHeight = baseHeight;
2011 mBaseDisplayDensity = baseDensity;
2012
2013 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
2014 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
2015 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
2016 mBaseDisplayWidth = mMaxUiWidth;
2017
2018 if (DEBUG_DISPLAY) {
2019 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
2020 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
2021 + " on display:" + getDisplayId());
2022 }
2023 }
2024
2025 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08002026
2027 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07002028 }
2029
Wale Ogunwaledb506192017-12-08 10:57:32 -08002030 void getStableRect(Rect out) {
2031 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08002032 }
2033
Wale Ogunwale61911492017-10-11 08:50:50 -07002034 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002035 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
2036 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07002037
Wale Ogunwale61911492017-10-11 08:50:50 -07002038 final TaskStack stack = new TaskStack(mService, stackId, controller);
2039 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002040 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07002041 }
2042
Andrii Kulian51c1b672017-04-07 18:39:32 -07002043 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002044 final DisplayContent prevDc = stack.getDisplayContent();
2045 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07002046 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
2047 + " which is not currently attached to any display");
2048 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002049 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07002050 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
2051 + " to its current displayId=" + mDisplayId);
2052 }
2053
Wale Ogunwale61911492017-10-11 08:50:50 -07002054 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07002055 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08002056 }
2057
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002058 @Override
2059 protected void addChild(DisplayChildWindowContainer child,
2060 Comparator<DisplayChildWindowContainer> comparator) {
2061 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2062 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002063
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002064 @Override
2065 protected void addChild(DisplayChildWindowContainer child, int index) {
2066 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2067 }
2068
2069 @Override
2070 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002071 // Only allow removal of direct children from this display if the display is in the process
2072 // of been removed.
2073 if (mRemovingDisplay) {
2074 super.removeChild(child);
2075 return;
2076 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002077 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002078 }
2079
Andrii Kuliand2765632016-12-12 22:26:34 -08002080 @Override
2081 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
2082 // Children of the display are statically ordered, so the real intention here is to perform
2083 // the operation on the display and not the static direct children.
2084 getParent().positionChildAt(position, this, includingParents);
2085 }
2086
Riddle Hsu57831b52018-07-27 00:31:48 +08002087 void positionStackAt(int position, TaskStack child, boolean includingParents) {
2088 mTaskStackContainers.positionChildAt(position, child, includingParents);
Winson Chung59a47ded2018-01-25 17:46:06 +00002089 layoutAndAssignWindowLayersIfNeeded();
2090 }
2091
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002092 /**
2093 * Used to obtain task ID when user taps on coordinate (x, y) in this display, and outside
2094 * current task in focus.
2095 *
2096 * This returns the task ID of the foremost task at (x, y) if the task is not home. Otherwise it
2097 * returns -1.
2098 *
2099 * @param x horizontal coordinate of the tap position
2100 * @param y vertical coordinate of the tap position
2101 * @return the task ID if a non-home task is found; -1 if not
2102 */
2103 int taskForTapOutside(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002104 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2105 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Garfield Tanf2e0a9e2018-10-16 09:42:02 -07002106 if (stack.isActivityTypeHome() && !stack.inMultiWindowMode()) {
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002107 // We skip not only home stack, but also everything behind home because user can't
Garfield Tanf2e0a9e2018-10-16 09:42:02 -07002108 // see them when home stack is isn't in multi-window mode.
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002109 break;
2110 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002111 final int taskId = stack.taskIdFromPoint(x, y);
2112 if (taskId != -1) {
2113 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07002114 }
2115 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002116 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07002117 }
2118
Chong Zhang8e89b312015-09-09 15:09:30 -07002119 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08002120 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07002121 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07002122 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07002123 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002124 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002125 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07002126 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2127 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002128 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002129 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002130 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002131
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002132 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
2133 if (mTmpTaskForResizePointSearchResult.searchDone) {
2134 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07002135 }
2136 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002137 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002138 }
2139
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002140 void updateTouchExcludeRegion() {
2141 final Task focusedTask = (mFocusedApp != null ? mFocusedApp.getTask() : null);
David Stevensee9e2772017-02-09 16:30:27 -08002142 if (focusedTask == null) {
2143 mTouchExcludeRegion.setEmpty();
2144 } else {
2145 mTouchExcludeRegion.set(mBaseDisplayRect);
2146 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
2147 mTmpRect2.setEmpty();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002148 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0;
2149 --stackNdx) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002150 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002151 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
2152 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08002153 }
2154 // If we removed the focused task above, add it back and only leave its
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002155 // outside touch area in the exclusion. TapDetector is not interested in
David Stevensee9e2772017-02-09 16:30:27 -08002156 // any touch inside the focused task itself.
2157 if (!mTmpRect2.isEmpty()) {
2158 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
2159 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08002160 }
lumark90120a82018-08-15 00:33:03 +08002161 if (mInputMethodWindow != null && mInputMethodWindow.isVisibleLw()) {
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002162 // If the input method is visible and the user is typing, we don't want these touch
2163 // events to be intercepted and used to change focus. This would likely cause a
2164 // disappearance of the input method.
lumark90120a82018-08-15 00:33:03 +08002165 mInputMethodWindow.getTouchableRegion(mTmpRegion);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002166 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002167 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002168 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002169 final WindowState win = mTapExcludedWindows.get(i);
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002170 win.getTouchableRegion(mTmpRegion);
2171 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
2172 }
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002173 for (int i = mTapExcludeProvidingWindows.size() - 1; i >= 0; i--) {
2174 final WindowState win = mTapExcludeProvidingWindows.valueAt(i);
2175 win.amendTapExcludeRegion(mTouchExcludeRegion);
2176 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08002177 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07002178 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002179 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07002180 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002181 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
2182 }
Craig Mautner1bef3892015-02-17 15:09:47 -08002183 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07002184 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08002185 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07002186 }
2187
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002188 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07002189 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002190 super.switchUser();
2191 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07002192 }
2193
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07002194 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002195 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2196 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07002197 }
2198 }
2199
Wale Ogunwale10124582016-09-15 20:25:50 -07002200 @Override
2201 void removeIfPossible() {
2202 if (isAnimating()) {
2203 mDeferredRemoval = true;
2204 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07002205 }
Wale Ogunwale10124582016-09-15 20:25:50 -07002206 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07002207 }
2208
Wale Ogunwale10124582016-09-15 20:25:50 -07002209 @Override
2210 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002211 mRemovingDisplay = true;
2212 try {
2213 super.removeImmediately();
2214 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Arthur Hungbe5ce212018-09-13 18:41:56 +08002215 if (mPointerEventDispatcher != null && mTapDetector != null) {
2216 unregisterPointerEventListener(mTapDetector);
2217 unregisterPointerEventListener(mService.mMousePositionTracker);
2218 mTapDetector = null;
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002219 }
Jorim Jaggia3844032018-01-03 15:54:43 +01002220 mService.mAnimator.removeDisplayLocked(mDisplayId);
Louis Chang207f9112018-07-30 08:58:54 +08002221 mWindowingLayer.release();
2222 mOverlayLayer.release();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002223 } finally {
2224 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08002225 }
Bryce Leef19cbe22018-02-02 15:09:21 -08002226
Arthur Hung39134b22018-08-14 11:58:28 +08002227 mInputMonitor.onRemoved();
Bryce Leef19cbe22018-02-02 15:09:21 -08002228 mService.onDisplayRemoved(mDisplayId);
Craig Mautner95da1082014-02-24 17:54:35 -08002229 }
2230
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002231 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07002232 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002233 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002234 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
2235
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002236 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002237 removeImmediately();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002238 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08002239 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002240 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08002241 }
2242
Andrii Kulian0214ed92017-05-16 13:44:05 -07002243 /** @return 'true' if removal of this display content is deferred due to active animation. */
2244 boolean isRemovalDeferred() {
2245 return mDeferredRemoval;
2246 }
2247
Wale Ogunwale10124582016-09-15 20:25:50 -07002248 boolean animateForIme(float interpolatedValue, float animationTarget,
2249 float dividerAnimationTarget) {
2250 boolean updated = false;
2251
Wale Ogunwale61911492017-10-11 08:50:50 -07002252 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2253 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002254 if (stack == null || !stack.isAdjustedForIme()) {
2255 continue;
2256 }
2257
2258 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
2259 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2260 updated = true;
2261 } else {
2262 mDividerControllerLocked.mLastAnimationProgress =
2263 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
2264 mDividerControllerLocked.mLastDividerProgress =
2265 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
2266 updated |= stack.updateAdjustForIme(
2267 mDividerControllerLocked.mLastAnimationProgress,
2268 mDividerControllerLocked.mLastDividerProgress,
2269 false /* force */);
2270 }
2271 if (interpolatedValue >= 1f) {
2272 stack.endImeAdjustAnimation();
2273 }
2274 }
2275
2276 return updated;
2277 }
2278
2279 boolean clearImeAdjustAnimation() {
2280 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002281 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2282 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002283 if (stack != null && stack.isAdjustedForIme()) {
2284 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2285 changed = true;
2286 }
2287 }
2288 return changed;
2289 }
2290
2291 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002292 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2293 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002294 if (stack.isVisible() && stack.isAdjustedForIme()) {
2295 stack.beginImeAdjustAnimation();
2296 }
2297 }
2298 }
2299
2300 void adjustForImeIfNeeded() {
lumark90120a82018-08-15 00:33:03 +08002301 final WindowState imeWin = mInputMethodWindow;
Wale Ogunwale10124582016-09-15 20:25:50 -07002302 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2303 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002304 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002305 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2306 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2307 imeTargetStack.getDockSide() : DOCKED_INVALID;
2308 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2309 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2310 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002311 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002312 final boolean imeHeightChanged = imeVisible &&
2313 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2314
2315 // The divider could be adjusted for IME position, or be thinner than usual,
2316 // or both. There are three possible cases:
2317 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2318 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2319 // - If IME is not visible, divider is not moved and is normal width.
2320
2321 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002322 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2323 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002324 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002325 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2326 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002327 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2328 } else {
2329 stack.resetAdjustedForIme(false);
2330 }
2331 }
2332 mDividerControllerLocked.setAdjustedForIme(
2333 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2334 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002335 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2336 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002337 stack.resetAdjustedForIme(!dockVisible);
2338 }
2339 mDividerControllerLocked.setAdjustedForIme(
2340 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2341 }
Winson Chung655332c2016-10-31 13:14:28 -07002342 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002343 }
2344
2345 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002346 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2347 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002348 stack.prepareFreezingTaskBounds();
2349 }
2350 }
2351
Wale Ogunwale94744212015-09-21 19:01:47 -07002352 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002353 getBounds(mTmpRect, newRotation);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002354
2355 // Compute a transform matrix to undo the coordinate space transformation,
2356 // and present the window at the same physical position it previously occupied.
2357 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2358 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2359
2360 mTmpRectF.set(bounds);
2361 mTmpMatrix.mapRect(mTmpRectF);
2362 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002363 }
2364
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002365 static int deltaRotation(int oldRotation, int newRotation) {
2366 int delta = newRotation - oldRotation;
2367 if (delta < 0) delta += 4;
2368 return delta;
2369 }
2370
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002371 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002372 Matrix outMatrix) {
2373 // For rotations without Z-ordering we don't need the target rectangle's position.
2374 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2375 displayHeight, outMatrix);
2376 }
2377
2378 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2379 float displayWidth, float displayHeight, Matrix outMatrix) {
2380 switch (rotation) {
2381 case ROTATION_0:
2382 outMatrix.reset();
2383 break;
2384 case ROTATION_270:
2385 outMatrix.setRotate(270, 0, 0);
2386 outMatrix.postTranslate(0, displayHeight);
2387 outMatrix.postTranslate(rectTop, 0);
2388 break;
2389 case ROTATION_180:
2390 outMatrix.reset();
2391 break;
2392 case ROTATION_90:
2393 outMatrix.setRotate(90, 0, 0);
2394 outMatrix.postTranslate(displayWidth, 0);
2395 outMatrix.postTranslate(-rectTop, rectLeft);
2396 break;
2397 }
2398 }
2399
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002400 @CallSuper
2401 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002402 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002403 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002404 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002405 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002406 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2407 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002408 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002409 }
2410 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2411 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002412 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2413 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002414 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002415 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002416 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2417 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002418 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002419 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002420 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2421 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002422 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002423 }
2424 proto.write(DPI, mBaseDisplayDensity);
2425 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002426 proto.write(ROTATION, mRotation);
2427 final ScreenRotationAnimation screenRotationAnimation =
2428 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2429 if (screenRotationAnimation != null) {
2430 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2431 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002432 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
Louis Chang7501e332018-08-20 13:08:39 +08002433 proto.write(SURFACE_SIZE, mSurfaceSize);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002434 if (mFocusedApp != null) {
2435 mFocusedApp.writeNameToProto(proto, FOCUSED_APP);
2436 }
Steven Timotiusaf03df62017-07-18 16:56:43 -07002437 proto.end(token);
2438 }
2439
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002440 @Override
2441 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2442 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002443 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2444 final String subPrefix = " " + prefix;
2445 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2446 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2447 pw.print("dpi");
2448 if (mInitialDisplayWidth != mBaseDisplayWidth
2449 || mInitialDisplayHeight != mBaseDisplayHeight
2450 || mInitialDisplayDensity != mBaseDisplayDensity) {
2451 pw.print(" base=");
2452 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2453 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2454 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002455 if (mDisplayScalingDisabled) {
2456 pw.println(" noscale");
2457 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002458 pw.print(" cur=");
2459 pw.print(mDisplayInfo.logicalWidth);
2460 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2461 pw.print(" app=");
2462 pw.print(mDisplayInfo.appWidth);
2463 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2464 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2465 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2466 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2467 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002468 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002469 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002470 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002471
Craig Mautnerdc548482014-02-05 13:35:24 -08002472 pw.println();
Adrian Roos5251b1d2018-03-23 18:57:43 +01002473 pw.print(prefix); pw.print("mLayoutSeq="); pw.println(mLayoutSeq);
Riddle Hsu654a6f92018-07-13 22:59:36 +08002474 pw.print(prefix);
2475 pw.print("mDeferredRotationPauseCount="); pw.println(mDeferredRotationPauseCount);
Adrian Roos5251b1d2018-03-23 18:57:43 +01002476
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002477 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
2478 if (mLastFocus != mCurrentFocus) {
2479 pw.print(" mLastFocus="); pw.println(mLastFocus);
2480 }
2481 if (mLosingFocus.size() > 0) {
2482 pw.println();
2483 pw.println(" Windows losing focus:");
2484 for (int i = mLosingFocus.size() - 1; i >= 0; i--) {
2485 final WindowState w = mLosingFocus.get(i);
2486 pw.print(" Losing #"); pw.print(i); pw.print(' ');
2487 pw.print(w);
2488 if (dumpAll) {
2489 pw.println(":");
2490 w.dump(pw, " ", true);
2491 } else {
2492 pw.println();
2493 }
2494 }
2495 }
2496 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
2497
Adrian Roos5251b1d2018-03-23 18:57:43 +01002498 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002499 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002500 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2501 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002502 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002503 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002504
Craig Mautnerdc548482014-02-05 13:35:24 -08002505 pw.println();
2506 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002507 pw.println();
2508 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002509 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002510 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002511 pw.print(" Exiting #"); pw.print(i);
2512 pw.print(' '); pw.print(token);
2513 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002514 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002515 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002516 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002517
Jorim Jaggi31f71702016-05-04 16:43:04 -07002518 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002519
2520 // Dump stack references
2521 final TaskStack homeStack = getHomeStack();
2522 if (homeStack != null) {
2523 pw.println(prefix + "homeStack=" + homeStack.getName());
2524 }
2525 final TaskStack pinnedStack = getPinnedStack();
2526 if (pinnedStack != null) {
2527 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2528 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002529 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002530 if (splitScreenPrimaryStack != null) {
2531 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2532 }
2533
2534 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002535 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002536 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002537 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002538
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002539 pw.println();
2540 mDisplayFrames.dump(prefix, pw);
Arthur Hung95b38a92018-07-20 18:56:12 +08002541 pw.println();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08002542 mDisplayPolicy.dump(prefix, pw);
2543 pw.println();
Riddle Hsuad256a12018-07-18 16:11:30 +08002544 mDisplayRotation.dump(prefix, pw);
2545 pw.println();
Arthur Hung95b38a92018-07-20 18:56:12 +08002546 mInputMonitor.dump(pw, " ");
Craig Mautner59c00972012-07-30 12:10:24 -07002547 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002548
2549 @Override
2550 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002551 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002552 }
2553
2554 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002555 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002556 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002557
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002558 /** Returns true if the stack in the windowing mode is visible. */
2559 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002560 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002561 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002562 }
2563
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002564 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002565 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002566 final int x = (int) xf;
2567 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002568 final WindowState touchedWin = getWindow(w -> {
2569 final int flags = w.mAttrs.flags;
2570 if (!w.isVisibleLw()) {
2571 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002572 }
2573 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002574 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002575 }
2576
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002577 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002578 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002579 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002580 }
2581
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002582 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002583
2584 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002585 return mTmpRegion.contains(x, y) || touchFlags == 0;
2586 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002587
2588 return touchedWin;
2589 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002590
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002591 boolean canAddToastWindowForUid(int uid) {
2592 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002593 // Also if the app is focused adding more than one toast at
2594 // a time for better backwards compatibility.
2595 final WindowState focusedWindowForUid = getWindow(w ->
2596 w.mOwnerUid == uid && w.isFocused());
2597 if (focusedWindowForUid != null) {
2598 return true;
2599 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002600 final WindowState win = getWindow(w ->
2601 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2602 && !w.mWindowRemovalAllowed);
2603 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002604 }
2605
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002606 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002607 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2608 return;
2609 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002610
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002611 // Used to communicate the old focus to the callback method.
2612 mTmpWindow = oldFocus;
2613
2614 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002615 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002616
2617 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002618 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002619
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002620 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002621
2622 if (mTmpWindow == null) {
2623 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2624 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002625 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002626 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002627 }
2628
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002629
2630 /**
2631 * Update the focused window and make some adjustments if the focus has changed.
2632 *
2633 * @param mode Indicates the situation we are in. Possible modes are:
2634 * {@link WindowManagerService#UPDATE_FOCUS_NORMAL},
2635 * {@link WindowManagerService#UPDATE_FOCUS_PLACING_SURFACES},
2636 * {@link WindowManagerService#UPDATE_FOCUS_WILL_PLACE_SURFACES},
2637 * {@link WindowManagerService#UPDATE_FOCUS_REMOVING_FOCUS}
2638 * @param updateInputWindows Whether to sync the window information to the input module.
2639 * @return {@code true} if the focused window has changed.
2640 */
2641 boolean updateFocusedWindowLocked(int mode, boolean updateInputWindows, boolean focusFound) {
2642 final WindowState newFocus = findFocusedWindow();
2643 if (mCurrentFocus == newFocus) {
2644 return false;
2645 }
2646 mService.mH.obtainMessage(REPORT_FOCUS_CHANGE, this).sendToTarget();
2647 boolean imWindowChanged = false;
2648 // TODO (b/111080190): Multi-Session IME
2649 if (!focusFound) {
2650 final WindowState imWindow = mInputMethodWindow;
2651 if (imWindow != null) {
2652 final WindowState prevTarget = mService.mInputMethodTarget;
2653
2654 final WindowState newTarget = computeImeTarget(true /* updateImeTarget*/);
2655 imWindowChanged = prevTarget != newTarget;
2656
2657 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS
2658 && mode != UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2659 assignWindowLayers(false /* setLayoutNeeded */);
2660 }
2661 }
2662 }
2663
2664 if (imWindowChanged) {
2665 mService.mWindowsChanged = true;
2666 setLayoutNeeded();
2667 }
2668
2669 if (DEBUG_FOCUS_LIGHT || mService.localLOGV) Slog.v(TAG_WM, "Changing focus from "
2670 + mCurrentFocus + " to " + newFocus + " displayId=" + getDisplayId()
2671 + " Callers=" + Debug.getCallers(4));
2672 final WindowState oldFocus = mCurrentFocus;
2673 mCurrentFocus = newFocus;
2674 mLosingFocus.remove(newFocus);
2675
2676 if (newFocus != null) {
2677 mWinAddedSinceNullFocus.clear();
2678 mWinRemovedSinceNullFocus.clear();
2679
2680 if (newFocus.canReceiveKeys()) {
2681 // Displaying a window implicitly causes dispatching to be unpaused.
2682 // This is to protect against bugs if someone pauses dispatching but
2683 // forgets to resume.
2684 newFocus.mToken.paused = false;
2685 }
2686 }
2687
2688 // System UI is only shown on the default display.
2689 int focusChanged = isDefaultDisplay
2690 ? mService.mPolicy.focusChangedLw(oldFocus, newFocus) : 0;
2691
2692 if (imWindowChanged && oldFocus != mInputMethodWindow) {
2693 // Focus of the input method window changed. Perform layout if needed.
2694 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2695 performLayout(true /*initial*/, updateInputWindows);
2696 focusChanged &= ~FINISH_LAYOUT_REDO_LAYOUT;
2697 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2698 // Client will do the layout, but we need to assign layers
2699 // for handleNewWindowLocked() below.
2700 assignWindowLayers(false /* setLayoutNeeded */);
2701 }
2702 }
2703
2704 if ((focusChanged & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2705 // The change in focus caused us to need to do a layout. Okay.
2706 setLayoutNeeded();
2707 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2708 performLayout(true /*initial*/, updateInputWindows);
2709 } else if (mode == UPDATE_FOCUS_REMOVING_FOCUS) {
2710 mService.mRoot.performSurfacePlacement(false);
2711 }
2712 }
2713
2714 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
2715 // If we defer assigning layers, then the caller is responsible for doing this part.
2716 getInputMonitor().setInputFocusLw(newFocus, updateInputWindows);
2717 }
2718
2719 adjustForImeIfNeeded();
2720
2721 // We may need to schedule some toast windows to be removed. The toasts for an app that
2722 // does not have input focus are removed within a timeout to prevent apps to redress
2723 // other apps' UI.
2724 scheduleToastWindowsTimeoutIfNeededLocked(oldFocus, newFocus);
2725
2726 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2727 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
2728 }
2729 return true;
2730 }
2731
2732 /**
2733 * Set the new focused app to this display.
2734 *
2735 * @param newFocus the new focused AppWindowToken.
2736 * @return true if the focused app is changed.
2737 */
2738 boolean setFocusedApp(AppWindowToken newFocus) {
2739 if (newFocus != null) {
2740 final DisplayContent appDisplay = newFocus.getDisplayContent();
2741 if (appDisplay != this) {
2742 throw new IllegalStateException(newFocus + " is not on " + getName()
2743 + " but " + ((appDisplay != null) ? appDisplay.getName() : "none"));
2744 }
2745 }
2746 if (mFocusedApp == newFocus) {
2747 return false;
2748 }
2749 mFocusedApp = newFocus;
2750 getInputMonitor().setFocusedAppLw(newFocus);
2751 updateTouchExcludeRegion();
2752 return true;
2753 }
2754
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002755 /** Updates the layer assignment of windows on this display. */
2756 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002757 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07002758 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002759 if (setLayoutNeeded) {
2760 setLayoutNeeded();
2761 }
Robert Carrb1579c82017-09-05 14:54:47 -07002762
Robert Carrf59b8dd2017-10-02 18:58:36 -07002763 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002764 // the application of this transaction until the animation pass triggers
2765 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2766 // the hiding and showing of surfaces.
2767 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002768 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002769 }
2770
Wale Ogunwale1666e312016-12-16 11:27:18 -08002771 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2772 // moving containers or resizing them. Need to investigate the best way to have it automatically
2773 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002774 void layoutAndAssignWindowLayersIfNeeded() {
2775 mService.mWindowsChanged = true;
2776 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002777
2778 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2779 false /*updateInputWindows*/)) {
2780 assignWindowLayers(false /* setLayoutNeeded */);
2781 }
2782
Arthur Hung95b38a92018-07-20 18:56:12 +08002783 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002784 mService.mWindowPlacerLocked.performSurfacePlacement();
Arthur Hung95b38a92018-07-20 18:56:12 +08002785 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002786 }
2787
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002788 /** Returns true if a leaked surface was destroyed */
2789 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002790 // Used to indicate that a surface was leaked.
2791 mTmpWindow = null;
2792 forAllWindows(w -> {
2793 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002794 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002795 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002796 }
2797 if (!mService.mSessions.contains(wsa.mSession)) {
2798 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002799 + w + " surface=" + wsa.mSurfaceController
2800 + " token=" + w.mToken
2801 + " pid=" + w.mSession.mPid
2802 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002803 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002804 mService.mForceRemoves.add(w);
2805 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002806 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002807 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002808 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002809 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002810 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002811 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002812 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002813 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002814 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002815
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002816 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002817 }
2818
2819 /**
lumark90120a82018-08-15 00:33:03 +08002820 * Set input method window for the display.
2821 * @param win Set when window added or Null when destroyed.
2822 */
2823 void setInputMethodWindowLocked(WindowState win) {
2824 mInputMethodWindow = win;
lumark70865a82018-09-17 16:56:26 +08002825 // Update display configuration for IME process.
2826 if (mInputMethodWindow != null) {
2827 final int imePid = mInputMethodWindow.mSession.mPid;
2828 mService.mAtmInternal.onImeWindowSetOnDisplay(imePid,
2829 mInputMethodWindow.getDisplayId());
2830 }
lumark90120a82018-08-15 00:33:03 +08002831 computeImeTarget(true /* updateImeTarget */);
2832 }
2833
2834 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002835 * Determine and return the window that should be the IME target.
2836 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2837 * @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 +00002838 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002839 WindowState computeImeTarget(boolean updateImeTarget) {
lumark90120a82018-08-15 00:33:03 +08002840 if (mInputMethodWindow == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002841 // There isn't an IME so there shouldn't be a target...That was easy!
2842 if (updateImeTarget) {
2843 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2844 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002845 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002846 }
2847 return null;
2848 }
2849
Chavi Weingarten3a748552018-05-14 17:32:42 +00002850 final WindowState curTarget = mService.mInputMethodTarget;
2851 if (!canUpdateImeTarget()) {
2852 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Defer updating IME target");
2853 return curTarget;
2854 }
2855
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002856 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2857 // same display. Or even when the current IME/target are not on the same screen as the next
2858 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002859 mUpdateImeTarget = updateImeTarget;
2860 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002861
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002862
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002863 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2864 // to be on top of it, but it is not -really- where input will go. So look down below
2865 // for a real window to target...
2866 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2867 final AppWindowToken token = target.mAppToken;
2868 if (token != null) {
2869 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2870 if (betterTarget != null) {
2871 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002872 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002873 }
2874 }
2875
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002876 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2877 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002878
chaviw1c13ad32018-06-12 16:44:23 -07002879 // Now, a special case -- if the last target's window is in the process of exiting, but
2880 // not removed, and the new target is home, keep on the last target to avoid flicker.
2881 // Home is a special case since its above other stacks in the ordering list, but layed
2882 // out below the others.
2883 if (curTarget != null && !curTarget.mRemoved && curTarget.isDisplayedLw()
2884 && curTarget.isClosing() && (target == null || target.isActivityTypeHome())) {
chaviw4ad54912018-05-30 11:05:44 -07002885 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "New target is home while current target is "
Chavi Weingarten3a748552018-05-14 17:32:42 +00002886 + "closing, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002887 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002888 }
2889
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002890 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2891 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002892
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002893 if (target == null) {
2894 if (updateImeTarget) {
2895 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2896 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2897 + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002898 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002899 }
2900
2901 return null;
2902 }
2903
2904 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002905 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2906 if (token != null) {
2907
2908 // Now some fun for dealing with window animations that modify the Z order. We need
2909 // to look at all windows below the current target that are in this app, finding the
2910 // highest visible one in layering.
2911 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002912 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002913 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002914 }
2915
2916 if (highestTarget != null) {
2917 final AppTransition appTransition = mService.mAppTransition;
2918 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
Jorim Jaggi35d328a2018-08-14 17:00:20 +02002919 + " animating=" + highestTarget.isAnimating());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002920
2921 if (appTransition.isTransitionSet()) {
2922 // If we are currently setting up for an animation, hold everything until we
2923 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002924 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002925 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002926 }
2927 }
2928 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002929
2930 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2931 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002932 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002933 }
2934
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002935 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002936 }
2937
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002938 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002939 if (target == mService.mInputMethodTarget
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002940 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002941 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002942 }
2943
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002944 mService.mInputMethodTarget = target;
2945 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002946 assignWindowLayers(false /* setLayoutNeeded */);
2947 }
2948
2949 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2950 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2951 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2952 }
2953
2954 // Used to indicate we have reached the first window in the range we are interested in.
2955 mTmpWindow = null;
2956
2957 // TODO: Figure-out a more efficient way to do this.
2958 final WindowState candidate = getWindow(w -> {
2959 if (w == top) {
2960 // Reached the first window in the range we are interested in.
2961 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002962 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002963 if (mTmpWindow == null) {
2964 return false;
2965 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002966
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002967 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2968 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002969 }
2970 // If we reached the bottom of the range of windows we are considering,
2971 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002972 if (w == bottom) {
2973 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002974 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002975 return false;
2976 });
2977
2978 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002979 }
2980
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002981 void setLayoutNeeded() {
2982 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2983 mLayoutNeeded = true;
2984 }
2985
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002986 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002987 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2988 mLayoutNeeded = false;
2989 }
2990
2991 boolean isLayoutNeeded() {
2992 return mLayoutNeeded;
2993 }
2994
Wale Ogunwale02319a62016-09-26 15:21:22 -07002995 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2996 if (mTokenMap.isEmpty()) {
2997 return;
2998 }
2999 pw.println(" Display #" + mDisplayId);
3000 final Iterator<WindowToken> it = mTokenMap.values().iterator();
3001 while (it.hasNext()) {
3002 final WindowToken token = it.next();
3003 pw.print(" ");
3004 pw.print(token);
3005 if (dumpAll) {
3006 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003007 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07003008 } else {
3009 pw.println();
3010 }
3011 }
3012 }
3013
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003014 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003015 final int[] index = new int[1];
3016 forAllWindows(w -> {
3017 final WindowStateAnimator wAnim = w.mWinAnimator;
3018 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
3019 index[0] = index[0] + 1;
3020 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003021 }
3022
Jorim Jaggife762342016-10-13 14:33:27 +02003023 /**
3024 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
3025 */
3026 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
3027 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003028 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02003029 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
3030 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01003031 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02003032 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003033 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02003034 }
3035
Wale Ogunwale494009b82016-10-21 09:01:38 -07003036 boolean checkWaitingForWindows() {
3037
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003038 mHaveBootMsg = false;
3039 mHaveApp = false;
3040 mHaveWallpaper = false;
3041 mHaveKeyguard = true;
3042
3043 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003044 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
3045 return true;
3046 }
3047 if (w.isDrawnLw()) {
3048 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003049 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003050 } else if (w.mAttrs.type == TYPE_APPLICATION
3051 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003052 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003053 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003054 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003055 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003056 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003057 }
3058 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003059 return false;
3060 });
3061
3062 if (visibleWindow != null) {
3063 // We have a visible window.
3064 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003065 }
3066
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003067 // if the wallpaper service is disabled on the device, we're never going to have
3068 // wallpaper, don't bother waiting for it
3069 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
3070 com.android.internal.R.bool.config_enableWallpaperService)
Daichi Hironoc1432222018-03-29 13:06:07 +09003071 && mService.mContext.getResources().getBoolean(
3072 com.android.internal.R.bool.config_checkWallpaperAtBoot)
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003073 && !mService.mOnlyCore;
3074
Wale Ogunwale494009b82016-10-21 09:01:38 -07003075 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
3076 "******** booted=" + mService.mSystemBooted
3077 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003078 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
3079 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
3080 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003081
3082 // If we are turning on the screen to show the boot message, don't do it until the boot
3083 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003084 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003085 return true;
3086 }
3087
3088 // If we are turning on the screen after the boot is completed normally, don't do so until
3089 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003090 if (mService.mSystemBooted
3091 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003092 return true;
3093 }
3094
3095 return false;
3096 }
3097
Jorim Jaggi8f520872018-08-14 17:00:20 +02003098 void updateWindowsForAnimator() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003099 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003100 }
3101
Jorim Jaggi8f520872018-08-14 17:00:20 +02003102 /**
3103 * Updates the {@link TaskStack#setAnimationBackground} for all windows.
3104 */
3105 void updateBackgroundForAnimator() {
Jorim Jaggi7156b652016-10-25 08:31:58 -07003106 resetAnimationBackgroundAnimator();
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003107 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003108 }
3109
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003110 boolean isInputMethodClientFocus(int uid, int pid) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003111 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003112 if (imFocus == null) {
3113 return false;
3114 }
3115
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003116 if (DEBUG_INPUT_METHOD) {
3117 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003118 Slog.i(TAG_WM, "Current focus: " + mCurrentFocus + " displayId=" + mDisplayId);
3119 Slog.i(TAG_WM, "Last focus: " + mLastFocus + " displayId=" + mDisplayId);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003120 }
3121
Wale Ogunwale494009b82016-10-21 09:01:38 -07003122 if (DEBUG_INPUT_METHOD) {
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003123 Slog.i(TAG_WM, "IM target uid/pid: " + imFocus.mSession.mUid
3124 + "/" + imFocus.mSession.mPid);
3125 Slog.i(TAG_WM, "Requesting client uid/pid: " + uid + "/" + pid);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003126 }
3127
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003128 return imFocus.mSession.mUid == uid && imFocus.mSession.mPid == pid;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003129 }
3130
3131 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003132 final WindowState win = getWindow(
3133 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
3134 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003135 }
3136
3137 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003138 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003139 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003140 final int curValue = w.mSystemUiVisibility;
3141 final int diff = (curValue ^ visibility) & globalDiff;
3142 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003143 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003144 w.mSeq++;
3145 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003146 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003147 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
3148 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07003149 visibility, newValue, diff);
3150 }
3151 } catch (RemoteException e) {
3152 // so sorry
3153 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003154 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003155 }
3156
3157 void onWindowFreezeTimeout() {
3158 Slog.w(TAG_WM, "Window freeze timeout expired.");
3159 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003160
3161 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07003162 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003163 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003164 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07003165 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003166 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
3167 - mService.mDisplayFreezeTime);
3168 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003169 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003170 mService.mWindowPlacerLocked.performSurfacePlacement();
3171 }
3172
3173 void waitForAllWindowsDrawn() {
3174 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003175 forAllWindows(w -> {
3176 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
3177 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
3178 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003179 // Force add to mResizingWindows.
chaviw9c81e632018-07-31 11:17:52 -07003180 w.resetLastContentInsets();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003181 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003182 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003183 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003184 }
3185
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003186 // TODO: Super crazy long method that should be broken down...
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003187 void applySurfaceChangesTransaction(boolean recoveringMemory) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003188
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003189 final int dw = mDisplayInfo.logicalWidth;
3190 final int dh = mDisplayInfo.logicalHeight;
3191 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
3192
3193 mTmpUpdateAllDrawn.clear();
3194
3195 int repeats = 0;
3196 do {
3197 repeats++;
3198 if (repeats > 6) {
3199 Slog.w(TAG, "Animation repeat aborted after too many iterations");
3200 clearLayoutNeeded();
3201 break;
3202 }
3203
3204 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
3205 pendingLayoutChanges);
3206
Andrii Kulian839def92016-11-02 10:58:58 -07003207 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
3208 // the wallpaper window jumping across displays.
3209 // Remove check for default display when there will be support for multiple wallpaper
3210 // targets (on different displays).
3211 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003212 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003213 }
3214
Riddle Hsu654a6f92018-07-13 22:59:36 +08003215 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003216 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
Robert Carrae606b42018-02-15 15:36:23 -08003217 if (mService.updateOrientationFromAppTokensLocked(mDisplayId)) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003218 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07003219 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003220 }
3221 }
3222
3223 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
3224 setLayoutNeeded();
3225 }
3226
3227 // FIRST LOOP: Perform a layout, if needed.
3228 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
3229 performLayout(repeats == 1, false /* updateInputWindows */);
3230 } else {
3231 Slog.w(TAG, "Layout repeat skipped after too many iterations");
3232 }
3233
3234 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
3235 pendingLayoutChanges = 0;
3236
3237 if (isDefaultDisplay) {
3238 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003239 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003240 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
3241 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
3242 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
3243 }
3244 } while (pendingLayoutChanges != 0);
3245
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003246 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003247
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003248 mTmpRecoveringMemory = recoveringMemory;
3249 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01003250 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003251
Jorim Jaggif1292892018-09-10 11:58:13 +02003252 mLastHasContent = mTmpApplySurfaceChangesTransactionState.displayHasContent;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003253 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Jorim Jaggif1292892018-09-10 11:58:13 +02003254 mLastHasContent,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003255 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
3256 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003257 true /* inTraversal, must call performTraversalInTrans... below */);
3258
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003259 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
3260 if (wallpaperVisible != mLastWallpaperVisible) {
3261 mLastWallpaperVisible = wallpaperVisible;
3262 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
3263 }
3264
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003265 while (!mTmpUpdateAllDrawn.isEmpty()) {
3266 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
3267 // See if any windows have been drawn, so they (and others associated with them)
3268 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07003269 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003270 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003271 }
3272
Bryce Leef3c6a472017-11-14 14:53:06 -08003273 private void updateBounds() {
3274 calculateBounds(mTmpBounds);
3275 setBounds(mTmpBounds);
3276 }
3277
3278 // Determines the current display bounds based on the current state
3279 private void calculateBounds(Rect out) {
3280 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
3281 final int orientation = mDisplayInfo.rotation;
3282 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
3283 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
3284 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
3285 int width = mDisplayInfo.logicalWidth;
3286 int left = (physWidth - width) / 2;
3287 int height = mDisplayInfo.logicalHeight;
3288 int top = (physHeight - height) / 2;
3289 out.set(left, top, left + width, top + height);
3290 }
3291
3292 @Override
3293 public void getBounds(Rect out) {
3294 calculateBounds(out);
3295 }
3296
3297 private void getBounds(Rect out, int orientation) {
3298 getBounds(out);
3299
3300 // Rotate the Rect if needed.
3301 final int currentRotation = mDisplayInfo.rotation;
3302 final int rotationDelta = deltaRotation(currentRotation, orientation);
3303 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
3304 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
3305 mTmpRectF.set(out);
3306 mTmpMatrix.mapRect(mTmpRectF);
3307 mTmpRectF.round(out);
3308 }
3309 }
3310
Louis Chang7501e332018-08-20 13:08:39 +08003311 int getSurfaceSize() {
3312 return mSurfaceSize;
3313 }
3314
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003315 void performLayout(boolean initial, boolean updateInputWindows) {
3316 if (!isLayoutNeeded()) {
3317 return;
3318 }
3319 clearLayoutNeeded();
3320
3321 final int dw = mDisplayInfo.logicalWidth;
3322 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003323 if (DEBUG_LAYOUT) {
3324 Slog.v(TAG, "-------------------------------------");
3325 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
3326 }
3327
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01003328 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
3329 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00003330 // TODO: Not sure if we really need to set the rotation here since we are updating from the
3331 // display info above...
3332 mDisplayFrames.mRotation = mRotation;
3333 mService.mPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003334 if (isDefaultDisplay) {
3335 // Not needed on non-default displays.
3336 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
3337 mService.mScreenRect.set(0, 0, dw, dh);
3338 }
3339
Adrian Roos5251b1d2018-03-23 18:57:43 +01003340 int seq = mLayoutSeq + 1;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003341 if (seq < 0) seq = 0;
Adrian Roos5251b1d2018-03-23 18:57:43 +01003342 mLayoutSeq = seq;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003343
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003344 // Used to indicate that we have processed the dream window and all additional windows are
3345 // behind it.
3346 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003347 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003348
3349 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003350 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003351
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003352 // Used to indicate that we have processed the dream window and all additional attached
3353 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003354 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003355 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003356
3357 // Now perform layout of attached windows, which usually depend on the position of the
3358 // window they are attached to. XXX does not deal with windows that are attached to windows
3359 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003360 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003361
3362 // Window frames may have changed. Tell the input dispatcher about it.
Arthur Hung95b38a92018-07-20 18:56:12 +08003363 mInputMonitor.layoutInputConsumers(dw, dh);
3364 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003365 if (updateInputWindows) {
Arthur Hung95b38a92018-07-20 18:56:12 +08003366 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003367 }
3368
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003369 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
3370 }
3371
3372 /**
3373 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
3374 * In portrait mode, it grabs the full screenshot.
3375 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003376 * @param config of the output bitmap
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003377 */
chaviw0315a1a2018-03-05 15:28:35 -08003378 Bitmap screenshotDisplayLocked(Bitmap.Config config) {
3379 if (!mService.mPolicy.isScreenOn()) {
3380 if (DEBUG_SCREENSHOT) {
3381 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003382 }
chaviw0315a1a2018-03-05 15:28:35 -08003383 return null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003384 }
chaviwfbe47df2017-11-10 16:14:49 -08003385
chaviw0315a1a2018-03-05 15:28:35 -08003386 int dw = mDisplayInfo.logicalWidth;
3387 int dh = mDisplayInfo.logicalHeight;
chaviwfbe47df2017-11-10 16:14:49 -08003388
chaviw0315a1a2018-03-05 15:28:35 -08003389 if (dw <= 0 || dh <= 0) {
3390 return null;
3391 }
chaviwfbe47df2017-11-10 16:14:49 -08003392
chaviw0315a1a2018-03-05 15:28:35 -08003393 final Rect frame = new Rect(0, 0, dw, dh);
chaviwfbe47df2017-11-10 16:14:49 -08003394
chaviw0315a1a2018-03-05 15:28:35 -08003395 // The screenshot API does not apply the current screen rotation.
3396 int rot = mDisplay.getRotation();
chaviwfbe47df2017-11-10 16:14:49 -08003397
chaviw0315a1a2018-03-05 15:28:35 -08003398 if (rot == ROTATION_90 || rot == ROTATION_270) {
3399 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3400 }
chaviwfbe47df2017-11-10 16:14:49 -08003401
chaviw0315a1a2018-03-05 15:28:35 -08003402 // SurfaceFlinger is not aware of orientation, so convert our logical
3403 // crop to SurfaceFlinger's portrait orientation.
3404 convertCropForSurfaceFlinger(frame, rot, dw, dh);
3405
3406 final ScreenRotationAnimation screenRotationAnimation =
3407 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3408 final boolean inRotation = screenRotationAnimation != null &&
3409 screenRotationAnimation.isAnimating();
3410 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
3411
3412 // TODO(b/68392460): We should screenshot Task controls directly
3413 // but it's difficult at the moment as the Task doesn't have the
3414 // correct size set.
chaviw08520a02018-09-10 16:44:56 -07003415 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, inRotation, rot);
chaviw0315a1a2018-03-05 15:28:35 -08003416 if (bitmap == null) {
3417 Slog.w(TAG_WM, "Failed to take screenshot");
3418 return null;
3419 }
3420
3421 // Create a copy of the screenshot that is immutable and backed in ashmem.
3422 // This greatly reduces the overhead of passing the bitmap between processes.
3423 final Bitmap ret = bitmap.createAshmemBitmap(config);
3424 bitmap.recycle();
3425 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003426 }
3427
3428 // TODO: Can this use createRotationMatrix()?
3429 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3430 if (rot == Surface.ROTATION_90) {
3431 final int tmp = crop.top;
3432 crop.top = dw - crop.right;
3433 crop.right = crop.bottom;
3434 crop.bottom = dw - crop.left;
3435 crop.left = tmp;
3436 } else if (rot == Surface.ROTATION_180) {
3437 int tmp = crop.top;
3438 crop.top = dh - crop.bottom;
3439 crop.bottom = dh - tmp;
3440 tmp = crop.right;
3441 crop.right = dw - crop.left;
3442 crop.left = dw - tmp;
3443 } else if (rot == Surface.ROTATION_270) {
3444 final int tmp = crop.top;
3445 crop.top = crop.left;
3446 crop.left = dh - crop.bottom;
3447 crop.bottom = crop.right;
3448 crop.right = dh - tmp;
3449 }
3450 }
3451
3452 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003453 // Used to indicate the layout is needed.
3454 mTmpWindow = null;
3455
3456 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003457 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003458 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003459 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003460 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003461 w.setDisplayLayoutNeeded();
3462 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003463 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003464
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003465 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003466 mService.mWindowPlacerLocked.performSurfacePlacement();
3467 }
3468 }
3469
Wale Ogunwale1666e312016-12-16 11:27:18 -08003470 void setExitingTokensHasVisible(boolean hasVisible) {
3471 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3472 mExitingTokens.get(i).hasVisible = hasVisible;
3473 }
3474
3475 // Initialize state of exiting applications.
3476 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3477 }
3478
3479 void removeExistingTokensIfPossible() {
3480 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3481 final WindowToken token = mExitingTokens.get(i);
3482 if (!token.hasVisible) {
3483 mExitingTokens.remove(i);
3484 }
3485 }
3486
3487 // Time to remove any exiting applications?
3488 mTaskStackContainers.removeExistingAppTokensIfPossible();
3489 }
3490
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003491 @Override
3492 void onDescendantOverrideConfigurationChanged() {
3493 setLayoutNeeded();
3494 mService.requestTraversal();
3495 }
3496
David Stevens9440dc82017-03-16 19:00:20 -07003497 boolean okToDisplay() {
3498 if (mDisplayId == DEFAULT_DISPLAY) {
3499 return !mService.mDisplayFrozen
3500 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3501 }
3502 return mDisplayInfo.state == Display.STATE_ON;
3503 }
3504
3505 boolean okToAnimate() {
3506 return okToDisplay() &&
3507 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3508 }
3509
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003510 static final class TaskForResizePointSearchResult {
3511 boolean searchDone;
3512 Task taskForResize;
3513
3514 void reset() {
3515 searchDone = false;
3516 taskForResize = null;
3517 }
3518 }
Robert Carr3b716242016-08-16 16:02:21 -07003519
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003520 private static final class ApplySurfaceChangesTransactionState {
3521 boolean displayHasContent;
3522 boolean obscured;
3523 boolean syswin;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003524 float preferredRefreshRate;
3525 int preferredModeId;
3526
3527 void reset() {
3528 displayHasContent = false;
3529 obscured = false;
3530 syswin = false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003531 preferredRefreshRate = 0;
3532 preferredModeId = 0;
3533 }
3534 }
3535
3536 private static final class ScreenshotApplicationState {
3537 WindowState appWin;
3538 int maxLayer;
3539 int minLayer;
3540 boolean screenshotReady;
3541
3542 void reset(boolean screenshotReady) {
3543 appWin = null;
3544 maxLayer = 0;
3545 minLayer = 0;
3546 this.screenshotReady = screenshotReady;
3547 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3548 }
3549 }
3550
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003551 /**
3552 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3553 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3554 * homogeneous children type which is currently required by sub-classes of
3555 * {@link WindowContainer} class.
3556 */
3557 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3558
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003559 DisplayChildWindowContainer(WindowManagerService service) {
3560 super(service);
3561 }
3562
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003563 @Override
3564 boolean fillsParent() {
3565 return true;
3566 }
3567
3568 @Override
3569 boolean isVisible() {
3570 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003571 }
3572 }
3573
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003574 /**
3575 * Window container class that contains all containers on this display relating to Apps.
3576 * I.e Activities.
3577 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003578 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003579 /**
3580 * A control placed at the appropriate level for transitions to occur.
3581 */
chaviw23ee71c2017-12-18 11:29:41 -08003582 SurfaceControl mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02003583 SurfaceControl mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02003584 SurfaceControl mHomeAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003585
Robert Carrf7a7ca82017-12-06 13:17:07 -08003586 /**
3587 * Given that the split-screen divider does not have an AppWindowToken, it
3588 * will have to live inside of a "NonAppWindowContainer", in particular
3589 * {@link DisplayContent#mAboveAppWindowsContainers}. However, in visual Z order
3590 * it will need to be interleaved with some of our children, appearing on top of
3591 * both docked stacks but underneath any assistant stacks.
3592 *
3593 * To solve this problem we have this anchor control, which will always exist so
3594 * we can always assign it the correct value in our {@link #assignChildLayers}.
3595 * Likewise since it always exists, {@link AboveAppWindowContainers} can always
3596 * assign the divider a layer relative to it. This way we prevent linking lifecycle
3597 * events between the two containers.
3598 */
3599 SurfaceControl mSplitScreenDividerAnchor = null;
3600
Wale Ogunwale61911492017-10-11 08:50:50 -07003601 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3602 // through the list to find them.
3603 private TaskStack mHomeStack = null;
3604 private TaskStack mPinnedStack = null;
3605 private TaskStack mSplitScreenPrimaryStack = null;
3606
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003607 TaskStackContainers(WindowManagerService service) {
3608 super(service);
3609 }
3610
Wale Ogunwale61911492017-10-11 08:50:50 -07003611 /**
3612 * Returns the topmost stack on the display that is compatible with the input windowing mode
3613 * and activity type. Null is no compatible stack on the display.
3614 */
3615 TaskStack getStack(int windowingMode, int activityType) {
3616 if (activityType == ACTIVITY_TYPE_HOME) {
3617 return mHomeStack;
3618 }
3619 if (windowingMode == WINDOWING_MODE_PINNED) {
3620 return mPinnedStack;
3621 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3622 return mSplitScreenPrimaryStack;
3623 }
3624 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3625 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003626 if (activityType == ACTIVITY_TYPE_UNDEFINED
3627 && windowingMode == stack.getWindowingMode()) {
3628 // Passing in undefined type means we want to match the topmost stack with the
3629 // windowing mode.
3630 return stack;
3631 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003632 if (stack.isCompatible(windowingMode, activityType)) {
3633 return stack;
3634 }
3635 }
3636 return null;
3637 }
3638
3639 @VisibleForTesting
3640 TaskStack getTopStack() {
3641 return mTaskStackContainers.getChildCount() > 0
3642 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3643 }
3644
3645 TaskStack getHomeStack() {
3646 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3647 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3648 }
3649 return mHomeStack;
3650 }
3651
3652 TaskStack getPinnedStack() {
3653 return mPinnedStack;
3654 }
3655
Matthew Ng64e77cf2017-10-31 14:01:31 -07003656 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003657 return mSplitScreenPrimaryStack;
3658 }
3659
Winson Chunge2d72172018-01-25 17:46:20 +00003660 ArrayList<Task> getVisibleTasks() {
3661 final ArrayList<Task> visibleTasks = new ArrayList<>();
3662 forAllTasks(task -> {
3663 if (task.isVisible()) {
3664 visibleTasks.add(task);
3665 }
3666 });
3667 return visibleTasks;
3668 }
3669
Andrii Kulian839def92016-11-02 10:58:58 -07003670 /**
3671 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003672 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003673 */
3674 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003675 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003676 addChild(stack, onTop);
3677 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003678 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003679
Wale Ogunwale61911492017-10-11 08:50:50 -07003680 void onStackWindowingModeChanged(TaskStack stack) {
3681 removeStackReferenceIfNeeded(stack);
3682 addStackReferenceIfNeeded(stack);
3683 if (stack == mPinnedStack && getTopStack() != stack) {
3684 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3685 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3686 }
3687 }
3688
3689 private void addStackReferenceIfNeeded(TaskStack stack) {
3690 if (stack.isActivityTypeHome()) {
3691 if (mHomeStack != null) {
Louis Chang7d0037c2018-08-13 12:42:06 +08003692 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
Wale Ogunwale61911492017-10-11 08:50:50 -07003693 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
Louis Chang7d0037c2018-08-13 12:42:06 +08003694
Wale Ogunwale61911492017-10-11 08:50:50 -07003695 }
Louis Chang7d0037c2018-08-13 12:42:06 +08003696 mHomeStack = stack;
Wale Ogunwale61911492017-10-11 08:50:50 -07003697 }
3698 final int windowingMode = stack.getWindowingMode();
3699 if (windowingMode == WINDOWING_MODE_PINNED) {
3700 if (mPinnedStack != null) {
3701 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3702 + mPinnedStack + " already exist on display=" + this
3703 + " stack=" + stack);
3704 }
3705 mPinnedStack = stack;
3706 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3707 if (mSplitScreenPrimaryStack != null) {
3708 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3709 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3710 + " already exist on display=" + this + " stack=" + stack);
3711 }
3712 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003713 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003714 }
3715 }
3716
3717 private void removeStackReferenceIfNeeded(TaskStack stack) {
3718 if (stack == mHomeStack) {
3719 mHomeStack = null;
3720 } else if (stack == mPinnedStack) {
3721 mPinnedStack = null;
3722 } else if (stack == mSplitScreenPrimaryStack) {
3723 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003724 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3725 mService.setDockedStackCreateStateLocked(
3726 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3727 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003728 }
Andrii Kulian839def92016-11-02 10:58:58 -07003729 }
3730
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003731 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003732 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3733 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003734 addChild(stack, addIndex);
3735 setLayoutNeeded();
3736 }
3737
Wale Ogunwale61911492017-10-11 08:50:50 -07003738 @Override
3739 protected void removeChild(TaskStack stack) {
3740 super.removeChild(stack);
3741 removeStackReferenceIfNeeded(stack);
3742 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003743
3744 @Override
3745 boolean isOnTop() {
3746 // Considered always on top
3747 return true;
3748 }
3749
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003750 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003751 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003752 if (child.getWindowConfiguration().isAlwaysOnTop()
3753 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003754 // This stack is always-on-top, override the default behavior.
3755 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3756
3757 // Moving to its current position, as we must call super but we don't want to
3758 // perform any meaningful action.
3759 final int currentPosition = mChildren.indexOf(child);
3760 super.positionChildAt(currentPosition, child, false /* includingParents */);
3761 return;
3762 }
3763
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003764 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3765 super.positionChildAt(targetPosition, child, includingParents);
3766
Riddle Hsu2f6e1742018-08-23 22:44:36 +08003767 if (includingParents) {
3768 // We still want to move the display of this stack container to top because even the
3769 // target position is adjusted to non-top, the intention of the condition is to have
3770 // higher z-order to gain focus (e.g. moving a task of a fullscreen stack to front
3771 // in a non-top display which is using picture-in-picture mode).
3772 final int topChildPosition = getChildCount() - 1;
3773 if (targetPosition < topChildPosition && position >= topChildPosition) {
3774 getParent().positionChildAt(POSITION_TOP, this /* child */,
3775 true /* includingParents */);
3776 }
3777 }
3778
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003779 setLayoutNeeded();
3780 }
3781
3782 /**
3783 * When stack is added or repositioned, find a proper position for it.
3784 * This will make sure that pinned stack always stays on top.
3785 * @param requestedPosition Position requested by caller.
3786 * @param stack Stack to be added or positioned.
3787 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3788 * @return The proper position for the stack.
3789 */
3790 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09003791 if (stack.inPinnedWindowingMode()) {
Kazuki Takisef85197b2018-06-18 18:18:36 +09003792 return POSITION_TOP;
Kazuki Takise148d00a2018-05-31 15:32:19 +09003793 }
3794
Kazuki Takisef85197b2018-06-18 18:18:36 +09003795 final int topChildPosition = mChildren.size() - 1;
3796 int belowAlwaysOnTopPosition = POSITION_BOTTOM;
3797 for (int i = topChildPosition; i >= 0; --i) {
3798 if (getStacks().get(i) != stack && !getStacks().get(i).isAlwaysOnTop()) {
3799 belowAlwaysOnTopPosition = i;
3800 break;
3801 }
3802 }
3803
3804 // The max possible position we can insert the stack at.
3805 int maxPosition = POSITION_TOP;
3806 // The min possible position we can insert the stack at.
3807 int minPosition = POSITION_BOTTOM;
3808
3809 if (stack.isAlwaysOnTop()) {
3810 if (hasPinnedStack()) {
3811 // Always-on-top stacks go below the pinned stack.
3812 maxPosition = getStacks().indexOf(mPinnedStack) - 1;
3813 }
3814 // Always-on-top stacks need to be above all other stacks.
3815 minPosition = belowAlwaysOnTopPosition !=
3816 POSITION_BOTTOM ? belowAlwaysOnTopPosition : topChildPosition;
3817 } else {
3818 // Other stacks need to be below the always-on-top stacks.
3819 maxPosition = belowAlwaysOnTopPosition !=
Arthur Hung928d8dc2018-07-18 15:38:05 +08003820 POSITION_BOTTOM ? belowAlwaysOnTopPosition : 0;
Kazuki Takisef85197b2018-06-18 18:18:36 +09003821 }
3822
3823 int targetPosition = requestedPosition;
3824 targetPosition = Math.min(targetPosition, maxPosition);
3825 targetPosition = Math.max(targetPosition, minPosition);
3826
3827 int prevPosition = getStacks().indexOf(stack);
3828 // The positions we calculated above (maxPosition, minPosition) do not take into
3829 // consideration the following edge cases.
3830 // 1) We need to adjust the position depending on the value "adding".
3831 // 2) When we are moving a stack to another position, we also need to adjust the
3832 // position depending on whether the stack is moving to a higher or lower position.
3833 if ((targetPosition != requestedPosition) &&
3834 (adding || targetPosition < prevPosition)) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09003835 targetPosition++;
3836 }
3837
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003838 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003839 }
3840
3841 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003842 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3843 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003844 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003845 if (super.forAllWindows(callback, traverseTopToBottom)) {
3846 return true;
3847 }
3848 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3849 return true;
3850 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003851 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003852 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3853 return true;
3854 }
3855 if (super.forAllWindows(callback, traverseTopToBottom)) {
3856 return true;
3857 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003858 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003859 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003860 }
3861
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003862 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003863 boolean traverseTopToBottom) {
3864 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3865 // app tokens.
3866 // TODO: Investigate if we need to continue to do this or if we can just process them
3867 // in-order.
3868 if (traverseTopToBottom) {
3869 for (int i = mChildren.size() - 1; i >= 0; --i) {
3870 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3871 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003872 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3873 traverseTopToBottom)) {
3874 return true;
3875 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003876 }
3877 }
3878 } else {
3879 final int count = mChildren.size();
3880 for (int i = 0; i < count; ++i) {
3881 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3882 final int appTokensCount = appTokens.size();
3883 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003884 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3885 traverseTopToBottom)) {
3886 return true;
3887 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003888 }
3889 }
3890 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003891 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003892 }
3893
Wale Ogunwale1666e312016-12-16 11:27:18 -08003894 void setExitingTokensHasVisible(boolean hasVisible) {
3895 for (int i = mChildren.size() - 1; i >= 0; --i) {
3896 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3897 for (int j = appTokens.size() - 1; j >= 0; --j) {
3898 appTokens.get(j).hasVisible = hasVisible;
3899 }
3900 }
3901 }
3902
3903 void removeExistingAppTokensIfPossible() {
3904 for (int i = mChildren.size() - 1; i >= 0; --i) {
3905 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3906 for (int j = appTokens.size() - 1; j >= 0; --j) {
3907 final AppWindowToken token = appTokens.get(j);
3908 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3909 && (!token.mIsExiting || token.isEmpty())) {
3910 // Make sure there is no animation running on this token, so any windows
3911 // associated with it will be removed as soon as their animations are
3912 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003913 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003914 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3915 "performLayout: App token exiting now removed" + token);
3916 token.removeIfPossible();
3917 }
3918 }
3919 }
3920 }
3921
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003922 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003923 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003924 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
3925 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003926 // Apps and their containers are not allowed to specify an orientation while the
Wale Ogunwaled0c225c2018-06-15 14:47:46 -07003927 // docked or freeform stack is visible...except for the home stack if the docked
3928 // stack is minimized and it actually set something and the bounds is different from
3929 // the display.
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003930 if (mHomeStack != null && mHomeStack.isVisible()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07003931 && mDividerControllerLocked.isMinimizedDock()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07003932 && !(mDividerControllerLocked.isHomeStackResizable()
3933 && mHomeStack.matchParentBounds())) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003934 final int orientation = mHomeStack.getOrientation();
3935 if (orientation != SCREEN_ORIENTATION_UNSET) {
3936 return orientation;
3937 }
3938 }
3939 return SCREEN_ORIENTATION_UNSPECIFIED;
3940 }
3941
3942 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07003943 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
3944 PackageManager.FEATURE_AUTOMOTIVE);
3945 if (isCar) {
3946 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
3947 // allow anything but the default orientation.
3948 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07003949 "Forcing UNSPECIFIED orientation in car for display id=" + mDisplayId
3950 + ". Ignoring " + orientation);
Dean Harding3e5a1522017-10-06 09:19:01 -07003951 return SCREEN_ORIENTATION_UNSPECIFIED;
3952 }
3953
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003954 if (orientation != SCREEN_ORIENTATION_UNSET
3955 && orientation != SCREEN_ORIENTATION_BEHIND) {
3956 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07003957 "App is requesting an orientation, return " + orientation
3958 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003959 return orientation;
3960 }
3961
3962 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07003963 "No app is requesting an orientation, return " + mLastOrientation
3964 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003965 // The next app has not been requested to be visible, so we keep the current orientation
3966 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003967 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003968 }
Robert Carrb1579c82017-09-05 14:54:47 -07003969
3970 @Override
3971 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carr2f8aa392018-01-31 14:46:51 -08003972 assignStackOrdering(t);
Robert Carrf7a7ca82017-12-06 13:17:07 -08003973
Robert Carr2f8aa392018-01-31 14:46:51 -08003974 for (int i = 0; i < mChildren.size(); i++) {
3975 final TaskStack s = mChildren.get(i);
3976 s.assignChildLayers(t);
3977 }
3978 }
3979
3980 void assignStackOrdering(SurfaceControl.Transaction t) {
Jorim Jaggibe418292018-03-26 16:14:12 +02003981
Robert Carrf7a7ca82017-12-06 13:17:07 -08003982 final int HOME_STACK_STATE = 0;
3983 final int NORMAL_STACK_STATE = 1;
3984 final int ALWAYS_ON_TOP_STATE = 2;
3985
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003986 int layer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08003987 int layerForAnimationLayer = 0;
Jorim Jaggibe418292018-03-26 16:14:12 +02003988 int layerForBoostedAnimationLayer = 0;
Jorim Jaggi391790622018-04-18 15:30:44 +02003989 int layerForHomeAnimationLayer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08003990
Robert Carrf7a7ca82017-12-06 13:17:07 -08003991 for (int state = 0; state <= ALWAYS_ON_TOP_STATE; state++) {
3992 for (int i = 0; i < mChildren.size(); i++) {
3993 final TaskStack s = mChildren.get(i);
Robert Carr2f8aa392018-01-31 14:46:51 -08003994 if (state == HOME_STACK_STATE && !s.isActivityTypeHome()) {
3995 continue;
3996 } else if (state == NORMAL_STACK_STATE && (s.isActivityTypeHome()
3997 || s.isAlwaysOnTop())) {
3998 continue;
3999 } else if (state == ALWAYS_ON_TOP_STATE && !s.isAlwaysOnTop()) {
4000 continue;
4001 }
4002 s.assignLayer(t, layer++);
4003 if (s.inSplitScreenWindowingMode() && mSplitScreenDividerAnchor != null) {
4004 t.setLayer(mSplitScreenDividerAnchor, layer++);
4005 }
Winson Chungd41f71d2018-03-16 15:26:07 -07004006 if ((s.isTaskAnimating() || s.isAppAnimating())
4007 && state != ALWAYS_ON_TOP_STATE) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004008 // Ensure the animation layer ends up above the
4009 // highest animating stack and no higher.
4010 layerForAnimationLayer = layer++;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004011 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004012 if (state != ALWAYS_ON_TOP_STATE) {
4013 layerForBoostedAnimationLayer = layer++;
4014 }
Robert Carrf7a7ca82017-12-06 13:17:07 -08004015 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004016 if (state == HOME_STACK_STATE) {
4017 layerForHomeAnimationLayer = layer++;
4018 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004019 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004020 if (mAppAnimationLayer != null) {
4021 t.setLayer(mAppAnimationLayer, layerForAnimationLayer);
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004022 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004023 if (mBoostedAppAnimationLayer != null) {
4024 t.setLayer(mBoostedAppAnimationLayer, layerForBoostedAnimationLayer);
4025 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004026 if (mHomeAppAnimationLayer != null) {
4027 t.setLayer(mHomeAppAnimationLayer, layerForHomeAnimationLayer);
4028 }
Robert Carrb1579c82017-09-05 14:54:47 -07004029 }
4030
4031 @Override
Jorim Jaggi391790622018-04-18 15:30:44 +02004032 SurfaceControl getAppAnimationLayer(@AnimationLayer int animationLayer) {
4033 switch (animationLayer) {
4034 case ANIMATION_LAYER_BOOSTED:
4035 return mBoostedAppAnimationLayer;
4036 case ANIMATION_LAYER_HOME:
4037 return mHomeAppAnimationLayer;
4038 case ANIMATION_LAYER_STANDARD:
4039 default:
4040 return mAppAnimationLayer;
4041 }
chaviw23ee71c2017-12-18 11:29:41 -08004042 }
4043
Robert Carrf7a7ca82017-12-06 13:17:07 -08004044 SurfaceControl getSplitScreenDividerAnchor() {
4045 return mSplitScreenDividerAnchor;
4046 }
4047
chaviw23ee71c2017-12-18 11:29:41 -08004048 @Override
Robert Carrb1579c82017-09-05 14:54:47 -07004049 void onParentSet() {
4050 super.onParentSet();
4051 if (getParent() != null) {
chaviw23ee71c2017-12-18 11:29:41 -08004052 mAppAnimationLayer = makeChildSurface(null)
4053 .setName("animationLayer")
4054 .build();
Jorim Jaggibe418292018-03-26 16:14:12 +02004055 mBoostedAppAnimationLayer = makeChildSurface(null)
4056 .setName("boostedAnimationLayer")
4057 .build();
Jorim Jaggi391790622018-04-18 15:30:44 +02004058 mHomeAppAnimationLayer = makeChildSurface(null)
4059 .setName("homeAnimationLayer")
4060 .build();
Robert Carrf7a7ca82017-12-06 13:17:07 -08004061 mSplitScreenDividerAnchor = makeChildSurface(null)
4062 .setName("splitScreenDividerAnchor")
4063 .build();
4064 getPendingTransaction()
4065 .show(mAppAnimationLayer)
Jorim Jaggibe418292018-03-26 16:14:12 +02004066 .show(mBoostedAppAnimationLayer)
Jorim Jaggi391790622018-04-18 15:30:44 +02004067 .show(mHomeAppAnimationLayer)
Robert Carrf7a7ca82017-12-06 13:17:07 -08004068 .show(mSplitScreenDividerAnchor);
chaviw23ee71c2017-12-18 11:29:41 -08004069 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07004070 } else {
chaviw23ee71c2017-12-18 11:29:41 -08004071 mAppAnimationLayer.destroy();
4072 mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02004073 mBoostedAppAnimationLayer.destroy();
4074 mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02004075 mHomeAppAnimationLayer.destroy();
4076 mHomeAppAnimationLayer = null;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004077 mSplitScreenDividerAnchor.destroy();
4078 mSplitScreenDividerAnchor = null;
Robert Carrb1579c82017-09-05 14:54:47 -07004079 }
4080 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004081 }
4082
Robert Carree4d4b92017-11-22 12:21:46 -08004083 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004084 AboveAppWindowContainers(String name, WindowManagerService service) {
4085 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08004086 }
4087
Robert Carrb9506032018-02-13 13:54:00 -08004088 @Override
Vishnu Nair83537a72018-07-19 21:27:48 -07004089 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
4090 final SurfaceControl.Builder builder = super.makeChildSurface(child);
4091 if (child instanceof WindowToken && ((WindowToken) child).mRoundedCornerOverlay) {
4092 // To draw above the ColorFade layer during the screen off transition, the
4093 // rounded corner overlays need to be at the root of the surface hierarchy.
4094 // TODO: move the ColorLayer into the display overlay layer such that this is not
4095 // necessary anymore.
4096 builder.setParent(null);
4097 }
4098 return builder;
4099 }
4100
4101 @Override
Robert Carrb9506032018-02-13 13:54:00 -08004102 void assignChildLayers(SurfaceControl.Transaction t) {
4103 assignChildLayers(t, null /* imeContainer */);
4104 }
4105
Robert Carree4d4b92017-11-22 12:21:46 -08004106 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
4107 boolean needAssignIme = imeContainer != null
4108 && imeContainer.getSurfaceControl() != null;
4109 for (int j = 0; j < mChildren.size(); ++j) {
4110 final WindowToken wt = mChildren.get(j);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004111
4112 // See {@link mSplitScreenDividerAnchor}
4113 if (wt.windowType == TYPE_DOCK_DIVIDER) {
4114 wt.assignRelativeLayer(t, mTaskStackContainers.getSplitScreenDividerAnchor(), 1);
4115 continue;
4116 }
Vishnu Nair83537a72018-07-19 21:27:48 -07004117 if (wt.mRoundedCornerOverlay) {
4118 wt.assignLayer(t, WindowManagerPolicy.COLOR_FADE_LAYER + 1);
4119 continue;
4120 }
Robert Carree4d4b92017-11-22 12:21:46 -08004121 wt.assignLayer(t, j);
4122 wt.assignChildLayers(t);
4123
4124 int layer = mService.mPolicy.getWindowLayerFromTypeLw(
4125 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08004126
4127 if (needAssignIme && layer >= mService.mPolicy.getWindowLayerFromTypeLw(
4128 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004129 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08004130 needAssignIme = false;
4131 }
4132 }
4133 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004134 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08004135 }
4136 }
4137 }
4138
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004139 /**
4140 * Window container class that contains all containers on this display that are not related to
4141 * Apps. E.g. status bar.
4142 */
Robert Carree4d4b92017-11-22 12:21:46 -08004143 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07004144 /**
4145 * Compares two child window tokens returns -1 if the first is lesser than the second in
4146 * terms of z-order and 1 otherwise.
4147 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004148 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07004149 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004150 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
4151 token1.mOwnerCanManageAppTokens)
4152 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
4153 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004154
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004155 private final Predicate<WindowState> mGetOrientingWindow = w -> {
4156 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
4157 return false;
4158 }
4159 final int req = w.mAttrs.screenOrientation;
4160 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
4161 || req == SCREEN_ORIENTATION_UNSET) {
4162 return false;
4163 }
4164 return true;
4165 };
4166
Wale Ogunwale3a931692016-11-02 16:49:48 -07004167 private final String mName;
chaviwf29223e2018-01-31 13:23:51 -08004168 private final Dimmer mDimmer = new Dimmer(this);
4169 private final Rect mTmpDimBoundsRect = new Rect();
4170
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004171 NonAppWindowContainers(String name, WindowManagerService service) {
4172 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07004173 mName = name;
4174 }
4175
4176 void addChild(WindowToken token) {
4177 addChild(token, mWindowComparator);
4178 }
4179
4180 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004181 int getOrientation() {
4182 final WindowManagerPolicy policy = mService.mPolicy;
4183 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004184 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004185
4186 if (win != null) {
4187 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004188 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004189 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07004190 if (mService.mKeyguardGoingAway) {
4191 // Keyguard can't affect the orientation if it is going away...
4192 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
4193 return SCREEN_ORIENTATION_UNSET;
4194 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004195 }
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004196 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req
4197 + " for display id=" + mDisplayId);
Andrii Kulian8ee72852017-03-10 10:36:45 -08004198 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004199 }
4200
Andrii Kulian8ee72852017-03-10 10:36:45 -08004201 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004202
Jorim Jaggi1c530592018-04-06 15:11:47 +02004203 // Only allow force setting the orientation when all unknown visibilities have been
4204 // resolved, as otherwise we just may be starting another occluding activity.
4205 final boolean isUnoccluding =
4206 mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
4207 && mService.mUnknownAppVisibilityController.allResolved();
4208 if (policy.isKeyguardShowingAndNotOccluded() || isUnoccluding) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004209 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004210 }
4211
4212 return SCREEN_ORIENTATION_UNSET;
4213 }
4214
4215 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07004216 String getName() {
4217 return mName;
4218 }
chaviwf29223e2018-01-31 13:23:51 -08004219
4220 @Override
4221 Dimmer getDimmer() {
4222 return mDimmer;
4223 }
4224
4225 @Override
4226 void prepareSurfaces() {
4227 mDimmer.resetDimStates();
4228 super.prepareSurfaces();
4229 getBounds(mTmpDimBoundsRect);
4230
4231 if (mDimmer.updateDims(getPendingTransaction(), mTmpDimBoundsRect)) {
4232 scheduleAnimation();
4233 }
4234 }
Robert Carr3b716242016-08-16 16:02:21 -07004235 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01004236
Robert Carr9034d962018-01-04 18:27:42 -08004237 private class NonMagnifiableWindowContainers extends NonAppWindowContainers {
4238 NonMagnifiableWindowContainers(String name, WindowManagerService service) {
4239 super(name, service);
4240 }
4241
4242 @Override
4243 void applyMagnificationSpec(Transaction t, MagnificationSpec spec) {
4244 }
4245 };
4246
Robert Carrb1579c82017-09-05 14:54:47 -07004247 SurfaceControl.Builder makeSurface(SurfaceSession s) {
4248 return mService.makeSurfaceBuilder(s)
4249 .setParent(mWindowingLayer);
4250 }
4251
4252 @Override
4253 SurfaceSession getSession() {
4254 return mSession;
4255 }
4256
4257 @Override
4258 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07004259 SurfaceSession s = child != null ? child.getSession() : getSession();
4260 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrb1579c82017-09-05 14:54:47 -07004261 b.setSize(mSurfaceSize, mSurfaceSize);
Robert Carrf59b8dd2017-10-02 18:58:36 -07004262
4263 if (child == null) {
4264 return b;
4265 }
4266
Robert Carree4d4b92017-11-22 12:21:46 -08004267 return b.setName(child.getName())
4268 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07004269 }
4270
4271 /**
4272 * The makeSurface variants are for use by the window-container
4273 * hierarchy. makeOverlay here is a function for various non windowing
4274 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
4275 * and other potpourii.
4276 */
4277 SurfaceControl.Builder makeOverlay() {
4278 return mService.makeSurfaceBuilder(mSession)
4279 .setParent(mOverlayLayer);
4280 }
4281
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004282 /**
4283 * Reparents the given surface to mOverlayLayer.
4284 */
4285 void reparentToOverlay(Transaction transaction, SurfaceControl surface) {
4286 transaction.reparent(surface, mOverlayLayer.getHandle());
4287 }
4288
Robert Carrb1579c82017-09-05 14:54:47 -07004289 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carr24be9ab2018-04-30 17:54:53 -07004290 if (spec.scale != 1.0) {
4291 mMagnificationSpec = spec;
4292 } else {
4293 mMagnificationSpec = null;
4294 }
4295
Robert Carrf59b8dd2017-10-02 18:58:36 -07004296 applyMagnificationSpec(getPendingTransaction(), spec);
4297 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07004298 }
4299
Robert Carr24be9ab2018-04-30 17:54:53 -07004300 void reapplyMagnificationSpec() {
4301 if (mMagnificationSpec != null) {
4302 applyMagnificationSpec(getPendingTransaction(), mMagnificationSpec);
4303 }
4304 }
4305
Robert Carrb1579c82017-09-05 14:54:47 -07004306 @Override
4307 void onParentSet() {
4308 // Since we are the top of the SurfaceControl hierarchy here
4309 // we create the root surfaces explicitly rather than chaining
4310 // up as the default implementation in onParentSet does. So we
4311 // explicitly do NOT call super here.
4312 }
4313
4314 @Override
4315 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07004316
4317 // These are layers as children of "mWindowingLayer"
4318 mBelowAppWindowsContainers.assignLayer(t, 0);
4319 mTaskStackContainers.assignLayer(t, 1);
4320 mAboveAppWindowsContainers.assignLayer(t, 2);
4321
4322 WindowState imeTarget = mService.mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08004323 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08004324
Robert Carree4d4b92017-11-22 12:21:46 -08004325 // In the case where we have an IME target that is not in split-screen
4326 // mode IME assignment is easy. We just need the IME to go directly above
4327 // the target. This way children of the target will naturally go above the IME
4328 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08004329 //
Robert Carree4d4b92017-11-22 12:21:46 -08004330 // In the case of split-screen windowing mode, we need to elevate the IME above the
4331 // docked divider while keeping the app itself below the docked divider, so instead
4332 // we use relative layering of the IME targets child windows, and place the
4333 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08004334 //
Robert Carr234b6332018-03-20 13:38:16 -07004335 // In the case the IME target is animating, the animation Z order may be different
4336 // than the WindowContainer Z order, so it's difficult to be sure we have the correct
4337 // IME target. In this case we just layer the IME over all transitions by placing it in the
4338 // above applications layer.
4339 //
Robert Carree4d4b92017-11-22 12:21:46 -08004340 // In the case where we have no IME target we assign it where it's base layer would
4341 // place it in the AboveAppWindowContainers.
Robert Carr234b6332018-03-20 13:38:16 -07004342 if (imeTarget != null && !(imeTarget.inSplitScreenWindowingMode()
4343 || imeTarget.mToken.isAppAnimating())
Robert Carree4d4b92017-11-22 12:21:46 -08004344 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004345 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07004346 // TODO: We need to use an extra level on the app surface to ensure
4347 // this is always above SurfaceView but always below attached window.
4348 1);
Robert Carree4d4b92017-11-22 12:21:46 -08004349 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07004350 }
4351
4352 // Above we have assigned layers to our children, now we ask them to assign
4353 // layers to their children.
4354 mBelowAppWindowsContainers.assignChildLayers(t);
4355 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08004356 mAboveAppWindowsContainers.assignChildLayers(t,
4357 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07004358 mImeWindowsContainers.assignChildLayers(t);
4359 }
4360
4361 /**
4362 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
4363 * that the IME target is one of the docked applications. We'd like the docked divider to be
4364 * above both of the applications, and we'd like the IME to be above the docked divider.
4365 * However we need child windows of the applications to be above the IME (Text drag handles).
4366 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
4367 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
4368 * with {@link #WindowState#assignLayer}
4369 */
4370 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
Robert Carrbb5de662018-04-26 13:29:51 -07004371 child.assignRelativeLayer(t, mImeWindowsContainers.getSurfaceControl(), 1);
Robert Carrb1579c82017-09-05 14:54:47 -07004372 }
4373
4374 @Override
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004375 void prepareSurfaces() {
4376 final ScreenRotationAnimation screenRotationAnimation =
4377 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
4378 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
4379 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
4380 mPendingTransaction.setMatrix(mWindowingLayer,
4381 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
4382 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
4383 mPendingTransaction.setPosition(mWindowingLayer,
4384 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
4385 mPendingTransaction.setAlpha(mWindowingLayer,
4386 screenRotationAnimation.getEnterTransformation().getAlpha());
4387 }
Chavi Weingartenb736e322018-02-23 00:27:54 +00004388
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004389 super.prepareSurfaces();
4390 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004391
Jorim Jaggibe418292018-03-26 16:14:12 +02004392 void assignStackOrdering() {
4393 mTaskStackContainers.assignStackOrdering(getPendingTransaction());
Robert Carr2f8aa392018-01-31 14:46:51 -08004394 }
Chavi Weingarten3a748552018-05-14 17:32:42 +00004395
4396 /**
4397 * Increment the deferral count to determine whether to update the IME target.
4398 */
4399 void deferUpdateImeTarget() {
4400 mDeferUpdateImeTargetCount++;
4401 }
4402
4403 /**
4404 * Decrement the deferral count to determine whether to update the IME target. If the count
4405 * reaches 0, a new ime target will get computed.
4406 */
4407 void continueUpdateImeTarget() {
4408 if (mDeferUpdateImeTargetCount == 0) {
4409 return;
4410 }
4411
4412 mDeferUpdateImeTargetCount--;
4413 if (mDeferUpdateImeTargetCount == 0) {
4414 computeImeTarget(true /* updateImeTarget */);
4415 }
4416 }
4417
4418 /**
4419 * @return Whether a new IME target should be computed.
4420 */
4421 private boolean canUpdateImeTarget() {
4422 return mDeferUpdateImeTargetCount == 0;
4423 }
Arthur Hung95b38a92018-07-20 18:56:12 +08004424
4425 InputMonitor getInputMonitor() {
4426 return mInputMonitor;
4427 }
Jorim Jaggif1292892018-09-10 11:58:13 +02004428
4429 /**
4430 * @return Cached value whether we told display manager that we have content.
4431 */
4432 boolean getLastHasContent() {
4433 return mLastHasContent;
4434 }
Arthur Hungbe5ce212018-09-13 18:41:56 +08004435
4436 void registerPointerEventListener(@NonNull PointerEventListener listener) {
4437 if (mPointerEventDispatcher != null) {
4438 mPointerEventDispatcher.registerInputEventListener(listener);
4439 }
4440 }
4441
4442 void unregisterPointerEventListener(@NonNull PointerEventListener listener) {
4443 if (mPointerEventDispatcher != null) {
4444 mPointerEventDispatcher.unregisterInputEventListener(listener);
4445 }
4446 }
Craig Mautner59c00972012-07-30 12:10:24 -07004447}