blob: fa9ae529c6e1c2ca1cdbb4da8b841137d327fb81 [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;
Riddle Hsuf53da812018-08-15 22:00:27 +0800123import android.annotation.IntDef;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700124import android.annotation.NonNull;
Dean Harding3e5a1522017-10-06 09:19:01 -0700125import android.content.pm.PackageManager;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700126import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700127import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700128import android.graphics.Bitmap;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700129import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800130import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700131import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700132import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100133import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700134import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700135import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700136import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700137import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700138import android.os.RemoteException;
139import android.os.SystemClock;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100140import android.os.Trace;
Riddle Hsuf53da812018-08-15 22:00:27 +0800141import android.os.UserHandle;
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800142import android.util.ArraySet;
Chong Zhang8e89b312015-09-09 15:09:30 -0700143import android.util.DisplayMetrics;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700144import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700145import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700146import android.view.Display;
Adrian Roos1cf585052018-01-03 18:43:27 +0100147import android.view.DisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700148import android.view.DisplayInfo;
Riddle Hsua4d6fa22018-08-11 00:50:39 +0800149import android.view.Gravity;
Arthur Hungbe5ce212018-09-13 18:41:56 +0800150import android.view.InputChannel;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700151import android.view.InputDevice;
Robert Carrb1579c82017-09-05 14:54:47 -0700152import android.view.MagnificationSpec;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700153import android.view.Surface;
154import android.view.SurfaceControl;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100155import android.view.SurfaceControl.Transaction;
Robert Carrb1579c82017-09-05 14:54:47 -0700156import android.view.SurfaceSession;
Arthur Hungbe5ce212018-09-13 18:41:56 +0800157import android.view.WindowManagerPolicyConstants.PointerEventListener;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700158
Bryce Lee48f4b572017-04-10 10:54:15 -0700159import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800160import com.android.internal.util.ToBooleanFunction;
Adrian Roose99bc052017-11-20 17:55:31 +0100161import com.android.server.policy.WindowManagerPolicy;
Issei Suzuki43190bd2018-08-20 17:28:41 +0200162import com.android.server.wm.utils.DisplayRotationUtil;
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100163import com.android.server.wm.utils.RotationCache;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100164import com.android.server.wm.utils.WmDisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700165
166import java.io.PrintWriter;
Riddle Hsuf53da812018-08-15 22:00:27 +0800167import java.lang.annotation.Retention;
168import java.lang.annotation.RetentionPolicy;
Craig Mautner59c00972012-07-30 12:10:24 -0700169import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700170import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700171import java.util.HashMap;
172import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700173import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700174import java.util.List;
Adrian Roos1cf585052018-01-03 18:43:27 +0100175import java.util.Objects;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800176import java.util.function.Consumer;
177import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700178
Craig Mautner59c00972012-07-30 12:10:24 -0700179/**
180 * Utility class for keeping track of the WindowStates and other pertinent contents of a
181 * particular Display.
182 *
183 * IMPORTANT: No method from this class should ever be used without holding
184 * WindowManagerService.mWindowMap.
185 */
Riddle Hsuad256a12018-07-18 16:11:30 +0800186class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer>
187 implements WindowManagerPolicy.DisplayContentInfo {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700188 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700189
Riddle Hsuf53da812018-08-15 22:00:27 +0800190 /** The default scaling mode that scales content automatically. */
191 static final int FORCE_SCALING_MODE_AUTO = 0;
192 /** For {@link #setForcedScalingMode} to apply flag {@link Display#FLAG_SCALING_DISABLED}. */
193 static final int FORCE_SCALING_MODE_DISABLED = 1;
194
195 @IntDef(prefix = { "FORCE_SCALING_MODE_" }, value = {
196 FORCE_SCALING_MODE_AUTO,
197 FORCE_SCALING_MODE_DISABLED
198 })
199 @Retention(RetentionPolicy.SOURCE)
200 @interface ForceScalingMode {}
201
Craig Mautner59c00972012-07-30 12:10:24 -0700202 /** Unique identifier of this stack. */
203 private final int mDisplayId;
204
Wale Ogunwale3a931692016-11-02 16:49:48 -0700205 /** The containers below are the only child containers the display can have. */
206 // Contains all window containers that are related to apps (Activities)
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100207 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers(mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700208 // Contains all non-app window containers that should be displayed above the app containers
209 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800210 private final AboveAppWindowContainers mAboveAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100211 new AboveAppWindowContainers("mAboveAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700212 // Contains all non-app window containers that should be displayed below the app containers
213 // (e.g. Wallpaper).
214 private final NonAppWindowContainers mBelowAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100215 new NonAppWindowContainers("mBelowAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700216 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
217 // 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 -0800218 // window containers together and move them in-sync if/when needed. We use a subclass of
219 // WindowContainer which is omitted from screen magnification, as the IME is never magnified.
220 private final NonMagnifiableWindowContainers mImeWindowsContainers =
221 new NonMagnifiableWindowContainers("mImeWindowsContainers", mService);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700222
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000223 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800224 private WindowState mTmpWindow2;
225 private WindowAnimator mTmpWindowAnimator;
226 private boolean mTmpRecoveringMemory;
227 private boolean mUpdateImeTarget;
228 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700229 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700230
Wale Ogunwale02319a62016-09-26 15:21:22 -0700231 // Mapping from a token IBinder to a WindowToken object on this display.
232 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
233
Andrii Kuliancd097992017-03-23 18:31:59 -0700234 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700235 int mInitialDisplayWidth = 0;
236 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700237 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700238
Adrian Roos1cf585052018-01-03 18:43:27 +0100239 DisplayCutout mInitialDisplayCutout;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100240 private final RotationCache<DisplayCutout, WmDisplayCutout> mDisplayCutoutCache
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100241 = new RotationCache<>(this::calculateDisplayCutoutForRotationUncached);
Adrian Roos1cf585052018-01-03 18:43:27 +0100242
Andrii Kuliancd097992017-03-23 18:31:59 -0700243 /**
244 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
245 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
246 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
247 */
Craig Mautner59c00972012-07-30 12:10:24 -0700248 int mBaseDisplayWidth = 0;
249 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700250 /**
251 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
252 * but can be set from Settings or via shell command "adb shell wm density".
253 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
254 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700255 int mBaseDisplayDensity = 0;
Riddle Hsuf53da812018-08-15 22:00:27 +0800256
257 /**
258 * Whether to disable display scaling. This can be set via shell command "adb shell wm scaling".
259 * @see WindowManagerService#setForcedDisplayScalingMode(int, int)
260 */
Jeff Brownd46747a2015-04-15 19:02:36 -0700261 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700262 private final DisplayInfo mDisplayInfo = new DisplayInfo();
263 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700264 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800265 private final DisplayPolicy mDisplayPolicy;
266 private DisplayRotation mDisplayRotation;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000267 DisplayFrames mDisplayFrames;
268
Andrii Kulian06d07d62017-03-14 11:11:47 -0700269 /**
270 * For default display it contains real metrics, empty for others.
271 * @see WindowManagerService#createWatermarkInTransaction()
272 */
273 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
274 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
275 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700276
Andrii Kulian06d07d62017-03-14 11:11:47 -0700277 /**
278 * Compat metrics computed based on {@link #mDisplayMetrics}.
279 * @see #updateDisplayAndOrientation(int)
280 */
281 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
282
283 /** The desired scaling factor for compatible apps. */
284 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700285
Andrii Kulian8ee72852017-03-10 10:36:45 -0800286 /**
287 * Current rotation of the display.
288 * Constants as per {@link android.view.Surface.Rotation}.
289 *
Robert Carrae606b42018-02-15 15:36:23 -0800290 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800291 */
292 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700293
Andrii Kulian8ee72852017-03-10 10:36:45 -0800294 /**
295 * Last applied orientation of the display.
296 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
297 *
298 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
299 */
300 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700301
Andrii Kulian8ee72852017-03-10 10:36:45 -0800302 /**
303 * Flag indicating that the application is receiving an orientation that has different metrics
304 * than it expected. E.g. Portrait instead of Landscape.
305 *
Robert Carrae606b42018-02-15 15:36:23 -0800306 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800307 */
308 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700309
Andrii Kulian8ee72852017-03-10 10:36:45 -0800310 /**
311 * Orientation forced by some window. If there is no visible window that specifies orientation
312 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
313 *
314 * @see NonAppWindowContainers#getOrientation()
315 */
316 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700317
Andrii Kulian8ee72852017-03-10 10:36:45 -0800318 /**
319 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
320 * occluded.
321 *
322 * @see NonAppWindowContainers#getOrientation()
323 */
324 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
325
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700326 /**
327 * Keep track of wallpaper visibility to notify changes.
328 */
329 private boolean mLastWallpaperVisible = false;
330
Andrii Kulian06d07d62017-03-14 11:11:47 -0700331 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700332
Craig Mautner39834192012-09-02 07:47:24 -0700333 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700334 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700335 int pendingLayoutChanges;
Riddle Hsu654a6f92018-07-13 22:59:36 +0800336 int mDeferredRotationPauseCount;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800337
Andrii Kulian839def92016-11-02 10:58:58 -0700338 // TODO(multi-display): remove some of the usages.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800339 @VisibleForTesting
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800340 boolean isDefaultDisplay;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800341
Andrii Kulianf0379de2018-03-14 16:24:07 -0700342 /**
343 * Flag indicating whether WindowManager should override info for this display in
344 * DisplayManager.
345 */
346 boolean mShouldOverrideDisplayConfiguration = true;
Craig Mautner39834192012-09-02 07:47:24 -0700347
Craig Mautnerdc548482014-02-05 13:35:24 -0800348 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700349 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800350
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700351 /** Detect user tapping outside of current focused task bounds .*/
352 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700353
Craig Mautner6601b7b2013-04-29 10:29:11 -0700354 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700355 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700356
Craig Mautner6601b7b2013-04-29 10:29:11 -0700357 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800358 private final Rect mTmpRect = new Rect();
359 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700360 private final RectF mTmpRectF = new RectF();
361 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800362 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700363
Issei Suzuki43190bd2018-08-20 17:28:41 +0200364
Bryce Leef3c6a472017-11-14 14:53:06 -0800365 /** Used for handing back size of display */
366 private final Rect mTmpBounds = new Rect();
367
Craig Mautner95da1082014-02-24 17:54:35 -0800368 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700369 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800370
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700371 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700372 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700373
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800374 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800375 /** A collection of windows that provide tap exclude regions inside of them. */
376 final ArraySet<WindowState> mTapExcludeProvidingWindows = new ArraySet<>();
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800377
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000378 private boolean mHaveBootMsg = false;
379 private boolean mHaveApp = false;
380 private boolean mHaveWallpaper = false;
381 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700382
383 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
384
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700385 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
386 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000387 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
388 new ApplySurfaceChangesTransactionState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700389
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700390 // True if this display is in the process of being removed. Used to determine if the removal of
391 // the display's direct children should be allowed.
392 private boolean mRemovingDisplay = false;
393
Bryce Leed1871262017-06-12 14:12:29 -0700394 // {@code false} if this display is in the processing of being created.
395 private boolean mDisplayReady = false;
396
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800397 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700398
Robert Carrb1579c82017-09-05 14:54:47 -0700399 private final SurfaceSession mSession = new SurfaceSession();
400
401 /**
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800402 * Window that is currently interacting with the user. This window is responsible for receiving
403 * key events and pointer events from the user.
404 */
405 WindowState mCurrentFocus = null;
406
407 /**
408 * The last focused window that we've notified the client that the focus is changed.
409 */
410 WindowState mLastFocus = null;
411
412 /**
413 * Windows that have lost input focus and are waiting for the new focus window to be displayed
414 * before they are told about this.
415 */
416 ArrayList<WindowState> mLosingFocus = new ArrayList<>();
417
418 /**
419 * The foreground app of this display. Windows below this app cannot be the focused window. If
420 * the user taps on the area outside of the task of the focused app, we will notify AM about the
421 * new task the user wants to interact with.
422 */
423 AppWindowToken mFocusedApp = null;
424
425 /** Windows added since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
426 final ArrayList<WindowState> mWinAddedSinceNullFocus = new ArrayList<>();
427
428 /** Windows removed since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
429 final ArrayList<WindowState> mWinRemovedSinceNullFocus = new ArrayList<>();
430
431 /**
Robert Carrb1579c82017-09-05 14:54:47 -0700432 * We organize all top-level Surfaces in to the following layers.
433 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800434 * and omitted from Screen-Magnification, for example the strict mode flash or
435 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700436 * {@link #mWindowingLayer} contains everything else.
437 */
438 private SurfaceControl mOverlayLayer;
439
440 /**
441 * See {@link #mOverlayLayer}
442 */
443 private SurfaceControl mWindowingLayer;
444
445 /**
446 * Specifies the size of the surfaces in {@link #mOverlayLayer} and {@link #mWindowingLayer}.
447 * <p>
448 * For these surfaces currently we use a surface based on the larger of width or height so we
449 * don't have to resize when rotating the display.
450 */
451 private int mSurfaceSize;
452
Adrian Roos5251b1d2018-03-23 18:57:43 +0100453 /**
454 * Sequence number for the current layout pass.
455 */
456 int mLayoutSeq = 0;
457
Chavi Weingarten3a748552018-05-14 17:32:42 +0000458 /**
459 * Specifies the count to determine whether to defer updating the IME target until ready.
460 */
461 private int mDeferUpdateImeTargetCount;
462
Jorim Jaggi9af095b2017-12-12 17:18:57 +0100463 /** Temporary float array to retrieve 3x3 matrix values. */
464 private final float[] mTmpFloats = new float[9];
465
Robert Carr24be9ab2018-04-30 17:54:53 -0700466 private MagnificationSpec mMagnificationSpec;
467
Arthur Hung95b38a92018-07-20 18:56:12 +0800468 private InputMonitor mInputMonitor;
469
Jorim Jaggif1292892018-09-10 11:58:13 +0200470 /** Caches the value whether told display manager that we have content. */
471 private boolean mLastHasContent;
472
Issei Suzuki43190bd2018-08-20 17:28:41 +0200473 private DisplayRotationUtil mRotationUtil = new DisplayRotationUtil();
474
lumark90120a82018-08-15 00:33:03 +0800475 /**
476 * The input method window for this display.
477 */
478 WindowState mInputMethodWindow;
479
Arthur Hungbe5ce212018-09-13 18:41:56 +0800480 private final PointerEventDispatcher mPointerEventDispatcher;
481
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800482 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
483 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800484 final AppWindowToken atoken = w.mAppToken;
485 if (winAnimator.mDrawState == READY_TO_SHOW) {
Jorim Jaggiaf0d6d22018-06-08 15:25:35 +0200486 if (atoken == null || atoken.canShowWindows()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800487 if (w.performShowLocked()) {
488 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
489 if (DEBUG_LAYOUT_REPEATS) {
490 mService.mWindowPlacerLocked.debugLayoutRepeats(
491 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
492 }
493 }
494 }
495 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800496 };
497
498 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
499 final WindowStateAnimator winAnimator = w.mWinAnimator;
500 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
501 return;
502 }
503
Jorim Jaggi8f520872018-08-14 17:00:20 +0200504 // If this window is animating, ensure the animation background is set.
505 final AnimationAdapter anim = w.mAppToken != null
506 ? w.mAppToken.getAnimation()
507 : w.getAnimation();
508 if (anim != null) {
509 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800510 if (color != 0) {
511 final TaskStack stack = w.getStack();
512 if (stack != null) {
513 stack.setAnimationBackground(winAnimator, color);
514 }
515 }
516 }
517 };
518
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800519 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
520 final int lostFocusUid = mTmpWindow.mOwnerUid;
521 final Handler handler = mService.mH;
522 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
523 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
524 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
525 w.mAttrs.hideTimeoutMilliseconds);
526 }
527 }
528 };
529
530 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800531 final AppWindowToken focusedApp = mFocusedApp;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800532 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
533 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
534
535 if (!w.canReceiveKeys()) {
536 return false;
537 }
538
539 final AppWindowToken wtoken = w.mAppToken;
540
541 // If this window's application has been removed, just skip it.
542 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
543 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
544 + (wtoken.removed ? "removed" : "sendingToBottom"));
545 return false;
546 }
547
548 if (focusedApp == null) {
549 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
550 + " using new focus @ " + w);
551 mTmpWindow = w;
552 return true;
553 }
554
555 if (!focusedApp.windowsAreFocusable()) {
556 // Current focused app windows aren't focusable...
557 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
558 + " focusable using new focus @ " + w);
559 mTmpWindow = w;
560 return true;
561 }
562
563 // Descend through all of the app tokens and find the first that either matches
564 // win.mAppToken (return win) or mFocusedApp (return null).
565 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
566 if (focusedApp.compareTo(wtoken) > 0) {
567 // App stack below focused app stack. No focus for you!!!
568 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
569 "findFocusedWindow: Reached focused app=" + focusedApp);
570 mTmpWindow = null;
571 return true;
572 }
573 }
574
575 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
576 mTmpWindow = w;
577 return true;
578 };
579
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800580 private final Consumer<WindowState> mPerformLayout = w -> {
581 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
582 // wasting time and funky changes while a window is animating away.
583 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
584 || w.isGoneForLayoutLw();
585
586 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
587 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
588 + " mLayoutAttached=" + w.mLayoutAttached
589 + " screen changed=" + w.isConfigChanged());
590 final AppWindowToken atoken = w.mAppToken;
591 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200592 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800593 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
594 + " parentHidden=" + w.isParentWindowHidden());
595 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200596 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800597 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
598 + " parentHidden=" + w.isParentWindowHidden());
599 }
600
601 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
602 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
603 // since that means "perform layout as normal, just don't display").
604 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
605 || ((w.isConfigChanged() || w.setReportResizeHints())
606 && !w.isGoneForLayoutLw() &&
607 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
608 (w.mHasSurface && w.mAppToken != null &&
609 w.mAppToken.layoutConfigChanges)))) {
610 if (!w.mLayoutAttached) {
611 if (mTmpInitial) {
612 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700613 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800614 }
615 if (w.mAttrs.type == TYPE_DREAM) {
616 // Don't layout windows behind a dream, so that if it does stuff like hide
617 // the status bar we won't get a bad transition when it goes away.
618 mTmpWindow = w;
619 }
620 w.mLayoutNeeded = false;
621 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200622 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000623 mService.mPolicy.layoutWindowLw(w, null, mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100624 w.mLayoutSeq = mLayoutSeq;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800625
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200626 // If this is the first layout, we need to initialize the last inset values as
627 // otherwise we'd immediately cause an unnecessary resize.
628 if (firstLayout) {
629 w.updateLastInsetValues();
630 }
631
Adrian Roos23df3a32018-03-15 15:41:13 +0100632 if (w.mAppToken != null) {
633 w.mAppToken.layoutLetterbox(w);
634 }
635
chaviw492139a2018-07-16 16:07:35 -0700636 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700637 + " mContainingFrame=" + w.getContainingFrame()
638 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800639 }
640 }
641 };
642
643 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
644 if (w.mLayoutAttached) {
645 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
646 + " mViewVisibility=" + w.mViewVisibility
647 + " mRelayoutCalled=" + w.mRelayoutCalled);
648 // If this view is GONE, then skip it -- keep the current frame, and let the caller
649 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
650 // windows, since that means "perform layout as normal, just don't display").
651 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
652 return;
653 }
654 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
655 || w.mLayoutNeeded) {
656 if (mTmpInitial) {
657 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700658 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800659 }
660 w.mLayoutNeeded = false;
661 w.prelayout();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000662 mService.mPolicy.layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100663 w.mLayoutSeq = mLayoutSeq;
chaviw492139a2018-07-16 16:07:35 -0700664 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700665 + " mContainingFrame=" + w.getContainingFrame()
666 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800667 }
668 } else if (w.mAttrs.type == TYPE_DREAM) {
669 // Don't layout windows behind a dream, so that if it does stuff like hide the
670 // status bar we won't get a bad transition when it goes away.
671 mTmpWindow = mTmpWindow2;
672 }
673 };
674
675 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
676 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
677 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
678 return w.canBeImeTarget();
679 };
680
681 private final Consumer<WindowState> mApplyPostLayoutPolicy =
682 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
683 mService.mInputMethodTarget);
684
685 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
686 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
687 final boolean obscuredChanged = w.mObscured !=
688 mTmpApplySurfaceChangesTransactionState.obscured;
689 final RootWindowContainer root = mService.mRoot;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800690
691 // Update effect.
692 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
693 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
694 final boolean isDisplayed = w.isDisplayedLw();
695
696 if (isDisplayed && w.isObscuringDisplay()) {
697 // This window completely covers everything behind it, so we want to leave all
698 // of them as undimmed (for performance reasons).
699 root.mObscuringWindow = w;
700 mTmpApplySurfaceChangesTransactionState.obscured = true;
701 }
702
703 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
704 root.handleNotObscuredLocked(w,
705 mTmpApplySurfaceChangesTransactionState.obscured,
706 mTmpApplySurfaceChangesTransactionState.syswin);
707
708 if (w.mHasSurface && isDisplayed) {
709 final int type = w.mAttrs.type;
710 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
711 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
712 mTmpApplySurfaceChangesTransactionState.syswin = true;
713 }
714 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
715 && w.mAttrs.preferredRefreshRate != 0) {
716 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
717 = w.mAttrs.preferredRefreshRate;
718 }
719 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
720 && w.mAttrs.preferredDisplayModeId != 0) {
721 mTmpApplySurfaceChangesTransactionState.preferredModeId
722 = w.mAttrs.preferredDisplayModeId;
723 }
724 }
725 }
726
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800727 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
728 && mWallpaperController.isWallpaperTarget(w)) {
729 // This is the wallpaper target and its obscured state changed... make sure the
730 // current wallpaper's visibility has been updated accordingly.
731 mWallpaperController.updateWallpaperVisibility();
732 }
733
chaviw161ea3e2018-01-31 12:01:12 -0800734 w.handleWindowMovedIfNeeded();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800735
736 final WindowStateAnimator winAnimator = w.mWinAnimator;
737
738 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
chaviw1454b392018-08-06 09:54:04 -0700739 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800740
741 // Moved from updateWindowsAndWallpaperLocked().
742 if (w.mHasSurface) {
743 // Take care of the window being ready to display.
744 final boolean committed = winAnimator.commitFinishDrawingLocked();
745 if (isDefaultDisplay && committed) {
746 if (w.mAttrs.type == TYPE_DREAM) {
747 // HACK: When a dream is shown, it may at that point hide the lock screen.
748 // So we need to redo the layout to let the phone window manager make this
749 // happen.
750 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
751 if (DEBUG_LAYOUT_REPEATS) {
752 surfacePlacer.debugLayoutRepeats(
753 "dream and commitFinishDrawingLocked true",
754 pendingLayoutChanges);
755 }
756 }
757 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
758 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
759 "First draw done in potential wallpaper target " + w);
760 root.mWallpaperMayChange = true;
761 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
762 if (DEBUG_LAYOUT_REPEATS) {
763 surfacePlacer.debugLayoutRepeats(
764 "wallpaper and commitFinishDrawingLocked true",
765 pendingLayoutChanges);
766 }
767 }
768 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800769 }
770
771 final AppWindowToken atoken = w.mAppToken;
772 if (atoken != null) {
Adrian Roos23df3a32018-03-15 15:41:13 +0100773 atoken.updateLetterboxSurface(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800774 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
775 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
776 mTmpUpdateAllDrawn.add(atoken);
777 }
778 }
779
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800780 if (!mLosingFocus.isEmpty() && w.isFocused() && w.isDisplayedLw()) {
781 mService.mH.obtainMessage(REPORT_LOSING_FOCUS, this).sendToTarget();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800782 }
783
784 w.updateResizingWindowIfNeeded();
785 };
786
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800787 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800788 * Create new {@link DisplayContent} instance, add itself to the root window container and
789 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700790 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800791 * @param service You know.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700792 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
793 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700794 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700795 DisplayContent(Display display, WindowManagerService service,
Bryce Leef19cbe22018-02-02 15:09:21 -0800796 WallpaperController wallpaperController, DisplayWindowController controller) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100797 super(service);
Bryce Leef19cbe22018-02-02 15:09:21 -0800798 setController(controller);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800799 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
800 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
801 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
802 + " new=" + display);
803 }
804
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700805 mDisplay = display;
806 mDisplayId = display.getDisplayId();
Wale Ogunwale0303c572016-10-20 10:16:29 -0700807 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700808 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700809 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700810 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100811 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo,
812 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700813 initializeDisplayBaseInfo();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800814 mDisplayPolicy = new DisplayPolicy(service);
815 mDisplayRotation = new DisplayRotation(service, this);
816 if (isDefaultDisplay) {
817 // The policy may be invoked right after here, so it requires the necessary default
818 // fields of this display content.
819 mService.mPolicy.setDefaultDisplay(this);
820 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800821 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700822 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700823
Robert Carr74a66a22018-02-23 12:17:51 -0800824 // We use this as our arbitrary surface size for buffer-less parents
825 // that don't impose cropping on their children. It may need to be larger
826 // than the display size because fullscreen windows can be shifted offscreen
827 // due to surfaceInsets. 2 times the largest display dimension feels like an
828 // appropriately arbitrary number. Eventually we would like to give SurfaceFlinger
829 // layers the ability to match their parent sizes and be able to skip
830 // such arbitrary size settings.
831 mSurfaceSize = Math.max(mBaseDisplayHeight, mBaseDisplayWidth) * 2;
Robert Carrb1579c82017-09-05 14:54:47 -0700832
833 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession)
834 .setSize(mSurfaceSize, mSurfaceSize)
835 .setOpaque(true);
836 mWindowingLayer = b.setName("Display Root").build();
837 mOverlayLayer = b.setName("Display Overlays").build();
838
Robert Carrf59b8dd2017-10-02 18:58:36 -0700839 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700840 .setLayerStack(mWindowingLayer, mDisplayId)
841 .show(mWindowingLayer)
842 .setLayer(mOverlayLayer, 1)
843 .setLayerStack(mOverlayLayer, mDisplayId)
844 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700845 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700846
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700847 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700848 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700849 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700850 super.addChild(mAboveAppWindowsContainers, null);
851 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800852
853 // Add itself as a child to the root container.
854 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700855
856 // TODO(b/62541591): evaluate whether this is the best spot to declare the
857 // {@link DisplayContent} ready for use.
858 mDisplayReady = true;
Arthur Hung95b38a92018-07-20 18:56:12 +0800859
Riddle Hsuf53da812018-08-15 22:00:27 +0800860 mService.mAnimator.addDisplayLocked(mDisplayId);
Arthur Hung39134b22018-08-14 11:58:28 +0800861 mInputMonitor = new InputMonitor(service, mDisplayId);
Arthur Hungbe5ce212018-09-13 18:41:56 +0800862
863 if (mService.mInputManager != null) {
864 final InputChannel inputChannel = mService.mInputManager.monitorInput("Display "
865 + mDisplayId, mDisplayId);
866 mPointerEventDispatcher = inputChannel != null
867 ? new PointerEventDispatcher(inputChannel) : null;
868 } else {
869 mPointerEventDispatcher = null;
870 }
Bryce Leed1871262017-06-12 14:12:29 -0700871 }
872
873 boolean isReady() {
874 // The display is ready when the system and the individual display are both ready.
875 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700876 }
877
878 int getDisplayId() {
879 return mDisplayId;
880 }
881
Wale Ogunwale02319a62016-09-26 15:21:22 -0700882 WindowToken getWindowToken(IBinder binder) {
883 return mTokenMap.get(binder);
884 }
885
886 AppWindowToken getAppWindowToken(IBinder binder) {
887 final WindowToken token = getWindowToken(binder);
888 if (token == null) {
889 return null;
890 }
891 return token.asAppWindowToken();
892 }
893
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700894 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700895 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
896 if (dc != null) {
897 // We currently don't support adding a window token to the display if the display
898 // already has the binder mapped to another token. If there is a use case for supporting
899 // this moving forward we will either need to merge the WindowTokens some how or have
900 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700901 throw new IllegalArgumentException("Can't map token=" + token + " to display="
902 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700903 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700904 if (binder == null) {
905 throw new IllegalArgumentException("Can't map token=" + token + " to display="
906 + getName() + " binder is null");
907 }
908 if (token == null) {
909 throw new IllegalArgumentException("Can't map null token to display="
910 + getName() + " binder=" + binder);
911 }
912
Wale Ogunwale02319a62016-09-26 15:21:22 -0700913 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700914
915 if (token.asAppWindowToken() == null) {
916 // Add non-app token to container hierarchy on the display. App tokens are added through
917 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700918 switch (token.windowType) {
919 case TYPE_WALLPAPER:
920 mBelowAppWindowsContainers.addChild(token);
921 break;
922 case TYPE_INPUT_METHOD:
923 case TYPE_INPUT_METHOD_DIALOG:
924 mImeWindowsContainers.addChild(token);
925 break;
926 default:
927 mAboveAppWindowsContainers.addChild(token);
928 break;
929 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700930 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700931 }
932
933 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700934 final WindowToken token = mTokenMap.remove(binder);
935 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800936 token.setExiting();
937 }
938 return token;
939 }
940
941 /** Changes the display the input window token is housed on to this one. */
942 void reParentWindowToken(WindowToken token) {
943 final DisplayContent prevDc = token.getDisplayContent();
944 if (prevDc == this) {
945 return;
946 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800947 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
948 && token.asAppWindowToken() == null) {
949 // Removed the token from the map, but made sure it's not an app token before removing
950 // from parent.
951 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700952 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800953
954 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700955 }
956
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700957 void removeAppToken(IBinder binder) {
958 final WindowToken token = removeWindowToken(binder);
959 if (token == null) {
960 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
961 return;
962 }
963
964 final AppWindowToken appToken = token.asAppWindowToken();
965
966 if (appToken == null) {
967 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
968 return;
969 }
970
971 appToken.onRemovedFromDisplay();
972 }
973
Riddle Hsuad256a12018-07-18 16:11:30 +0800974 @Override
Riddle Hsu86cb7de2018-08-13 23:29:58 +0800975 DisplayWindowController getController() {
976 return (DisplayWindowController) super.getController();
977 }
978
979 @Override
Riddle Hsuad256a12018-07-18 16:11:30 +0800980 public Display getDisplay() {
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700981 return mDisplay;
982 }
983
Craig Mautner59c00972012-07-30 12:10:24 -0700984 DisplayInfo getDisplayInfo() {
985 return mDisplayInfo;
986 }
987
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700988 DisplayMetrics getDisplayMetrics() {
989 return mDisplayMetrics;
990 }
991
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800992 DisplayPolicy getDisplayPolicy() {
993 return mDisplayPolicy;
994 }
995
Riddle Hsuad256a12018-07-18 16:11:30 +0800996 @Override
997 public DisplayRotation getDisplayRotation() {
998 return mDisplayRotation;
999 }
1000
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001001 @VisibleForTesting
1002 void setDisplayRotation(DisplayRotation displayRotation) {
1003 mDisplayRotation = displayRotation;
1004 }
1005
Andrii Kulian8ee72852017-03-10 10:36:45 -08001006 int getRotation() {
1007 return mRotation;
1008 }
1009
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001010 @VisibleForTesting
Andrii Kulian8ee72852017-03-10 10:36:45 -08001011 void setRotation(int newRotation) {
1012 mRotation = newRotation;
Riddle Hsuad256a12018-07-18 16:11:30 +08001013 mDisplayRotation.setRotation(newRotation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08001014 }
1015
1016 int getLastOrientation() {
1017 return mLastOrientation;
1018 }
1019
1020 void setLastOrientation(int orientation) {
1021 mLastOrientation = orientation;
1022 }
1023
1024 boolean getAltOrientation() {
1025 return mAltOrientation;
1026 }
1027
1028 void setAltOrientation(boolean altOrientation) {
1029 mAltOrientation = altOrientation;
1030 }
1031
1032 int getLastWindowForcedOrientation() {
1033 return mLastWindowForcedOrientation;
1034 }
1035
Andrii Kulian06d07d62017-03-14 11:11:47 -07001036 /**
Riddle Hsu654a6f92018-07-13 22:59:36 +08001037 * Temporarily pauses rotation changes until resumed.
1038 *
1039 * This can be used to prevent rotation changes from occurring while the user is
1040 * performing certain operations, such as drag and drop.
1041 *
1042 * This call nests and must be matched by an equal number of calls to
1043 * {@link #resumeRotationLocked}.
1044 */
1045 void pauseRotationLocked() {
1046 mDeferredRotationPauseCount++;
1047 }
1048
1049 /**
1050 * Resumes normal rotation changes after being paused.
1051 */
1052 void resumeRotationLocked() {
1053 if (mDeferredRotationPauseCount <= 0) {
1054 return;
1055 }
1056
1057 mDeferredRotationPauseCount--;
1058 if (mDeferredRotationPauseCount == 0) {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001059 updateRotationAndSendNewConfigIfNeeded();
Riddle Hsu654a6f92018-07-13 22:59:36 +08001060 }
1061 }
1062
1063 /**
Riddle Hsuad256a12018-07-18 16:11:30 +08001064 * If this is true we have updated our desired orientation, but not yet changed the real
1065 * orientation our applied our screen rotation animation. For example, because a previous
1066 * screen rotation was in progress.
1067 *
1068 * @return {@code true} if the there is an ongoing rotation change.
1069 */
1070 boolean rotationNeedsUpdate() {
1071 final int lastOrientation = getLastOrientation();
1072 final int oldRotation = getRotation();
1073 final boolean oldAltOrientation = getAltOrientation();
1074
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001075 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Riddle Hsuad256a12018-07-18 16:11:30 +08001076 final boolean altOrientation = !mDisplayRotation.rotationHasCompatibleMetrics(
1077 lastOrientation, rotation);
1078 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1079 return false;
1080 }
1081 return true;
1082 }
1083
1084 /**
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001085 * Update rotation of the display and send configuration if the rotation is changed.
1086 *
1087 * @return {@code true} if the rotation has been changed and the new config is sent.
1088 */
1089 boolean updateRotationAndSendNewConfigIfNeeded() {
1090 final boolean changed = updateRotationUnchecked(false /* forceUpdate */);
1091 if (changed) {
1092 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
1093 }
1094 return changed;
1095 }
1096
1097 /**
Andrii Kulian06d07d62017-03-14 11:11:47 -07001098 * Update rotation of the display.
1099 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001100 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
1101 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001102 */
Robert Carrae606b42018-02-15 15:36:23 -08001103 boolean updateRotationUnchecked() {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001104 return updateRotationUnchecked(false /* forceUpdate */);
1105 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001106
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001107 /**
1108 * Update rotation of the display with an option to force the update.
1109 * @param forceUpdate Force the rotation update. Sometimes in WM we might skip updating
1110 * orientation because we're waiting for some rotation to finish or display
1111 * to unfreeze, which results in configuration of the previously visible
1112 * activity being applied to a newly visible one. Forcing the rotation
1113 * update allows to workaround this issue.
1114 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
1115 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
1116 */
1117 boolean updateRotationUnchecked(boolean forceUpdate) {
1118 ScreenRotationAnimation screenRotationAnimation;
1119 if (!forceUpdate) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001120 if (mDeferredRotationPauseCount > 0) {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001121 // Rotation updates have been paused temporarily. Defer the update until
1122 // updates have been resumed.
1123 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
1124 return false;
1125 }
1126
1127 screenRotationAnimation =
1128 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
1129 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
1130 // Rotation updates cannot be performed while the previous rotation change
1131 // animation is still in progress. Skip this update. We will try updating
1132 // again after the animation is finished and the display is unfrozen.
1133 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
1134 return false;
1135 }
1136 if (mService.mDisplayFrozen) {
1137 // Even if the screen rotation animation has finished (e.g. isAnimating
1138 // returns false), there is still some time where we haven't yet unfrozen
1139 // the display. We also need to abort rotation here.
1140 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
1141 "Deferring rotation, still finishing previous rotation");
1142 return false;
1143 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001144 }
1145
1146 if (!mService.mDisplayEnabled) {
1147 // No point choosing a rotation if the display is not enabled.
1148 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
1149 return false;
1150 }
1151
1152 final int oldRotation = mRotation;
1153 final int lastOrientation = mLastOrientation;
1154 final boolean oldAltOrientation = mAltOrientation;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001155 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001156 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Computed rotation=" + rotation + " for display id="
1157 + mDisplayId + " based on lastOrientation=" + lastOrientation
1158 + " and oldRotation=" + oldRotation);
Riddle Hsuad256a12018-07-18 16:11:30 +08001159 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(mDisplayRotation,
1160 oldRotation, rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001161
Robert Carr0e007272017-10-02 13:00:55 -07001162 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001163 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001164 if (seamlessRotated != null && !forceUpdate) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001165 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
1166 // to complete (that is, waiting for windows to redraw). It's tempting to check
1167 // w.mSeamlessRotationCount but that could be incorrect in the case of
1168 // window-removal.
1169 return false;
1170 }
Robert Carr0e007272017-10-02 13:00:55 -07001171
1172 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001173 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -07001174 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -07001175 mayRotateSeamlessly = false;
1176 }
1177 for (int i = 0; i < mService.mSessions.size(); i++) {
1178 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
1179 mayRotateSeamlessly = false;
1180 break;
1181 }
1182 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001183 }
Robert Carr0e007272017-10-02 13:00:55 -07001184 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001185
1186 // TODO: Implement forced rotation changes.
1187 // Set mAltOrientation to indicate that the application is receiving
1188 // an orientation that has different metrics than it expected.
1189 // eg. Portrait instead of Landscape.
1190
Riddle Hsuad256a12018-07-18 16:11:30 +08001191 final boolean altOrientation = !mDisplayRotation.rotationHasCompatibleMetrics(
Andrii Kulian06d07d62017-03-14 11:11:47 -07001192 lastOrientation, rotation);
1193
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001194 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1195 + " selected orientation " + lastOrientation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001196 + ", got rotation " + rotation + " which has "
1197 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1198
1199 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1200 // No change.
1201 return false;
1202 }
1203
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001204 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1205 + " rotation changed to " + rotation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001206 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1207 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1208
1209 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1210 mService.mWaitingForConfig = true;
1211 }
1212
Riddle Hsuad256a12018-07-18 16:11:30 +08001213 setRotation(rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001214 mAltOrientation = altOrientation;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001215
1216 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
Riddle Hsu654a6f92018-07-13 22:59:36 +08001217 mService.mH.sendNewMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1218 this, WINDOW_FREEZE_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001219
1220 setLayoutNeeded();
1221 final int[] anim = new int[2];
Robert Carrf59b8dd2017-10-02 18:58:36 -07001222 mService.mPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001223
1224 if (!rotateSeamlessly) {
Robert Carrae606b42018-02-15 15:36:23 -08001225 mService.startFreezingDisplayLocked(anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001226 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1227 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1228 mDisplayId);
1229 } else {
1230 // The screen rotation animation uses a screenshot to freeze the screen
1231 // while windows resize underneath.
1232 // When we are rotating seamlessly, we allow the elements to transition
1233 // to their rotated state independently and without a freeze required.
1234 screenRotationAnimation = null;
1235
Robert Carrefc75702018-02-16 11:46:16 -08001236 mService.startSeamlessRotation();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001237 }
1238
1239 // We need to update our screen size information to match the new rotation. If the rotation
1240 // has actually changed then this method will return true and, according to the comment at
1241 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1242 // By updating the Display info here it will be available to
1243 // #computeScreenConfiguration() later.
1244 updateDisplayAndOrientation(getConfiguration().uiMode);
1245
Robert Carrae606b42018-02-15 15:36:23 -08001246 // NOTE: We disable the rotation in the emulator because
1247 // it doesn't support hardware OpenGL emulation yet.
1248 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1249 && screenRotationAnimation.hasScreenshot()) {
1250 if (screenRotationAnimation.setRotation(getPendingTransaction(), rotation,
1251 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1252 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1253 mService.scheduleAnimationLocked();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001254 }
1255 }
1256
Vishnu Nair83537a72018-07-19 21:27:48 -07001257 forAllWindows(w -> {
Vishnu Nairddd80742018-08-21 14:12:46 -07001258 w.seamlesslyRotateIfAllowed(getPendingTransaction(), oldRotation, rotation,
1259 rotateSeamlessly);
Vishnu Nair83537a72018-07-19 21:27:48 -07001260 }, true /* traverseTopToBottom */);
1261
Robert Carrae606b42018-02-15 15:36:23 -08001262 mService.mDisplayManagerInternal.performTraversal(getPendingTransaction());
1263 scheduleAnimation();
1264
Andrii Kulian06d07d62017-03-14 11:11:47 -07001265 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001266 if (w.mHasSurface && !rotateSeamlessly) {
1267 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001268 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001269 mService.mRoot.mOrientationChangeComplete = false;
1270 w.mLastFreezeDuration = 0;
1271 }
1272 w.mReportOrientationChanged = true;
1273 }, true /* traverseTopToBottom */);
1274
1275 if (rotateSeamlessly) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001276 mService.mH.sendNewMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1277 this, SEAMLESS_ROTATION_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001278 }
1279
1280 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1281 final WindowManagerService.RotationWatcher rotationWatcher
1282 = mService.mRotationWatchers.get(i);
1283 if (rotationWatcher.mDisplayId == mDisplayId) {
1284 try {
1285 rotationWatcher.mWatcher.onRotationChanged(rotation);
1286 } catch (RemoteException e) {
1287 // Ignore
1288 }
1289 }
1290 }
1291
1292 // TODO (multi-display): Magnification is supported only for the default display.
1293 // Announce rotation only if we will not animate as we already have the
1294 // windows in final state. Otherwise, we make this call at the rotation end.
1295 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1296 && isDefaultDisplay) {
1297 mService.mAccessibilityController.onRotationChangedLocked(this);
1298 }
1299
1300 return true;
1301 }
1302
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001303 void configureDisplayPolicy() {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001304 final int width = mBaseDisplayWidth;
1305 final int height = mBaseDisplayHeight;
1306 final int shortSize;
1307 final int longSize;
1308 if (width > height) {
1309 shortSize = height;
1310 longSize = width;
1311 } else {
1312 shortSize = width;
1313 longSize = height;
1314 }
1315
1316 final int shortSizeDp = shortSize * DisplayMetrics.DENSITY_DEFAULT / mBaseDisplayDensity;
1317 final int longSizeDp = longSize * DisplayMetrics.DENSITY_DEFAULT / mBaseDisplayDensity;
1318
1319 mDisplayRotation.configure(width, height, shortSizeDp, longSizeDp);
1320 mDisplayPolicy.configure(width, height, shortSizeDp);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001321
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001322 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
1323 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Arthur Hungbe5ce212018-09-13 18:41:56 +08001324
1325 // Tap Listeners are supported for:
1326 // 1. All physical displays (multi-display).
1327 // 2. VirtualDisplays on VR, AA (and everything else).
1328 if (mPointerEventDispatcher != null && mTapDetector == null) {
1329 if (DEBUG_DISPLAY) {
1330 Slog.d(TAG,
1331 "Registering PointerEventListener for DisplayId: " + mDisplayId);
1332 }
1333 mTapDetector = new TaskTapPointerEventListener(mService, this);
1334 registerPointerEventListener(mTapDetector);
1335 registerPointerEventListener(mService.mMousePositionTracker);
1336 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001337 }
1338
Andrii Kulian06d07d62017-03-14 11:11:47 -07001339 /**
1340 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1341 * changed.
1342 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1343 */
1344 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1345 // Use the effective "visual" dimensions based on current rotation
1346 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1347 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1348 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1349 int dw = realdw;
1350 int dh = realdh;
1351
1352 if (mAltOrientation) {
1353 if (realdw > realdh) {
1354 // Turn landscape into portrait.
1355 int maxw = (int)(realdh/1.3f);
1356 if (maxw < realdw) {
1357 dw = maxw;
1358 }
1359 } else {
1360 // Turn portrait into landscape.
1361 int maxh = (int)(realdw/1.3f);
1362 if (maxh < realdh) {
1363 dh = maxh;
1364 }
1365 }
1366 }
1367
1368 // Update application display metrics.
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001369 final WmDisplayCutout wmDisplayCutout = calculateDisplayCutoutForRotation(mRotation);
1370 final DisplayCutout displayCutout = wmDisplayCutout.getDisplayCutout();
1371
Andrii Kulian06d07d62017-03-14 11:11:47 -07001372 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001373 mDisplayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001374 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001375 mDisplayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001376 mDisplayInfo.rotation = mRotation;
1377 mDisplayInfo.logicalWidth = dw;
1378 mDisplayInfo.logicalHeight = dh;
1379 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1380 mDisplayInfo.appWidth = appWidth;
1381 mDisplayInfo.appHeight = appHeight;
1382 if (isDefaultDisplay) {
1383 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1384 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1385 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001386 mDisplayInfo.displayCutout = displayCutout.isEmpty() ? null : displayCutout;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001387 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1388 if (mDisplayScalingDisabled) {
1389 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1390 } else {
1391 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1392 }
1393
Andrii Kulianf0379de2018-03-14 16:24:07 -07001394 // We usually set the override info in DisplayManager so that we get consistent display
1395 // metrics values when displays are changing and don't send out new values until WM is aware
1396 // of them. However, we don't do this for displays that serve as containers for ActivityView
1397 // because we don't want letter-/pillar-boxing during resize.
1398 final DisplayInfo overrideDisplayInfo = mShouldOverrideDisplayConfiguration
1399 ? mDisplayInfo : null;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001400 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
Andrii Kulianf0379de2018-03-14 16:24:07 -07001401 overrideDisplayInfo);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001402
1403 mBaseDisplayRect.set(0, 0, dw, dh);
1404
1405 if (isDefaultDisplay) {
1406 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1407 mCompatDisplayMetrics);
1408 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001409
1410 updateBounds();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001411 return mDisplayInfo;
1412 }
1413
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001414 WmDisplayCutout calculateDisplayCutoutForRotation(int rotation) {
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001415 return mDisplayCutoutCache.getOrCompute(mInitialDisplayCutout, rotation);
1416 }
1417
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001418 private WmDisplayCutout calculateDisplayCutoutForRotationUncached(
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001419 DisplayCutout cutout, int rotation) {
Adrian Roos24264212018-02-19 16:26:15 +01001420 if (cutout == null || cutout == DisplayCutout.NO_CUTOUT) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001421 return WmDisplayCutout.NO_CUTOUT;
Adrian Roos1cf585052018-01-03 18:43:27 +01001422 }
Adrian Roos11c25582018-02-19 18:06:36 +01001423 if (rotation == ROTATION_0) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001424 return WmDisplayCutout.computeSafeInsets(
1425 cutout, mInitialDisplayWidth, mInitialDisplayHeight);
Adrian Roos24264212018-02-19 16:26:15 +01001426 }
Adrian Roosbed538e2018-02-21 17:50:07 +01001427 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Issei Suzuki43190bd2018-08-20 17:28:41 +02001428 final Rect[] newBounds = mRotationUtil.getRotatedBounds(
1429 WmDisplayCutout.computeSafeInsets(
Jorim Jaggi60640512018-06-29 01:14:31 +02001430 cutout, mInitialDisplayWidth, mInitialDisplayHeight)
Issei Suzuki43190bd2018-08-20 17:28:41 +02001431 .getDisplayCutout().getBoundingRectsAll(),
1432 rotation, mInitialDisplayWidth, mInitialDisplayHeight);
1433 return WmDisplayCutout.computeSafeInsets(DisplayCutout.fromBounds(newBounds),
Adrian Roos24264212018-02-19 16:26:15 +01001434 rotated ? mInitialDisplayHeight : mInitialDisplayWidth,
1435 rotated ? mInitialDisplayWidth : mInitialDisplayHeight);
Adrian Roos1cf585052018-01-03 18:43:27 +01001436 }
1437
Andrii Kulian06d07d62017-03-14 11:11:47 -07001438 /**
1439 * Compute display configuration based on display properties and policy settings.
1440 * Do not call if mDisplayReady == false.
1441 */
1442 void computeScreenConfiguration(Configuration config) {
1443 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1444
1445 final int dw = displayInfo.logicalWidth;
1446 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001447 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
Garfield Tane0846042018-07-26 13:42:04 -07001448 config.windowConfiguration.setWindowingMode(getWindowingMode());
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001449
Adrian Roos11c25582018-02-19 18:06:36 +01001450 final float density = mDisplayMetrics.density;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001451 config.screenWidthDp =
1452 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001453 config.uiMode, mDisplayId, displayInfo.displayCutout) / density);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001454 config.screenHeightDp =
1455 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001456 config.uiMode, mDisplayId, displayInfo.displayCutout) / density);
Bryce Lee7566d762017-03-30 09:34:15 -07001457
Adrian Roos11c25582018-02-19 18:06:36 +01001458 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh,
1459 displayInfo.displayCutout, mTmpRect);
Bryce Lee7566d762017-03-30 09:34:15 -07001460 final int leftInset = mTmpRect.left;
1461 final int topInset = mTmpRect.top;
1462 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001463 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1464 leftInset + displayInfo.appWidth /* right */,
1465 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001466 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1467 || displayInfo.rotation == Surface.ROTATION_270);
1468
1469 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
Adrian Roos11c25582018-02-19 18:06:36 +01001470 density, config);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001471
1472 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1473 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1474 ? Configuration.SCREENLAYOUT_ROUND_YES
1475 : Configuration.SCREENLAYOUT_ROUND_NO);
1476
1477 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1478 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1479 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1480 dh, mDisplayId);
1481 config.densityDpi = displayInfo.logicalDensityDpi;
1482
1483 config.colorMode =
Chia-I Wuf739bad2018-05-09 12:36:57 -07001484 ((displayInfo.isHdr() && mService.hasHdrSupport())
Andrii Kulian06d07d62017-03-14 11:11:47 -07001485 ? Configuration.COLOR_MODE_HDR_YES
1486 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001487 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001488 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1489 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1490
1491 // Update the configuration based on available input devices, lid switch,
1492 // and platform configuration.
1493 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1494 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1495 config.navigation = Configuration.NAVIGATION_NONAV;
1496
1497 int keyboardPresence = 0;
1498 int navigationPresence = 0;
1499 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1500 final int len = devices != null ? devices.length : 0;
1501 for (int i = 0; i < len; i++) {
1502 InputDevice device = devices[i];
1503 if (!device.isVirtual()) {
1504 final int sources = device.getSources();
1505 final int presenceFlag = device.isExternal() ?
1506 WindowManagerPolicy.PRESENCE_EXTERNAL :
1507 WindowManagerPolicy.PRESENCE_INTERNAL;
1508
1509 // TODO(multi-display): Configure on per-display basis.
1510 if (mService.mIsTouchDevice) {
1511 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1512 InputDevice.SOURCE_TOUCHSCREEN) {
1513 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1514 }
1515 } else {
1516 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1517 }
1518
1519 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1520 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1521 navigationPresence |= presenceFlag;
1522 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1523 && config.navigation == Configuration.NAVIGATION_NONAV) {
1524 config.navigation = Configuration.NAVIGATION_DPAD;
1525 navigationPresence |= presenceFlag;
1526 }
1527
1528 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1529 config.keyboard = Configuration.KEYBOARD_QWERTY;
1530 keyboardPresence |= presenceFlag;
1531 }
1532 }
1533 }
1534
1535 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1536 config.navigation = Configuration.NAVIGATION_DPAD;
1537 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1538 }
1539
1540 // Determine whether a hard keyboard is available and enabled.
1541 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1542 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1543 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1544 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1545 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1546 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1547 }
1548
1549 // Let the policy update hidden states.
1550 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1551 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1552 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1553 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1554 }
1555
1556 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1557 int displayId) {
1558 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1559 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1560 final int unrotDw, unrotDh;
1561 if (rotated) {
1562 unrotDw = dh;
1563 unrotDh = dw;
1564 } else {
1565 unrotDw = dw;
1566 unrotDh = dh;
1567 }
1568 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1569 displayId);
1570 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1571 displayId);
1572 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1573 displayId);
1574 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1575 displayId);
1576 return sw;
1577 }
1578
1579 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1580 DisplayMetrics dm, int dw, int dh, int displayId) {
1581 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001582 displayId, mDisplayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001583 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001584 uiMode, displayId, mDisplayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001585 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1586 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1587 if (curSize == 0 || size < curSize) {
1588 curSize = size;
1589 }
1590 return curSize;
1591 }
1592
1593 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1594 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1595
1596 // We need to determine the smallest width that will occur under normal
1597 // operation. To this, start with the base screen size and compute the
1598 // width under the different possible rotations. We need to un-rotate
1599 // the current screen dimensions before doing this.
1600 int unrotDw, unrotDh;
1601 if (rotated) {
1602 unrotDw = dh;
1603 unrotDh = dw;
1604 } else {
1605 unrotDw = dw;
1606 unrotDh = dh;
1607 }
1608 displayInfo.smallestNominalAppWidth = 1<<30;
1609 displayInfo.smallestNominalAppHeight = 1<<30;
1610 displayInfo.largestNominalAppWidth = 0;
1611 displayInfo.largestNominalAppHeight = 0;
1612 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1613 unrotDh);
1614 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1615 unrotDw);
1616 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1617 unrotDh);
1618 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1619 unrotDw);
1620 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1621 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1622 displayId);
1623 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1624 displayId);
1625 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1626 displayId);
1627 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1628 displayId);
1629 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1630 outConfig.screenLayout = sl;
1631 }
1632
1633 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1634 int uiMode, int displayId) {
1635 // Get the app screen size at this rotation.
Adrian Roos11c25582018-02-19 18:06:36 +01001636 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId,
1637 mDisplayInfo.displayCutout);
1638 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId,
1639 mDisplayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001640
1641 // Compute the screen layout size class for this rotation.
1642 int longSize = w;
1643 int shortSize = h;
1644 if (longSize < shortSize) {
1645 int tmp = longSize;
1646 longSize = shortSize;
1647 shortSize = tmp;
1648 }
1649 longSize = (int)(longSize/density);
1650 shortSize = (int)(shortSize/density);
1651 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1652 }
1653
1654 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1655 int uiMode, int dw, int dh) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001656 final DisplayCutout displayCutout = calculateDisplayCutoutForRotation(
1657 rotation).getDisplayCutout();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001658 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001659 displayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001660 if (width < displayInfo.smallestNominalAppWidth) {
1661 displayInfo.smallestNominalAppWidth = width;
1662 }
1663 if (width > displayInfo.largestNominalAppWidth) {
1664 displayInfo.largestNominalAppWidth = width;
1665 }
1666 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001667 displayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001668 if (height < displayInfo.smallestNominalAppHeight) {
1669 displayInfo.smallestNominalAppHeight = height;
1670 }
1671 if (height > displayInfo.largestNominalAppHeight) {
1672 displayInfo.largestNominalAppHeight = height;
1673 }
1674 }
1675
Riddle Hsua4d6fa22018-08-11 00:50:39 +08001676 /**
1677 * Apps that use the compact menu panel (as controlled by the panelMenuIsCompact
1678 * theme attribute) on devices that feature a physical options menu key attempt to position
1679 * their menu panel window along the edge of the screen nearest the physical menu key.
1680 * This lowers the travel distance between invoking the menu panel and selecting
1681 * a menu option.
1682 *
1683 * This method helps control where that menu is placed. Its current implementation makes
1684 * assumptions about the menu key and its relationship to the screen based on whether
1685 * the device's natural orientation is portrait (width < height) or landscape.
1686 *
1687 * The menu key is assumed to be located along the bottom edge of natural-portrait
1688 * devices and along the right edge of natural-landscape devices. If these assumptions
1689 * do not hold for the target device, this method should be changed to reflect that.
1690 *
1691 * @return A {@link Gravity} value for placing the options menu window.
1692 */
1693 int getPreferredOptionsPanelGravity() {
1694 final int rotation = getRotation();
1695 if (mInitialDisplayWidth < mInitialDisplayHeight) {
1696 // On devices with a natural orientation of portrait.
1697 switch (rotation) {
1698 default:
1699 case Surface.ROTATION_0:
1700 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1701 case Surface.ROTATION_90:
1702 return Gravity.RIGHT | Gravity.BOTTOM;
1703 case Surface.ROTATION_180:
1704 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1705 case Surface.ROTATION_270:
1706 return Gravity.START | Gravity.BOTTOM;
1707 }
1708 }
1709
1710 // On devices with a natural orientation of landscape.
1711 switch (rotation) {
1712 default:
1713 case Surface.ROTATION_0:
1714 return Gravity.RIGHT | Gravity.BOTTOM;
1715 case Surface.ROTATION_90:
1716 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1717 case Surface.ROTATION_180:
1718 return Gravity.START | Gravity.BOTTOM;
1719 case Surface.ROTATION_270:
1720 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1721 }
1722 }
1723
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001724 DockedStackDividerController getDockedDividerController() {
1725 return mDividerControllerLocked;
1726 }
1727
Winson Chung655332c2016-10-31 13:14:28 -07001728 PinnedStackController getPinnedStackController() {
1729 return mPinnedStackControllerLocked;
1730 }
1731
Jeff Browna506a6e2013-06-04 00:02:38 -07001732 /**
1733 * Returns true if the specified UID has access to this display.
1734 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001735 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001736 return mDisplay.hasAccess(uid);
1737 }
1738
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001739 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001740 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001741 }
1742
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001743 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001744 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001745 }
1746
Wale Ogunwale61911492017-10-11 08:50:50 -07001747 /**
1748 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1749 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001750 TaskStack getSplitScreenPrimaryStack() {
1751 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001752 return (stack != null && stack.isVisible()) ? stack : null;
1753 }
1754
Robert Carr9785cf32018-04-25 15:06:07 -07001755 boolean hasSplitScreenPrimaryStack() {
1756 return getSplitScreenPrimaryStack() != null;
1757 }
1758
Wale Ogunwale61911492017-10-11 08:50:50 -07001759 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001760 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001761 * not visible.
1762 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001763 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1764 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001765 }
1766
1767 TaskStack getPinnedStack() {
1768 return mTaskStackContainers.getPinnedStack();
1769 }
1770
1771 private boolean hasPinnedStack() {
1772 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001773 }
1774
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001775 /**
1776 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1777 * Null is no compatible stack on the display.
1778 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001779 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001780 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1781 }
1782
1783 /**
1784 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1785 * activity type. Null is no compatible stack on the display.
1786 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001787 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001788 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001789 }
1790
Bryce Lee48f4b572017-04-10 10:54:15 -07001791 @VisibleForTesting
Kazuki Takise148d00a2018-05-31 15:32:19 +09001792 WindowList<TaskStack> getStacks() {
1793 return mTaskStackContainers.mChildren;
1794 }
1795
1796 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001797 TaskStack getTopStack() {
1798 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001799 }
1800
Winson Chunge2d72172018-01-25 17:46:20 +00001801 ArrayList<Task> getVisibleTasks() {
1802 return mTaskStackContainers.getVisibleTasks();
1803 }
1804
Wale Ogunwale61911492017-10-11 08:50:50 -07001805 void onStackWindowingModeChanged(TaskStack stack) {
1806 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001807 }
1808
Andrii Kulian441e4492016-09-29 15:25:00 -07001809 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001810 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001811 super.onConfigurationChanged(newParentConfig);
1812
Andrii Kulian3a507b52016-09-19 18:14:12 -07001813 // The display size information is heavily dependent on the resources in the current
1814 // configuration, so we need to reconfigure it every time the configuration changes.
Riddle Hsuf53da812018-08-15 22:00:27 +08001815 // See {@link #configureDisplayPolicy}...sigh...
Andrii Kulian3a507b52016-09-19 18:14:12 -07001816 mService.reconfigureDisplayLocked(this);
1817
Bryce Leef3c6a472017-11-14 14:53:06 -08001818 final DockedStackDividerController dividerController = getDockedDividerController();
1819
1820 if (dividerController != null) {
1821 getDockedDividerController().onConfigurationChanged();
1822 }
1823
1824 final PinnedStackController pinnedStackController = getPinnedStackController();
1825
1826 if (pinnedStackController != null) {
1827 getPinnedStackController().onConfigurationChanged();
1828 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001829 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001830
Andrii Kulian441e4492016-09-29 15:25:00 -07001831 /**
1832 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1833 * bounds were updated.
1834 */
Bryce Leeaea60d22018-01-31 14:42:15 -08001835 void updateStackBoundsAfterConfigChange(@NonNull List<TaskStack> changedStackList) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001836 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1837 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001838 if (stack.updateBoundsAfterConfigChange()) {
Bryce Leeaea60d22018-01-31 14:42:15 -08001839 changedStackList.add(stack);
Andrii Kulian3a507b52016-09-19 18:14:12 -07001840 }
1841 }
Winson Chung32c566f2017-04-11 18:31:21 -07001842
1843 // If there was no pinned stack, we still need to notify the controller of the display info
1844 // update as a result of the config change. We do this here to consolidate the flow between
1845 // changes when there is and is not a stack.
Wale Ogunwale61911492017-10-11 08:50:50 -07001846 if (!hasPinnedStack()) {
Winson Chung32c566f2017-04-11 18:31:21 -07001847 mPinnedStackControllerLocked.onDisplayInfoChanged();
1848 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001849 }
1850
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001851 @Override
1852 boolean fillsParent() {
1853 return true;
1854 }
1855
1856 @Override
1857 boolean isVisible() {
1858 return true;
1859 }
1860
1861 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001862 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001863 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001864 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001865 }
1866
Robert Carr9785cf32018-04-25 15:06:07 -07001867 /**
1868 * In split-screen mode we process the IME containers above the docked divider
1869 * rather than directly above their target.
1870 */
1871 private boolean skipTraverseChild(WindowContainer child) {
1872 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null
1873 && !hasSplitScreenPrimaryStack()) {
1874 return true;
1875 }
1876 return false;
1877 }
1878
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001879 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001880 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1881 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1882 // target, or here in order if there isn't an IME target.
1883 if (traverseTopToBottom) {
1884 for (int i = mChildren.size() - 1; i >= 0; --i) {
1885 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001886 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001887 continue;
1888 }
Robert Carr9785cf32018-04-25 15:06:07 -07001889
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001890 if (child.forAllWindows(callback, traverseTopToBottom)) {
1891 return true;
1892 }
1893 }
1894 } else {
1895 final int count = mChildren.size();
1896 for (int i = 0; i < count; i++) {
1897 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001898 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001899 continue;
1900 }
Robert Carr9785cf32018-04-25 15:06:07 -07001901
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001902 if (child.forAllWindows(callback, traverseTopToBottom)) {
1903 return true;
1904 }
1905 }
1906 }
1907 return false;
1908 }
1909
1910 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1911 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1912 }
1913
Wale Ogunwale399c8692017-05-08 14:22:42 -07001914 @Override
1915 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001916 final WindowManagerPolicy policy = mService.mPolicy;
1917
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001918 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001919 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001920 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1921 + " is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001922 // If the display is frozen, some activities may be in the middle of restarting, and
1923 // thus have removed their old window. If the window has the flag to hide the lock
1924 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1925 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001926 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001927 } else if (policy.isKeyguardLocked()) {
1928 // Use the last orientation the while the display is frozen with the keyguard
1929 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1930 // window. We don't want to check the show when locked window directly though as
1931 // things aren't stable while the display is frozen, for example the window could be
1932 // momentarily unavailable due to activity relaunch.
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001933 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1934 + " is frozen while keyguard locked, return " + mLastOrientation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08001935 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001936 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001937 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001938 final int orientation = mAboveAppWindowsContainers.getOrientation();
1939 if (orientation != SCREEN_ORIENTATION_UNSET) {
1940 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001941 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001942 }
1943
Wale Ogunwale399c8692017-05-08 14:22:42 -07001944 // Top system windows are not requesting an orientation. Start searching from apps.
1945 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001946 }
1947
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001948 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001949 // Check if display metrics changed and update base values if needed.
1950 updateBaseDisplayMetricsIfNeeded();
1951
Craig Mautner722285e2012-09-07 13:55:58 -07001952 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001953 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001954
Wale Ogunwale61911492017-10-11 08:50:50 -07001955 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1956 mTaskStackContainers.getChildAt(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001957 }
Craig Mautner722285e2012-09-07 13:55:58 -07001958 }
1959
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001960 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001961 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1962 if (displayManagerInternal != null) {
1963 // Bootstrap the default logical display from the display manager.
1964 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1965 if (newDisplayInfo != null) {
1966 mDisplayInfo.copyFrom(newDisplayInfo);
1967 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001968 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001969
Andrii Kuliancd097992017-03-23 18:31:59 -07001970 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1971 mDisplayInfo.logicalDensityDpi);
1972 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1973 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1974 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01001975 mInitialDisplayCutout = mDisplayInfo.displayCutout;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001976 }
1977
Andrii Kuliancd097992017-03-23 18:31:59 -07001978 /**
1979 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1980 * values.
1981 */
1982 private void updateBaseDisplayMetricsIfNeeded() {
1983 // Get real display metrics without overrides from WM.
1984 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1985 final int orientation = mDisplayInfo.rotation;
1986 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1987 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1988 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1989 final int newDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01001990 final DisplayCutout newCutout = mDisplayInfo.displayCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07001991
1992 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1993 || mInitialDisplayHeight != newHeight
Adrian Roos1cf585052018-01-03 18:43:27 +01001994 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi
1995 || !Objects.equals(mInitialDisplayCutout, newCutout);
Andrii Kuliancd097992017-03-23 18:31:59 -07001996
1997 if (displayMetricsChanged) {
1998 // Check if display size or density is forced.
1999 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
2000 || mBaseDisplayHeight != mInitialDisplayHeight;
2001 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
2002
2003 // If there is an override set for base values - use it, otherwise use new values.
2004 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
2005 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
2006 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
2007
2008 // Real display metrics changed, so we should also update initial values.
2009 mInitialDisplayWidth = newWidth;
2010 mInitialDisplayHeight = newHeight;
2011 mInitialDisplayDensity = newDensity;
Adrian Roos1cf585052018-01-03 18:43:27 +01002012 mInitialDisplayCutout = newCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07002013 mService.reconfigureDisplayLocked(this);
2014 }
2015 }
2016
Bryce Lee27cec322017-03-21 09:41:37 -07002017 /** Sets the maximum width the screen resolution can be */
2018 void setMaxUiWidth(int width) {
2019 if (DEBUG_DISPLAY) {
2020 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
2021 }
2022
2023 mMaxUiWidth = width;
2024
2025 // Update existing metrics.
2026 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
2027 }
2028
2029 /** Update base (override) display metrics. */
2030 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
2031 mBaseDisplayWidth = baseWidth;
2032 mBaseDisplayHeight = baseHeight;
2033 mBaseDisplayDensity = baseDensity;
2034
2035 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
2036 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
2037 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
2038 mBaseDisplayWidth = mMaxUiWidth;
2039
2040 if (DEBUG_DISPLAY) {
2041 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
2042 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
2043 + " on display:" + getDisplayId());
2044 }
2045 }
2046
2047 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08002048
2049 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07002050 }
2051
Riddle Hsuf53da812018-08-15 22:00:27 +08002052 /**
2053 * Forces this display to use the specified density.
2054 *
2055 * @param density The density in DPI to use. If the value equals to initial density, the setting
2056 * will be cleared.
2057 * @param userId The target user to apply. Only meaningful when this is default display. If the
2058 * user id is {@link UserHandle#USER_CURRENT}, it means to apply current settings
2059 * so only need to configure display.
2060 */
2061 void setForcedDensity(int density, int userId) {
2062 final boolean clear = density == mInitialDisplayDensity;
2063 final boolean updateCurrent = userId == UserHandle.USER_CURRENT;
2064 if (mService.mCurrentUserId == userId || updateCurrent) {
2065 mBaseDisplayDensity = density;
2066 mService.reconfigureDisplayLocked(this);
2067 }
2068 if (updateCurrent) {
2069 // We are applying existing settings so no need to save it again.
2070 return;
2071 }
2072
2073 if (density == mInitialDisplayDensity) {
2074 density = 0;
2075 }
2076 mService.mDisplaySettings.setForcedDensity(this, density, userId);
2077 }
2078
2079 /** @param mode {@link #FORCE_SCALING_MODE_AUTO} or {@link #FORCE_SCALING_MODE_DISABLED}. */
2080 void setForcedScalingMode(@ForceScalingMode int mode) {
2081 if (mode != FORCE_SCALING_MODE_DISABLED) {
2082 mode = FORCE_SCALING_MODE_AUTO;
2083 }
2084
2085 mDisplayScalingDisabled = (mode != FORCE_SCALING_MODE_AUTO);
2086 Slog.i(TAG_WM, "Using display scaling mode: " + (mDisplayScalingDisabled ? "off" : "auto"));
2087 mService.reconfigureDisplayLocked(this);
2088
2089 mService.mDisplaySettings.setForcedScalingMode(this, mode);
2090 }
2091
2092 /** If the given width and height equal to initial size, the setting will be cleared. */
2093 void setForcedSize(int width, int height) {
2094 final boolean clear = mInitialDisplayWidth == width && mInitialDisplayHeight == height;
2095 if (!clear) {
2096 // Set some sort of reasonable bounds on the size of the display that we will try
2097 // to emulate.
2098 final int minSize = 200;
2099 final int maxScale = 2;
2100 width = Math.min(Math.max(width, minSize), mInitialDisplayWidth * maxScale);
2101 height = Math.min(Math.max(height, minSize), mInitialDisplayHeight * maxScale);
2102 }
2103
2104 Slog.i(TAG_WM, "Using new display size: " + width + "x" + height);
2105 updateBaseDisplayMetrics(width, height, mBaseDisplayDensity);
2106 mService.reconfigureDisplayLocked(this);
2107
2108 if (clear) {
2109 width = height = 0;
2110 }
2111 mService.mDisplaySettings.setForcedSize(this, width, height);
2112 }
2113
Wale Ogunwaledb506192017-12-08 10:57:32 -08002114 void getStableRect(Rect out) {
2115 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08002116 }
2117
Wale Ogunwale61911492017-10-11 08:50:50 -07002118 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002119 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
2120 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07002121
Wale Ogunwale61911492017-10-11 08:50:50 -07002122 final TaskStack stack = new TaskStack(mService, stackId, controller);
2123 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002124 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07002125 }
2126
Andrii Kulian51c1b672017-04-07 18:39:32 -07002127 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002128 final DisplayContent prevDc = stack.getDisplayContent();
2129 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07002130 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
2131 + " which is not currently attached to any display");
2132 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002133 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07002134 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
2135 + " to its current displayId=" + mDisplayId);
2136 }
2137
Wale Ogunwale61911492017-10-11 08:50:50 -07002138 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07002139 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08002140 }
2141
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002142 @Override
2143 protected void addChild(DisplayChildWindowContainer child,
2144 Comparator<DisplayChildWindowContainer> comparator) {
2145 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2146 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002147
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002148 @Override
2149 protected void addChild(DisplayChildWindowContainer child, int index) {
2150 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2151 }
2152
2153 @Override
2154 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002155 // Only allow removal of direct children from this display if the display is in the process
2156 // of been removed.
2157 if (mRemovingDisplay) {
2158 super.removeChild(child);
2159 return;
2160 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002161 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002162 }
2163
Andrii Kuliand2765632016-12-12 22:26:34 -08002164 @Override
2165 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
2166 // Children of the display are statically ordered, so the real intention here is to perform
2167 // the operation on the display and not the static direct children.
2168 getParent().positionChildAt(position, this, includingParents);
2169 }
2170
Riddle Hsu57831b52018-07-27 00:31:48 +08002171 void positionStackAt(int position, TaskStack child, boolean includingParents) {
2172 mTaskStackContainers.positionChildAt(position, child, includingParents);
Winson Chung59a47ded2018-01-25 17:46:06 +00002173 layoutAndAssignWindowLayersIfNeeded();
2174 }
2175
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002176 /**
2177 * Used to obtain task ID when user taps on coordinate (x, y) in this display, and outside
2178 * current task in focus.
2179 *
2180 * This returns the task ID of the foremost task at (x, y) if the task is not home. Otherwise it
2181 * returns -1.
2182 *
2183 * @param x horizontal coordinate of the tap position
2184 * @param y vertical coordinate of the tap position
2185 * @return the task ID if a non-home task is found; -1 if not
2186 */
2187 int taskForTapOutside(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002188 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2189 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Garfield Tanf2e0a9e2018-10-16 09:42:02 -07002190 if (stack.isActivityTypeHome() && !stack.inMultiWindowMode()) {
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002191 // We skip not only home stack, but also everything behind home because user can't
Garfield Tanf2e0a9e2018-10-16 09:42:02 -07002192 // see them when home stack is isn't in multi-window mode.
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002193 break;
2194 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002195 final int taskId = stack.taskIdFromPoint(x, y);
2196 if (taskId != -1) {
2197 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07002198 }
2199 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002200 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07002201 }
2202
Chong Zhang8e89b312015-09-09 15:09:30 -07002203 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08002204 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07002205 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07002206 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07002207 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002208 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002209 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07002210 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2211 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002212 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002213 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002214 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002215
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002216 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
2217 if (mTmpTaskForResizePointSearchResult.searchDone) {
2218 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07002219 }
2220 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002221 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002222 }
2223
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002224 void updateTouchExcludeRegion() {
2225 final Task focusedTask = (mFocusedApp != null ? mFocusedApp.getTask() : null);
David Stevensee9e2772017-02-09 16:30:27 -08002226 if (focusedTask == null) {
2227 mTouchExcludeRegion.setEmpty();
2228 } else {
2229 mTouchExcludeRegion.set(mBaseDisplayRect);
2230 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
2231 mTmpRect2.setEmpty();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002232 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0;
2233 --stackNdx) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002234 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002235 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
2236 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08002237 }
2238 // If we removed the focused task above, add it back and only leave its
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002239 // outside touch area in the exclusion. TapDetector is not interested in
David Stevensee9e2772017-02-09 16:30:27 -08002240 // any touch inside the focused task itself.
2241 if (!mTmpRect2.isEmpty()) {
2242 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
2243 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08002244 }
lumark90120a82018-08-15 00:33:03 +08002245 if (mInputMethodWindow != null && mInputMethodWindow.isVisibleLw()) {
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002246 // If the input method is visible and the user is typing, we don't want these touch
2247 // events to be intercepted and used to change focus. This would likely cause a
2248 // disappearance of the input method.
lumark90120a82018-08-15 00:33:03 +08002249 mInputMethodWindow.getTouchableRegion(mTmpRegion);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002250 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002251 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002252 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002253 final WindowState win = mTapExcludedWindows.get(i);
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002254 win.getTouchableRegion(mTmpRegion);
2255 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
2256 }
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002257 for (int i = mTapExcludeProvidingWindows.size() - 1; i >= 0; i--) {
2258 final WindowState win = mTapExcludeProvidingWindows.valueAt(i);
2259 win.amendTapExcludeRegion(mTouchExcludeRegion);
2260 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08002261 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07002262 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002263 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07002264 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002265 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
2266 }
Craig Mautner1bef3892015-02-17 15:09:47 -08002267 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07002268 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08002269 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07002270 }
2271
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002272 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07002273 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002274 super.switchUser();
2275 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07002276 }
2277
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07002278 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002279 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2280 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07002281 }
2282 }
2283
Wale Ogunwale10124582016-09-15 20:25:50 -07002284 @Override
2285 void removeIfPossible() {
2286 if (isAnimating()) {
2287 mDeferredRemoval = true;
2288 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07002289 }
Wale Ogunwale10124582016-09-15 20:25:50 -07002290 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07002291 }
2292
Wale Ogunwale10124582016-09-15 20:25:50 -07002293 @Override
2294 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002295 mRemovingDisplay = true;
2296 try {
2297 super.removeImmediately();
2298 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Arthur Hungbe5ce212018-09-13 18:41:56 +08002299 if (mPointerEventDispatcher != null && mTapDetector != null) {
2300 unregisterPointerEventListener(mTapDetector);
2301 unregisterPointerEventListener(mService.mMousePositionTracker);
2302 mTapDetector = null;
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002303 }
Jorim Jaggia3844032018-01-03 15:54:43 +01002304 mService.mAnimator.removeDisplayLocked(mDisplayId);
Louis Chang207f9112018-07-30 08:58:54 +08002305 mWindowingLayer.release();
2306 mOverlayLayer.release();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002307 } finally {
2308 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08002309 }
Bryce Leef19cbe22018-02-02 15:09:21 -08002310
Arthur Hung39134b22018-08-14 11:58:28 +08002311 mInputMonitor.onRemoved();
Riddle Hsuf53da812018-08-15 22:00:27 +08002312 mService.mWindowPlacerLocked.requestTraversal();
Craig Mautner95da1082014-02-24 17:54:35 -08002313 }
2314
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002315 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07002316 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002317 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002318 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
2319
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002320 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002321 removeImmediately();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002322 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08002323 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002324 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08002325 }
2326
Andrii Kulian0214ed92017-05-16 13:44:05 -07002327 /** @return 'true' if removal of this display content is deferred due to active animation. */
2328 boolean isRemovalDeferred() {
2329 return mDeferredRemoval;
2330 }
2331
Wale Ogunwale10124582016-09-15 20:25:50 -07002332 boolean animateForIme(float interpolatedValue, float animationTarget,
2333 float dividerAnimationTarget) {
2334 boolean updated = false;
2335
Wale Ogunwale61911492017-10-11 08:50:50 -07002336 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2337 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002338 if (stack == null || !stack.isAdjustedForIme()) {
2339 continue;
2340 }
2341
2342 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
2343 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2344 updated = true;
2345 } else {
2346 mDividerControllerLocked.mLastAnimationProgress =
2347 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
2348 mDividerControllerLocked.mLastDividerProgress =
2349 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
2350 updated |= stack.updateAdjustForIme(
2351 mDividerControllerLocked.mLastAnimationProgress,
2352 mDividerControllerLocked.mLastDividerProgress,
2353 false /* force */);
2354 }
2355 if (interpolatedValue >= 1f) {
2356 stack.endImeAdjustAnimation();
2357 }
2358 }
2359
2360 return updated;
2361 }
2362
2363 boolean clearImeAdjustAnimation() {
2364 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002365 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2366 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002367 if (stack != null && stack.isAdjustedForIme()) {
2368 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2369 changed = true;
2370 }
2371 }
2372 return changed;
2373 }
2374
2375 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002376 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2377 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002378 if (stack.isVisible() && stack.isAdjustedForIme()) {
2379 stack.beginImeAdjustAnimation();
2380 }
2381 }
2382 }
2383
2384 void adjustForImeIfNeeded() {
lumark90120a82018-08-15 00:33:03 +08002385 final WindowState imeWin = mInputMethodWindow;
Wale Ogunwale10124582016-09-15 20:25:50 -07002386 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2387 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002388 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002389 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2390 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2391 imeTargetStack.getDockSide() : DOCKED_INVALID;
2392 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2393 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2394 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002395 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002396 final boolean imeHeightChanged = imeVisible &&
2397 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2398
2399 // The divider could be adjusted for IME position, or be thinner than usual,
2400 // or both. There are three possible cases:
2401 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2402 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2403 // - If IME is not visible, divider is not moved and is normal width.
2404
2405 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002406 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2407 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002408 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002409 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2410 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002411 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2412 } else {
2413 stack.resetAdjustedForIme(false);
2414 }
2415 }
2416 mDividerControllerLocked.setAdjustedForIme(
2417 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2418 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002419 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2420 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002421 stack.resetAdjustedForIme(!dockVisible);
2422 }
2423 mDividerControllerLocked.setAdjustedForIme(
2424 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2425 }
Winson Chung655332c2016-10-31 13:14:28 -07002426 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002427 }
2428
2429 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002430 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2431 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002432 stack.prepareFreezingTaskBounds();
2433 }
2434 }
2435
Wale Ogunwale94744212015-09-21 19:01:47 -07002436 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002437 getBounds(mTmpRect, newRotation);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002438
2439 // Compute a transform matrix to undo the coordinate space transformation,
2440 // and present the window at the same physical position it previously occupied.
2441 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2442 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2443
2444 mTmpRectF.set(bounds);
2445 mTmpMatrix.mapRect(mTmpRectF);
2446 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002447 }
2448
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002449 static int deltaRotation(int oldRotation, int newRotation) {
2450 int delta = newRotation - oldRotation;
2451 if (delta < 0) delta += 4;
2452 return delta;
2453 }
2454
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002455 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002456 Matrix outMatrix) {
2457 // For rotations without Z-ordering we don't need the target rectangle's position.
2458 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2459 displayHeight, outMatrix);
2460 }
2461
2462 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2463 float displayWidth, float displayHeight, Matrix outMatrix) {
2464 switch (rotation) {
2465 case ROTATION_0:
2466 outMatrix.reset();
2467 break;
2468 case ROTATION_270:
2469 outMatrix.setRotate(270, 0, 0);
2470 outMatrix.postTranslate(0, displayHeight);
2471 outMatrix.postTranslate(rectTop, 0);
2472 break;
2473 case ROTATION_180:
2474 outMatrix.reset();
2475 break;
2476 case ROTATION_90:
2477 outMatrix.setRotate(90, 0, 0);
2478 outMatrix.postTranslate(displayWidth, 0);
2479 outMatrix.postTranslate(-rectTop, rectLeft);
2480 break;
2481 }
2482 }
2483
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002484 @CallSuper
2485 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002486 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002487 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002488 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002489 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002490 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2491 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002492 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002493 }
2494 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2495 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002496 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2497 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002498 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002499 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002500 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2501 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002502 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002503 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002504 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2505 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002506 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002507 }
2508 proto.write(DPI, mBaseDisplayDensity);
2509 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002510 proto.write(ROTATION, mRotation);
2511 final ScreenRotationAnimation screenRotationAnimation =
2512 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2513 if (screenRotationAnimation != null) {
2514 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2515 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002516 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
Louis Chang7501e332018-08-20 13:08:39 +08002517 proto.write(SURFACE_SIZE, mSurfaceSize);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002518 if (mFocusedApp != null) {
2519 mFocusedApp.writeNameToProto(proto, FOCUSED_APP);
2520 }
Steven Timotiusaf03df62017-07-18 16:56:43 -07002521 proto.end(token);
2522 }
2523
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002524 @Override
2525 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2526 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002527 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2528 final String subPrefix = " " + prefix;
2529 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2530 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2531 pw.print("dpi");
2532 if (mInitialDisplayWidth != mBaseDisplayWidth
2533 || mInitialDisplayHeight != mBaseDisplayHeight
2534 || mInitialDisplayDensity != mBaseDisplayDensity) {
2535 pw.print(" base=");
2536 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2537 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2538 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002539 if (mDisplayScalingDisabled) {
2540 pw.println(" noscale");
2541 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002542 pw.print(" cur=");
2543 pw.print(mDisplayInfo.logicalWidth);
2544 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2545 pw.print(" app=");
2546 pw.print(mDisplayInfo.appWidth);
2547 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2548 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2549 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2550 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2551 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002552 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002553 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002554 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002555
Craig Mautnerdc548482014-02-05 13:35:24 -08002556 pw.println();
Adrian Roos5251b1d2018-03-23 18:57:43 +01002557 pw.print(prefix); pw.print("mLayoutSeq="); pw.println(mLayoutSeq);
Riddle Hsu654a6f92018-07-13 22:59:36 +08002558 pw.print(prefix);
2559 pw.print("mDeferredRotationPauseCount="); pw.println(mDeferredRotationPauseCount);
Adrian Roos5251b1d2018-03-23 18:57:43 +01002560
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002561 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
2562 if (mLastFocus != mCurrentFocus) {
2563 pw.print(" mLastFocus="); pw.println(mLastFocus);
2564 }
2565 if (mLosingFocus.size() > 0) {
2566 pw.println();
2567 pw.println(" Windows losing focus:");
2568 for (int i = mLosingFocus.size() - 1; i >= 0; i--) {
2569 final WindowState w = mLosingFocus.get(i);
2570 pw.print(" Losing #"); pw.print(i); pw.print(' ');
2571 pw.print(w);
2572 if (dumpAll) {
2573 pw.println(":");
2574 w.dump(pw, " ", true);
2575 } else {
2576 pw.println();
2577 }
2578 }
2579 }
2580 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
2581
Adrian Roos5251b1d2018-03-23 18:57:43 +01002582 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002583 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002584 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2585 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002586 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002587 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002588
Craig Mautnerdc548482014-02-05 13:35:24 -08002589 pw.println();
2590 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002591 pw.println();
2592 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002593 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002594 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002595 pw.print(" Exiting #"); pw.print(i);
2596 pw.print(' '); pw.print(token);
2597 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002598 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002599 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002600 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002601
Jorim Jaggi31f71702016-05-04 16:43:04 -07002602 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002603
2604 // Dump stack references
2605 final TaskStack homeStack = getHomeStack();
2606 if (homeStack != null) {
2607 pw.println(prefix + "homeStack=" + homeStack.getName());
2608 }
2609 final TaskStack pinnedStack = getPinnedStack();
2610 if (pinnedStack != null) {
2611 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2612 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002613 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002614 if (splitScreenPrimaryStack != null) {
2615 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2616 }
2617
2618 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002619 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002620 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002621 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002622
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002623 pw.println();
2624 mDisplayFrames.dump(prefix, pw);
Arthur Hung95b38a92018-07-20 18:56:12 +08002625 pw.println();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08002626 mDisplayPolicy.dump(prefix, pw);
2627 pw.println();
Riddle Hsuad256a12018-07-18 16:11:30 +08002628 mDisplayRotation.dump(prefix, pw);
2629 pw.println();
Arthur Hung95b38a92018-07-20 18:56:12 +08002630 mInputMonitor.dump(pw, " ");
Craig Mautner59c00972012-07-30 12:10:24 -07002631 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002632
2633 @Override
2634 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002635 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002636 }
2637
2638 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002639 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002640 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002641
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002642 /** Returns true if the stack in the windowing mode is visible. */
2643 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002644 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002645 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002646 }
2647
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002648 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002649 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002650 final int x = (int) xf;
2651 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002652 final WindowState touchedWin = getWindow(w -> {
2653 final int flags = w.mAttrs.flags;
2654 if (!w.isVisibleLw()) {
2655 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002656 }
2657 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002658 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002659 }
2660
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002661 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002662 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002663 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002664 }
2665
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002666 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002667
2668 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002669 return mTmpRegion.contains(x, y) || touchFlags == 0;
2670 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002671
2672 return touchedWin;
2673 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002674
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002675 boolean canAddToastWindowForUid(int uid) {
2676 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002677 // Also if the app is focused adding more than one toast at
2678 // a time for better backwards compatibility.
2679 final WindowState focusedWindowForUid = getWindow(w ->
2680 w.mOwnerUid == uid && w.isFocused());
2681 if (focusedWindowForUid != null) {
2682 return true;
2683 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002684 final WindowState win = getWindow(w ->
2685 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2686 && !w.mWindowRemovalAllowed);
2687 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002688 }
2689
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002690 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002691 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2692 return;
2693 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002694
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002695 // Used to communicate the old focus to the callback method.
2696 mTmpWindow = oldFocus;
2697
2698 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002699 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002700
2701 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002702 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002703
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002704 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002705
2706 if (mTmpWindow == null) {
2707 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2708 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002709 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002710 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002711 }
2712
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002713
2714 /**
2715 * Update the focused window and make some adjustments if the focus has changed.
2716 *
2717 * @param mode Indicates the situation we are in. Possible modes are:
2718 * {@link WindowManagerService#UPDATE_FOCUS_NORMAL},
2719 * {@link WindowManagerService#UPDATE_FOCUS_PLACING_SURFACES},
2720 * {@link WindowManagerService#UPDATE_FOCUS_WILL_PLACE_SURFACES},
2721 * {@link WindowManagerService#UPDATE_FOCUS_REMOVING_FOCUS}
2722 * @param updateInputWindows Whether to sync the window information to the input module.
2723 * @return {@code true} if the focused window has changed.
2724 */
2725 boolean updateFocusedWindowLocked(int mode, boolean updateInputWindows, boolean focusFound) {
2726 final WindowState newFocus = findFocusedWindow();
2727 if (mCurrentFocus == newFocus) {
2728 return false;
2729 }
2730 mService.mH.obtainMessage(REPORT_FOCUS_CHANGE, this).sendToTarget();
2731 boolean imWindowChanged = false;
2732 // TODO (b/111080190): Multi-Session IME
2733 if (!focusFound) {
2734 final WindowState imWindow = mInputMethodWindow;
2735 if (imWindow != null) {
2736 final WindowState prevTarget = mService.mInputMethodTarget;
2737
2738 final WindowState newTarget = computeImeTarget(true /* updateImeTarget*/);
2739 imWindowChanged = prevTarget != newTarget;
2740
2741 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS
2742 && mode != UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2743 assignWindowLayers(false /* setLayoutNeeded */);
2744 }
2745 }
2746 }
2747
2748 if (imWindowChanged) {
2749 mService.mWindowsChanged = true;
2750 setLayoutNeeded();
2751 }
2752
2753 if (DEBUG_FOCUS_LIGHT || mService.localLOGV) Slog.v(TAG_WM, "Changing focus from "
2754 + mCurrentFocus + " to " + newFocus + " displayId=" + getDisplayId()
2755 + " Callers=" + Debug.getCallers(4));
2756 final WindowState oldFocus = mCurrentFocus;
2757 mCurrentFocus = newFocus;
2758 mLosingFocus.remove(newFocus);
2759
2760 if (newFocus != null) {
2761 mWinAddedSinceNullFocus.clear();
2762 mWinRemovedSinceNullFocus.clear();
2763
2764 if (newFocus.canReceiveKeys()) {
2765 // Displaying a window implicitly causes dispatching to be unpaused.
2766 // This is to protect against bugs if someone pauses dispatching but
2767 // forgets to resume.
2768 newFocus.mToken.paused = false;
2769 }
2770 }
2771
2772 // System UI is only shown on the default display.
2773 int focusChanged = isDefaultDisplay
2774 ? mService.mPolicy.focusChangedLw(oldFocus, newFocus) : 0;
2775
2776 if (imWindowChanged && oldFocus != mInputMethodWindow) {
2777 // Focus of the input method window changed. Perform layout if needed.
2778 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2779 performLayout(true /*initial*/, updateInputWindows);
2780 focusChanged &= ~FINISH_LAYOUT_REDO_LAYOUT;
2781 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2782 // Client will do the layout, but we need to assign layers
2783 // for handleNewWindowLocked() below.
2784 assignWindowLayers(false /* setLayoutNeeded */);
2785 }
2786 }
2787
2788 if ((focusChanged & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2789 // The change in focus caused us to need to do a layout. Okay.
2790 setLayoutNeeded();
2791 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2792 performLayout(true /*initial*/, updateInputWindows);
2793 } else if (mode == UPDATE_FOCUS_REMOVING_FOCUS) {
2794 mService.mRoot.performSurfacePlacement(false);
2795 }
2796 }
2797
2798 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
2799 // If we defer assigning layers, then the caller is responsible for doing this part.
2800 getInputMonitor().setInputFocusLw(newFocus, updateInputWindows);
2801 }
2802
2803 adjustForImeIfNeeded();
2804
2805 // We may need to schedule some toast windows to be removed. The toasts for an app that
2806 // does not have input focus are removed within a timeout to prevent apps to redress
2807 // other apps' UI.
2808 scheduleToastWindowsTimeoutIfNeededLocked(oldFocus, newFocus);
2809
2810 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2811 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
2812 }
2813 return true;
2814 }
2815
2816 /**
2817 * Set the new focused app to this display.
2818 *
2819 * @param newFocus the new focused AppWindowToken.
2820 * @return true if the focused app is changed.
2821 */
2822 boolean setFocusedApp(AppWindowToken newFocus) {
2823 if (newFocus != null) {
2824 final DisplayContent appDisplay = newFocus.getDisplayContent();
2825 if (appDisplay != this) {
2826 throw new IllegalStateException(newFocus + " is not on " + getName()
2827 + " but " + ((appDisplay != null) ? appDisplay.getName() : "none"));
2828 }
2829 }
2830 if (mFocusedApp == newFocus) {
2831 return false;
2832 }
2833 mFocusedApp = newFocus;
2834 getInputMonitor().setFocusedAppLw(newFocus);
2835 updateTouchExcludeRegion();
2836 return true;
2837 }
2838
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002839 /** Updates the layer assignment of windows on this display. */
2840 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002841 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07002842 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002843 if (setLayoutNeeded) {
2844 setLayoutNeeded();
2845 }
Robert Carrb1579c82017-09-05 14:54:47 -07002846
Robert Carrf59b8dd2017-10-02 18:58:36 -07002847 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002848 // the application of this transaction until the animation pass triggers
2849 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2850 // the hiding and showing of surfaces.
2851 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002852 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002853 }
2854
Wale Ogunwale1666e312016-12-16 11:27:18 -08002855 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2856 // moving containers or resizing them. Need to investigate the best way to have it automatically
2857 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002858 void layoutAndAssignWindowLayersIfNeeded() {
2859 mService.mWindowsChanged = true;
2860 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002861
2862 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2863 false /*updateInputWindows*/)) {
2864 assignWindowLayers(false /* setLayoutNeeded */);
2865 }
2866
Arthur Hung95b38a92018-07-20 18:56:12 +08002867 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002868 mService.mWindowPlacerLocked.performSurfacePlacement();
Arthur Hung95b38a92018-07-20 18:56:12 +08002869 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002870 }
2871
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002872 /** Returns true if a leaked surface was destroyed */
2873 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002874 // Used to indicate that a surface was leaked.
2875 mTmpWindow = null;
2876 forAllWindows(w -> {
2877 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002878 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002879 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002880 }
2881 if (!mService.mSessions.contains(wsa.mSession)) {
2882 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002883 + w + " surface=" + wsa.mSurfaceController
2884 + " token=" + w.mToken
2885 + " pid=" + w.mSession.mPid
2886 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002887 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002888 mService.mForceRemoves.add(w);
2889 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002890 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002891 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002892 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002893 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002894 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002895 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002896 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002897 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002898 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002899
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002900 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002901 }
2902
2903 /**
lumark90120a82018-08-15 00:33:03 +08002904 * Set input method window for the display.
2905 * @param win Set when window added or Null when destroyed.
2906 */
2907 void setInputMethodWindowLocked(WindowState win) {
2908 mInputMethodWindow = win;
lumark70865a82018-09-17 16:56:26 +08002909 // Update display configuration for IME process.
2910 if (mInputMethodWindow != null) {
2911 final int imePid = mInputMethodWindow.mSession.mPid;
2912 mService.mAtmInternal.onImeWindowSetOnDisplay(imePid,
2913 mInputMethodWindow.getDisplayId());
2914 }
lumark90120a82018-08-15 00:33:03 +08002915 computeImeTarget(true /* updateImeTarget */);
2916 }
2917
2918 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002919 * Determine and return the window that should be the IME target.
2920 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2921 * @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 +00002922 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002923 WindowState computeImeTarget(boolean updateImeTarget) {
lumark90120a82018-08-15 00:33:03 +08002924 if (mInputMethodWindow == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002925 // There isn't an IME so there shouldn't be a target...That was easy!
2926 if (updateImeTarget) {
2927 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2928 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002929 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002930 }
2931 return null;
2932 }
2933
Chavi Weingarten3a748552018-05-14 17:32:42 +00002934 final WindowState curTarget = mService.mInputMethodTarget;
2935 if (!canUpdateImeTarget()) {
2936 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Defer updating IME target");
2937 return curTarget;
2938 }
2939
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002940 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2941 // same display. Or even when the current IME/target are not on the same screen as the next
2942 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002943 mUpdateImeTarget = updateImeTarget;
2944 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002945
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002946
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002947 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2948 // to be on top of it, but it is not -really- where input will go. So look down below
2949 // for a real window to target...
2950 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2951 final AppWindowToken token = target.mAppToken;
2952 if (token != null) {
2953 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2954 if (betterTarget != null) {
2955 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002956 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002957 }
2958 }
2959
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002960 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2961 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002962
chaviw1c13ad32018-06-12 16:44:23 -07002963 // Now, a special case -- if the last target's window is in the process of exiting, but
2964 // not removed, and the new target is home, keep on the last target to avoid flicker.
2965 // Home is a special case since its above other stacks in the ordering list, but layed
2966 // out below the others.
2967 if (curTarget != null && !curTarget.mRemoved && curTarget.isDisplayedLw()
2968 && curTarget.isClosing() && (target == null || target.isActivityTypeHome())) {
chaviw4ad54912018-05-30 11:05:44 -07002969 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "New target is home while current target is "
Chavi Weingarten3a748552018-05-14 17:32:42 +00002970 + "closing, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002971 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002972 }
2973
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002974 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2975 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002976
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002977 if (target == null) {
2978 if (updateImeTarget) {
2979 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2980 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2981 + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002982 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002983 }
2984
2985 return null;
2986 }
2987
2988 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002989 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2990 if (token != null) {
2991
2992 // Now some fun for dealing with window animations that modify the Z order. We need
2993 // to look at all windows below the current target that are in this app, finding the
2994 // highest visible one in layering.
2995 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002996 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002997 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002998 }
2999
3000 if (highestTarget != null) {
3001 final AppTransition appTransition = mService.mAppTransition;
3002 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
Jorim Jaggi35d328a2018-08-14 17:00:20 +02003003 + " animating=" + highestTarget.isAnimating());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003004
3005 if (appTransition.isTransitionSet()) {
3006 // If we are currently setting up for an animation, hold everything until we
3007 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003008 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003009 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003010 }
3011 }
3012 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003013
3014 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
3015 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003016 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003017 }
3018
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003019 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003020 }
3021
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003022 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003023 if (target == mService.mInputMethodTarget
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003024 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003025 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003026 }
3027
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003028 mService.mInputMethodTarget = target;
3029 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003030 assignWindowLayers(false /* setLayoutNeeded */);
3031 }
3032
3033 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
3034 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3035 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
3036 }
3037
3038 // Used to indicate we have reached the first window in the range we are interested in.
3039 mTmpWindow = null;
3040
3041 // TODO: Figure-out a more efficient way to do this.
3042 final WindowState candidate = getWindow(w -> {
3043 if (w == top) {
3044 // Reached the first window in the range we are interested in.
3045 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003046 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003047 if (mTmpWindow == null) {
3048 return false;
3049 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003050
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003051 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3052 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003053 }
3054 // If we reached the bottom of the range of windows we are considering,
3055 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003056 if (w == bottom) {
3057 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003058 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003059 return false;
3060 });
3061
3062 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003063 }
3064
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003065 void setLayoutNeeded() {
3066 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
3067 mLayoutNeeded = true;
3068 }
3069
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003070 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003071 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
3072 mLayoutNeeded = false;
3073 }
3074
3075 boolean isLayoutNeeded() {
3076 return mLayoutNeeded;
3077 }
3078
Wale Ogunwale02319a62016-09-26 15:21:22 -07003079 void dumpTokens(PrintWriter pw, boolean dumpAll) {
3080 if (mTokenMap.isEmpty()) {
3081 return;
3082 }
3083 pw.println(" Display #" + mDisplayId);
3084 final Iterator<WindowToken> it = mTokenMap.values().iterator();
3085 while (it.hasNext()) {
3086 final WindowToken token = it.next();
3087 pw.print(" ");
3088 pw.print(token);
3089 if (dumpAll) {
3090 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003091 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07003092 } else {
3093 pw.println();
3094 }
3095 }
3096 }
3097
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003098 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003099 final int[] index = new int[1];
3100 forAllWindows(w -> {
3101 final WindowStateAnimator wAnim = w.mWinAnimator;
3102 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
3103 index[0] = index[0] + 1;
3104 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003105 }
3106
Jorim Jaggife762342016-10-13 14:33:27 +02003107 /**
3108 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
3109 */
3110 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
3111 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003112 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02003113 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
3114 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01003115 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02003116 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003117 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02003118 }
3119
Wale Ogunwale494009b82016-10-21 09:01:38 -07003120 boolean checkWaitingForWindows() {
3121
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003122 mHaveBootMsg = false;
3123 mHaveApp = false;
3124 mHaveWallpaper = false;
3125 mHaveKeyguard = true;
3126
3127 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003128 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
3129 return true;
3130 }
3131 if (w.isDrawnLw()) {
3132 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003133 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003134 } else if (w.mAttrs.type == TYPE_APPLICATION
3135 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003136 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003137 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003138 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003139 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003140 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003141 }
3142 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003143 return false;
3144 });
3145
3146 if (visibleWindow != null) {
3147 // We have a visible window.
3148 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003149 }
3150
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003151 // if the wallpaper service is disabled on the device, we're never going to have
3152 // wallpaper, don't bother waiting for it
3153 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
3154 com.android.internal.R.bool.config_enableWallpaperService)
Daichi Hironoc1432222018-03-29 13:06:07 +09003155 && mService.mContext.getResources().getBoolean(
3156 com.android.internal.R.bool.config_checkWallpaperAtBoot)
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003157 && !mService.mOnlyCore;
3158
Wale Ogunwale494009b82016-10-21 09:01:38 -07003159 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
3160 "******** booted=" + mService.mSystemBooted
3161 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003162 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
3163 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
3164 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003165
3166 // If we are turning on the screen to show the boot message, don't do it until the boot
3167 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003168 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003169 return true;
3170 }
3171
3172 // If we are turning on the screen after the boot is completed normally, don't do so until
3173 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003174 if (mService.mSystemBooted
3175 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003176 return true;
3177 }
3178
3179 return false;
3180 }
3181
Jorim Jaggi8f520872018-08-14 17:00:20 +02003182 void updateWindowsForAnimator() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003183 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003184 }
3185
Jorim Jaggi8f520872018-08-14 17:00:20 +02003186 /**
3187 * Updates the {@link TaskStack#setAnimationBackground} for all windows.
3188 */
3189 void updateBackgroundForAnimator() {
Jorim Jaggi7156b652016-10-25 08:31:58 -07003190 resetAnimationBackgroundAnimator();
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003191 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003192 }
3193
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003194 boolean isInputMethodClientFocus(int uid, int pid) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003195 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003196 if (imFocus == null) {
3197 return false;
3198 }
3199
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003200 if (DEBUG_INPUT_METHOD) {
3201 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003202 Slog.i(TAG_WM, "Current focus: " + mCurrentFocus + " displayId=" + mDisplayId);
3203 Slog.i(TAG_WM, "Last focus: " + mLastFocus + " displayId=" + mDisplayId);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003204 }
3205
Wale Ogunwale494009b82016-10-21 09:01:38 -07003206 if (DEBUG_INPUT_METHOD) {
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003207 Slog.i(TAG_WM, "IM target uid/pid: " + imFocus.mSession.mUid
3208 + "/" + imFocus.mSession.mPid);
3209 Slog.i(TAG_WM, "Requesting client uid/pid: " + uid + "/" + pid);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003210 }
3211
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003212 return imFocus.mSession.mUid == uid && imFocus.mSession.mPid == pid;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003213 }
3214
3215 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003216 final WindowState win = getWindow(
3217 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
3218 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003219 }
3220
3221 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003222 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003223 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003224 final int curValue = w.mSystemUiVisibility;
3225 final int diff = (curValue ^ visibility) & globalDiff;
3226 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003227 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003228 w.mSeq++;
3229 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003230 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003231 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
3232 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07003233 visibility, newValue, diff);
3234 }
3235 } catch (RemoteException e) {
3236 // so sorry
3237 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003238 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003239 }
3240
3241 void onWindowFreezeTimeout() {
3242 Slog.w(TAG_WM, "Window freeze timeout expired.");
3243 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003244
3245 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07003246 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003247 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003248 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07003249 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003250 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
3251 - mService.mDisplayFreezeTime);
3252 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003253 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003254 mService.mWindowPlacerLocked.performSurfacePlacement();
3255 }
3256
3257 void waitForAllWindowsDrawn() {
3258 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003259 forAllWindows(w -> {
3260 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
3261 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
3262 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003263 // Force add to mResizingWindows.
chaviw9c81e632018-07-31 11:17:52 -07003264 w.resetLastContentInsets();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003265 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003266 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003267 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003268 }
3269
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003270 // TODO: Super crazy long method that should be broken down...
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003271 void applySurfaceChangesTransaction(boolean recoveringMemory) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003272
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003273 final int dw = mDisplayInfo.logicalWidth;
3274 final int dh = mDisplayInfo.logicalHeight;
3275 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
3276
3277 mTmpUpdateAllDrawn.clear();
3278
3279 int repeats = 0;
3280 do {
3281 repeats++;
3282 if (repeats > 6) {
3283 Slog.w(TAG, "Animation repeat aborted after too many iterations");
3284 clearLayoutNeeded();
3285 break;
3286 }
3287
3288 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
3289 pendingLayoutChanges);
3290
Andrii Kulian839def92016-11-02 10:58:58 -07003291 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
3292 // the wallpaper window jumping across displays.
3293 // Remove check for default display when there will be support for multiple wallpaper
3294 // targets (on different displays).
3295 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003296 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003297 }
3298
Riddle Hsu654a6f92018-07-13 22:59:36 +08003299 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003300 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
Robert Carrae606b42018-02-15 15:36:23 -08003301 if (mService.updateOrientationFromAppTokensLocked(mDisplayId)) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003302 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07003303 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003304 }
3305 }
3306
3307 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
3308 setLayoutNeeded();
3309 }
3310
3311 // FIRST LOOP: Perform a layout, if needed.
3312 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
3313 performLayout(repeats == 1, false /* updateInputWindows */);
3314 } else {
3315 Slog.w(TAG, "Layout repeat skipped after too many iterations");
3316 }
3317
3318 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
3319 pendingLayoutChanges = 0;
3320
3321 if (isDefaultDisplay) {
3322 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003323 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003324 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
3325 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
3326 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
3327 }
3328 } while (pendingLayoutChanges != 0);
3329
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003330 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003331
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003332 mTmpRecoveringMemory = recoveringMemory;
3333 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01003334 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003335
Jorim Jaggif1292892018-09-10 11:58:13 +02003336 mLastHasContent = mTmpApplySurfaceChangesTransactionState.displayHasContent;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003337 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Jorim Jaggif1292892018-09-10 11:58:13 +02003338 mLastHasContent,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003339 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
3340 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003341 true /* inTraversal, must call performTraversalInTrans... below */);
3342
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003343 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
3344 if (wallpaperVisible != mLastWallpaperVisible) {
3345 mLastWallpaperVisible = wallpaperVisible;
3346 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
3347 }
3348
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003349 while (!mTmpUpdateAllDrawn.isEmpty()) {
3350 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
3351 // See if any windows have been drawn, so they (and others associated with them)
3352 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07003353 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003354 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003355 }
3356
Bryce Leef3c6a472017-11-14 14:53:06 -08003357 private void updateBounds() {
3358 calculateBounds(mTmpBounds);
3359 setBounds(mTmpBounds);
3360 }
3361
3362 // Determines the current display bounds based on the current state
3363 private void calculateBounds(Rect out) {
3364 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
3365 final int orientation = mDisplayInfo.rotation;
3366 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
3367 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
3368 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
3369 int width = mDisplayInfo.logicalWidth;
3370 int left = (physWidth - width) / 2;
3371 int height = mDisplayInfo.logicalHeight;
3372 int top = (physHeight - height) / 2;
3373 out.set(left, top, left + width, top + height);
3374 }
3375
3376 @Override
3377 public void getBounds(Rect out) {
3378 calculateBounds(out);
3379 }
3380
3381 private void getBounds(Rect out, int orientation) {
3382 getBounds(out);
3383
3384 // Rotate the Rect if needed.
3385 final int currentRotation = mDisplayInfo.rotation;
3386 final int rotationDelta = deltaRotation(currentRotation, orientation);
3387 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
3388 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
3389 mTmpRectF.set(out);
3390 mTmpMatrix.mapRect(mTmpRectF);
3391 mTmpRectF.round(out);
3392 }
3393 }
3394
Louis Chang7501e332018-08-20 13:08:39 +08003395 int getSurfaceSize() {
3396 return mSurfaceSize;
3397 }
3398
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003399 void performLayout(boolean initial, boolean updateInputWindows) {
3400 if (!isLayoutNeeded()) {
3401 return;
3402 }
3403 clearLayoutNeeded();
3404
3405 final int dw = mDisplayInfo.logicalWidth;
3406 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003407 if (DEBUG_LAYOUT) {
3408 Slog.v(TAG, "-------------------------------------");
3409 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
3410 }
3411
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01003412 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
3413 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00003414 // TODO: Not sure if we really need to set the rotation here since we are updating from the
3415 // display info above...
3416 mDisplayFrames.mRotation = mRotation;
3417 mService.mPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003418 if (isDefaultDisplay) {
3419 // Not needed on non-default displays.
3420 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
3421 mService.mScreenRect.set(0, 0, dw, dh);
3422 }
3423
Adrian Roos5251b1d2018-03-23 18:57:43 +01003424 int seq = mLayoutSeq + 1;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003425 if (seq < 0) seq = 0;
Adrian Roos5251b1d2018-03-23 18:57:43 +01003426 mLayoutSeq = seq;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003427
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003428 // Used to indicate that we have processed the dream window and all additional windows are
3429 // behind it.
3430 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003431 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003432
3433 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003434 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003435
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003436 // Used to indicate that we have processed the dream window and all additional attached
3437 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003438 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003439 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003440
3441 // Now perform layout of attached windows, which usually depend on the position of the
3442 // window they are attached to. XXX does not deal with windows that are attached to windows
3443 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003444 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003445
3446 // Window frames may have changed. Tell the input dispatcher about it.
Arthur Hung95b38a92018-07-20 18:56:12 +08003447 mInputMonitor.layoutInputConsumers(dw, dh);
3448 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003449 if (updateInputWindows) {
Arthur Hung95b38a92018-07-20 18:56:12 +08003450 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003451 }
3452
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003453 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
3454 }
3455
3456 /**
3457 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
3458 * In portrait mode, it grabs the full screenshot.
3459 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003460 * @param config of the output bitmap
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003461 */
chaviw0315a1a2018-03-05 15:28:35 -08003462 Bitmap screenshotDisplayLocked(Bitmap.Config config) {
3463 if (!mService.mPolicy.isScreenOn()) {
3464 if (DEBUG_SCREENSHOT) {
3465 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003466 }
chaviw0315a1a2018-03-05 15:28:35 -08003467 return null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003468 }
chaviwfbe47df2017-11-10 16:14:49 -08003469
chaviw0315a1a2018-03-05 15:28:35 -08003470 int dw = mDisplayInfo.logicalWidth;
3471 int dh = mDisplayInfo.logicalHeight;
chaviwfbe47df2017-11-10 16:14:49 -08003472
chaviw0315a1a2018-03-05 15:28:35 -08003473 if (dw <= 0 || dh <= 0) {
3474 return null;
3475 }
chaviwfbe47df2017-11-10 16:14:49 -08003476
chaviw0315a1a2018-03-05 15:28:35 -08003477 final Rect frame = new Rect(0, 0, dw, dh);
chaviwfbe47df2017-11-10 16:14:49 -08003478
chaviw0315a1a2018-03-05 15:28:35 -08003479 // The screenshot API does not apply the current screen rotation.
3480 int rot = mDisplay.getRotation();
chaviwfbe47df2017-11-10 16:14:49 -08003481
chaviw0315a1a2018-03-05 15:28:35 -08003482 if (rot == ROTATION_90 || rot == ROTATION_270) {
3483 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3484 }
chaviwfbe47df2017-11-10 16:14:49 -08003485
chaviw0315a1a2018-03-05 15:28:35 -08003486 // SurfaceFlinger is not aware of orientation, so convert our logical
3487 // crop to SurfaceFlinger's portrait orientation.
3488 convertCropForSurfaceFlinger(frame, rot, dw, dh);
3489
3490 final ScreenRotationAnimation screenRotationAnimation =
3491 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3492 final boolean inRotation = screenRotationAnimation != null &&
3493 screenRotationAnimation.isAnimating();
3494 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
3495
3496 // TODO(b/68392460): We should screenshot Task controls directly
3497 // but it's difficult at the moment as the Task doesn't have the
3498 // correct size set.
chaviw08520a02018-09-10 16:44:56 -07003499 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, inRotation, rot);
chaviw0315a1a2018-03-05 15:28:35 -08003500 if (bitmap == null) {
3501 Slog.w(TAG_WM, "Failed to take screenshot");
3502 return null;
3503 }
3504
3505 // Create a copy of the screenshot that is immutable and backed in ashmem.
3506 // This greatly reduces the overhead of passing the bitmap between processes.
3507 final Bitmap ret = bitmap.createAshmemBitmap(config);
3508 bitmap.recycle();
3509 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003510 }
3511
3512 // TODO: Can this use createRotationMatrix()?
3513 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3514 if (rot == Surface.ROTATION_90) {
3515 final int tmp = crop.top;
3516 crop.top = dw - crop.right;
3517 crop.right = crop.bottom;
3518 crop.bottom = dw - crop.left;
3519 crop.left = tmp;
3520 } else if (rot == Surface.ROTATION_180) {
3521 int tmp = crop.top;
3522 crop.top = dh - crop.bottom;
3523 crop.bottom = dh - tmp;
3524 tmp = crop.right;
3525 crop.right = dw - crop.left;
3526 crop.left = dw - tmp;
3527 } else if (rot == Surface.ROTATION_270) {
3528 final int tmp = crop.top;
3529 crop.top = crop.left;
3530 crop.left = dh - crop.bottom;
3531 crop.bottom = crop.right;
3532 crop.right = dh - tmp;
3533 }
3534 }
3535
3536 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003537 // Used to indicate the layout is needed.
3538 mTmpWindow = null;
3539
3540 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003541 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003542 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003543 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003544 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003545 w.setDisplayLayoutNeeded();
3546 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003547 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003548
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003549 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003550 mService.mWindowPlacerLocked.performSurfacePlacement();
3551 }
3552 }
3553
Wale Ogunwale1666e312016-12-16 11:27:18 -08003554 void setExitingTokensHasVisible(boolean hasVisible) {
3555 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3556 mExitingTokens.get(i).hasVisible = hasVisible;
3557 }
3558
3559 // Initialize state of exiting applications.
3560 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3561 }
3562
3563 void removeExistingTokensIfPossible() {
3564 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3565 final WindowToken token = mExitingTokens.get(i);
3566 if (!token.hasVisible) {
3567 mExitingTokens.remove(i);
3568 }
3569 }
3570
3571 // Time to remove any exiting applications?
3572 mTaskStackContainers.removeExistingAppTokensIfPossible();
3573 }
3574
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003575 @Override
3576 void onDescendantOverrideConfigurationChanged() {
3577 setLayoutNeeded();
3578 mService.requestTraversal();
3579 }
3580
David Stevens9440dc82017-03-16 19:00:20 -07003581 boolean okToDisplay() {
3582 if (mDisplayId == DEFAULT_DISPLAY) {
3583 return !mService.mDisplayFrozen
3584 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3585 }
3586 return mDisplayInfo.state == Display.STATE_ON;
3587 }
3588
3589 boolean okToAnimate() {
3590 return okToDisplay() &&
3591 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3592 }
3593
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003594 static final class TaskForResizePointSearchResult {
3595 boolean searchDone;
3596 Task taskForResize;
3597
3598 void reset() {
3599 searchDone = false;
3600 taskForResize = null;
3601 }
3602 }
Robert Carr3b716242016-08-16 16:02:21 -07003603
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003604 private static final class ApplySurfaceChangesTransactionState {
3605 boolean displayHasContent;
3606 boolean obscured;
3607 boolean syswin;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003608 float preferredRefreshRate;
3609 int preferredModeId;
3610
3611 void reset() {
3612 displayHasContent = false;
3613 obscured = false;
3614 syswin = false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003615 preferredRefreshRate = 0;
3616 preferredModeId = 0;
3617 }
3618 }
3619
3620 private static final class ScreenshotApplicationState {
3621 WindowState appWin;
3622 int maxLayer;
3623 int minLayer;
3624 boolean screenshotReady;
3625
3626 void reset(boolean screenshotReady) {
3627 appWin = null;
3628 maxLayer = 0;
3629 minLayer = 0;
3630 this.screenshotReady = screenshotReady;
3631 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3632 }
3633 }
3634
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003635 /**
3636 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3637 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3638 * homogeneous children type which is currently required by sub-classes of
3639 * {@link WindowContainer} class.
3640 */
3641 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3642
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003643 DisplayChildWindowContainer(WindowManagerService service) {
3644 super(service);
3645 }
3646
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003647 @Override
3648 boolean fillsParent() {
3649 return true;
3650 }
3651
3652 @Override
3653 boolean isVisible() {
3654 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003655 }
3656 }
3657
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003658 /**
3659 * Window container class that contains all containers on this display relating to Apps.
3660 * I.e Activities.
3661 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003662 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003663 /**
3664 * A control placed at the appropriate level for transitions to occur.
3665 */
chaviw23ee71c2017-12-18 11:29:41 -08003666 SurfaceControl mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02003667 SurfaceControl mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02003668 SurfaceControl mHomeAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003669
Robert Carrf7a7ca82017-12-06 13:17:07 -08003670 /**
3671 * Given that the split-screen divider does not have an AppWindowToken, it
3672 * will have to live inside of a "NonAppWindowContainer", in particular
3673 * {@link DisplayContent#mAboveAppWindowsContainers}. However, in visual Z order
3674 * it will need to be interleaved with some of our children, appearing on top of
3675 * both docked stacks but underneath any assistant stacks.
3676 *
3677 * To solve this problem we have this anchor control, which will always exist so
3678 * we can always assign it the correct value in our {@link #assignChildLayers}.
3679 * Likewise since it always exists, {@link AboveAppWindowContainers} can always
3680 * assign the divider a layer relative to it. This way we prevent linking lifecycle
3681 * events between the two containers.
3682 */
3683 SurfaceControl mSplitScreenDividerAnchor = null;
3684
Wale Ogunwale61911492017-10-11 08:50:50 -07003685 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3686 // through the list to find them.
3687 private TaskStack mHomeStack = null;
3688 private TaskStack mPinnedStack = null;
3689 private TaskStack mSplitScreenPrimaryStack = null;
3690
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003691 TaskStackContainers(WindowManagerService service) {
3692 super(service);
3693 }
3694
Wale Ogunwale61911492017-10-11 08:50:50 -07003695 /**
3696 * Returns the topmost stack on the display that is compatible with the input windowing mode
3697 * and activity type. Null is no compatible stack on the display.
3698 */
3699 TaskStack getStack(int windowingMode, int activityType) {
3700 if (activityType == ACTIVITY_TYPE_HOME) {
3701 return mHomeStack;
3702 }
3703 if (windowingMode == WINDOWING_MODE_PINNED) {
3704 return mPinnedStack;
3705 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3706 return mSplitScreenPrimaryStack;
3707 }
3708 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3709 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003710 if (activityType == ACTIVITY_TYPE_UNDEFINED
3711 && windowingMode == stack.getWindowingMode()) {
3712 // Passing in undefined type means we want to match the topmost stack with the
3713 // windowing mode.
3714 return stack;
3715 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003716 if (stack.isCompatible(windowingMode, activityType)) {
3717 return stack;
3718 }
3719 }
3720 return null;
3721 }
3722
3723 @VisibleForTesting
3724 TaskStack getTopStack() {
3725 return mTaskStackContainers.getChildCount() > 0
3726 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3727 }
3728
3729 TaskStack getHomeStack() {
3730 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3731 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3732 }
3733 return mHomeStack;
3734 }
3735
3736 TaskStack getPinnedStack() {
3737 return mPinnedStack;
3738 }
3739
Matthew Ng64e77cf2017-10-31 14:01:31 -07003740 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003741 return mSplitScreenPrimaryStack;
3742 }
3743
Winson Chunge2d72172018-01-25 17:46:20 +00003744 ArrayList<Task> getVisibleTasks() {
3745 final ArrayList<Task> visibleTasks = new ArrayList<>();
3746 forAllTasks(task -> {
3747 if (task.isVisible()) {
3748 visibleTasks.add(task);
3749 }
3750 });
3751 return visibleTasks;
3752 }
3753
Andrii Kulian839def92016-11-02 10:58:58 -07003754 /**
3755 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003756 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003757 */
3758 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003759 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003760 addChild(stack, onTop);
3761 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003762 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003763
Wale Ogunwale61911492017-10-11 08:50:50 -07003764 void onStackWindowingModeChanged(TaskStack stack) {
3765 removeStackReferenceIfNeeded(stack);
3766 addStackReferenceIfNeeded(stack);
3767 if (stack == mPinnedStack && getTopStack() != stack) {
3768 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3769 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3770 }
3771 }
3772
3773 private void addStackReferenceIfNeeded(TaskStack stack) {
3774 if (stack.isActivityTypeHome()) {
3775 if (mHomeStack != null) {
Louis Chang7d0037c2018-08-13 12:42:06 +08003776 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
Wale Ogunwale61911492017-10-11 08:50:50 -07003777 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
Louis Chang7d0037c2018-08-13 12:42:06 +08003778
Wale Ogunwale61911492017-10-11 08:50:50 -07003779 }
Louis Chang7d0037c2018-08-13 12:42:06 +08003780 mHomeStack = stack;
Wale Ogunwale61911492017-10-11 08:50:50 -07003781 }
3782 final int windowingMode = stack.getWindowingMode();
3783 if (windowingMode == WINDOWING_MODE_PINNED) {
3784 if (mPinnedStack != null) {
3785 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3786 + mPinnedStack + " already exist on display=" + this
3787 + " stack=" + stack);
3788 }
3789 mPinnedStack = stack;
3790 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3791 if (mSplitScreenPrimaryStack != null) {
3792 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3793 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3794 + " already exist on display=" + this + " stack=" + stack);
3795 }
3796 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003797 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003798 }
3799 }
3800
3801 private void removeStackReferenceIfNeeded(TaskStack stack) {
3802 if (stack == mHomeStack) {
3803 mHomeStack = null;
3804 } else if (stack == mPinnedStack) {
3805 mPinnedStack = null;
3806 } else if (stack == mSplitScreenPrimaryStack) {
3807 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003808 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3809 mService.setDockedStackCreateStateLocked(
3810 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3811 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003812 }
Andrii Kulian839def92016-11-02 10:58:58 -07003813 }
3814
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003815 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003816 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3817 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003818 addChild(stack, addIndex);
3819 setLayoutNeeded();
3820 }
3821
Wale Ogunwale61911492017-10-11 08:50:50 -07003822 @Override
3823 protected void removeChild(TaskStack stack) {
3824 super.removeChild(stack);
3825 removeStackReferenceIfNeeded(stack);
3826 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003827
3828 @Override
3829 boolean isOnTop() {
3830 // Considered always on top
3831 return true;
3832 }
3833
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003834 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003835 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003836 if (child.getWindowConfiguration().isAlwaysOnTop()
3837 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003838 // This stack is always-on-top, override the default behavior.
3839 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3840
3841 // Moving to its current position, as we must call super but we don't want to
3842 // perform any meaningful action.
3843 final int currentPosition = mChildren.indexOf(child);
3844 super.positionChildAt(currentPosition, child, false /* includingParents */);
3845 return;
3846 }
3847
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003848 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3849 super.positionChildAt(targetPosition, child, includingParents);
3850
Riddle Hsu2f6e1742018-08-23 22:44:36 +08003851 if (includingParents) {
3852 // We still want to move the display of this stack container to top because even the
3853 // target position is adjusted to non-top, the intention of the condition is to have
3854 // higher z-order to gain focus (e.g. moving a task of a fullscreen stack to front
3855 // in a non-top display which is using picture-in-picture mode).
3856 final int topChildPosition = getChildCount() - 1;
3857 if (targetPosition < topChildPosition && position >= topChildPosition) {
3858 getParent().positionChildAt(POSITION_TOP, this /* child */,
3859 true /* includingParents */);
3860 }
3861 }
3862
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003863 setLayoutNeeded();
3864 }
3865
3866 /**
3867 * When stack is added or repositioned, find a proper position for it.
3868 * This will make sure that pinned stack always stays on top.
3869 * @param requestedPosition Position requested by caller.
3870 * @param stack Stack to be added or positioned.
3871 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3872 * @return The proper position for the stack.
3873 */
3874 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09003875 if (stack.inPinnedWindowingMode()) {
Kazuki Takisef85197b2018-06-18 18:18:36 +09003876 return POSITION_TOP;
Kazuki Takise148d00a2018-05-31 15:32:19 +09003877 }
3878
Kazuki Takisef85197b2018-06-18 18:18:36 +09003879 final int topChildPosition = mChildren.size() - 1;
3880 int belowAlwaysOnTopPosition = POSITION_BOTTOM;
3881 for (int i = topChildPosition; i >= 0; --i) {
3882 if (getStacks().get(i) != stack && !getStacks().get(i).isAlwaysOnTop()) {
3883 belowAlwaysOnTopPosition = i;
3884 break;
3885 }
3886 }
3887
3888 // The max possible position we can insert the stack at.
3889 int maxPosition = POSITION_TOP;
3890 // The min possible position we can insert the stack at.
3891 int minPosition = POSITION_BOTTOM;
3892
3893 if (stack.isAlwaysOnTop()) {
3894 if (hasPinnedStack()) {
3895 // Always-on-top stacks go below the pinned stack.
3896 maxPosition = getStacks().indexOf(mPinnedStack) - 1;
3897 }
3898 // Always-on-top stacks need to be above all other stacks.
3899 minPosition = belowAlwaysOnTopPosition !=
3900 POSITION_BOTTOM ? belowAlwaysOnTopPosition : topChildPosition;
3901 } else {
3902 // Other stacks need to be below the always-on-top stacks.
3903 maxPosition = belowAlwaysOnTopPosition !=
Arthur Hung928d8dc2018-07-18 15:38:05 +08003904 POSITION_BOTTOM ? belowAlwaysOnTopPosition : 0;
Kazuki Takisef85197b2018-06-18 18:18:36 +09003905 }
3906
3907 int targetPosition = requestedPosition;
3908 targetPosition = Math.min(targetPosition, maxPosition);
3909 targetPosition = Math.max(targetPosition, minPosition);
3910
3911 int prevPosition = getStacks().indexOf(stack);
3912 // The positions we calculated above (maxPosition, minPosition) do not take into
3913 // consideration the following edge cases.
3914 // 1) We need to adjust the position depending on the value "adding".
3915 // 2) When we are moving a stack to another position, we also need to adjust the
3916 // position depending on whether the stack is moving to a higher or lower position.
3917 if ((targetPosition != requestedPosition) &&
3918 (adding || targetPosition < prevPosition)) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09003919 targetPosition++;
3920 }
3921
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003922 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003923 }
3924
3925 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003926 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3927 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003928 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003929 if (super.forAllWindows(callback, traverseTopToBottom)) {
3930 return true;
3931 }
3932 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3933 return true;
3934 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003935 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003936 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3937 return true;
3938 }
3939 if (super.forAllWindows(callback, traverseTopToBottom)) {
3940 return true;
3941 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003942 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003943 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003944 }
3945
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003946 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003947 boolean traverseTopToBottom) {
3948 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3949 // app tokens.
3950 // TODO: Investigate if we need to continue to do this or if we can just process them
3951 // in-order.
3952 if (traverseTopToBottom) {
3953 for (int i = mChildren.size() - 1; i >= 0; --i) {
3954 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3955 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003956 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3957 traverseTopToBottom)) {
3958 return true;
3959 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003960 }
3961 }
3962 } else {
3963 final int count = mChildren.size();
3964 for (int i = 0; i < count; ++i) {
3965 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3966 final int appTokensCount = appTokens.size();
3967 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003968 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3969 traverseTopToBottom)) {
3970 return true;
3971 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003972 }
3973 }
3974 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003975 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003976 }
3977
Wale Ogunwale1666e312016-12-16 11:27:18 -08003978 void setExitingTokensHasVisible(boolean hasVisible) {
3979 for (int i = mChildren.size() - 1; i >= 0; --i) {
3980 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3981 for (int j = appTokens.size() - 1; j >= 0; --j) {
3982 appTokens.get(j).hasVisible = hasVisible;
3983 }
3984 }
3985 }
3986
3987 void removeExistingAppTokensIfPossible() {
3988 for (int i = mChildren.size() - 1; i >= 0; --i) {
3989 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3990 for (int j = appTokens.size() - 1; j >= 0; --j) {
3991 final AppWindowToken token = appTokens.get(j);
3992 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3993 && (!token.mIsExiting || token.isEmpty())) {
3994 // Make sure there is no animation running on this token, so any windows
3995 // associated with it will be removed as soon as their animations are
3996 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003997 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003998 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3999 "performLayout: App token exiting now removed" + token);
4000 token.removeIfPossible();
4001 }
4002 }
4003 }
4004 }
4005
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004006 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004007 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004008 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
4009 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004010 // Apps and their containers are not allowed to specify an orientation while the
Wale Ogunwaled0c225c2018-06-15 14:47:46 -07004011 // docked or freeform stack is visible...except for the home stack if the docked
4012 // stack is minimized and it actually set something and the bounds is different from
4013 // the display.
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004014 if (mHomeStack != null && mHomeStack.isVisible()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004015 && mDividerControllerLocked.isMinimizedDock()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004016 && !(mDividerControllerLocked.isHomeStackResizable()
4017 && mHomeStack.matchParentBounds())) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004018 final int orientation = mHomeStack.getOrientation();
4019 if (orientation != SCREEN_ORIENTATION_UNSET) {
4020 return orientation;
4021 }
4022 }
4023 return SCREEN_ORIENTATION_UNSPECIFIED;
4024 }
4025
4026 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07004027 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
4028 PackageManager.FEATURE_AUTOMOTIVE);
4029 if (isCar) {
4030 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
4031 // allow anything but the default orientation.
4032 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004033 "Forcing UNSPECIFIED orientation in car for display id=" + mDisplayId
4034 + ". Ignoring " + orientation);
Dean Harding3e5a1522017-10-06 09:19:01 -07004035 return SCREEN_ORIENTATION_UNSPECIFIED;
4036 }
4037
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004038 if (orientation != SCREEN_ORIENTATION_UNSET
4039 && orientation != SCREEN_ORIENTATION_BEHIND) {
4040 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004041 "App is requesting an orientation, return " + orientation
4042 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004043 return orientation;
4044 }
4045
4046 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004047 "No app is requesting an orientation, return " + mLastOrientation
4048 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004049 // The next app has not been requested to be visible, so we keep the current orientation
4050 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08004051 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004052 }
Robert Carrb1579c82017-09-05 14:54:47 -07004053
4054 @Override
4055 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004056 assignStackOrdering(t);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004057
Robert Carr2f8aa392018-01-31 14:46:51 -08004058 for (int i = 0; i < mChildren.size(); i++) {
4059 final TaskStack s = mChildren.get(i);
4060 s.assignChildLayers(t);
4061 }
4062 }
4063
4064 void assignStackOrdering(SurfaceControl.Transaction t) {
Jorim Jaggibe418292018-03-26 16:14:12 +02004065
Robert Carrf7a7ca82017-12-06 13:17:07 -08004066 final int HOME_STACK_STATE = 0;
4067 final int NORMAL_STACK_STATE = 1;
4068 final int ALWAYS_ON_TOP_STATE = 2;
4069
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004070 int layer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08004071 int layerForAnimationLayer = 0;
Jorim Jaggibe418292018-03-26 16:14:12 +02004072 int layerForBoostedAnimationLayer = 0;
Jorim Jaggi391790622018-04-18 15:30:44 +02004073 int layerForHomeAnimationLayer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08004074
Robert Carrf7a7ca82017-12-06 13:17:07 -08004075 for (int state = 0; state <= ALWAYS_ON_TOP_STATE; state++) {
4076 for (int i = 0; i < mChildren.size(); i++) {
4077 final TaskStack s = mChildren.get(i);
Robert Carr2f8aa392018-01-31 14:46:51 -08004078 if (state == HOME_STACK_STATE && !s.isActivityTypeHome()) {
4079 continue;
4080 } else if (state == NORMAL_STACK_STATE && (s.isActivityTypeHome()
4081 || s.isAlwaysOnTop())) {
4082 continue;
4083 } else if (state == ALWAYS_ON_TOP_STATE && !s.isAlwaysOnTop()) {
4084 continue;
4085 }
4086 s.assignLayer(t, layer++);
4087 if (s.inSplitScreenWindowingMode() && mSplitScreenDividerAnchor != null) {
4088 t.setLayer(mSplitScreenDividerAnchor, layer++);
4089 }
Winson Chungd41f71d2018-03-16 15:26:07 -07004090 if ((s.isTaskAnimating() || s.isAppAnimating())
4091 && state != ALWAYS_ON_TOP_STATE) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004092 // Ensure the animation layer ends up above the
4093 // highest animating stack and no higher.
4094 layerForAnimationLayer = layer++;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004095 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004096 if (state != ALWAYS_ON_TOP_STATE) {
4097 layerForBoostedAnimationLayer = layer++;
4098 }
Robert Carrf7a7ca82017-12-06 13:17:07 -08004099 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004100 if (state == HOME_STACK_STATE) {
4101 layerForHomeAnimationLayer = layer++;
4102 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004103 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004104 if (mAppAnimationLayer != null) {
4105 t.setLayer(mAppAnimationLayer, layerForAnimationLayer);
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004106 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004107 if (mBoostedAppAnimationLayer != null) {
4108 t.setLayer(mBoostedAppAnimationLayer, layerForBoostedAnimationLayer);
4109 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004110 if (mHomeAppAnimationLayer != null) {
4111 t.setLayer(mHomeAppAnimationLayer, layerForHomeAnimationLayer);
4112 }
Robert Carrb1579c82017-09-05 14:54:47 -07004113 }
4114
4115 @Override
Jorim Jaggi391790622018-04-18 15:30:44 +02004116 SurfaceControl getAppAnimationLayer(@AnimationLayer int animationLayer) {
4117 switch (animationLayer) {
4118 case ANIMATION_LAYER_BOOSTED:
4119 return mBoostedAppAnimationLayer;
4120 case ANIMATION_LAYER_HOME:
4121 return mHomeAppAnimationLayer;
4122 case ANIMATION_LAYER_STANDARD:
4123 default:
4124 return mAppAnimationLayer;
4125 }
chaviw23ee71c2017-12-18 11:29:41 -08004126 }
4127
Robert Carrf7a7ca82017-12-06 13:17:07 -08004128 SurfaceControl getSplitScreenDividerAnchor() {
4129 return mSplitScreenDividerAnchor;
4130 }
4131
chaviw23ee71c2017-12-18 11:29:41 -08004132 @Override
Robert Carrb1579c82017-09-05 14:54:47 -07004133 void onParentSet() {
4134 super.onParentSet();
4135 if (getParent() != null) {
chaviw23ee71c2017-12-18 11:29:41 -08004136 mAppAnimationLayer = makeChildSurface(null)
4137 .setName("animationLayer")
4138 .build();
Jorim Jaggibe418292018-03-26 16:14:12 +02004139 mBoostedAppAnimationLayer = makeChildSurface(null)
4140 .setName("boostedAnimationLayer")
4141 .build();
Jorim Jaggi391790622018-04-18 15:30:44 +02004142 mHomeAppAnimationLayer = makeChildSurface(null)
4143 .setName("homeAnimationLayer")
4144 .build();
Robert Carrf7a7ca82017-12-06 13:17:07 -08004145 mSplitScreenDividerAnchor = makeChildSurface(null)
4146 .setName("splitScreenDividerAnchor")
4147 .build();
4148 getPendingTransaction()
4149 .show(mAppAnimationLayer)
Jorim Jaggibe418292018-03-26 16:14:12 +02004150 .show(mBoostedAppAnimationLayer)
Jorim Jaggi391790622018-04-18 15:30:44 +02004151 .show(mHomeAppAnimationLayer)
Robert Carrf7a7ca82017-12-06 13:17:07 -08004152 .show(mSplitScreenDividerAnchor);
chaviw23ee71c2017-12-18 11:29:41 -08004153 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07004154 } else {
chaviw23ee71c2017-12-18 11:29:41 -08004155 mAppAnimationLayer.destroy();
4156 mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02004157 mBoostedAppAnimationLayer.destroy();
4158 mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02004159 mHomeAppAnimationLayer.destroy();
4160 mHomeAppAnimationLayer = null;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004161 mSplitScreenDividerAnchor.destroy();
4162 mSplitScreenDividerAnchor = null;
Robert Carrb1579c82017-09-05 14:54:47 -07004163 }
4164 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004165 }
4166
Robert Carree4d4b92017-11-22 12:21:46 -08004167 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004168 AboveAppWindowContainers(String name, WindowManagerService service) {
4169 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08004170 }
4171
Robert Carrb9506032018-02-13 13:54:00 -08004172 @Override
Vishnu Nair83537a72018-07-19 21:27:48 -07004173 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
4174 final SurfaceControl.Builder builder = super.makeChildSurface(child);
4175 if (child instanceof WindowToken && ((WindowToken) child).mRoundedCornerOverlay) {
4176 // To draw above the ColorFade layer during the screen off transition, the
4177 // rounded corner overlays need to be at the root of the surface hierarchy.
4178 // TODO: move the ColorLayer into the display overlay layer such that this is not
4179 // necessary anymore.
4180 builder.setParent(null);
4181 }
4182 return builder;
4183 }
4184
4185 @Override
Robert Carrb9506032018-02-13 13:54:00 -08004186 void assignChildLayers(SurfaceControl.Transaction t) {
4187 assignChildLayers(t, null /* imeContainer */);
4188 }
4189
Robert Carree4d4b92017-11-22 12:21:46 -08004190 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
4191 boolean needAssignIme = imeContainer != null
4192 && imeContainer.getSurfaceControl() != null;
4193 for (int j = 0; j < mChildren.size(); ++j) {
4194 final WindowToken wt = mChildren.get(j);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004195
4196 // See {@link mSplitScreenDividerAnchor}
4197 if (wt.windowType == TYPE_DOCK_DIVIDER) {
4198 wt.assignRelativeLayer(t, mTaskStackContainers.getSplitScreenDividerAnchor(), 1);
4199 continue;
4200 }
Vishnu Nair83537a72018-07-19 21:27:48 -07004201 if (wt.mRoundedCornerOverlay) {
4202 wt.assignLayer(t, WindowManagerPolicy.COLOR_FADE_LAYER + 1);
4203 continue;
4204 }
Robert Carree4d4b92017-11-22 12:21:46 -08004205 wt.assignLayer(t, j);
4206 wt.assignChildLayers(t);
4207
4208 int layer = mService.mPolicy.getWindowLayerFromTypeLw(
4209 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08004210
4211 if (needAssignIme && layer >= mService.mPolicy.getWindowLayerFromTypeLw(
4212 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004213 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08004214 needAssignIme = false;
4215 }
4216 }
4217 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004218 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08004219 }
4220 }
4221 }
4222
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004223 /**
4224 * Window container class that contains all containers on this display that are not related to
4225 * Apps. E.g. status bar.
4226 */
Robert Carree4d4b92017-11-22 12:21:46 -08004227 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07004228 /**
4229 * Compares two child window tokens returns -1 if the first is lesser than the second in
4230 * terms of z-order and 1 otherwise.
4231 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004232 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07004233 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004234 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
4235 token1.mOwnerCanManageAppTokens)
4236 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
4237 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004238
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004239 private final Predicate<WindowState> mGetOrientingWindow = w -> {
4240 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
4241 return false;
4242 }
4243 final int req = w.mAttrs.screenOrientation;
4244 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
4245 || req == SCREEN_ORIENTATION_UNSET) {
4246 return false;
4247 }
4248 return true;
4249 };
4250
Wale Ogunwale3a931692016-11-02 16:49:48 -07004251 private final String mName;
chaviwf29223e2018-01-31 13:23:51 -08004252 private final Dimmer mDimmer = new Dimmer(this);
4253 private final Rect mTmpDimBoundsRect = new Rect();
4254
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004255 NonAppWindowContainers(String name, WindowManagerService service) {
4256 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07004257 mName = name;
4258 }
4259
4260 void addChild(WindowToken token) {
4261 addChild(token, mWindowComparator);
4262 }
4263
4264 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004265 int getOrientation() {
4266 final WindowManagerPolicy policy = mService.mPolicy;
4267 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004268 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004269
4270 if (win != null) {
4271 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004272 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004273 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07004274 if (mService.mKeyguardGoingAway) {
4275 // Keyguard can't affect the orientation if it is going away...
4276 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
4277 return SCREEN_ORIENTATION_UNSET;
4278 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004279 }
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004280 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req
4281 + " for display id=" + mDisplayId);
Andrii Kulian8ee72852017-03-10 10:36:45 -08004282 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004283 }
4284
Andrii Kulian8ee72852017-03-10 10:36:45 -08004285 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004286
Jorim Jaggi1c530592018-04-06 15:11:47 +02004287 // Only allow force setting the orientation when all unknown visibilities have been
4288 // resolved, as otherwise we just may be starting another occluding activity.
4289 final boolean isUnoccluding =
4290 mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
4291 && mService.mUnknownAppVisibilityController.allResolved();
4292 if (policy.isKeyguardShowingAndNotOccluded() || isUnoccluding) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004293 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004294 }
4295
4296 return SCREEN_ORIENTATION_UNSET;
4297 }
4298
4299 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07004300 String getName() {
4301 return mName;
4302 }
chaviwf29223e2018-01-31 13:23:51 -08004303
4304 @Override
4305 Dimmer getDimmer() {
4306 return mDimmer;
4307 }
4308
4309 @Override
4310 void prepareSurfaces() {
4311 mDimmer.resetDimStates();
4312 super.prepareSurfaces();
4313 getBounds(mTmpDimBoundsRect);
4314
4315 if (mDimmer.updateDims(getPendingTransaction(), mTmpDimBoundsRect)) {
4316 scheduleAnimation();
4317 }
4318 }
Robert Carr3b716242016-08-16 16:02:21 -07004319 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01004320
Robert Carr9034d962018-01-04 18:27:42 -08004321 private class NonMagnifiableWindowContainers extends NonAppWindowContainers {
4322 NonMagnifiableWindowContainers(String name, WindowManagerService service) {
4323 super(name, service);
4324 }
4325
4326 @Override
4327 void applyMagnificationSpec(Transaction t, MagnificationSpec spec) {
4328 }
4329 };
4330
Robert Carrb1579c82017-09-05 14:54:47 -07004331 SurfaceControl.Builder makeSurface(SurfaceSession s) {
4332 return mService.makeSurfaceBuilder(s)
4333 .setParent(mWindowingLayer);
4334 }
4335
4336 @Override
4337 SurfaceSession getSession() {
4338 return mSession;
4339 }
4340
4341 @Override
4342 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07004343 SurfaceSession s = child != null ? child.getSession() : getSession();
4344 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrb1579c82017-09-05 14:54:47 -07004345 b.setSize(mSurfaceSize, mSurfaceSize);
Robert Carrf59b8dd2017-10-02 18:58:36 -07004346
4347 if (child == null) {
4348 return b;
4349 }
4350
Robert Carree4d4b92017-11-22 12:21:46 -08004351 return b.setName(child.getName())
4352 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07004353 }
4354
4355 /**
4356 * The makeSurface variants are for use by the window-container
4357 * hierarchy. makeOverlay here is a function for various non windowing
4358 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
4359 * and other potpourii.
4360 */
4361 SurfaceControl.Builder makeOverlay() {
4362 return mService.makeSurfaceBuilder(mSession)
4363 .setParent(mOverlayLayer);
4364 }
4365
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004366 /**
4367 * Reparents the given surface to mOverlayLayer.
4368 */
4369 void reparentToOverlay(Transaction transaction, SurfaceControl surface) {
4370 transaction.reparent(surface, mOverlayLayer.getHandle());
4371 }
4372
Robert Carrb1579c82017-09-05 14:54:47 -07004373 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carr24be9ab2018-04-30 17:54:53 -07004374 if (spec.scale != 1.0) {
4375 mMagnificationSpec = spec;
4376 } else {
4377 mMagnificationSpec = null;
4378 }
4379
Robert Carrf59b8dd2017-10-02 18:58:36 -07004380 applyMagnificationSpec(getPendingTransaction(), spec);
4381 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07004382 }
4383
Robert Carr24be9ab2018-04-30 17:54:53 -07004384 void reapplyMagnificationSpec() {
4385 if (mMagnificationSpec != null) {
4386 applyMagnificationSpec(getPendingTransaction(), mMagnificationSpec);
4387 }
4388 }
4389
Robert Carrb1579c82017-09-05 14:54:47 -07004390 @Override
4391 void onParentSet() {
4392 // Since we are the top of the SurfaceControl hierarchy here
4393 // we create the root surfaces explicitly rather than chaining
4394 // up as the default implementation in onParentSet does. So we
4395 // explicitly do NOT call super here.
4396 }
4397
4398 @Override
4399 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07004400
4401 // These are layers as children of "mWindowingLayer"
4402 mBelowAppWindowsContainers.assignLayer(t, 0);
4403 mTaskStackContainers.assignLayer(t, 1);
4404 mAboveAppWindowsContainers.assignLayer(t, 2);
4405
4406 WindowState imeTarget = mService.mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08004407 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08004408
Robert Carree4d4b92017-11-22 12:21:46 -08004409 // In the case where we have an IME target that is not in split-screen
4410 // mode IME assignment is easy. We just need the IME to go directly above
4411 // the target. This way children of the target will naturally go above the IME
4412 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08004413 //
Robert Carree4d4b92017-11-22 12:21:46 -08004414 // In the case of split-screen windowing mode, we need to elevate the IME above the
4415 // docked divider while keeping the app itself below the docked divider, so instead
4416 // we use relative layering of the IME targets child windows, and place the
4417 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08004418 //
Robert Carr234b6332018-03-20 13:38:16 -07004419 // In the case the IME target is animating, the animation Z order may be different
4420 // than the WindowContainer Z order, so it's difficult to be sure we have the correct
4421 // IME target. In this case we just layer the IME over all transitions by placing it in the
4422 // above applications layer.
4423 //
Robert Carree4d4b92017-11-22 12:21:46 -08004424 // In the case where we have no IME target we assign it where it's base layer would
4425 // place it in the AboveAppWindowContainers.
Robert Carr234b6332018-03-20 13:38:16 -07004426 if (imeTarget != null && !(imeTarget.inSplitScreenWindowingMode()
4427 || imeTarget.mToken.isAppAnimating())
Robert Carree4d4b92017-11-22 12:21:46 -08004428 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004429 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07004430 // TODO: We need to use an extra level on the app surface to ensure
4431 // this is always above SurfaceView but always below attached window.
4432 1);
Robert Carree4d4b92017-11-22 12:21:46 -08004433 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07004434 }
4435
4436 // Above we have assigned layers to our children, now we ask them to assign
4437 // layers to their children.
4438 mBelowAppWindowsContainers.assignChildLayers(t);
4439 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08004440 mAboveAppWindowsContainers.assignChildLayers(t,
4441 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07004442 mImeWindowsContainers.assignChildLayers(t);
4443 }
4444
4445 /**
4446 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
4447 * that the IME target is one of the docked applications. We'd like the docked divider to be
4448 * above both of the applications, and we'd like the IME to be above the docked divider.
4449 * However we need child windows of the applications to be above the IME (Text drag handles).
4450 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
4451 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
4452 * with {@link #WindowState#assignLayer}
4453 */
4454 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
Robert Carrbb5de662018-04-26 13:29:51 -07004455 child.assignRelativeLayer(t, mImeWindowsContainers.getSurfaceControl(), 1);
Robert Carrb1579c82017-09-05 14:54:47 -07004456 }
4457
4458 @Override
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004459 void prepareSurfaces() {
4460 final ScreenRotationAnimation screenRotationAnimation =
4461 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
4462 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
4463 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
4464 mPendingTransaction.setMatrix(mWindowingLayer,
4465 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
4466 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
4467 mPendingTransaction.setPosition(mWindowingLayer,
4468 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
4469 mPendingTransaction.setAlpha(mWindowingLayer,
4470 screenRotationAnimation.getEnterTransformation().getAlpha());
4471 }
Chavi Weingartenb736e322018-02-23 00:27:54 +00004472
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004473 super.prepareSurfaces();
4474 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004475
Jorim Jaggibe418292018-03-26 16:14:12 +02004476 void assignStackOrdering() {
4477 mTaskStackContainers.assignStackOrdering(getPendingTransaction());
Robert Carr2f8aa392018-01-31 14:46:51 -08004478 }
Chavi Weingarten3a748552018-05-14 17:32:42 +00004479
4480 /**
4481 * Increment the deferral count to determine whether to update the IME target.
4482 */
4483 void deferUpdateImeTarget() {
4484 mDeferUpdateImeTargetCount++;
4485 }
4486
4487 /**
4488 * Decrement the deferral count to determine whether to update the IME target. If the count
4489 * reaches 0, a new ime target will get computed.
4490 */
4491 void continueUpdateImeTarget() {
4492 if (mDeferUpdateImeTargetCount == 0) {
4493 return;
4494 }
4495
4496 mDeferUpdateImeTargetCount--;
4497 if (mDeferUpdateImeTargetCount == 0) {
4498 computeImeTarget(true /* updateImeTarget */);
4499 }
4500 }
4501
4502 /**
4503 * @return Whether a new IME target should be computed.
4504 */
4505 private boolean canUpdateImeTarget() {
4506 return mDeferUpdateImeTargetCount == 0;
4507 }
Arthur Hung95b38a92018-07-20 18:56:12 +08004508
4509 InputMonitor getInputMonitor() {
4510 return mInputMonitor;
4511 }
Jorim Jaggif1292892018-09-10 11:58:13 +02004512
4513 /**
4514 * @return Cached value whether we told display manager that we have content.
4515 */
4516 boolean getLastHasContent() {
4517 return mLastHasContent;
4518 }
Arthur Hungbe5ce212018-09-13 18:41:56 +08004519
4520 void registerPointerEventListener(@NonNull PointerEventListener listener) {
4521 if (mPointerEventDispatcher != null) {
4522 mPointerEventDispatcher.registerInputEventListener(listener);
4523 }
4524 }
4525
4526 void unregisterPointerEventListener(@NonNull PointerEventListener listener) {
4527 if (mPointerEventDispatcher != null) {
4528 mPointerEventDispatcher.unregisterInputEventListener(listener);
4529 }
4530 }
Craig Mautner59c00972012-07-30 12:10:24 -07004531}