blob: 17424a58506a314097b8ac2fa96c0f419c508700 [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;
chaviwff2e7d82018-11-02 11:11:27 -070032import static android.view.InsetsState.TYPE_IME;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070033import static android.view.Surface.ROTATION_0;
34import static android.view.Surface.ROTATION_180;
35import static android.view.Surface.ROTATION_270;
36import static android.view.Surface.ROTATION_90;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070037import static android.view.View.GONE;
Wale Ogunwale10124582016-09-15 20:25:50 -070038import static android.view.WindowManager.DOCKED_BOTTOM;
39import static android.view.WindowManager.DOCKED_INVALID;
40import static android.view.WindowManager.DOCKED_TOP;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080041import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
42import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
43import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale494009b82016-10-21 09:01:38 -070044import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070045import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070046import static android.view.WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE;
47import static android.view.WindowManager.LayoutParams.NEEDS_MENU_UNSET;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070048import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale494009b82016-10-21 09:01:38 -070049import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070050import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale494009b82016-10-21 09:01:38 -070051import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
Robert Carrf7a7ca82017-12-06 13:17:07 -080052import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
Wale Ogunwale494009b82016-10-21 09:01:38 -070053import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070054import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070055import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
56import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Wale Ogunwale494009b82016-10-21 09:01:38 -070057import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070058import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
59import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070060import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070061import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Wale Ogunwale3a256e62018-12-06 14:41:18 -080062import static android.view.WindowManager.TRANSIT_ACTIVITY_OPEN;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080063import static android.view.WindowManager.TRANSIT_KEYGUARD_UNOCCLUDE;
Wale Ogunwale3a256e62018-12-06 14:41:18 -080064import static android.view.WindowManager.TRANSIT_TASK_OPEN;
65import static android.view.WindowManager.TRANSIT_TASK_TO_FRONT;
Tiger Huang51c5a1d2018-12-11 20:24:51 +080066
Adrian Roose99bc052017-11-20 17:55:31 +010067import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
68import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
69import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
70import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080071import static com.android.server.wm.DisplayContentProto.ABOVE_APP_WINDOWS;
lumark588a3e82018-07-20 18:53:54 +080072import static com.android.server.wm.DisplayContentProto.APP_TRANSITION;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080073import static com.android.server.wm.DisplayContentProto.BELOW_APP_WINDOWS;
74import static com.android.server.wm.DisplayContentProto.DISPLAY_FRAMES;
75import static com.android.server.wm.DisplayContentProto.DISPLAY_INFO;
76import static com.android.server.wm.DisplayContentProto.DOCKED_STACK_DIVIDER_CONTROLLER;
77import static com.android.server.wm.DisplayContentProto.DPI;
Tiger Huang1e5b10a2018-07-30 20:19:51 +080078import static com.android.server.wm.DisplayContentProto.FOCUSED_APP;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080079import static com.android.server.wm.DisplayContentProto.ID;
80import static com.android.server.wm.DisplayContentProto.IME_WINDOWS;
81import static com.android.server.wm.DisplayContentProto.PINNED_STACK_CONTROLLER;
82import static com.android.server.wm.DisplayContentProto.ROTATION;
83import static com.android.server.wm.DisplayContentProto.SCREEN_ROTATION_ANIMATION;
84import static com.android.server.wm.DisplayContentProto.STACKS;
85import static com.android.server.wm.DisplayContentProto.WINDOW_CONTAINER;
Wale Ogunwale1666e312016-12-16 11:27:18 -080086import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
lumark588a3e82018-07-20 18:53:54 +080087import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_APP_TRANSITIONS;
Wale Ogunwale494009b82016-10-21 09:01:38 -070088import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070089import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070090import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
91import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070092import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070093import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070094import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020095import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070096import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070097import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070098import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -080099import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700100import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700101import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700102import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700103import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800104import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700105import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Tiger Huang51c5a1d2018-12-11 20:24:51 +0800106import static com.android.server.wm.WindowManagerService.H.REPORT_FOCUS_CHANGE;
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800107import static com.android.server.wm.WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800108import static com.android.server.wm.WindowManagerService.H.REPORT_LOSING_FOCUS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700109import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
110import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700111import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700112import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700113import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
114import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800115import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_PLACING_SURFACES;
116import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_REMOVING_FOCUS;
117import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_ASSIGN_LAYERS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700118import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700119import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700120import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700121import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700122import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700123import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700124import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700125import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700126import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700127
lumark588a3e82018-07-20 18:53:54 +0800128import android.animation.AnimationHandler;
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700129import android.annotation.CallSuper;
Riddle Hsuf53da812018-08-15 22:00:27 +0800130import android.annotation.IntDef;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700131import android.annotation.NonNull;
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200132import android.annotation.Nullable;
Dean Harding3e5a1522017-10-06 09:19:01 -0700133import android.content.pm.PackageManager;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700134import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700135import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700136import android.graphics.Bitmap;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700137import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800138import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700139import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700140import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100141import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700142import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700143import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700144import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700145import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700146import android.os.RemoteException;
147import android.os.SystemClock;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100148import android.os.Trace;
Riddle Hsuf53da812018-08-15 22:00:27 +0800149import android.os.UserHandle;
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800150import android.util.ArraySet;
Chong Zhang8e89b312015-09-09 15:09:30 -0700151import android.util.DisplayMetrics;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700152import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700153import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700154import android.view.Display;
Adrian Roos1cf585052018-01-03 18:43:27 +0100155import android.view.DisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700156import android.view.DisplayInfo;
Riddle Hsua4d6fa22018-08-11 00:50:39 +0800157import android.view.Gravity;
Arthur Hungbe5ce212018-09-13 18:41:56 +0800158import android.view.InputChannel;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700159import android.view.InputDevice;
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200160import android.view.InsetsState.InternalInsetType;
Robert Carrb1579c82017-09-05 14:54:47 -0700161import android.view.MagnificationSpec;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700162import android.view.Surface;
163import android.view.SurfaceControl;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100164import android.view.SurfaceControl.Transaction;
Robert Carrb1579c82017-09-05 14:54:47 -0700165import android.view.SurfaceSession;
Tiger Huang7c610aa2018-10-27 00:01:01 +0800166import android.view.View;
lumark588a3e82018-07-20 18:53:54 +0800167import android.view.WindowManager;
Evan Rosky39b6f232018-10-30 18:35:41 -0700168import android.view.WindowManagerPolicyConstants.PointerEventListener;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700169
Bryce Lee48f4b572017-04-10 10:54:15 -0700170import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800171import com.android.internal.util.ToBooleanFunction;
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200172import com.android.internal.util.function.TriConsumer;
Winson Chung6ba767a2018-12-13 18:20:11 -0800173import com.android.server.AnimationThread;
Adrian Roose99bc052017-11-20 17:55:31 +0100174import com.android.server.policy.WindowManagerPolicy;
Issei Suzuki43190bd2018-08-20 17:28:41 +0200175import com.android.server.wm.utils.DisplayRotationUtil;
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100176import com.android.server.wm.utils.RotationCache;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100177import com.android.server.wm.utils.WmDisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700178
179import java.io.PrintWriter;
Riddle Hsuf53da812018-08-15 22:00:27 +0800180import java.lang.annotation.Retention;
181import java.lang.annotation.RetentionPolicy;
Craig Mautner59c00972012-07-30 12:10:24 -0700182import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700183import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700184import java.util.HashMap;
185import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700186import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700187import java.util.List;
Adrian Roos1cf585052018-01-03 18:43:27 +0100188import java.util.Objects;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800189import java.util.function.Consumer;
190import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700191
Craig Mautner59c00972012-07-30 12:10:24 -0700192/**
193 * Utility class for keeping track of the WindowStates and other pertinent contents of a
194 * particular Display.
Craig Mautner59c00972012-07-30 12:10:24 -0700195 */
Riddle Hsuad256a12018-07-18 16:11:30 +0800196class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer>
197 implements WindowManagerPolicy.DisplayContentInfo {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700198 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700199
Riddle Hsuf53da812018-08-15 22:00:27 +0800200 /** The default scaling mode that scales content automatically. */
201 static final int FORCE_SCALING_MODE_AUTO = 0;
202 /** For {@link #setForcedScalingMode} to apply flag {@link Display#FLAG_SCALING_DISABLED}. */
203 static final int FORCE_SCALING_MODE_DISABLED = 1;
204
205 @IntDef(prefix = { "FORCE_SCALING_MODE_" }, value = {
206 FORCE_SCALING_MODE_AUTO,
207 FORCE_SCALING_MODE_DISABLED
208 })
209 @Retention(RetentionPolicy.SOURCE)
210 @interface ForceScalingMode {}
211
Craig Mautner59c00972012-07-30 12:10:24 -0700212 /** Unique identifier of this stack. */
213 private final int mDisplayId;
214
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800215 // TODO: Remove once unification is complete.
216 ActivityDisplay mAcitvityDisplay;
217
Wale Ogunwale3a931692016-11-02 16:49:48 -0700218 /** The containers below are the only child containers the display can have. */
219 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800220 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers(mWmService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700221 // Contains all non-app window containers that should be displayed above the app containers
222 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800223 private final AboveAppWindowContainers mAboveAppWindowsContainers =
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800224 new AboveAppWindowContainers("mAboveAppWindowsContainers", mWmService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700225 // Contains all non-app window containers that should be displayed below the app containers
226 // (e.g. Wallpaper).
227 private final NonAppWindowContainers mBelowAppWindowsContainers =
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800228 new NonAppWindowContainers("mBelowAppWindowsContainers", mWmService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700229 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
230 // 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 -0800231 // window containers together and move them in-sync if/when needed. We use a subclass of
232 // WindowContainer which is omitted from screen magnification, as the IME is never magnified.
233 private final NonMagnifiableWindowContainers mImeWindowsContainers =
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800234 new NonMagnifiableWindowContainers("mImeWindowsContainers", mWmService);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700235
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000236 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800237 private WindowState mTmpWindow2;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800238 private boolean mTmpRecoveringMemory;
239 private boolean mUpdateImeTarget;
240 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700241 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700242
lumark588a3e82018-07-20 18:53:54 +0800243 final AppTransition mAppTransition;
244 final AppTransitionController mAppTransitionController;
245 boolean mSkipAppTransitionAnimation = false;
246
247 final ArraySet<AppWindowToken> mOpeningApps = new ArraySet<>();
248 final ArraySet<AppWindowToken> mClosingApps = new ArraySet<>();
249 final UnknownAppVisibilityController mUnknownAppVisibilityController;
250 BoundsAnimationController mBoundsAnimationController;
251
252 /**
253 * List of clients without a transtiton animation that we notify once we are done
254 * transitioning since they won't be notified through the app window animator.
255 */
256 final List<IBinder> mNoAnimationNotifyOnTransitionFinished = new ArrayList<>();
257
Wale Ogunwale02319a62016-09-26 15:21:22 -0700258 // Mapping from a token IBinder to a WindowToken object on this display.
259 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
260
Andrii Kuliancd097992017-03-23 18:31:59 -0700261 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700262 int mInitialDisplayWidth = 0;
263 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700264 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700265
Adrian Roos1cf585052018-01-03 18:43:27 +0100266 DisplayCutout mInitialDisplayCutout;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100267 private final RotationCache<DisplayCutout, WmDisplayCutout> mDisplayCutoutCache
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100268 = new RotationCache<>(this::calculateDisplayCutoutForRotationUncached);
Adrian Roos1cf585052018-01-03 18:43:27 +0100269
Andrii Kuliancd097992017-03-23 18:31:59 -0700270 /**
271 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
272 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
273 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
274 */
Craig Mautner59c00972012-07-30 12:10:24 -0700275 int mBaseDisplayWidth = 0;
276 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700277 /**
278 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
279 * but can be set from Settings or via shell command "adb shell wm density".
280 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
281 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700282 int mBaseDisplayDensity = 0;
Riddle Hsuf53da812018-08-15 22:00:27 +0800283
284 /**
285 * Whether to disable display scaling. This can be set via shell command "adb shell wm scaling".
286 * @see WindowManagerService#setForcedDisplayScalingMode(int, int)
287 */
Jeff Brownd46747a2015-04-15 19:02:36 -0700288 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700289 private final DisplayInfo mDisplayInfo = new DisplayInfo();
290 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700291 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800292 private final DisplayPolicy mDisplayPolicy;
293 private DisplayRotation mDisplayRotation;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000294 DisplayFrames mDisplayFrames;
295
Andrii Kulian06d07d62017-03-14 11:11:47 -0700296 /**
297 * For default display it contains real metrics, empty for others.
298 * @see WindowManagerService#createWatermarkInTransaction()
299 */
300 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800301
302 /** @see #computeCompatSmallestWidth(boolean, int, int, int, DisplayCutout) */
Andrii Kulian06d07d62017-03-14 11:11:47 -0700303 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700304
Andrii Kulian06d07d62017-03-14 11:11:47 -0700305 /**
306 * Compat metrics computed based on {@link #mDisplayMetrics}.
307 * @see #updateDisplayAndOrientation(int)
308 */
309 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
310
311 /** The desired scaling factor for compatible apps. */
312 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700313
Andrii Kulian8ee72852017-03-10 10:36:45 -0800314 /**
315 * Current rotation of the display.
316 * Constants as per {@link android.view.Surface.Rotation}.
317 *
Robert Carrae606b42018-02-15 15:36:23 -0800318 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800319 */
320 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700321
Andrii Kulian8ee72852017-03-10 10:36:45 -0800322 /**
323 * Last applied orientation of the display.
324 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
325 *
Riddle Hsu4e611772018-10-31 18:58:28 +0800326 * @see #updateOrientationFromAppTokens()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800327 */
328 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700329
Andrii Kulian8ee72852017-03-10 10:36:45 -0800330 /**
331 * Flag indicating that the application is receiving an orientation that has different metrics
332 * than it expected. E.g. Portrait instead of Landscape.
333 *
Robert Carrae606b42018-02-15 15:36:23 -0800334 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800335 */
336 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700337
Andrii Kulian8ee72852017-03-10 10:36:45 -0800338 /**
339 * Orientation forced by some window. If there is no visible window that specifies orientation
340 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
341 *
342 * @see NonAppWindowContainers#getOrientation()
343 */
344 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700345
Andrii Kulian8ee72852017-03-10 10:36:45 -0800346 /**
347 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
348 * occluded.
349 *
350 * @see NonAppWindowContainers#getOrientation()
351 */
352 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
353
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700354 /**
355 * Keep track of wallpaper visibility to notify changes.
356 */
357 private boolean mLastWallpaperVisible = false;
358
Andrii Kulian06d07d62017-03-14 11:11:47 -0700359 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700360
Craig Mautner39834192012-09-02 07:47:24 -0700361 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700362 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700363 int pendingLayoutChanges;
Riddle Hsu654a6f92018-07-13 22:59:36 +0800364 int mDeferredRotationPauseCount;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800365
Vishnu Nairba183352018-11-21 11:16:49 -0800366 /**
367 * Used to gate application window layout until we have sent the complete configuration.
368 * TODO: There are still scenarios where we may be out of sync with the client. Ideally
369 * we want to replace this flag with a mechanism that will confirm the configuration
370 * applied by the client is the one expected by the system server.
371 */
372 boolean mWaitingForConfig;
373
Andrii Kulian839def92016-11-02 10:58:58 -0700374 // TODO(multi-display): remove some of the usages.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800375 @VisibleForTesting
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800376 boolean isDefaultDisplay;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800377
Andrii Kulianf0379de2018-03-14 16:24:07 -0700378 /**
379 * Flag indicating whether WindowManager should override info for this display in
380 * DisplayManager.
381 */
382 boolean mShouldOverrideDisplayConfiguration = true;
Craig Mautner39834192012-09-02 07:47:24 -0700383
Craig Mautnerdc548482014-02-05 13:35:24 -0800384 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700385 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800386
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700387 /** Detect user tapping outside of current focused task bounds .*/
388 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700389
Craig Mautner6601b7b2013-04-29 10:29:11 -0700390 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700391 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700392
Craig Mautner6601b7b2013-04-29 10:29:11 -0700393 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800394 private final Rect mTmpRect = new Rect();
395 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700396 private final RectF mTmpRectF = new RectF();
397 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800398 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700399
Bryce Leef3c6a472017-11-14 14:53:06 -0800400 /** Used for handing back size of display */
401 private final Rect mTmpBounds = new Rect();
402
Garfield Tan90b04282018-12-11 14:04:42 -0800403 private final Configuration mTmpConfiguration = new Configuration();
404
Craig Mautner95da1082014-02-24 17:54:35 -0800405 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700406 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800407
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700408 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700409 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700410
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800411 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800412 /** A collection of windows that provide tap exclude regions inside of them. */
413 final ArraySet<WindowState> mTapExcludeProvidingWindows = new ArraySet<>();
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800414
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000415 private boolean mHaveBootMsg = false;
416 private boolean mHaveApp = false;
417 private boolean mHaveWallpaper = false;
418 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700419
420 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
421
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700422 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
423 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000424 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
425 new ApplySurfaceChangesTransactionState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700426
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700427 // True if this display is in the process of being removed. Used to determine if the removal of
428 // the display's direct children should be allowed.
429 private boolean mRemovingDisplay = false;
430
Bryce Leed1871262017-06-12 14:12:29 -0700431 // {@code false} if this display is in the processing of being created.
432 private boolean mDisplayReady = false;
433
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800434 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700435
wilsonshihc32538e2018-11-07 17:27:34 +0800436 boolean mWallpaperMayChange = false;
437
Robert Carrb1579c82017-09-05 14:54:47 -0700438 private final SurfaceSession mSession = new SurfaceSession();
439
440 /**
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800441 * Window that is currently interacting with the user. This window is responsible for receiving
442 * key events and pointer events from the user.
443 */
444 WindowState mCurrentFocus = null;
445
446 /**
447 * The last focused window that we've notified the client that the focus is changed.
448 */
449 WindowState mLastFocus = null;
450
451 /**
452 * Windows that have lost input focus and are waiting for the new focus window to be displayed
453 * before they are told about this.
454 */
455 ArrayList<WindowState> mLosingFocus = new ArrayList<>();
456
457 /**
458 * The foreground app of this display. Windows below this app cannot be the focused window. If
459 * the user taps on the area outside of the task of the focused app, we will notify AM about the
460 * new task the user wants to interact with.
461 */
462 AppWindowToken mFocusedApp = null;
463
464 /** Windows added since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
465 final ArrayList<WindowState> mWinAddedSinceNullFocus = new ArrayList<>();
466
467 /** Windows removed since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
468 final ArrayList<WindowState> mWinRemovedSinceNullFocus = new ArrayList<>();
469
470 /**
Robert Carrb1579c82017-09-05 14:54:47 -0700471 * We organize all top-level Surfaces in to the following layers.
472 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800473 * and omitted from Screen-Magnification, for example the strict mode flash or
474 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700475 * {@link #mWindowingLayer} contains everything else.
476 */
477 private SurfaceControl mOverlayLayer;
478
479 /**
480 * See {@link #mOverlayLayer}
481 */
482 private SurfaceControl mWindowingLayer;
483
484 /**
Adrian Roos5251b1d2018-03-23 18:57:43 +0100485 * Sequence number for the current layout pass.
486 */
487 int mLayoutSeq = 0;
488
Chavi Weingarten3a748552018-05-14 17:32:42 +0000489 /**
490 * Specifies the count to determine whether to defer updating the IME target until ready.
491 */
492 private int mDeferUpdateImeTargetCount;
493
Jorim Jaggi9af095b2017-12-12 17:18:57 +0100494 /** Temporary float array to retrieve 3x3 matrix values. */
495 private final float[] mTmpFloats = new float[9];
496
Robert Carr24be9ab2018-04-30 17:54:53 -0700497 private MagnificationSpec mMagnificationSpec;
498
Arthur Hung95b38a92018-07-20 18:56:12 +0800499 private InputMonitor mInputMonitor;
500
Jorim Jaggif1292892018-09-10 11:58:13 +0200501 /** Caches the value whether told display manager that we have content. */
502 private boolean mLastHasContent;
503
Issei Suzuki43190bd2018-08-20 17:28:41 +0200504 private DisplayRotationUtil mRotationUtil = new DisplayRotationUtil();
505
lumark90120a82018-08-15 00:33:03 +0800506 /**
507 * The input method window for this display.
508 */
509 WindowState mInputMethodWindow;
510
lumarkff0ab692018-11-05 20:32:30 +0800511 /**
512 * This just indicates the window the input method is on top of, not
513 * necessarily the window its input is going to.
514 */
515 WindowState mInputMethodTarget;
516
517 /** If true hold off on modifying the animation layer of mInputMethodTarget */
518 boolean mInputMethodTargetWaitingAnim;
519
Arthur Hungbe5ce212018-09-13 18:41:56 +0800520 private final PointerEventDispatcher mPointerEventDispatcher;
521
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200522 private final InsetsStateController mInsetsStateController;
523
Tiger Huang7c610aa2018-10-27 00:01:01 +0800524 // Last systemUiVisibility we received from status bar.
525 private int mLastStatusBarVisibility = 0;
526 // Last systemUiVisibility we dispatched to windows.
527 private int mLastDispatchedSystemUiVisibility = 0;
528
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800529 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
530 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800531 final AppWindowToken atoken = w.mAppToken;
532 if (winAnimator.mDrawState == READY_TO_SHOW) {
Jorim Jaggiaf0d6d22018-06-08 15:25:35 +0200533 if (atoken == null || atoken.canShowWindows()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800534 if (w.performShowLocked()) {
535 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
536 if (DEBUG_LAYOUT_REPEATS) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800537 mWmService.mWindowPlacerLocked.debugLayoutRepeats(
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800538 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
539 }
540 }
541 }
542 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800543 };
544
545 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
546 final WindowStateAnimator winAnimator = w.mWinAnimator;
547 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
548 return;
549 }
550
Jorim Jaggi8f520872018-08-14 17:00:20 +0200551 // If this window is animating, ensure the animation background is set.
552 final AnimationAdapter anim = w.mAppToken != null
553 ? w.mAppToken.getAnimation()
554 : w.getAnimation();
555 if (anim != null) {
556 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800557 if (color != 0) {
558 final TaskStack stack = w.getStack();
559 if (stack != null) {
560 stack.setAnimationBackground(winAnimator, color);
561 }
562 }
563 }
564 };
565
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800566 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
567 final int lostFocusUid = mTmpWindow.mOwnerUid;
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800568 final Handler handler = mWmService.mH;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800569 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
570 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
571 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
572 w.mAttrs.hideTimeoutMilliseconds);
573 }
574 }
575 };
576
577 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800578 final AppWindowToken focusedApp = mFocusedApp;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800579 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
580 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
581
582 if (!w.canReceiveKeys()) {
583 return false;
584 }
585
586 final AppWindowToken wtoken = w.mAppToken;
587
588 // If this window's application has been removed, just skip it.
589 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
590 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
591 + (wtoken.removed ? "removed" : "sendingToBottom"));
592 return false;
593 }
594
595 if (focusedApp == null) {
596 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
597 + " using new focus @ " + w);
598 mTmpWindow = w;
599 return true;
600 }
601
602 if (!focusedApp.windowsAreFocusable()) {
603 // Current focused app windows aren't focusable...
604 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
605 + " focusable using new focus @ " + w);
606 mTmpWindow = w;
607 return true;
608 }
609
610 // Descend through all of the app tokens and find the first that either matches
611 // win.mAppToken (return win) or mFocusedApp (return null).
612 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
613 if (focusedApp.compareTo(wtoken) > 0) {
614 // App stack below focused app stack. No focus for you!!!
615 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
616 "findFocusedWindow: Reached focused app=" + focusedApp);
617 mTmpWindow = null;
618 return true;
619 }
620 }
621
622 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
623 mTmpWindow = w;
624 return true;
625 };
626
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800627 private final Consumer<WindowState> mPerformLayout = w -> {
628 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
629 // wasting time and funky changes while a window is animating away.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800630 final boolean gone = (mTmpWindow != null && mWmService.mPolicy.canBeHiddenByKeyguardLw(w))
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800631 || w.isGoneForLayoutLw();
632
633 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
634 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
635 + " mLayoutAttached=" + w.mLayoutAttached
636 + " screen changed=" + w.isConfigChanged());
637 final AppWindowToken atoken = w.mAppToken;
638 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200639 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800640 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
641 + " parentHidden=" + w.isParentWindowHidden());
642 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200643 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800644 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
645 + " parentHidden=" + w.isParentWindowHidden());
646 }
647
648 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
649 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
650 // since that means "perform layout as normal, just don't display").
651 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
652 || ((w.isConfigChanged() || w.setReportResizeHints())
653 && !w.isGoneForLayoutLw() &&
654 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
655 (w.mHasSurface && w.mAppToken != null &&
656 w.mAppToken.layoutConfigChanges)))) {
657 if (!w.mLayoutAttached) {
658 if (mTmpInitial) {
659 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700660 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800661 }
662 if (w.mAttrs.type == TYPE_DREAM) {
663 // Don't layout windows behind a dream, so that if it does stuff like hide
664 // the status bar we won't get a bad transition when it goes away.
665 mTmpWindow = w;
666 }
667 w.mLayoutNeeded = false;
668 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200669 final boolean firstLayout = !w.isLaidOut();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800670 getDisplayPolicy().layoutWindowLw(w, null, mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100671 w.mLayoutSeq = mLayoutSeq;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800672
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200673 // If this is the first layout, we need to initialize the last inset values as
674 // otherwise we'd immediately cause an unnecessary resize.
675 if (firstLayout) {
676 w.updateLastInsetValues();
677 }
678
Adrian Roos23df3a32018-03-15 15:41:13 +0100679 if (w.mAppToken != null) {
680 w.mAppToken.layoutLetterbox(w);
681 }
682
chaviw492139a2018-07-16 16:07:35 -0700683 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700684 + " mContainingFrame=" + w.getContainingFrame()
685 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800686 }
687 }
688 };
689
690 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
691 if (w.mLayoutAttached) {
692 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
693 + " mViewVisibility=" + w.mViewVisibility
694 + " mRelayoutCalled=" + w.mRelayoutCalled);
695 // If this view is GONE, then skip it -- keep the current frame, and let the caller
696 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
697 // windows, since that means "perform layout as normal, just don't display").
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800698 if (mTmpWindow != null && mWmService.mPolicy.canBeHiddenByKeyguardLw(w)) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800699 return;
700 }
701 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
702 || w.mLayoutNeeded) {
703 if (mTmpInitial) {
704 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700705 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800706 }
707 w.mLayoutNeeded = false;
708 w.prelayout();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800709 getDisplayPolicy().layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100710 w.mLayoutSeq = mLayoutSeq;
chaviw492139a2018-07-16 16:07:35 -0700711 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700712 + " mContainingFrame=" + w.getContainingFrame()
713 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800714 }
715 } else if (w.mAttrs.type == TYPE_DREAM) {
716 // Don't layout windows behind a dream, so that if it does stuff like hide the
717 // status bar we won't get a bad transition when it goes away.
718 mTmpWindow = mTmpWindow2;
719 }
720 };
721
722 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
723 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
724 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
725 return w.canBeImeTarget();
726 };
727
728 private final Consumer<WindowState> mApplyPostLayoutPolicy =
Tiger Huang7c610aa2018-10-27 00:01:01 +0800729 w -> getDisplayPolicy().applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
lumarkff0ab692018-11-05 20:32:30 +0800730 mInputMethodTarget);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800731
732 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800733 final WindowSurfacePlacer surfacePlacer = mWmService.mWindowPlacerLocked;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800734 final boolean obscuredChanged = w.mObscured !=
735 mTmpApplySurfaceChangesTransactionState.obscured;
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800736 final RootWindowContainer root = mWmService.mRoot;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800737
738 // Update effect.
739 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
740 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
741 final boolean isDisplayed = w.isDisplayedLw();
742
743 if (isDisplayed && w.isObscuringDisplay()) {
744 // This window completely covers everything behind it, so we want to leave all
745 // of them as undimmed (for performance reasons).
746 root.mObscuringWindow = w;
747 mTmpApplySurfaceChangesTransactionState.obscured = true;
748 }
749
750 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
751 root.handleNotObscuredLocked(w,
752 mTmpApplySurfaceChangesTransactionState.obscured,
753 mTmpApplySurfaceChangesTransactionState.syswin);
754
755 if (w.mHasSurface && isDisplayed) {
756 final int type = w.mAttrs.type;
757 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
758 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
759 mTmpApplySurfaceChangesTransactionState.syswin = true;
760 }
761 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
762 && w.mAttrs.preferredRefreshRate != 0) {
763 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
764 = w.mAttrs.preferredRefreshRate;
765 }
766 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
767 && w.mAttrs.preferredDisplayModeId != 0) {
768 mTmpApplySurfaceChangesTransactionState.preferredModeId
769 = w.mAttrs.preferredDisplayModeId;
770 }
771 }
772 }
773
wilsonshihc32538e2018-11-07 17:27:34 +0800774 if (obscuredChanged && w.isVisibleLw() && mWallpaperController.isWallpaperTarget(w)) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800775 // This is the wallpaper target and its obscured state changed... make sure the
776 // current wallpaper's visibility has been updated accordingly.
777 mWallpaperController.updateWallpaperVisibility();
778 }
779
chaviw161ea3e2018-01-31 12:01:12 -0800780 w.handleWindowMovedIfNeeded();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800781
782 final WindowStateAnimator winAnimator = w.mWinAnimator;
783
784 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
chaviw1454b392018-08-06 09:54:04 -0700785 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800786
787 // Moved from updateWindowsAndWallpaperLocked().
788 if (w.mHasSurface) {
789 // Take care of the window being ready to display.
790 final boolean committed = winAnimator.commitFinishDrawingLocked();
791 if (isDefaultDisplay && committed) {
792 if (w.mAttrs.type == TYPE_DREAM) {
793 // HACK: When a dream is shown, it may at that point hide the lock screen.
794 // So we need to redo the layout to let the phone window manager make this
795 // happen.
796 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
797 if (DEBUG_LAYOUT_REPEATS) {
798 surfacePlacer.debugLayoutRepeats(
799 "dream and commitFinishDrawingLocked true",
800 pendingLayoutChanges);
801 }
802 }
803 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
804 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
805 "First draw done in potential wallpaper target " + w);
wilsonshihc32538e2018-11-07 17:27:34 +0800806 mWallpaperMayChange = true;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800807 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
808 if (DEBUG_LAYOUT_REPEATS) {
809 surfacePlacer.debugLayoutRepeats(
810 "wallpaper and commitFinishDrawingLocked true",
811 pendingLayoutChanges);
812 }
813 }
814 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800815 }
816
817 final AppWindowToken atoken = w.mAppToken;
818 if (atoken != null) {
Adrian Roos23df3a32018-03-15 15:41:13 +0100819 atoken.updateLetterboxSurface(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800820 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
821 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
822 mTmpUpdateAllDrawn.add(atoken);
823 }
824 }
825
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800826 if (!mLosingFocus.isEmpty() && w.isFocused() && w.isDisplayedLw()) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800827 mWmService.mH.obtainMessage(REPORT_LOSING_FOCUS, this).sendToTarget();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800828 }
829
830 w.updateResizingWindowIfNeeded();
831 };
832
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800833 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800834 * Create new {@link DisplayContent} instance, add itself to the root window container and
835 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700836 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800837 * @param service You know.
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800838 * @param activityDisplay The ActivityDisplay for the display container.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700839 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700840 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800841 ActivityDisplay activityDisplay) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100842 super(service);
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800843 mAcitvityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -0800844 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
845 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
846 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
847 + " new=" + display);
848 }
849
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700850 mDisplay = display;
851 mDisplayId = display.getDisplayId();
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800852 mWallpaperController = new WallpaperController(mWmService, this);
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700853 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700854 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700855 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100856 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo,
857 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700858 initializeDisplayBaseInfo();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700859
lumark588a3e82018-07-20 18:53:54 +0800860 mAppTransition = new AppTransition(service.mContext, service, this);
861 mAppTransition.registerListenerLocked(service.mActivityManagerAppTransitionNotifier);
862 mAppTransitionController = new AppTransitionController(service, this);
863 mUnknownAppVisibilityController = new UnknownAppVisibilityController(service, this);
864
865 AnimationHandler animationHandler = new AnimationHandler();
866 mBoundsAnimationController = new BoundsAnimationController(service.mContext,
Winson Chung6ba767a2018-12-13 18:20:11 -0800867 mAppTransition, AnimationThread.getHandler(), animationHandler);
lumark588a3e82018-07-20 18:53:54 +0800868
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800869 if (mWmService.mInputManager != null) {
870 final InputChannel inputChannel = mWmService.mInputManager.monitorInput("Display "
Tiger Huang7c610aa2018-10-27 00:01:01 +0800871 + mDisplayId, mDisplayId);
872 mPointerEventDispatcher = inputChannel != null
873 ? new PointerEventDispatcher(inputChannel) : null;
874 } else {
875 mPointerEventDispatcher = null;
876 }
877 mDisplayPolicy = new DisplayPolicy(service, this);
878 mDisplayRotation = new DisplayRotation(service, this);
879 if (isDefaultDisplay) {
880 // The policy may be invoked right after here, so it requires the necessary default
881 // fields of this display content.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800882 mWmService.mPolicy.setDefaultDisplay(this);
Tiger Huang7c610aa2018-10-27 00:01:01 +0800883 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800884 if (mWmService.mDisplayReady) {
Tiger Huang7c610aa2018-10-27 00:01:01 +0800885 mDisplayPolicy.onConfigurationChanged();
886 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800887 if (mWmService.mSystemReady) {
Tiger Huang7c610aa2018-10-27 00:01:01 +0800888 mDisplayPolicy.systemReady();
889 }
890 mDividerControllerLocked = new DockedStackDividerController(service, this);
891 mPinnedStackControllerLocked = new PinnedStackController(service, this);
892
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800893 final SurfaceControl.Builder b = mWmService.makeSurfaceBuilder(mSession).setOpaque(true);
Robert Carrb1579c82017-09-05 14:54:47 -0700894 mWindowingLayer = b.setName("Display Root").build();
895 mOverlayLayer = b.setName("Display Overlays").build();
896
Robert Carrf59b8dd2017-10-02 18:58:36 -0700897 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700898 .setLayerStack(mWindowingLayer, mDisplayId)
899 .show(mWindowingLayer)
900 .setLayer(mOverlayLayer, 1)
901 .setLayerStack(mOverlayLayer, mDisplayId)
902 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700903 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700904
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700905 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700906 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700907 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700908 super.addChild(mAboveAppWindowsContainers, null);
909 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800910
911 // Add itself as a child to the root container.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800912 mWmService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700913
914 // TODO(b/62541591): evaluate whether this is the best spot to declare the
915 // {@link DisplayContent} ready for use.
916 mDisplayReady = true;
Arthur Hung95b38a92018-07-20 18:56:12 +0800917
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800918 mWmService.mAnimator.addDisplayLocked(mDisplayId);
Arthur Hung39134b22018-08-14 11:58:28 +0800919 mInputMonitor = new InputMonitor(service, mDisplayId);
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200920 mInsetsStateController = new InsetsStateController(this);
Bryce Leed1871262017-06-12 14:12:29 -0700921 }
922
923 boolean isReady() {
924 // The display is ready when the system and the individual display are both ready.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800925 return mWmService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700926 }
927
928 int getDisplayId() {
929 return mDisplayId;
930 }
931
Wale Ogunwale02319a62016-09-26 15:21:22 -0700932 WindowToken getWindowToken(IBinder binder) {
933 return mTokenMap.get(binder);
934 }
935
936 AppWindowToken getAppWindowToken(IBinder binder) {
937 final WindowToken token = getWindowToken(binder);
938 if (token == null) {
939 return null;
940 }
941 return token.asAppWindowToken();
942 }
943
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700944 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800945 final DisplayContent dc = mWmService.mRoot.getWindowTokenDisplay(token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700946 if (dc != null) {
947 // We currently don't support adding a window token to the display if the display
948 // already has the binder mapped to another token. If there is a use case for supporting
949 // this moving forward we will either need to merge the WindowTokens some how or have
950 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700951 throw new IllegalArgumentException("Can't map token=" + token + " to display="
952 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700953 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700954 if (binder == null) {
955 throw new IllegalArgumentException("Can't map token=" + token + " to display="
956 + getName() + " binder is null");
957 }
958 if (token == null) {
959 throw new IllegalArgumentException("Can't map null token to display="
960 + getName() + " binder=" + binder);
961 }
962
Wale Ogunwale02319a62016-09-26 15:21:22 -0700963 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700964
965 if (token.asAppWindowToken() == null) {
966 // Add non-app token to container hierarchy on the display. App tokens are added through
967 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700968 switch (token.windowType) {
969 case TYPE_WALLPAPER:
970 mBelowAppWindowsContainers.addChild(token);
971 break;
972 case TYPE_INPUT_METHOD:
973 case TYPE_INPUT_METHOD_DIALOG:
974 mImeWindowsContainers.addChild(token);
975 break;
976 default:
977 mAboveAppWindowsContainers.addChild(token);
978 break;
979 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700980 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700981 }
982
983 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700984 final WindowToken token = mTokenMap.remove(binder);
985 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800986 token.setExiting();
987 }
988 return token;
989 }
990
991 /** Changes the display the input window token is housed on to this one. */
992 void reParentWindowToken(WindowToken token) {
993 final DisplayContent prevDc = token.getDisplayContent();
994 if (prevDc == this) {
995 return;
996 }
Riddle Hsu85bd04b2018-11-17 00:34:36 +0800997 if (prevDc != null) {
998 if (prevDc.mTokenMap.remove(token.token) != null && token.asAppWindowToken() == null) {
999 // Removed the token from the map, but made sure it's not an app token before
1000 // removing from parent.
1001 token.getParent().removeChild(token);
1002 }
1003 if (prevDc.mLastFocus == mCurrentFocus) {
1004 // The window has become the focus of this display, so it should not be notified
1005 // that it lost focus from the previous display.
1006 prevDc.mLastFocus = null;
1007 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001008 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -08001009
1010 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -07001011 }
1012
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001013 void removeAppToken(IBinder binder) {
1014 final WindowToken token = removeWindowToken(binder);
1015 if (token == null) {
1016 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
1017 return;
1018 }
1019
1020 final AppWindowToken appToken = token.asAppWindowToken();
1021
1022 if (appToken == null) {
1023 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
1024 return;
1025 }
1026
1027 appToken.onRemovedFromDisplay();
1028 }
1029
Riddle Hsuad256a12018-07-18 16:11:30 +08001030 @Override
1031 public Display getDisplay() {
Craig Mautnerb47bbc32012-08-22 17:41:48 -07001032 return mDisplay;
1033 }
1034
Craig Mautner59c00972012-07-30 12:10:24 -07001035 DisplayInfo getDisplayInfo() {
1036 return mDisplayInfo;
1037 }
1038
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001039 DisplayMetrics getDisplayMetrics() {
1040 return mDisplayMetrics;
1041 }
1042
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001043 DisplayPolicy getDisplayPolicy() {
1044 return mDisplayPolicy;
1045 }
1046
Riddle Hsuad256a12018-07-18 16:11:30 +08001047 @Override
1048 public DisplayRotation getDisplayRotation() {
1049 return mDisplayRotation;
1050 }
1051
Jorim Jaggif96c90a2018-09-26 16:55:15 +02001052 /**
1053 * Marks a window as providing insets for the rest of the windows in the system.
1054 *
1055 * @param type The type of inset this window provides.
1056 * @param win The window.
1057 * @param frameProvider Function to compute the frame, or {@code null} if the just the frame of
1058 * the window should be taken.
1059 */
1060 void setInsetProvider(@InternalInsetType int type, WindowState win,
1061 @Nullable TriConsumer<DisplayFrames, WindowState, Rect> frameProvider) {
1062 mInsetsStateController.getSourceProvider(type).setWindow(win, frameProvider);
1063 }
1064
1065 InsetsStateController getInsetsStateController() {
1066 return mInsetsStateController;
1067 }
1068
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001069 @VisibleForTesting
1070 void setDisplayRotation(DisplayRotation displayRotation) {
1071 mDisplayRotation = displayRotation;
1072 }
1073
Andrii Kulian8ee72852017-03-10 10:36:45 -08001074 int getRotation() {
1075 return mRotation;
1076 }
1077
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001078 @VisibleForTesting
Andrii Kulian8ee72852017-03-10 10:36:45 -08001079 void setRotation(int newRotation) {
1080 mRotation = newRotation;
Riddle Hsuad256a12018-07-18 16:11:30 +08001081 mDisplayRotation.setRotation(newRotation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08001082 }
1083
1084 int getLastOrientation() {
1085 return mLastOrientation;
1086 }
1087
Andrii Kulian8ee72852017-03-10 10:36:45 -08001088 boolean getAltOrientation() {
1089 return mAltOrientation;
1090 }
1091
Andrii Kulian8ee72852017-03-10 10:36:45 -08001092 int getLastWindowForcedOrientation() {
1093 return mLastWindowForcedOrientation;
1094 }
1095
Andrii Kulian06d07d62017-03-14 11:11:47 -07001096 /**
Riddle Hsu654a6f92018-07-13 22:59:36 +08001097 * Temporarily pauses rotation changes until resumed.
1098 *
1099 * This can be used to prevent rotation changes from occurring while the user is
1100 * performing certain operations, such as drag and drop.
1101 *
1102 * This call nests and must be matched by an equal number of calls to
1103 * {@link #resumeRotationLocked}.
1104 */
1105 void pauseRotationLocked() {
1106 mDeferredRotationPauseCount++;
1107 }
1108
1109 /**
1110 * Resumes normal rotation changes after being paused.
1111 */
1112 void resumeRotationLocked() {
1113 if (mDeferredRotationPauseCount <= 0) {
1114 return;
1115 }
1116
1117 mDeferredRotationPauseCount--;
1118 if (mDeferredRotationPauseCount == 0) {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001119 updateRotationAndSendNewConfigIfNeeded();
Riddle Hsu654a6f92018-07-13 22:59:36 +08001120 }
1121 }
1122
1123 /**
Riddle Hsuad256a12018-07-18 16:11:30 +08001124 * If this is true we have updated our desired orientation, but not yet changed the real
1125 * orientation our applied our screen rotation animation. For example, because a previous
1126 * screen rotation was in progress.
1127 *
1128 * @return {@code true} if the there is an ongoing rotation change.
1129 */
1130 boolean rotationNeedsUpdate() {
1131 final int lastOrientation = getLastOrientation();
1132 final int oldRotation = getRotation();
1133 final boolean oldAltOrientation = getAltOrientation();
1134
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001135 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Riddle Hsuad256a12018-07-18 16:11:30 +08001136 final boolean altOrientation = !mDisplayRotation.rotationHasCompatibleMetrics(
1137 lastOrientation, rotation);
1138 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1139 return false;
1140 }
1141 return true;
1142 }
1143
Wale Ogunwale3a256e62018-12-06 14:41:18 -08001144 /**
1145 * The display content may have configuration set from {@link #DisplayWindowSettings}. This
1146 * callback let the owner of container know there is existing configuration to prevent the
1147 * values from being replaced by the initializing {@link #ActivityDisplay}.
1148 */
1149 void initializeDisplayOverrideConfiguration() {
1150 if (mAcitvityDisplay != null) {
1151 mAcitvityDisplay.onInitializeOverrideConfiguration(getRequestedOverrideConfiguration());
1152 }
1153 }
1154
Riddle Hsu4e611772018-10-31 18:58:28 +08001155 /** Notify the configuration change of this display. */
1156 void sendNewConfiguration() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001157 mWmService.mH.obtainMessage(SEND_NEW_CONFIGURATION, this).sendToTarget();
Riddle Hsu4e611772018-10-31 18:58:28 +08001158 }
1159
Garfield Tan90b04282018-12-11 14:04:42 -08001160 @Override
1161 boolean onDescendantOrientationChanged(IBinder freezeDisplayToken,
1162 ConfigurationContainer requestingContainer) {
1163 final Configuration config = updateOrientationFromAppTokens(
1164 getRequestedOverrideConfiguration(), freezeDisplayToken, false);
1165 // If display rotation class tells us that it doesn't consider app requested orientation,
1166 // this display won't rotate just because of an app changes its requested orientation. Thus
1167 // it indicates that this display chooses not to handle this request.
1168 final boolean handled = getDisplayRotation().respectAppRequestedOrientation();
1169 if (config == null) {
1170 return handled;
1171 }
1172
1173 if (handled && requestingContainer instanceof ActivityRecord) {
1174 final ActivityRecord activityRecord = (ActivityRecord) requestingContainer;
1175 final boolean kept = mWmService.mAtmService.updateDisplayOverrideConfigurationLocked(
1176 config, activityRecord, false /* deferResume */, getDisplayId());
1177 activityRecord.frozenBeforeDestroy = true;
1178 if (!kept) {
1179 mWmService.mAtmService.mRootActivityContainer.resumeFocusedStacksTopActivities();
1180 }
1181 } else {
1182 // We have a new configuration to push so we need to update ATMS for now.
1183 // TODO: Clean up display configuration push between ATMS and WMS after unification.
1184 mWmService.mAtmService.updateDisplayOverrideConfigurationLocked(
1185 config, null /* starting */, false /* deferResume */, getDisplayId());
1186 }
1187 return handled;
1188 }
1189
Riddle Hsu4e611772018-10-31 18:58:28 +08001190 /**
1191 * Determine the new desired orientation of this display.
1192 *
1193 * The orientation is computed from non-application windows first. If none of the
1194 * non-application windows specify orientation, the orientation is computed from application
1195 * tokens.
1196 *
1197 * @return {@code true} if the orientation is changed.
1198 */
1199 boolean updateOrientationFromAppTokens() {
1200 return updateOrientationFromAppTokens(false /* forceUpdate */);
1201 }
1202
Garfield Tan90b04282018-12-11 14:04:42 -08001203 /**
1204 * Update orientation of the target display, returning a non-null new Configuration if it has
1205 * changed from the current orientation. If a non-null configuration is returned, someone must
1206 * call {@link WindowManagerService#setNewDisplayOverrideConfiguration(Configuration,
1207 * DisplayContent)} to tell the window manager it can unfreeze the screen. This will typically
1208 * be done by calling {@link WindowManagerService#sendNewConfiguration(int)}.
1209 */
1210 Configuration updateOrientationFromAppTokens(Configuration currentConfig,
1211 IBinder freezeDisplayToken, boolean forceUpdate) {
1212 if (!mDisplayReady) {
1213 return null;
1214 }
1215
1216 Configuration config = null;
1217 if (updateOrientationFromAppTokens(forceUpdate)) {
1218 // If we changed the orientation but mOrientationChangeComplete is already true,
1219 // we used seamless rotation, and we don't need to freeze the screen.
1220 if (freezeDisplayToken != null && !mWmService.mRoot.mOrientationChangeComplete) {
1221 final AppWindowToken atoken = getAppWindowToken(freezeDisplayToken);
1222 if (atoken != null) {
1223 atoken.startFreezingScreen();
1224 }
1225 }
1226 config = new Configuration();
1227 computeScreenConfiguration(config);
1228 } else if (currentConfig != null) {
1229 // No obvious action we need to take, but if our current state mismatches the
1230 // activity manager's, update it, disregarding font scale, which should remain set
1231 // to the value of the previous configuration.
1232 // Here we're calling Configuration#unset() instead of setToDefaults() because we
1233 // need to keep override configs clear of non-empty values (e.g. fontSize).
1234 mTmpConfiguration.unset();
1235 mTmpConfiguration.updateFrom(currentConfig);
1236 computeScreenConfiguration(mTmpConfiguration);
1237 if (currentConfig.diff(mTmpConfiguration) != 0) {
1238 mWaitingForConfig = true;
1239 setLayoutNeeded();
1240 int[] anim = new int[2];
1241 getDisplayPolicy().selectRotationAnimationLw(anim);
1242
1243 mWmService.startFreezingDisplayLocked(anim[0], anim[1], this);
1244 config = new Configuration(mTmpConfiguration);
1245 }
1246 }
1247
1248 return config;
1249 }
1250
1251
1252 private boolean updateOrientationFromAppTokens(boolean forceUpdate) {
Riddle Hsu4e611772018-10-31 18:58:28 +08001253 final int req = getOrientation();
1254 if (req != mLastOrientation || forceUpdate) {
1255 mLastOrientation = req;
1256 mDisplayRotation.setCurrentOrientation(req);
1257 return updateRotationUnchecked(forceUpdate);
1258 }
1259 return false;
1260 }
1261
Riddle Hsuad256a12018-07-18 16:11:30 +08001262 /**
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001263 * Update rotation of the display and send configuration if the rotation is changed.
1264 *
1265 * @return {@code true} if the rotation has been changed and the new config is sent.
1266 */
1267 boolean updateRotationAndSendNewConfigIfNeeded() {
1268 final boolean changed = updateRotationUnchecked(false /* forceUpdate */);
1269 if (changed) {
Riddle Hsu4e611772018-10-31 18:58:28 +08001270 sendNewConfiguration();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001271 }
1272 return changed;
1273 }
1274
1275 /**
Andrii Kulian06d07d62017-03-14 11:11:47 -07001276 * Update rotation of the display.
1277 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001278 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
1279 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001280 */
Robert Carrae606b42018-02-15 15:36:23 -08001281 boolean updateRotationUnchecked() {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001282 return updateRotationUnchecked(false /* forceUpdate */);
1283 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001284
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001285 /**
Evan Roskye747c3e2018-10-30 20:06:41 -07001286 * Update rotation of the DisplayContent with an option to force the update. This updates
1287 * the container's perception of rotation and, depending on the top activities, will freeze
1288 * the screen or start seamless rotation. The display itself gets rotated in
1289 * {@link #applyRotationLocked} during {@link WindowManagerService#sendNewConfiguration}.
1290 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001291 * @param forceUpdate Force the rotation update. Sometimes in WM we might skip updating
1292 * orientation because we're waiting for some rotation to finish or display
1293 * to unfreeze, which results in configuration of the previously visible
1294 * activity being applied to a newly visible one. Forcing the rotation
1295 * update allows to workaround this issue.
1296 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
Evan Roskye747c3e2018-10-30 20:06:41 -07001297 * {@link WindowManagerService#sendNewConfiguration(int)} TO COMPLETE THE ROTATION AND
1298 * UNFREEZE THE SCREEN.
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001299 */
1300 boolean updateRotationUnchecked(boolean forceUpdate) {
1301 ScreenRotationAnimation screenRotationAnimation;
1302 if (!forceUpdate) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001303 if (mDeferredRotationPauseCount > 0) {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001304 // Rotation updates have been paused temporarily. Defer the update until
1305 // updates have been resumed.
1306 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
1307 return false;
1308 }
1309
1310 screenRotationAnimation =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001311 mWmService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001312 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
1313 // Rotation updates cannot be performed while the previous rotation change
1314 // animation is still in progress. Skip this update. We will try updating
1315 // again after the animation is finished and the display is unfrozen.
1316 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
1317 return false;
1318 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001319 if (mWmService.mDisplayFrozen) {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001320 // Even if the screen rotation animation has finished (e.g. isAnimating
1321 // returns false), there is still some time where we haven't yet unfrozen
1322 // the display. We also need to abort rotation here.
1323 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
1324 "Deferring rotation, still finishing previous rotation");
1325 return false;
1326 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001327 }
1328
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001329 if (!mWmService.mDisplayEnabled) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001330 // No point choosing a rotation if the display is not enabled.
1331 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
1332 return false;
1333 }
1334
1335 final int oldRotation = mRotation;
1336 final int lastOrientation = mLastOrientation;
1337 final boolean oldAltOrientation = mAltOrientation;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001338 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001339 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Computed rotation=" + rotation + " for display id="
1340 + mDisplayId + " based on lastOrientation=" + lastOrientation
1341 + " and oldRotation=" + oldRotation);
Tiger Huang7c610aa2018-10-27 00:01:01 +08001342 boolean mayRotateSeamlessly = mDisplayPolicy.shouldRotateSeamlessly(mDisplayRotation,
Riddle Hsuad256a12018-07-18 16:11:30 +08001343 oldRotation, rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001344
Robert Carr0e007272017-10-02 13:00:55 -07001345 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001346 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001347 if (seamlessRotated != null && !forceUpdate) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001348 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
1349 // to complete (that is, waiting for windows to redraw). It's tempting to check
1350 // w.mSeamlessRotationCount but that could be incorrect in the case of
1351 // window-removal.
1352 return false;
1353 }
Robert Carr0e007272017-10-02 13:00:55 -07001354
1355 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001356 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -07001357 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -07001358 mayRotateSeamlessly = false;
1359 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001360 for (int i = 0; i < mWmService.mSessions.size(); i++) {
1361 if (mWmService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
Robert Carr0e007272017-10-02 13:00:55 -07001362 mayRotateSeamlessly = false;
1363 break;
1364 }
1365 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001366 }
Robert Carr0e007272017-10-02 13:00:55 -07001367 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001368
1369 // TODO: Implement forced rotation changes.
1370 // Set mAltOrientation to indicate that the application is receiving
1371 // an orientation that has different metrics than it expected.
1372 // eg. Portrait instead of Landscape.
1373
Riddle Hsuad256a12018-07-18 16:11:30 +08001374 final boolean altOrientation = !mDisplayRotation.rotationHasCompatibleMetrics(
Andrii Kulian06d07d62017-03-14 11:11:47 -07001375 lastOrientation, rotation);
1376
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001377 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1378 + " selected orientation " + lastOrientation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001379 + ", got rotation " + rotation + " which has "
1380 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1381
1382 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1383 // No change.
1384 return false;
1385 }
1386
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001387 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1388 + " rotation changed to " + rotation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001389 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1390 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1391
1392 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
Vishnu Nairba183352018-11-21 11:16:49 -08001393 mWaitingForConfig = true;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001394 }
1395
Evan Roskye747c3e2018-10-30 20:06:41 -07001396 mRotation = rotation;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001397 mAltOrientation = altOrientation;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001398
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001399 mWmService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1400 mWmService.mH.sendNewMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
Riddle Hsu654a6f92018-07-13 22:59:36 +08001401 this, WINDOW_FREEZE_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001402
1403 setLayoutNeeded();
1404 final int[] anim = new int[2];
Tiger Huang7c610aa2018-10-27 00:01:01 +08001405 mDisplayPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001406
1407 if (!rotateSeamlessly) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001408 mWmService.startFreezingDisplayLocked(anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001409 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001410 } else {
1411 // The screen rotation animation uses a screenshot to freeze the screen
1412 // while windows resize underneath.
1413 // When we are rotating seamlessly, we allow the elements to transition
1414 // to their rotated state independently and without a freeze required.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001415 mWmService.startSeamlessRotation();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001416 }
1417
Evan Roskye747c3e2018-10-30 20:06:41 -07001418 return true;
1419 }
1420
1421 /**
1422 * Applies the rotation transaction. This must be called after {@link #updateRotationUnchecked}
1423 * (if it returned {@code true}) to actually finish the rotation.
1424 *
1425 * @param oldRotation the rotation we are coming from.
1426 * @param rotation the rotation to apply.
1427 */
1428 void applyRotationLocked(final int oldRotation, final int rotation) {
1429 mDisplayRotation.setRotation(rotation);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001430 final boolean rotateSeamlessly = mWmService.isRotatingSeamlessly();
Evan Roskye747c3e2018-10-30 20:06:41 -07001431 ScreenRotationAnimation screenRotationAnimation = rotateSeamlessly
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001432 ? null : mWmService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001433 // We need to update our screen size information to match the new rotation. If the rotation
1434 // has actually changed then this method will return true and, according to the comment at
1435 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1436 // By updating the Display info here it will be available to
1437 // #computeScreenConfiguration() later.
1438 updateDisplayAndOrientation(getConfiguration().uiMode);
1439
Robert Carrae606b42018-02-15 15:36:23 -08001440 // NOTE: We disable the rotation in the emulator because
1441 // it doesn't support hardware OpenGL emulation yet.
1442 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1443 && screenRotationAnimation.hasScreenshot()) {
1444 if (screenRotationAnimation.setRotation(getPendingTransaction(), rotation,
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001445 MAX_ANIMATION_DURATION, mWmService.getTransitionAnimationScaleLocked(),
Robert Carrae606b42018-02-15 15:36:23 -08001446 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001447 mWmService.scheduleAnimationLocked();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001448 }
1449 }
1450
Vishnu Nair83537a72018-07-19 21:27:48 -07001451 forAllWindows(w -> {
Vishnu Nairddd80742018-08-21 14:12:46 -07001452 w.seamlesslyRotateIfAllowed(getPendingTransaction(), oldRotation, rotation,
1453 rotateSeamlessly);
Vishnu Nair83537a72018-07-19 21:27:48 -07001454 }, true /* traverseTopToBottom */);
1455
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001456 mWmService.mDisplayManagerInternal.performTraversal(getPendingTransaction());
Robert Carrae606b42018-02-15 15:36:23 -08001457 scheduleAnimation();
1458
Andrii Kulian06d07d62017-03-14 11:11:47 -07001459 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001460 if (w.mHasSurface && !rotateSeamlessly) {
1461 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001462 w.setOrientationChanging(true);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001463 mWmService.mRoot.mOrientationChangeComplete = false;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001464 w.mLastFreezeDuration = 0;
1465 }
1466 w.mReportOrientationChanged = true;
1467 }, true /* traverseTopToBottom */);
1468
1469 if (rotateSeamlessly) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001470 mWmService.mH.sendNewMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
Riddle Hsu654a6f92018-07-13 22:59:36 +08001471 this, SEAMLESS_ROTATION_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001472 }
1473
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001474 for (int i = mWmService.mRotationWatchers.size() - 1; i >= 0; i--) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001475 final WindowManagerService.RotationWatcher rotationWatcher
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001476 = mWmService.mRotationWatchers.get(i);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001477 if (rotationWatcher.mDisplayId == mDisplayId) {
1478 try {
1479 rotationWatcher.mWatcher.onRotationChanged(rotation);
1480 } catch (RemoteException e) {
1481 // Ignore
1482 }
1483 }
1484 }
1485
1486 // TODO (multi-display): Magnification is supported only for the default display.
1487 // Announce rotation only if we will not animate as we already have the
1488 // windows in final state. Otherwise, we make this call at the rotation end.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001489 if (screenRotationAnimation == null && mWmService.mAccessibilityController != null
Andrii Kulian06d07d62017-03-14 11:11:47 -07001490 && isDefaultDisplay) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001491 mWmService.mAccessibilityController.onRotationChangedLocked(this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001492 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001493 }
1494
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001495 void configureDisplayPolicy() {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001496 final int width = mBaseDisplayWidth;
1497 final int height = mBaseDisplayHeight;
1498 final int shortSize;
1499 final int longSize;
1500 if (width > height) {
1501 shortSize = height;
1502 longSize = width;
1503 } else {
1504 shortSize = width;
1505 longSize = height;
1506 }
1507
1508 final int shortSizeDp = shortSize * DisplayMetrics.DENSITY_DEFAULT / mBaseDisplayDensity;
1509 final int longSizeDp = longSize * DisplayMetrics.DENSITY_DEFAULT / mBaseDisplayDensity;
1510
1511 mDisplayRotation.configure(width, height, shortSizeDp, longSizeDp);
1512 mDisplayPolicy.configure(width, height, shortSizeDp);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001513
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001514 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
1515 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Arthur Hungbe5ce212018-09-13 18:41:56 +08001516
1517 // Tap Listeners are supported for:
1518 // 1. All physical displays (multi-display).
1519 // 2. VirtualDisplays on VR, AA (and everything else).
1520 if (mPointerEventDispatcher != null && mTapDetector == null) {
1521 if (DEBUG_DISPLAY) {
1522 Slog.d(TAG,
1523 "Registering PointerEventListener for DisplayId: " + mDisplayId);
1524 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001525 mTapDetector = new TaskTapPointerEventListener(mWmService, this);
Arthur Hungbe5ce212018-09-13 18:41:56 +08001526 registerPointerEventListener(mTapDetector);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001527 registerPointerEventListener(mWmService.mMousePositionTracker);
Arthur Hungbe5ce212018-09-13 18:41:56 +08001528 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001529 }
1530
Andrii Kulian06d07d62017-03-14 11:11:47 -07001531 /**
1532 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1533 * changed.
1534 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1535 */
1536 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1537 // Use the effective "visual" dimensions based on current rotation
1538 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1539 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1540 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1541 int dw = realdw;
1542 int dh = realdh;
1543
1544 if (mAltOrientation) {
1545 if (realdw > realdh) {
1546 // Turn landscape into portrait.
1547 int maxw = (int)(realdh/1.3f);
1548 if (maxw < realdw) {
1549 dw = maxw;
1550 }
1551 } else {
1552 // Turn portrait into landscape.
1553 int maxh = (int)(realdw/1.3f);
1554 if (maxh < realdh) {
1555 dh = maxh;
1556 }
1557 }
1558 }
1559
1560 // Update application display metrics.
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001561 final WmDisplayCutout wmDisplayCutout = calculateDisplayCutoutForRotation(mRotation);
1562 final DisplayCutout displayCutout = wmDisplayCutout.getDisplayCutout();
1563
Tiger Huang7c610aa2018-10-27 00:01:01 +08001564 final int appWidth = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1565 displayCutout);
1566 final int appHeight = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1567 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001568 mDisplayInfo.rotation = mRotation;
1569 mDisplayInfo.logicalWidth = dw;
1570 mDisplayInfo.logicalHeight = dh;
1571 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1572 mDisplayInfo.appWidth = appWidth;
1573 mDisplayInfo.appHeight = appHeight;
1574 if (isDefaultDisplay) {
1575 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1576 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1577 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001578 mDisplayInfo.displayCutout = displayCutout.isEmpty() ? null : displayCutout;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001579 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1580 if (mDisplayScalingDisabled) {
1581 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1582 } else {
1583 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1584 }
1585
Andrii Kulianf0379de2018-03-14 16:24:07 -07001586 // We usually set the override info in DisplayManager so that we get consistent display
1587 // metrics values when displays are changing and don't send out new values until WM is aware
1588 // of them. However, we don't do this for displays that serve as containers for ActivityView
1589 // because we don't want letter-/pillar-boxing during resize.
1590 final DisplayInfo overrideDisplayInfo = mShouldOverrideDisplayConfiguration
1591 ? mDisplayInfo : null;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001592 mWmService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
Andrii Kulianf0379de2018-03-14 16:24:07 -07001593 overrideDisplayInfo);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001594
1595 mBaseDisplayRect.set(0, 0, dw, dh);
1596
1597 if (isDefaultDisplay) {
1598 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1599 mCompatDisplayMetrics);
1600 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001601
Andrii Kulian06d07d62017-03-14 11:11:47 -07001602 return mDisplayInfo;
1603 }
1604
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001605 WmDisplayCutout calculateDisplayCutoutForRotation(int rotation) {
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001606 return mDisplayCutoutCache.getOrCompute(mInitialDisplayCutout, rotation);
1607 }
1608
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001609 private WmDisplayCutout calculateDisplayCutoutForRotationUncached(
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001610 DisplayCutout cutout, int rotation) {
Adrian Roos24264212018-02-19 16:26:15 +01001611 if (cutout == null || cutout == DisplayCutout.NO_CUTOUT) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001612 return WmDisplayCutout.NO_CUTOUT;
Adrian Roos1cf585052018-01-03 18:43:27 +01001613 }
Adrian Roos11c25582018-02-19 18:06:36 +01001614 if (rotation == ROTATION_0) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001615 return WmDisplayCutout.computeSafeInsets(
1616 cutout, mInitialDisplayWidth, mInitialDisplayHeight);
Adrian Roos24264212018-02-19 16:26:15 +01001617 }
Adrian Roosbed538e2018-02-21 17:50:07 +01001618 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Issei Suzuki43190bd2018-08-20 17:28:41 +02001619 final Rect[] newBounds = mRotationUtil.getRotatedBounds(
1620 WmDisplayCutout.computeSafeInsets(
Jorim Jaggi60640512018-06-29 01:14:31 +02001621 cutout, mInitialDisplayWidth, mInitialDisplayHeight)
Issei Suzuki43190bd2018-08-20 17:28:41 +02001622 .getDisplayCutout().getBoundingRectsAll(),
1623 rotation, mInitialDisplayWidth, mInitialDisplayHeight);
1624 return WmDisplayCutout.computeSafeInsets(DisplayCutout.fromBounds(newBounds),
Adrian Roos24264212018-02-19 16:26:15 +01001625 rotated ? mInitialDisplayHeight : mInitialDisplayWidth,
1626 rotated ? mInitialDisplayWidth : mInitialDisplayHeight);
Adrian Roos1cf585052018-01-03 18:43:27 +01001627 }
1628
Andrii Kulian06d07d62017-03-14 11:11:47 -07001629 /**
1630 * Compute display configuration based on display properties and policy settings.
1631 * Do not call if mDisplayReady == false.
1632 */
1633 void computeScreenConfiguration(Configuration config) {
1634 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
Evan Roskye747c3e2018-10-30 20:06:41 -07001635 calculateBounds(displayInfo, mTmpBounds);
1636 config.windowConfiguration.setBounds(mTmpBounds);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001637
1638 final int dw = displayInfo.logicalWidth;
1639 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001640 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
Garfield Tane0846042018-07-26 13:42:04 -07001641 config.windowConfiguration.setWindowingMode(getWindowingMode());
Yunfan Chen7daa6ac2018-11-29 18:16:44 -08001642 config.windowConfiguration.setDisplayWindowingMode(getWindowingMode());
Evan Roskye747c3e2018-10-30 20:06:41 -07001643 config.windowConfiguration.setRotation(displayInfo.rotation);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001644
Adrian Roos11c25582018-02-19 18:06:36 +01001645 final float density = mDisplayMetrics.density;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001646 config.screenWidthDp =
Tiger Huang7c610aa2018-10-27 00:01:01 +08001647 (int)(mDisplayPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1648 config.uiMode, displayInfo.displayCutout) / density);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001649 config.screenHeightDp =
Tiger Huang7c610aa2018-10-27 00:01:01 +08001650 (int)(mDisplayPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1651 config.uiMode, displayInfo.displayCutout) / density);
Bryce Lee7566d762017-03-30 09:34:15 -07001652
Tiger Huang7c610aa2018-10-27 00:01:01 +08001653 mDisplayPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh,
Adrian Roos11c25582018-02-19 18:06:36 +01001654 displayInfo.displayCutout, mTmpRect);
Bryce Lee7566d762017-03-30 09:34:15 -07001655 final int leftInset = mTmpRect.left;
1656 final int topInset = mTmpRect.top;
1657 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001658 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1659 leftInset + displayInfo.appWidth /* right */,
1660 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001661 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1662 || displayInfo.rotation == Surface.ROTATION_270);
1663
Tiger Huang7c610aa2018-10-27 00:01:01 +08001664 computeSizeRangesAndScreenLayout(displayInfo, rotated, config.uiMode, dw, dh, density,
1665 config);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001666
1667 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1668 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1669 ? Configuration.SCREENLAYOUT_ROUND_YES
1670 : Configuration.SCREENLAYOUT_ROUND_NO);
1671
1672 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1673 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1674 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001675 dh, displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001676 config.densityDpi = displayInfo.logicalDensityDpi;
1677
1678 config.colorMode =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001679 ((displayInfo.isHdr() && mWmService.hasHdrSupport())
Andrii Kulian06d07d62017-03-14 11:11:47 -07001680 ? Configuration.COLOR_MODE_HDR_YES
1681 : Configuration.COLOR_MODE_HDR_NO)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001682 | (displayInfo.isWideColorGamut() && mWmService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001683 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1684 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1685
1686 // Update the configuration based on available input devices, lid switch,
1687 // and platform configuration.
1688 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1689 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1690 config.navigation = Configuration.NAVIGATION_NONAV;
1691
1692 int keyboardPresence = 0;
1693 int navigationPresence = 0;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001694 final InputDevice[] devices = mWmService.mInputManager.getInputDevices();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001695 final int len = devices != null ? devices.length : 0;
1696 for (int i = 0; i < len; i++) {
1697 InputDevice device = devices[i];
1698 if (!device.isVirtual()) {
1699 final int sources = device.getSources();
1700 final int presenceFlag = device.isExternal() ?
1701 WindowManagerPolicy.PRESENCE_EXTERNAL :
1702 WindowManagerPolicy.PRESENCE_INTERNAL;
1703
1704 // TODO(multi-display): Configure on per-display basis.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001705 if (mWmService.mIsTouchDevice) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001706 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1707 InputDevice.SOURCE_TOUCHSCREEN) {
1708 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1709 }
1710 } else {
1711 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1712 }
1713
1714 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1715 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1716 navigationPresence |= presenceFlag;
1717 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1718 && config.navigation == Configuration.NAVIGATION_NONAV) {
1719 config.navigation = Configuration.NAVIGATION_DPAD;
1720 navigationPresence |= presenceFlag;
1721 }
1722
1723 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1724 config.keyboard = Configuration.KEYBOARD_QWERTY;
1725 keyboardPresence |= presenceFlag;
1726 }
1727 }
1728 }
1729
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001730 if (config.navigation == Configuration.NAVIGATION_NONAV && mWmService.mHasPermanentDpad) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001731 config.navigation = Configuration.NAVIGATION_DPAD;
1732 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1733 }
1734
1735 // Determine whether a hard keyboard is available and enabled.
1736 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1737 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001738 if (hardKeyboardAvailable != mWmService.mHardKeyboardAvailable) {
1739 mWmService.mHardKeyboardAvailable = hardKeyboardAvailable;
1740 mWmService.mH.removeMessages(REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1741 mWmService.mH.sendEmptyMessage(REPORT_HARD_KEYBOARD_STATUS_CHANGE);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001742 }
1743
Tiger Huang7c610aa2018-10-27 00:01:01 +08001744 mDisplayPolicy.updateConfigurationDependentBehaviors();
1745
Andrii Kulian06d07d62017-03-14 11:11:47 -07001746 // Let the policy update hidden states.
1747 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1748 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1749 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001750 mWmService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001751 }
1752
1753 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001754 DisplayCutout displayCutout) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001755 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1756 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1757 final int unrotDw, unrotDh;
1758 if (rotated) {
1759 unrotDw = dh;
1760 unrotDh = dw;
1761 } else {
1762 unrotDw = dw;
1763 unrotDh = dh;
1764 }
1765 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001766 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001767 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001768 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001769 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001770 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001771 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001772 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001773 return sw;
1774 }
1775
1776 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001777 DisplayMetrics dm, int dw, int dh, DisplayCutout displayCutout) {
1778 dm.noncompatWidthPixels = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1779 displayCutout);
1780 dm.noncompatHeightPixels = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode,
1781 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001782 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1783 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1784 if (curSize == 0 || size < curSize) {
1785 curSize = size;
1786 }
1787 return curSize;
1788 }
1789
Tiger Huang7c610aa2018-10-27 00:01:01 +08001790 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, boolean rotated,
1791 int uiMode, int dw, int dh, float density, Configuration outConfig) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001792
1793 // We need to determine the smallest width that will occur under normal
1794 // operation. To this, start with the base screen size and compute the
1795 // width under the different possible rotations. We need to un-rotate
1796 // the current screen dimensions before doing this.
1797 int unrotDw, unrotDh;
1798 if (rotated) {
1799 unrotDw = dh;
1800 unrotDh = dw;
1801 } else {
1802 unrotDw = dw;
1803 unrotDh = dh;
1804 }
1805 displayInfo.smallestNominalAppWidth = 1<<30;
1806 displayInfo.smallestNominalAppHeight = 1<<30;
1807 displayInfo.largestNominalAppWidth = 0;
1808 displayInfo.largestNominalAppHeight = 0;
Tiger Huang7c610aa2018-10-27 00:01:01 +08001809 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_0, uiMode, unrotDw, unrotDh);
1810 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_90, uiMode, unrotDh, unrotDw);
1811 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_180, uiMode, unrotDw, unrotDh);
1812 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_270, uiMode, unrotDh, unrotDw);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001813 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1814 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001815 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001816 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001817 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001818 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001819 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001820 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001821 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001822 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1823 outConfig.screenLayout = sl;
1824 }
1825
1826 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001827 int uiMode, DisplayCutout displayCutout) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001828 // Get the app screen size at this rotation.
Tiger Huang7c610aa2018-10-27 00:01:01 +08001829 int w = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayCutout);
1830 int h = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001831
1832 // Compute the screen layout size class for this rotation.
1833 int longSize = w;
1834 int shortSize = h;
1835 if (longSize < shortSize) {
1836 int tmp = longSize;
1837 longSize = shortSize;
1838 shortSize = tmp;
1839 }
1840 longSize = (int)(longSize/density);
1841 shortSize = (int)(shortSize/density);
1842 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1843 }
1844
Tiger Huang7c610aa2018-10-27 00:01:01 +08001845 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int rotation,
Andrii Kulian06d07d62017-03-14 11:11:47 -07001846 int uiMode, int dw, int dh) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001847 final DisplayCutout displayCutout = calculateDisplayCutoutForRotation(
1848 rotation).getDisplayCutout();
Tiger Huang7c610aa2018-10-27 00:01:01 +08001849 final int width = mDisplayPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1850 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001851 if (width < displayInfo.smallestNominalAppWidth) {
1852 displayInfo.smallestNominalAppWidth = width;
1853 }
1854 if (width > displayInfo.largestNominalAppWidth) {
1855 displayInfo.largestNominalAppWidth = width;
1856 }
Tiger Huang7c610aa2018-10-27 00:01:01 +08001857 final int height = mDisplayPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1858 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001859 if (height < displayInfo.smallestNominalAppHeight) {
1860 displayInfo.smallestNominalAppHeight = height;
1861 }
1862 if (height > displayInfo.largestNominalAppHeight) {
1863 displayInfo.largestNominalAppHeight = height;
1864 }
1865 }
1866
Riddle Hsua4d6fa22018-08-11 00:50:39 +08001867 /**
1868 * Apps that use the compact menu panel (as controlled by the panelMenuIsCompact
1869 * theme attribute) on devices that feature a physical options menu key attempt to position
1870 * their menu panel window along the edge of the screen nearest the physical menu key.
1871 * This lowers the travel distance between invoking the menu panel and selecting
1872 * a menu option.
1873 *
1874 * This method helps control where that menu is placed. Its current implementation makes
1875 * assumptions about the menu key and its relationship to the screen based on whether
1876 * the device's natural orientation is portrait (width < height) or landscape.
1877 *
1878 * The menu key is assumed to be located along the bottom edge of natural-portrait
1879 * devices and along the right edge of natural-landscape devices. If these assumptions
1880 * do not hold for the target device, this method should be changed to reflect that.
1881 *
1882 * @return A {@link Gravity} value for placing the options menu window.
1883 */
1884 int getPreferredOptionsPanelGravity() {
1885 final int rotation = getRotation();
1886 if (mInitialDisplayWidth < mInitialDisplayHeight) {
1887 // On devices with a natural orientation of portrait.
1888 switch (rotation) {
1889 default:
1890 case Surface.ROTATION_0:
1891 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1892 case Surface.ROTATION_90:
1893 return Gravity.RIGHT | Gravity.BOTTOM;
1894 case Surface.ROTATION_180:
1895 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1896 case Surface.ROTATION_270:
1897 return Gravity.START | Gravity.BOTTOM;
1898 }
1899 }
1900
1901 // On devices with a natural orientation of landscape.
1902 switch (rotation) {
1903 default:
1904 case Surface.ROTATION_0:
1905 return Gravity.RIGHT | Gravity.BOTTOM;
1906 case Surface.ROTATION_90:
1907 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1908 case Surface.ROTATION_180:
1909 return Gravity.START | Gravity.BOTTOM;
1910 case Surface.ROTATION_270:
1911 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1912 }
1913 }
1914
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001915 DockedStackDividerController getDockedDividerController() {
1916 return mDividerControllerLocked;
1917 }
1918
Winson Chung655332c2016-10-31 13:14:28 -07001919 PinnedStackController getPinnedStackController() {
1920 return mPinnedStackControllerLocked;
1921 }
1922
Jeff Browna506a6e2013-06-04 00:02:38 -07001923 /**
1924 * Returns true if the specified UID has access to this display.
1925 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001926 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001927 return mDisplay.hasAccess(uid);
1928 }
1929
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001930 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001931 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001932 }
1933
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001934 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001935 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001936 }
1937
Wale Ogunwale61911492017-10-11 08:50:50 -07001938 /**
1939 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1940 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001941 TaskStack getSplitScreenPrimaryStack() {
1942 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001943 return (stack != null && stack.isVisible()) ? stack : null;
1944 }
1945
Robert Carr9785cf32018-04-25 15:06:07 -07001946 boolean hasSplitScreenPrimaryStack() {
1947 return getSplitScreenPrimaryStack() != null;
1948 }
1949
Wale Ogunwale61911492017-10-11 08:50:50 -07001950 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001951 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001952 * not visible.
1953 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001954 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1955 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001956 }
1957
1958 TaskStack getPinnedStack() {
1959 return mTaskStackContainers.getPinnedStack();
1960 }
1961
1962 private boolean hasPinnedStack() {
1963 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001964 }
1965
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001966 /**
1967 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1968 * Null is no compatible stack on the display.
1969 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001970 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001971 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1972 }
1973
1974 /**
1975 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1976 * activity type. Null is no compatible stack on the display.
1977 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001978 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001979 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001980 }
1981
Bryce Lee48f4b572017-04-10 10:54:15 -07001982 @VisibleForTesting
Kazuki Takise148d00a2018-05-31 15:32:19 +09001983 WindowList<TaskStack> getStacks() {
1984 return mTaskStackContainers.mChildren;
1985 }
1986
1987 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001988 TaskStack getTopStack() {
1989 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001990 }
1991
Winson Chunge2d72172018-01-25 17:46:20 +00001992 ArrayList<Task> getVisibleTasks() {
1993 return mTaskStackContainers.getVisibleTasks();
1994 }
1995
Wale Ogunwale61911492017-10-11 08:50:50 -07001996 void onStackWindowingModeChanged(TaskStack stack) {
1997 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001998 }
1999
Andrii Kulian441e4492016-09-29 15:25:00 -07002000 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07002001 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07002002 super.onConfigurationChanged(newParentConfig);
Tiger Huang7c610aa2018-10-27 00:01:01 +08002003 if (mDisplayPolicy != null) {
2004 mDisplayPolicy.onConfigurationChanged();
2005 }
Andrii Kulian441e4492016-09-29 15:25:00 -07002006
Evan Roskye747c3e2018-10-30 20:06:41 -07002007 // If there was no pinned stack, we still need to notify the controller of the display info
2008 // update as a result of the config change.
2009 if (mPinnedStackControllerLocked != null && !hasPinnedStack()) {
2010 mPinnedStackControllerLocked.onDisplayInfoChanged(getDisplayInfo());
2011 }
Evan Roskye747c3e2018-10-30 20:06:41 -07002012 }
2013
2014 /**
2015 * Updates the resources used by docked/pinned controllers. This needs to be called at the
2016 * beginning of a configuration update cascade since the metrics from these resources are used
2017 * for bounds calculations. Since ActivityDisplay initiates the configuration update, this
2018 * should be called from there instead of DisplayContent's onConfigurationChanged.
2019 */
2020 void preOnConfigurationChanged() {
Bryce Leef3c6a472017-11-14 14:53:06 -08002021 final DockedStackDividerController dividerController = getDockedDividerController();
2022
2023 if (dividerController != null) {
2024 getDockedDividerController().onConfigurationChanged();
2025 }
2026
2027 final PinnedStackController pinnedStackController = getPinnedStackController();
2028
2029 if (pinnedStackController != null) {
2030 getPinnedStackController().onConfigurationChanged();
2031 }
Andrii Kulian441e4492016-09-29 15:25:00 -07002032 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07002033
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07002034 @Override
2035 boolean fillsParent() {
2036 return true;
2037 }
2038
2039 @Override
2040 boolean isVisible() {
2041 return true;
2042 }
2043
2044 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002045 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002046 super.onAppTransitionDone();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002047 mWmService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002048 }
2049
Yunfan Chen7daa6ac2018-11-29 18:16:44 -08002050 @Override
2051 public void setWindowingMode(int windowingMode) {
2052 super.setWindowingMode(windowingMode);
2053 super.setDisplayWindowingMode(windowingMode);
2054 }
2055
2056 @Override
2057 void setDisplayWindowingMode(int windowingMode) {
2058 setWindowingMode(windowingMode);
2059 }
2060
Robert Carr9785cf32018-04-25 15:06:07 -07002061 /**
2062 * In split-screen mode we process the IME containers above the docked divider
2063 * rather than directly above their target.
2064 */
2065 private boolean skipTraverseChild(WindowContainer child) {
lumarkff0ab692018-11-05 20:32:30 +08002066 if (child == mImeWindowsContainers && mInputMethodTarget != null
Robert Carr9785cf32018-04-25 15:06:07 -07002067 && !hasSplitScreenPrimaryStack()) {
2068 return true;
2069 }
2070 return false;
2071 }
2072
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07002073 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08002074 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
2075 // Special handling so we can process IME windows with #forAllImeWindows above their IME
2076 // target, or here in order if there isn't an IME target.
2077 if (traverseTopToBottom) {
2078 for (int i = mChildren.size() - 1; i >= 0; --i) {
2079 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07002080 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08002081 continue;
2082 }
Robert Carr9785cf32018-04-25 15:06:07 -07002083
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08002084 if (child.forAllWindows(callback, traverseTopToBottom)) {
2085 return true;
2086 }
2087 }
2088 } else {
2089 final int count = mChildren.size();
2090 for (int i = 0; i < count; i++) {
2091 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07002092 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08002093 continue;
2094 }
Robert Carr9785cf32018-04-25 15:06:07 -07002095
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08002096 if (child.forAllWindows(callback, traverseTopToBottom)) {
2097 return true;
2098 }
2099 }
2100 }
2101 return false;
2102 }
2103
2104 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
2105 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
2106 }
2107
Wale Ogunwale399c8692017-05-08 14:22:42 -07002108 @Override
2109 int getOrientation() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002110 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002111
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002112 if (mWmService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08002113 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07002114 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
2115 + " is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002116 // If the display is frozen, some activities may be in the middle of restarting, and
2117 // thus have removed their old window. If the window has the flag to hide the lock
2118 // screen, then the lock screen can re-appear and inflict its own orientation on us.
2119 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08002120 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002121 } else if (policy.isKeyguardLocked()) {
2122 // Use the last orientation the while the display is frozen with the keyguard
2123 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
2124 // window. We don't want to check the show when locked window directly though as
2125 // things aren't stable while the display is frozen, for example the window could be
2126 // momentarily unavailable due to activity relaunch.
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07002127 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
2128 + " is frozen while keyguard locked, return " + mLastOrientation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08002129 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07002130 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002131 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002132 final int orientation = mAboveAppWindowsContainers.getOrientation();
2133 if (orientation != SCREEN_ORIENTATION_UNSET) {
2134 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002135 }
Wale Ogunwale51362492016-09-08 17:49:17 -07002136 }
2137
Wale Ogunwale399c8692017-05-08 14:22:42 -07002138 // Top system windows are not requesting an orientation. Start searching from apps.
2139 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07002140 }
2141
Craig Mautner46ac6fa2013-08-01 10:06:34 -07002142 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07002143 // Check if display metrics changed and update base values if needed.
2144 updateBaseDisplayMetricsIfNeeded();
2145
Craig Mautner722285e2012-09-07 13:55:58 -07002146 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07002147 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07002148
Garfield Tan2f145f22018-11-01 15:27:03 -07002149 onDisplayChanged(this);
Craig Mautner722285e2012-09-07 13:55:58 -07002150 }
2151
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002152 void initializeDisplayBaseInfo() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002153 final DisplayManagerInternal displayManagerInternal = mWmService.mDisplayManagerInternal;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002154 if (displayManagerInternal != null) {
2155 // Bootstrap the default logical display from the display manager.
2156 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
2157 if (newDisplayInfo != null) {
2158 mDisplayInfo.copyFrom(newDisplayInfo);
2159 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002160 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002161
Andrii Kuliancd097992017-03-23 18:31:59 -07002162 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
2163 mDisplayInfo.logicalDensityDpi);
2164 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
2165 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
2166 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002167 mInitialDisplayCutout = mDisplayInfo.displayCutout;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002168 }
2169
Andrii Kuliancd097992017-03-23 18:31:59 -07002170 /**
2171 * If display metrics changed, overrides are not set and it's not just a rotation - update base
2172 * values.
2173 */
2174 private void updateBaseDisplayMetricsIfNeeded() {
2175 // Get real display metrics without overrides from WM.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002176 mWmService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
Andrii Kuliancd097992017-03-23 18:31:59 -07002177 final int orientation = mDisplayInfo.rotation;
2178 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2179 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
2180 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
2181 final int newDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002182 final DisplayCutout newCutout = mDisplayInfo.displayCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07002183
2184 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
2185 || mInitialDisplayHeight != newHeight
Adrian Roos1cf585052018-01-03 18:43:27 +01002186 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi
2187 || !Objects.equals(mInitialDisplayCutout, newCutout);
Andrii Kuliancd097992017-03-23 18:31:59 -07002188
2189 if (displayMetricsChanged) {
2190 // Check if display size or density is forced.
2191 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
2192 || mBaseDisplayHeight != mInitialDisplayHeight;
2193 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
2194
2195 // If there is an override set for base values - use it, otherwise use new values.
2196 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
2197 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
2198 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
2199
2200 // Real display metrics changed, so we should also update initial values.
2201 mInitialDisplayWidth = newWidth;
2202 mInitialDisplayHeight = newHeight;
2203 mInitialDisplayDensity = newDensity;
Adrian Roos1cf585052018-01-03 18:43:27 +01002204 mInitialDisplayCutout = newCutout;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002205 mWmService.reconfigureDisplayLocked(this);
Andrii Kuliancd097992017-03-23 18:31:59 -07002206 }
2207 }
2208
Bryce Lee27cec322017-03-21 09:41:37 -07002209 /** Sets the maximum width the screen resolution can be */
2210 void setMaxUiWidth(int width) {
2211 if (DEBUG_DISPLAY) {
2212 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
2213 }
2214
2215 mMaxUiWidth = width;
2216
2217 // Update existing metrics.
2218 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
2219 }
2220
2221 /** Update base (override) display metrics. */
2222 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
2223 mBaseDisplayWidth = baseWidth;
2224 mBaseDisplayHeight = baseHeight;
2225 mBaseDisplayDensity = baseDensity;
2226
2227 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
2228 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
2229 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
2230 mBaseDisplayWidth = mMaxUiWidth;
2231
2232 if (DEBUG_DISPLAY) {
2233 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
2234 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
2235 + " on display:" + getDisplayId());
2236 }
2237 }
2238
2239 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08002240
2241 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07002242 }
2243
Riddle Hsuf53da812018-08-15 22:00:27 +08002244 /**
2245 * Forces this display to use the specified density.
2246 *
2247 * @param density The density in DPI to use. If the value equals to initial density, the setting
2248 * will be cleared.
2249 * @param userId The target user to apply. Only meaningful when this is default display. If the
2250 * user id is {@link UserHandle#USER_CURRENT}, it means to apply current settings
2251 * so only need to configure display.
2252 */
2253 void setForcedDensity(int density, int userId) {
2254 final boolean clear = density == mInitialDisplayDensity;
2255 final boolean updateCurrent = userId == UserHandle.USER_CURRENT;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002256 if (mWmService.mCurrentUserId == userId || updateCurrent) {
Riddle Hsuf53da812018-08-15 22:00:27 +08002257 mBaseDisplayDensity = density;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002258 mWmService.reconfigureDisplayLocked(this);
Riddle Hsuf53da812018-08-15 22:00:27 +08002259 }
2260 if (updateCurrent) {
2261 // We are applying existing settings so no need to save it again.
2262 return;
2263 }
2264
2265 if (density == mInitialDisplayDensity) {
2266 density = 0;
2267 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002268 mWmService.mDisplayWindowSettings.setForcedDensity(this, density, userId);
Riddle Hsuf53da812018-08-15 22:00:27 +08002269 }
2270
2271 /** @param mode {@link #FORCE_SCALING_MODE_AUTO} or {@link #FORCE_SCALING_MODE_DISABLED}. */
2272 void setForcedScalingMode(@ForceScalingMode int mode) {
2273 if (mode != FORCE_SCALING_MODE_DISABLED) {
2274 mode = FORCE_SCALING_MODE_AUTO;
2275 }
2276
2277 mDisplayScalingDisabled = (mode != FORCE_SCALING_MODE_AUTO);
2278 Slog.i(TAG_WM, "Using display scaling mode: " + (mDisplayScalingDisabled ? "off" : "auto"));
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002279 mWmService.reconfigureDisplayLocked(this);
Riddle Hsuf53da812018-08-15 22:00:27 +08002280
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002281 mWmService.mDisplayWindowSettings.setForcedScalingMode(this, mode);
Riddle Hsuf53da812018-08-15 22:00:27 +08002282 }
2283
2284 /** If the given width and height equal to initial size, the setting will be cleared. */
2285 void setForcedSize(int width, int height) {
2286 final boolean clear = mInitialDisplayWidth == width && mInitialDisplayHeight == height;
2287 if (!clear) {
2288 // Set some sort of reasonable bounds on the size of the display that we will try
2289 // to emulate.
2290 final int minSize = 200;
2291 final int maxScale = 2;
2292 width = Math.min(Math.max(width, minSize), mInitialDisplayWidth * maxScale);
2293 height = Math.min(Math.max(height, minSize), mInitialDisplayHeight * maxScale);
2294 }
2295
2296 Slog.i(TAG_WM, "Using new display size: " + width + "x" + height);
2297 updateBaseDisplayMetrics(width, height, mBaseDisplayDensity);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002298 mWmService.reconfigureDisplayLocked(this);
Riddle Hsuf53da812018-08-15 22:00:27 +08002299
2300 if (clear) {
2301 width = height = 0;
2302 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002303 mWmService.mDisplayWindowSettings.setForcedSize(this, width, height);
Riddle Hsuf53da812018-08-15 22:00:27 +08002304 }
2305
Wale Ogunwaledb506192017-12-08 10:57:32 -08002306 void getStableRect(Rect out) {
2307 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08002308 }
2309
Wale Ogunwale61911492017-10-11 08:50:50 -07002310 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002311 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
2312 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07002313
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002314 final TaskStack stack = new TaskStack(mWmService, stackId, controller);
Wale Ogunwale61911492017-10-11 08:50:50 -07002315 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002316 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07002317 }
2318
Andrii Kulian51c1b672017-04-07 18:39:32 -07002319 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002320 final DisplayContent prevDc = stack.getDisplayContent();
2321 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07002322 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
2323 + " which is not currently attached to any display");
2324 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002325 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07002326 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
2327 + " to its current displayId=" + mDisplayId);
2328 }
2329
lumark588a3e82018-07-20 18:53:54 +08002330 // Clean up all pending transitions when stack reparent to another display.
2331 stack.forAllAppWindows(AppWindowToken::removeFromPendingTransition);
2332
Wale Ogunwale61911492017-10-11 08:50:50 -07002333 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07002334 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08002335 }
2336
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002337 @Override
2338 protected void addChild(DisplayChildWindowContainer child,
2339 Comparator<DisplayChildWindowContainer> comparator) {
2340 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2341 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002342
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002343 @Override
2344 protected void addChild(DisplayChildWindowContainer child, int index) {
2345 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2346 }
2347
2348 @Override
2349 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002350 // Only allow removal of direct children from this display if the display is in the process
2351 // of been removed.
2352 if (mRemovingDisplay) {
2353 super.removeChild(child);
2354 return;
2355 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002356 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002357 }
2358
Andrii Kuliand2765632016-12-12 22:26:34 -08002359 @Override
2360 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
2361 // Children of the display are statically ordered, so the real intention here is to perform
2362 // the operation on the display and not the static direct children.
2363 getParent().positionChildAt(position, this, includingParents);
2364 }
2365
Riddle Hsu57831b52018-07-27 00:31:48 +08002366 void positionStackAt(int position, TaskStack child, boolean includingParents) {
2367 mTaskStackContainers.positionChildAt(position, child, includingParents);
Winson Chung59a47ded2018-01-25 17:46:06 +00002368 layoutAndAssignWindowLayersIfNeeded();
2369 }
2370
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002371 /**
2372 * Used to obtain task ID when user taps on coordinate (x, y) in this display, and outside
2373 * current task in focus.
2374 *
2375 * This returns the task ID of the foremost task at (x, y) if the task is not home. Otherwise it
2376 * returns -1.
2377 *
2378 * @param x horizontal coordinate of the tap position
2379 * @param y vertical coordinate of the tap position
2380 * @return the task ID if a non-home task is found; -1 if not
2381 */
2382 int taskForTapOutside(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002383 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2384 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Garfield Tanf2e0a9e2018-10-16 09:42:02 -07002385 if (stack.isActivityTypeHome() && !stack.inMultiWindowMode()) {
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002386 // We skip not only home stack, but also everything behind home because user can't
Garfield Tanf2e0a9e2018-10-16 09:42:02 -07002387 // see them when home stack is isn't in multi-window mode.
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002388 break;
2389 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002390 final int taskId = stack.taskIdFromPoint(x, y);
2391 if (taskId != -1) {
2392 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07002393 }
2394 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002395 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07002396 }
2397
Chong Zhang8e89b312015-09-09 15:09:30 -07002398 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08002399 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07002400 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07002401 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07002402 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002403 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002404 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07002405 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2406 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002407 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002408 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002409 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002410
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002411 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
2412 if (mTmpTaskForResizePointSearchResult.searchDone) {
2413 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07002414 }
2415 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002416 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002417 }
2418
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002419 void updateTouchExcludeRegion() {
2420 final Task focusedTask = (mFocusedApp != null ? mFocusedApp.getTask() : null);
David Stevensee9e2772017-02-09 16:30:27 -08002421 if (focusedTask == null) {
2422 mTouchExcludeRegion.setEmpty();
2423 } else {
2424 mTouchExcludeRegion.set(mBaseDisplayRect);
2425 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
2426 mTmpRect2.setEmpty();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002427 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0;
2428 --stackNdx) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002429 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002430 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
2431 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08002432 }
2433 // If we removed the focused task above, add it back and only leave its
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002434 // outside touch area in the exclusion. TapDetector is not interested in
David Stevensee9e2772017-02-09 16:30:27 -08002435 // any touch inside the focused task itself.
2436 if (!mTmpRect2.isEmpty()) {
2437 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
2438 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08002439 }
lumark90120a82018-08-15 00:33:03 +08002440 if (mInputMethodWindow != null && mInputMethodWindow.isVisibleLw()) {
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002441 // If the input method is visible and the user is typing, we don't want these touch
2442 // events to be intercepted and used to change focus. This would likely cause a
2443 // disappearance of the input method.
lumark90120a82018-08-15 00:33:03 +08002444 mInputMethodWindow.getTouchableRegion(mTmpRegion);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002445 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002446 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002447 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002448 final WindowState win = mTapExcludedWindows.get(i);
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002449 win.getTouchableRegion(mTmpRegion);
2450 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
2451 }
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002452 for (int i = mTapExcludeProvidingWindows.size() - 1; i >= 0; i--) {
2453 final WindowState win = mTapExcludeProvidingWindows.valueAt(i);
2454 win.amendTapExcludeRegion(mTouchExcludeRegion);
2455 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08002456 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07002457 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002458 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07002459 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002460 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
2461 }
Craig Mautner1bef3892015-02-17 15:09:47 -08002462 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07002463 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08002464 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07002465 }
2466
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002467 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07002468 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002469 super.switchUser();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002470 mWmService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07002471 }
2472
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07002473 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002474 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2475 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07002476 }
2477 }
2478
Wale Ogunwale10124582016-09-15 20:25:50 -07002479 @Override
2480 void removeIfPossible() {
2481 if (isAnimating()) {
2482 mDeferredRemoval = true;
2483 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07002484 }
Wale Ogunwale10124582016-09-15 20:25:50 -07002485 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07002486 }
2487
Wale Ogunwale10124582016-09-15 20:25:50 -07002488 @Override
2489 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002490 mRemovingDisplay = true;
2491 try {
lumark588a3e82018-07-20 18:53:54 +08002492 // Clear all transitions & screen frozen states when removing display.
2493 mOpeningApps.clear();
2494 mClosingApps.clear();
2495 mUnknownAppVisibilityController.clear();
2496 mAppTransition.removeAppTransitionTimeoutCallbacks();
2497 handleAnimatingStoppedAndTransition();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002498 mWmService.stopFreezingDisplayLocked();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002499 super.removeImmediately();
2500 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Arthur Hungbe5ce212018-09-13 18:41:56 +08002501 if (mPointerEventDispatcher != null && mTapDetector != null) {
2502 unregisterPointerEventListener(mTapDetector);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002503 unregisterPointerEventListener(mWmService.mMousePositionTracker);
Arthur Hungbe5ce212018-09-13 18:41:56 +08002504 mTapDetector = null;
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002505 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002506 mWmService.mAnimator.removeDisplayLocked(mDisplayId);
Louis Chang207f9112018-07-30 08:58:54 +08002507 mWindowingLayer.release();
2508 mOverlayLayer.release();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002509 } finally {
Riddle Hsu4e611772018-10-31 18:58:28 +08002510 mDisplayReady = false;
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002511 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08002512 }
Robert Carr679ccb02018-08-08 15:32:35 -07002513
Tiger Huang7c610aa2018-10-27 00:01:01 +08002514 mDisplayPolicy.onDisplayRemoved();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002515 mWmService.mWindowPlacerLocked.requestTraversal();
Craig Mautner95da1082014-02-24 17:54:35 -08002516 }
2517
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002518 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07002519 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002520 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002521 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
2522
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002523 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002524 removeImmediately();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002525 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08002526 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002527 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08002528 }
2529
Andrii Kulian0214ed92017-05-16 13:44:05 -07002530 /** @return 'true' if removal of this display content is deferred due to active animation. */
2531 boolean isRemovalDeferred() {
2532 return mDeferredRemoval;
2533 }
2534
Wale Ogunwale10124582016-09-15 20:25:50 -07002535 boolean animateForIme(float interpolatedValue, float animationTarget,
2536 float dividerAnimationTarget) {
2537 boolean updated = false;
2538
Wale Ogunwale61911492017-10-11 08:50:50 -07002539 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2540 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002541 if (stack == null || !stack.isAdjustedForIme()) {
2542 continue;
2543 }
2544
2545 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
2546 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2547 updated = true;
2548 } else {
2549 mDividerControllerLocked.mLastAnimationProgress =
2550 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
2551 mDividerControllerLocked.mLastDividerProgress =
2552 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
2553 updated |= stack.updateAdjustForIme(
2554 mDividerControllerLocked.mLastAnimationProgress,
2555 mDividerControllerLocked.mLastDividerProgress,
2556 false /* force */);
2557 }
2558 if (interpolatedValue >= 1f) {
2559 stack.endImeAdjustAnimation();
2560 }
2561 }
2562
2563 return updated;
2564 }
2565
2566 boolean clearImeAdjustAnimation() {
2567 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002568 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2569 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002570 if (stack != null && stack.isAdjustedForIme()) {
2571 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2572 changed = true;
2573 }
2574 }
2575 return changed;
2576 }
2577
2578 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002579 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2580 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002581 if (stack.isVisible() && stack.isAdjustedForIme()) {
2582 stack.beginImeAdjustAnimation();
2583 }
2584 }
2585 }
2586
2587 void adjustForImeIfNeeded() {
lumark90120a82018-08-15 00:33:03 +08002588 final WindowState imeWin = mInputMethodWindow;
Wale Ogunwale10124582016-09-15 20:25:50 -07002589 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2590 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002591 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002592 final TaskStack imeTargetStack = mWmService.getImeFocusStackLocked();
Wale Ogunwale10124582016-09-15 20:25:50 -07002593 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2594 imeTargetStack.getDockSide() : DOCKED_INVALID;
2595 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2596 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2597 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002598 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002599 final boolean imeHeightChanged = imeVisible &&
2600 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2601
2602 // The divider could be adjusted for IME position, or be thinner than usual,
2603 // or both. There are three possible cases:
2604 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2605 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2606 // - If IME is not visible, divider is not moved and is normal width.
2607
2608 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002609 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2610 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002611 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002612 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2613 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002614 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2615 } else {
2616 stack.resetAdjustedForIme(false);
2617 }
2618 }
2619 mDividerControllerLocked.setAdjustedForIme(
2620 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2621 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002622 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2623 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002624 stack.resetAdjustedForIme(!dockVisible);
2625 }
2626 mDividerControllerLocked.setAdjustedForIme(
2627 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2628 }
Winson Chung655332c2016-10-31 13:14:28 -07002629 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002630 }
2631
2632 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002633 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2634 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002635 stack.prepareFreezingTaskBounds();
2636 }
2637 }
2638
Wale Ogunwale94744212015-09-21 19:01:47 -07002639 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002640 getBounds(mTmpRect, newRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002641 rotateBounds(mTmpRect, oldRotation, newRotation, bounds);
2642 }
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002643
Evan Rosky39b6f232018-10-30 18:35:41 -07002644 void rotateBounds(Rect parentBounds, int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002645 // Compute a transform matrix to undo the coordinate space transformation,
2646 // and present the window at the same physical position it previously occupied.
2647 final int deltaRotation = deltaRotation(newRotation, oldRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002648 createRotationMatrix(
2649 deltaRotation, parentBounds.width(), parentBounds.height(), mTmpMatrix);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002650
2651 mTmpRectF.set(bounds);
2652 mTmpMatrix.mapRect(mTmpRectF);
2653 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002654 }
2655
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002656 static int deltaRotation(int oldRotation, int newRotation) {
2657 int delta = newRotation - oldRotation;
2658 if (delta < 0) delta += 4;
2659 return delta;
2660 }
2661
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002662 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002663 Matrix outMatrix) {
2664 // For rotations without Z-ordering we don't need the target rectangle's position.
2665 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2666 displayHeight, outMatrix);
2667 }
2668
2669 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2670 float displayWidth, float displayHeight, Matrix outMatrix) {
2671 switch (rotation) {
2672 case ROTATION_0:
2673 outMatrix.reset();
2674 break;
2675 case ROTATION_270:
2676 outMatrix.setRotate(270, 0, 0);
2677 outMatrix.postTranslate(0, displayHeight);
2678 outMatrix.postTranslate(rectTop, 0);
2679 break;
2680 case ROTATION_180:
2681 outMatrix.reset();
2682 break;
2683 case ROTATION_90:
2684 outMatrix.setRotate(90, 0, 0);
2685 outMatrix.postTranslate(displayWidth, 0);
2686 outMatrix.postTranslate(-rectTop, rectLeft);
2687 break;
2688 }
2689 }
2690
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002691 @CallSuper
2692 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002693 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002694 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002695 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002696 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002697 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2698 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002699 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002700 }
2701 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2702 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002703 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2704 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002705 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002706 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002707 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2708 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002709 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002710 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002711 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2712 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002713 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002714 }
2715 proto.write(DPI, mBaseDisplayDensity);
2716 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002717 proto.write(ROTATION, mRotation);
2718 final ScreenRotationAnimation screenRotationAnimation =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002719 mWmService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002720 if (screenRotationAnimation != null) {
2721 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2722 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002723 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
lumark588a3e82018-07-20 18:53:54 +08002724 mAppTransition.writeToProto(proto, APP_TRANSITION);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002725 if (mFocusedApp != null) {
2726 mFocusedApp.writeNameToProto(proto, FOCUSED_APP);
2727 }
Steven Timotiusaf03df62017-07-18 16:56:43 -07002728 proto.end(token);
2729 }
2730
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002731 @Override
2732 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2733 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002734 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2735 final String subPrefix = " " + prefix;
2736 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2737 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2738 pw.print("dpi");
2739 if (mInitialDisplayWidth != mBaseDisplayWidth
2740 || mInitialDisplayHeight != mBaseDisplayHeight
2741 || mInitialDisplayDensity != mBaseDisplayDensity) {
2742 pw.print(" base=");
2743 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2744 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2745 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002746 if (mDisplayScalingDisabled) {
2747 pw.println(" noscale");
2748 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002749 pw.print(" cur=");
2750 pw.print(mDisplayInfo.logicalWidth);
2751 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2752 pw.print(" app=");
2753 pw.print(mDisplayInfo.appWidth);
2754 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2755 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2756 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2757 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2758 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002759 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002760 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002761 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002762
Craig Mautnerdc548482014-02-05 13:35:24 -08002763 pw.println();
Adrian Roos5251b1d2018-03-23 18:57:43 +01002764 pw.print(prefix); pw.print("mLayoutSeq="); pw.println(mLayoutSeq);
Riddle Hsu654a6f92018-07-13 22:59:36 +08002765 pw.print(prefix);
2766 pw.print("mDeferredRotationPauseCount="); pw.println(mDeferredRotationPauseCount);
Adrian Roos5251b1d2018-03-23 18:57:43 +01002767
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002768 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
2769 if (mLastFocus != mCurrentFocus) {
2770 pw.print(" mLastFocus="); pw.println(mLastFocus);
2771 }
2772 if (mLosingFocus.size() > 0) {
2773 pw.println();
2774 pw.println(" Windows losing focus:");
2775 for (int i = mLosingFocus.size() - 1; i >= 0; i--) {
2776 final WindowState w = mLosingFocus.get(i);
2777 pw.print(" Losing #"); pw.print(i); pw.print(' ');
2778 pw.print(w);
2779 if (dumpAll) {
2780 pw.println(":");
2781 w.dump(pw, " ", true);
2782 } else {
2783 pw.println();
2784 }
2785 }
2786 }
2787 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
Tiger Huang7c610aa2018-10-27 00:01:01 +08002788 if (mLastStatusBarVisibility != 0) {
2789 pw.print(" mLastStatusBarVisibility=0x");
2790 pw.println(Integer.toHexString(mLastStatusBarVisibility));
2791 }
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002792
Adrian Roos5251b1d2018-03-23 18:57:43 +01002793 pw.println();
wilsonshihc32538e2018-11-07 17:27:34 +08002794 mWallpaperController.dump(pw, " ");
2795
2796 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002797 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002798 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2799 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002800 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002801 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002802
Craig Mautnerdc548482014-02-05 13:35:24 -08002803 pw.println();
2804 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002805 pw.println();
2806 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002807 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002808 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002809 pw.print(" Exiting #"); pw.print(i);
2810 pw.print(' '); pw.print(token);
2811 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002812 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002813 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002814 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002815
Jorim Jaggi31f71702016-05-04 16:43:04 -07002816 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002817
2818 // Dump stack references
2819 final TaskStack homeStack = getHomeStack();
2820 if (homeStack != null) {
2821 pw.println(prefix + "homeStack=" + homeStack.getName());
2822 }
2823 final TaskStack pinnedStack = getPinnedStack();
2824 if (pinnedStack != null) {
2825 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2826 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002827 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002828 if (splitScreenPrimaryStack != null) {
2829 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2830 }
2831
2832 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002833 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002834 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002835 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002836
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002837 pw.println();
2838 mDisplayFrames.dump(prefix, pw);
Arthur Hung95b38a92018-07-20 18:56:12 +08002839 pw.println();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08002840 mDisplayPolicy.dump(prefix, pw);
2841 pw.println();
Riddle Hsuad256a12018-07-18 16:11:30 +08002842 mDisplayRotation.dump(prefix, pw);
2843 pw.println();
Arthur Hung95b38a92018-07-20 18:56:12 +08002844 mInputMonitor.dump(pw, " ");
Jorim Jaggif96c90a2018-09-26 16:55:15 +02002845 pw.println();
2846 mInsetsStateController.dump(prefix, pw);
Craig Mautner59c00972012-07-30 12:10:24 -07002847 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002848
2849 @Override
2850 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002851 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002852 }
2853
2854 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002855 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002856 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002857
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002858 /** Returns true if the stack in the windowing mode is visible. */
2859 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002860 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002861 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002862 }
2863
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002864 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002865 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002866 final int x = (int) xf;
2867 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002868 final WindowState touchedWin = getWindow(w -> {
2869 final int flags = w.mAttrs.flags;
2870 if (!w.isVisibleLw()) {
2871 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002872 }
2873 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002874 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002875 }
2876
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002877 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002878 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002879 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002880 }
2881
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002882 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002883
2884 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002885 return mTmpRegion.contains(x, y) || touchFlags == 0;
2886 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002887
2888 return touchedWin;
2889 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002890
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002891 boolean canAddToastWindowForUid(int uid) {
2892 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002893 // Also if the app is focused adding more than one toast at
2894 // a time for better backwards compatibility.
2895 final WindowState focusedWindowForUid = getWindow(w ->
2896 w.mOwnerUid == uid && w.isFocused());
2897 if (focusedWindowForUid != null) {
2898 return true;
2899 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002900 final WindowState win = getWindow(w ->
2901 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2902 && !w.mWindowRemovalAllowed);
2903 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002904 }
2905
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002906 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002907 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2908 return;
2909 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002910
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002911 // Used to communicate the old focus to the callback method.
2912 mTmpWindow = oldFocus;
2913
2914 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002915 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002916
Tiger Huang51c5a1d2018-12-11 20:24:51 +08002917 WindowState findFocusedWindowIfNeeded() {
2918 return (mWmService.mPerDisplayFocusEnabled
2919 || mWmService.mRoot.mTopFocusedAppByProcess.isEmpty()) ? findFocusedWindow() : null;
2920 }
2921
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002922 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002923 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002924
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002925 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002926
2927 if (mTmpWindow == null) {
2928 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2929 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002930 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002931 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002932 }
2933
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002934 /**
2935 * Update the focused window and make some adjustments if the focus has changed.
2936 *
2937 * @param mode Indicates the situation we are in. Possible modes are:
2938 * {@link WindowManagerService#UPDATE_FOCUS_NORMAL},
2939 * {@link WindowManagerService#UPDATE_FOCUS_PLACING_SURFACES},
2940 * {@link WindowManagerService#UPDATE_FOCUS_WILL_PLACE_SURFACES},
2941 * {@link WindowManagerService#UPDATE_FOCUS_REMOVING_FOCUS}
2942 * @param updateInputWindows Whether to sync the window information to the input module.
2943 * @return {@code true} if the focused window has changed.
2944 */
Tiger Huang51c5a1d2018-12-11 20:24:51 +08002945 boolean updateFocusedWindowLocked(int mode, boolean updateInputWindows) {
2946 WindowState newFocus = findFocusedWindowIfNeeded();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002947 if (mCurrentFocus == newFocus) {
2948 return false;
2949 }
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002950 boolean imWindowChanged = false;
Tiger Huang51c5a1d2018-12-11 20:24:51 +08002951 final WindowState imWindow = mInputMethodWindow;
2952 if (imWindow != null) {
2953 final WindowState prevTarget = mInputMethodTarget;
2954 final WindowState newTarget = computeImeTarget(true /* updateImeTarget*/);
2955 imWindowChanged = prevTarget != newTarget;
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002956
Tiger Huang51c5a1d2018-12-11 20:24:51 +08002957 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS
2958 && mode != UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2959 assignWindowLayers(false /* setLayoutNeeded */);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002960 }
2961 }
2962
2963 if (imWindowChanged) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002964 mWmService.mWindowsChanged = true;
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002965 setLayoutNeeded();
Tiger Huang51c5a1d2018-12-11 20:24:51 +08002966 newFocus = findFocusedWindowIfNeeded();
2967 }
2968 if (mCurrentFocus != newFocus) {
2969 mWmService.mH.obtainMessage(REPORT_FOCUS_CHANGE, this).sendToTarget();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002970 }
2971
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002972 if (DEBUG_FOCUS_LIGHT || mWmService.localLOGV) Slog.v(TAG_WM, "Changing focus from "
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002973 + mCurrentFocus + " to " + newFocus + " displayId=" + getDisplayId()
2974 + " Callers=" + Debug.getCallers(4));
2975 final WindowState oldFocus = mCurrentFocus;
2976 mCurrentFocus = newFocus;
2977 mLosingFocus.remove(newFocus);
2978
2979 if (newFocus != null) {
2980 mWinAddedSinceNullFocus.clear();
2981 mWinRemovedSinceNullFocus.clear();
2982
2983 if (newFocus.canReceiveKeys()) {
2984 // Displaying a window implicitly causes dispatching to be unpaused.
2985 // This is to protect against bugs if someone pauses dispatching but
2986 // forgets to resume.
2987 newFocus.mToken.paused = false;
2988 }
2989 }
2990
Tiger Huang7c610aa2018-10-27 00:01:01 +08002991 int focusChanged = getDisplayPolicy().focusChangedLw(oldFocus, newFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002992
2993 if (imWindowChanged && oldFocus != mInputMethodWindow) {
2994 // Focus of the input method window changed. Perform layout if needed.
2995 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2996 performLayout(true /*initial*/, updateInputWindows);
2997 focusChanged &= ~FINISH_LAYOUT_REDO_LAYOUT;
2998 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2999 // Client will do the layout, but we need to assign layers
3000 // for handleNewWindowLocked() below.
3001 assignWindowLayers(false /* setLayoutNeeded */);
3002 }
3003 }
3004
3005 if ((focusChanged & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
3006 // The change in focus caused us to need to do a layout. Okay.
3007 setLayoutNeeded();
3008 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
3009 performLayout(true /*initial*/, updateInputWindows);
3010 } else if (mode == UPDATE_FOCUS_REMOVING_FOCUS) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003011 mWmService.mRoot.performSurfacePlacement(false);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003012 }
3013 }
3014
3015 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
3016 // If we defer assigning layers, then the caller is responsible for doing this part.
3017 getInputMonitor().setInputFocusLw(newFocus, updateInputWindows);
3018 }
3019
3020 adjustForImeIfNeeded();
3021
3022 // We may need to schedule some toast windows to be removed. The toasts for an app that
3023 // does not have input focus are removed within a timeout to prevent apps to redress
3024 // other apps' UI.
3025 scheduleToastWindowsTimeoutIfNeededLocked(oldFocus, newFocus);
3026
3027 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
3028 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
3029 }
3030 return true;
3031 }
3032
3033 /**
3034 * Set the new focused app to this display.
3035 *
3036 * @param newFocus the new focused AppWindowToken.
3037 * @return true if the focused app is changed.
3038 */
3039 boolean setFocusedApp(AppWindowToken newFocus) {
3040 if (newFocus != null) {
3041 final DisplayContent appDisplay = newFocus.getDisplayContent();
3042 if (appDisplay != this) {
3043 throw new IllegalStateException(newFocus + " is not on " + getName()
3044 + " but " + ((appDisplay != null) ? appDisplay.getName() : "none"));
3045 }
3046 }
3047 if (mFocusedApp == newFocus) {
3048 return false;
3049 }
3050 mFocusedApp = newFocus;
3051 getInputMonitor().setFocusedAppLw(newFocus);
3052 updateTouchExcludeRegion();
3053 return true;
3054 }
3055
Wale Ogunwalec69694a2016-10-18 13:51:15 -07003056 /** Updates the layer assignment of windows on this display. */
3057 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003058 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07003059 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07003060 if (setLayoutNeeded) {
3061 setLayoutNeeded();
3062 }
Robert Carrb1579c82017-09-05 14:54:47 -07003063
Robert Carrf59b8dd2017-10-02 18:58:36 -07003064 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07003065 // the application of this transaction until the animation pass triggers
3066 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
3067 // the hiding and showing of surfaces.
3068 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003069 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07003070 }
3071
Wale Ogunwale1666e312016-12-16 11:27:18 -08003072 // TODO: This should probably be called any time a visual change is made to the hierarchy like
3073 // moving containers or resizing them. Need to investigate the best way to have it automatically
3074 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003075 void layoutAndAssignWindowLayersIfNeeded() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003076 mWmService.mWindowsChanged = true;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003077 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003078
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003079 if (!mWmService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003080 false /*updateInputWindows*/)) {
3081 assignWindowLayers(false /* setLayoutNeeded */);
3082 }
3083
Arthur Hung95b38a92018-07-20 18:56:12 +08003084 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003085 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Arthur Hung95b38a92018-07-20 18:56:12 +08003086 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003087 }
3088
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003089 /** Returns true if a leaked surface was destroyed */
3090 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003091 // Used to indicate that a surface was leaked.
3092 mTmpWindow = null;
3093 forAllWindows(w -> {
3094 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003095 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003096 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003097 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003098 if (!mWmService.mSessions.contains(wsa.mSession)) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003099 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003100 + w + " surface=" + wsa.mSurfaceController
3101 + " token=" + w.mToken
3102 + " pid=" + w.mSession.mPid
3103 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003104 wsa.destroySurface();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003105 mWmService.mForceRemoves.add(w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003106 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07003107 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003108 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003109 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02003110 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003111 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003112 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003113 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003114 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003115 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003116
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003117 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003118 }
3119
3120 /**
lumark90120a82018-08-15 00:33:03 +08003121 * Set input method window for the display.
3122 * @param win Set when window added or Null when destroyed.
3123 */
3124 void setInputMethodWindowLocked(WindowState win) {
3125 mInputMethodWindow = win;
lumark70865a82018-09-17 16:56:26 +08003126 // Update display configuration for IME process.
3127 if (mInputMethodWindow != null) {
3128 final int imePid = mInputMethodWindow.mSession.mPid;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003129 mWmService.mAtmInternal.onImeWindowSetOnDisplay(imePid,
lumark70865a82018-09-17 16:56:26 +08003130 mInputMethodWindow.getDisplayId());
3131 }
lumark90120a82018-08-15 00:33:03 +08003132 computeImeTarget(true /* updateImeTarget */);
Jorim Jaggif96c90a2018-09-26 16:55:15 +02003133 mInsetsStateController.getSourceProvider(TYPE_IME).setWindow(win,
3134 null /* frameProvider */);
lumark90120a82018-08-15 00:33:03 +08003135 }
3136
3137 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003138 * Determine and return the window that should be the IME target.
3139 * @param updateImeTarget If true the system IME target will be updated to match what we found.
3140 * @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 +00003141 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003142 WindowState computeImeTarget(boolean updateImeTarget) {
lumark90120a82018-08-15 00:33:03 +08003143 if (mInputMethodWindow == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003144 // There isn't an IME so there shouldn't be a target...That was easy!
3145 if (updateImeTarget) {
3146 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
lumarkff0ab692018-11-05 20:32:30 +08003147 + mInputMethodTarget + " to null since mInputMethodWindow is null");
3148 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003149 }
3150 return null;
3151 }
3152
lumarkff0ab692018-11-05 20:32:30 +08003153 final WindowState curTarget = mInputMethodTarget;
Chavi Weingarten3a748552018-05-14 17:32:42 +00003154 if (!canUpdateImeTarget()) {
3155 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Defer updating IME target");
3156 return curTarget;
3157 }
3158
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003159 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
3160 // same display. Or even when the current IME/target are not on the same screen as the next
3161 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08003162 mUpdateImeTarget = updateImeTarget;
3163 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003164
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003165
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003166 // Yet more tricksyness! If this window is a "starting" window, we do actually want
3167 // to be on top of it, but it is not -really- where input will go. So look down below
3168 // for a real window to target...
3169 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
3170 final AppWindowToken token = target.mAppToken;
3171 if (token != null) {
3172 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
3173 if (betterTarget != null) {
3174 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003175 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003176 }
3177 }
3178
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003179 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
lumarkff0ab692018-11-05 20:32:30 +08003180 "Proposed new IME target: " + target + " for display: " + getDisplayId());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003181
chaviw1c13ad32018-06-12 16:44:23 -07003182 // Now, a special case -- if the last target's window is in the process of exiting, but
3183 // not removed, and the new target is home, keep on the last target to avoid flicker.
3184 // Home is a special case since its above other stacks in the ordering list, but layed
3185 // out below the others.
3186 if (curTarget != null && !curTarget.mRemoved && curTarget.isDisplayedLw()
3187 && curTarget.isClosing() && (target == null || target.isActivityTypeHome())) {
chaviw4ad54912018-05-30 11:05:44 -07003188 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "New target is home while current target is "
Chavi Weingarten3a748552018-05-14 17:32:42 +00003189 + "closing, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003190 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003191 }
3192
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003193 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
3194 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003195
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003196 if (target == null) {
3197 if (updateImeTarget) {
3198 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
3199 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
3200 + Debug.getCallers(4) : ""));
lumarkff0ab692018-11-05 20:32:30 +08003201 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003202 }
3203
3204 return null;
3205 }
3206
3207 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003208 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
3209 if (token != null) {
3210
3211 // Now some fun for dealing with window animations that modify the Z order. We need
3212 // to look at all windows below the current target that are in this app, finding the
3213 // highest visible one in layering.
3214 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003215 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003216 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003217 }
3218
3219 if (highestTarget != null) {
lumark588a3e82018-07-20 18:53:54 +08003220 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, mAppTransition + " " + highestTarget
Jorim Jaggi35d328a2018-08-14 17:00:20 +02003221 + " animating=" + highestTarget.isAnimating());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003222
lumark588a3e82018-07-20 18:53:54 +08003223 if (mAppTransition.isTransitionSet()) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003224 // If we are currently setting up for an animation, hold everything until we
3225 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003226 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003227 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003228 }
3229 }
3230 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003231
3232 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
3233 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003234 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003235 }
3236
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003237 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003238 }
3239
lumarkff0ab692018-11-05 20:32:30 +08003240 /**
3241 * Calling {@link #computeImeTarget(boolean)} to update the input method target window in
3242 * the candidate app window token if needed.
3243 */
3244 void computeImeTargetIfNeeded(AppWindowToken candidate) {
3245 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == candidate) {
3246 computeImeTarget(true /* updateImeTarget */);
3247 }
3248 }
3249
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003250 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
lumarkff0ab692018-11-05 20:32:30 +08003251 if (target == mInputMethodTarget && mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003252 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003253 }
3254
lumarkff0ab692018-11-05 20:32:30 +08003255 mInputMethodTarget = target;
3256 mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003257 assignWindowLayers(false /* setLayoutNeeded */);
3258 }
3259
3260 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
3261 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3262 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
3263 }
3264
3265 // Used to indicate we have reached the first window in the range we are interested in.
3266 mTmpWindow = null;
3267
3268 // TODO: Figure-out a more efficient way to do this.
3269 final WindowState candidate = getWindow(w -> {
3270 if (w == top) {
3271 // Reached the first window in the range we are interested in.
3272 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003273 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003274 if (mTmpWindow == null) {
3275 return false;
3276 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003277
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003278 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3279 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003280 }
3281 // If we reached the bottom of the range of windows we are considering,
3282 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003283 if (w == bottom) {
3284 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003285 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003286 return false;
3287 });
3288
3289 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003290 }
3291
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003292 void setLayoutNeeded() {
3293 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
3294 mLayoutNeeded = true;
3295 }
3296
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003297 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003298 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
3299 mLayoutNeeded = false;
3300 }
3301
3302 boolean isLayoutNeeded() {
3303 return mLayoutNeeded;
3304 }
3305
Wale Ogunwale02319a62016-09-26 15:21:22 -07003306 void dumpTokens(PrintWriter pw, boolean dumpAll) {
3307 if (mTokenMap.isEmpty()) {
3308 return;
3309 }
3310 pw.println(" Display #" + mDisplayId);
3311 final Iterator<WindowToken> it = mTokenMap.values().iterator();
3312 while (it.hasNext()) {
3313 final WindowToken token = it.next();
3314 pw.print(" ");
3315 pw.print(token);
3316 if (dumpAll) {
3317 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003318 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07003319 } else {
3320 pw.println();
3321 }
3322 }
lumark588a3e82018-07-20 18:53:54 +08003323
3324 if (!mOpeningApps.isEmpty() || !mClosingApps.isEmpty()) {
3325 pw.println();
3326 if (mOpeningApps.size() > 0) {
3327 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
3328 }
3329 if (mClosingApps.size() > 0) {
3330 pw.print(" mClosingApps="); pw.println(mClosingApps);
3331 }
3332 }
3333
3334 mUnknownAppVisibilityController.dump(pw, " ");
Wale Ogunwale02319a62016-09-26 15:21:22 -07003335 }
3336
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003337 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003338 final int[] index = new int[1];
3339 forAllWindows(w -> {
3340 final WindowStateAnimator wAnim = w.mWinAnimator;
3341 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
3342 index[0] = index[0] + 1;
3343 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003344 }
3345
Jorim Jaggife762342016-10-13 14:33:27 +02003346 /**
3347 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
3348 */
3349 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003350 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003351 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02003352 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
3353 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01003354 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02003355 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003356 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02003357 }
3358
Wale Ogunwale494009b82016-10-21 09:01:38 -07003359 boolean checkWaitingForWindows() {
3360
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003361 mHaveBootMsg = false;
3362 mHaveApp = false;
3363 mHaveWallpaper = false;
3364 mHaveKeyguard = true;
3365
3366 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003367 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
3368 return true;
3369 }
3370 if (w.isDrawnLw()) {
3371 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003372 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003373 } else if (w.mAttrs.type == TYPE_APPLICATION
3374 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003375 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003376 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003377 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003378 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003379 mHaveKeyguard = mWmService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003380 }
3381 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003382 return false;
3383 });
3384
3385 if (visibleWindow != null) {
3386 // We have a visible window.
3387 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003388 }
3389
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003390 // if the wallpaper service is disabled on the device, we're never going to have
3391 // wallpaper, don't bother waiting for it
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003392 boolean wallpaperEnabled = mWmService.mContext.getResources().getBoolean(
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003393 com.android.internal.R.bool.config_enableWallpaperService)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003394 && mWmService.mContext.getResources().getBoolean(
Daichi Hironoc1432222018-03-29 13:06:07 +09003395 com.android.internal.R.bool.config_checkWallpaperAtBoot)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003396 && !mWmService.mOnlyCore;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003397
Wale Ogunwale494009b82016-10-21 09:01:38 -07003398 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003399 "******** booted=" + mWmService.mSystemBooted
3400 + " msg=" + mWmService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003401 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
3402 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
3403 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003404
3405 // If we are turning on the screen to show the boot message, don't do it until the boot
3406 // message is actually displayed.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003407 if (!mWmService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003408 return true;
3409 }
3410
3411 // If we are turning on the screen after the boot is completed normally, don't do so until
3412 // we have the application and wallpaper.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003413 if (mWmService.mSystemBooted
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003414 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003415 return true;
3416 }
3417
3418 return false;
3419 }
3420
Jorim Jaggi8f520872018-08-14 17:00:20 +02003421 void updateWindowsForAnimator() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003422 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003423 }
3424
Jorim Jaggi8f520872018-08-14 17:00:20 +02003425 /**
3426 * Updates the {@link TaskStack#setAnimationBackground} for all windows.
3427 */
3428 void updateBackgroundForAnimator() {
Jorim Jaggi7156b652016-10-25 08:31:58 -07003429 resetAnimationBackgroundAnimator();
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003430 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003431 }
3432
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003433 boolean isInputMethodClientFocus(int uid, int pid) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003434 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003435 if (imFocus == null) {
3436 return false;
3437 }
3438
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003439 if (DEBUG_INPUT_METHOD) {
3440 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003441 Slog.i(TAG_WM, "Current focus: " + mCurrentFocus + " displayId=" + mDisplayId);
3442 Slog.i(TAG_WM, "Last focus: " + mLastFocus + " displayId=" + mDisplayId);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003443 }
3444
Wale Ogunwale494009b82016-10-21 09:01:38 -07003445 if (DEBUG_INPUT_METHOD) {
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003446 Slog.i(TAG_WM, "IM target uid/pid: " + imFocus.mSession.mUid
3447 + "/" + imFocus.mSession.mPid);
3448 Slog.i(TAG_WM, "Requesting client uid/pid: " + uid + "/" + pid);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003449 }
3450
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003451 return imFocus.mSession.mUid == uid && imFocus.mSession.mPid == pid;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003452 }
3453
3454 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003455 final WindowState win = getWindow(
3456 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
3457 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003458 }
3459
Tiger Huang7c610aa2018-10-27 00:01:01 +08003460 void statusBarVisibilityChanged(int visibility) {
3461 mLastStatusBarVisibility = visibility;
3462 visibility = getDisplayPolicy().adjustSystemUiVisibilityLw(visibility);
3463 updateStatusBarVisibilityLocked(visibility);
3464 }
3465
3466 private boolean updateStatusBarVisibilityLocked(int visibility) {
3467 if (mLastDispatchedSystemUiVisibility == visibility) {
3468 return false;
3469 }
3470 final int globalDiff = (visibility ^ mLastDispatchedSystemUiVisibility)
3471 // We are only interested in differences of one of the
3472 // clearable flags...
3473 & View.SYSTEM_UI_CLEARABLE_FLAGS
3474 // ...if it has actually been cleared.
3475 & ~visibility;
3476
3477 mLastDispatchedSystemUiVisibility = visibility;
3478 if (isDefaultDisplay) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003479 mWmService.mInputManager.setSystemUiVisibility(visibility);
Tiger Huang7c610aa2018-10-27 00:01:01 +08003480 }
3481 updateSystemUiVisibility(visibility, globalDiff);
3482 return true;
3483 }
3484
Wale Ogunwale494009b82016-10-21 09:01:38 -07003485 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003486 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003487 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003488 final int curValue = w.mSystemUiVisibility;
3489 final int diff = (curValue ^ visibility) & globalDiff;
3490 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003491 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003492 w.mSeq++;
3493 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003494 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003495 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
3496 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07003497 visibility, newValue, diff);
3498 }
3499 } catch (RemoteException e) {
3500 // so sorry
3501 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003502 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003503 }
3504
Tiger Huang7c610aa2018-10-27 00:01:01 +08003505 void reevaluateStatusBarVisibility() {
3506 int visibility = getDisplayPolicy().adjustSystemUiVisibilityLw(mLastStatusBarVisibility);
3507 if (updateStatusBarVisibilityLocked(visibility)) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003508 mWmService.mWindowPlacerLocked.requestTraversal();
Tiger Huang7c610aa2018-10-27 00:01:01 +08003509 }
3510 }
3511
Wale Ogunwale494009b82016-10-21 09:01:38 -07003512 void onWindowFreezeTimeout() {
3513 Slog.w(TAG_WM, "Window freeze timeout expired.");
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003514 mWmService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003515
3516 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07003517 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003518 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003519 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07003520 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003521 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003522 - mWmService.mDisplayFreezeTime);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003523 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003524 }, true /* traverseTopToBottom */);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003525 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003526 }
3527
3528 void waitForAllWindowsDrawn() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003529 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003530 forAllWindows(w -> {
3531 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
3532 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
3533 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003534 // Force add to mResizingWindows.
chaviw9c81e632018-07-31 11:17:52 -07003535 w.resetLastContentInsets();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003536 mWmService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003537 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003538 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003539 }
3540
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003541 // TODO: Super crazy long method that should be broken down...
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003542 void applySurfaceChangesTransaction(boolean recoveringMemory) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003543 final WindowSurfacePlacer surfacePlacer = mWmService.mWindowPlacerLocked;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003544
3545 mTmpUpdateAllDrawn.clear();
3546
3547 int repeats = 0;
3548 do {
3549 repeats++;
3550 if (repeats > 6) {
3551 Slog.w(TAG, "Animation repeat aborted after too many iterations");
3552 clearLayoutNeeded();
3553 break;
3554 }
3555
3556 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
3557 pendingLayoutChanges);
3558
wilsonshihc32538e2018-11-07 17:27:34 +08003559 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
3560 mWallpaperController.adjustWallpaperWindows();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003561 }
3562
Riddle Hsu654a6f92018-07-13 22:59:36 +08003563 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003564 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
Riddle Hsu4e611772018-10-31 18:58:28 +08003565 if (updateOrientationFromAppTokens()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003566 setLayoutNeeded();
Riddle Hsu4e611772018-10-31 18:58:28 +08003567 sendNewConfiguration();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003568 }
3569 }
3570
3571 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
3572 setLayoutNeeded();
3573 }
3574
3575 // FIRST LOOP: Perform a layout, if needed.
3576 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
3577 performLayout(repeats == 1, false /* updateInputWindows */);
3578 } else {
3579 Slog.w(TAG, "Layout repeat skipped after too many iterations");
3580 }
3581
3582 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
3583 pendingLayoutChanges = 0;
3584
Tiger Huang7c610aa2018-10-27 00:01:01 +08003585 mDisplayPolicy.beginPostLayoutPolicyLw();
3586 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
3587 pendingLayoutChanges |= mDisplayPolicy.finishPostLayoutPolicyLw();
3588 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
3589 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
Jorim Jaggif96c90a2018-09-26 16:55:15 +02003590 mInsetsStateController.onPostLayout();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003591 } while (pendingLayoutChanges != 0);
3592
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003593 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003594
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003595 mTmpRecoveringMemory = recoveringMemory;
3596 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01003597 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003598
Jorim Jaggif1292892018-09-10 11:58:13 +02003599 mLastHasContent = mTmpApplySurfaceChangesTransactionState.displayHasContent;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003600 mWmService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Jorim Jaggif1292892018-09-10 11:58:13 +02003601 mLastHasContent,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003602 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
3603 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003604 true /* inTraversal, must call performTraversalInTrans... below */);
3605
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003606 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
3607 if (wallpaperVisible != mLastWallpaperVisible) {
3608 mLastWallpaperVisible = wallpaperVisible;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003609 mWmService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003610 }
3611
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003612 while (!mTmpUpdateAllDrawn.isEmpty()) {
3613 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
3614 // See if any windows have been drawn, so they (and others associated with them)
3615 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07003616 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003617 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003618 }
3619
Bryce Leef3c6a472017-11-14 14:53:06 -08003620 private void updateBounds() {
Evan Rosky39b6f232018-10-30 18:35:41 -07003621 calculateBounds(mDisplayInfo, mTmpBounds);
Bryce Leef3c6a472017-11-14 14:53:06 -08003622 setBounds(mTmpBounds);
3623 }
3624
3625 // Determines the current display bounds based on the current state
Evan Rosky39b6f232018-10-30 18:35:41 -07003626 private void calculateBounds(DisplayInfo displayInfo, Rect out) {
Bryce Leef3c6a472017-11-14 14:53:06 -08003627 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Evan Rosky39b6f232018-10-30 18:35:41 -07003628 final int rotation = displayInfo.rotation;
3629 boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Bryce Leef3c6a472017-11-14 14:53:06 -08003630 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
3631 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Evan Rosky39b6f232018-10-30 18:35:41 -07003632 int width = displayInfo.logicalWidth;
Bryce Leef3c6a472017-11-14 14:53:06 -08003633 int left = (physWidth - width) / 2;
Evan Rosky39b6f232018-10-30 18:35:41 -07003634 int height = displayInfo.logicalHeight;
Bryce Leef3c6a472017-11-14 14:53:06 -08003635 int top = (physHeight - height) / 2;
3636 out.set(left, top, left + width, top + height);
3637 }
3638
3639 @Override
3640 public void getBounds(Rect out) {
Evan Rosky39b6f232018-10-30 18:35:41 -07003641 calculateBounds(mDisplayInfo, out);
Bryce Leef3c6a472017-11-14 14:53:06 -08003642 }
3643
3644 private void getBounds(Rect out, int orientation) {
3645 getBounds(out);
3646
3647 // Rotate the Rect if needed.
3648 final int currentRotation = mDisplayInfo.rotation;
3649 final int rotationDelta = deltaRotation(currentRotation, orientation);
3650 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
3651 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
3652 mTmpRectF.set(out);
3653 mTmpMatrix.mapRect(mTmpRectF);
3654 mTmpRectF.round(out);
3655 }
3656 }
3657
Evan Rosky730f6e82018-12-03 17:40:11 -08003658 /** @returns the orientation of the display when it's rotation is ROTATION_0. */
3659 int getNaturalOrientation() {
3660 return mBaseDisplayWidth < mBaseDisplayHeight
3661 ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
3662 }
3663
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003664 void performLayout(boolean initial, boolean updateInputWindows) {
3665 if (!isLayoutNeeded()) {
3666 return;
3667 }
3668 clearLayoutNeeded();
3669
3670 final int dw = mDisplayInfo.logicalWidth;
3671 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003672 if (DEBUG_LAYOUT) {
3673 Slog.v(TAG, "-------------------------------------");
3674 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
3675 }
3676
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01003677 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
3678 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00003679 // TODO: Not sure if we really need to set the rotation here since we are updating from the
3680 // display info above...
3681 mDisplayFrames.mRotation = mRotation;
Tiger Huang7c610aa2018-10-27 00:01:01 +08003682 mDisplayPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003683
Adrian Roos5251b1d2018-03-23 18:57:43 +01003684 int seq = mLayoutSeq + 1;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003685 if (seq < 0) seq = 0;
Adrian Roos5251b1d2018-03-23 18:57:43 +01003686 mLayoutSeq = seq;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003687
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003688 // Used to indicate that we have processed the dream window and all additional windows are
3689 // behind it.
3690 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003691 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003692
3693 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003694 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003695
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003696 // Used to indicate that we have processed the dream window and all additional attached
3697 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003698 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003699 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003700
3701 // Now perform layout of attached windows, which usually depend on the position of the
3702 // window they are attached to. XXX does not deal with windows that are attached to windows
3703 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003704 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003705
3706 // Window frames may have changed. Tell the input dispatcher about it.
Arthur Hung95b38a92018-07-20 18:56:12 +08003707 mInputMonitor.layoutInputConsumers(dw, dh);
3708 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003709 if (updateInputWindows) {
Arthur Hung95b38a92018-07-20 18:56:12 +08003710 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003711 }
3712
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003713 mWmService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003714 }
3715
3716 /**
3717 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
3718 * In portrait mode, it grabs the full screenshot.
3719 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003720 * @param config of the output bitmap
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003721 */
chaviw0315a1a2018-03-05 15:28:35 -08003722 Bitmap screenshotDisplayLocked(Bitmap.Config config) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003723 if (!mWmService.mPolicy.isScreenOn()) {
chaviw0315a1a2018-03-05 15:28:35 -08003724 if (DEBUG_SCREENSHOT) {
3725 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003726 }
chaviw0315a1a2018-03-05 15:28:35 -08003727 return null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003728 }
chaviwfbe47df2017-11-10 16:14:49 -08003729
chaviw0315a1a2018-03-05 15:28:35 -08003730 int dw = mDisplayInfo.logicalWidth;
3731 int dh = mDisplayInfo.logicalHeight;
chaviwfbe47df2017-11-10 16:14:49 -08003732
chaviw0315a1a2018-03-05 15:28:35 -08003733 if (dw <= 0 || dh <= 0) {
3734 return null;
3735 }
chaviwfbe47df2017-11-10 16:14:49 -08003736
chaviw0315a1a2018-03-05 15:28:35 -08003737 final Rect frame = new Rect(0, 0, dw, dh);
chaviwfbe47df2017-11-10 16:14:49 -08003738
chaviw0315a1a2018-03-05 15:28:35 -08003739 // The screenshot API does not apply the current screen rotation.
3740 int rot = mDisplay.getRotation();
chaviwfbe47df2017-11-10 16:14:49 -08003741
chaviw0315a1a2018-03-05 15:28:35 -08003742 if (rot == ROTATION_90 || rot == ROTATION_270) {
3743 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3744 }
chaviwfbe47df2017-11-10 16:14:49 -08003745
chaviw0315a1a2018-03-05 15:28:35 -08003746 // SurfaceFlinger is not aware of orientation, so convert our logical
3747 // crop to SurfaceFlinger's portrait orientation.
3748 convertCropForSurfaceFlinger(frame, rot, dw, dh);
3749
3750 final ScreenRotationAnimation screenRotationAnimation =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003751 mWmService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
chaviw0315a1a2018-03-05 15:28:35 -08003752 final boolean inRotation = screenRotationAnimation != null &&
3753 screenRotationAnimation.isAnimating();
3754 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
3755
3756 // TODO(b/68392460): We should screenshot Task controls directly
3757 // but it's difficult at the moment as the Task doesn't have the
3758 // correct size set.
chaviw08520a02018-09-10 16:44:56 -07003759 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, inRotation, rot);
chaviw0315a1a2018-03-05 15:28:35 -08003760 if (bitmap == null) {
3761 Slog.w(TAG_WM, "Failed to take screenshot");
3762 return null;
3763 }
3764
3765 // Create a copy of the screenshot that is immutable and backed in ashmem.
3766 // This greatly reduces the overhead of passing the bitmap between processes.
3767 final Bitmap ret = bitmap.createAshmemBitmap(config);
3768 bitmap.recycle();
3769 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003770 }
3771
3772 // TODO: Can this use createRotationMatrix()?
3773 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3774 if (rot == Surface.ROTATION_90) {
3775 final int tmp = crop.top;
3776 crop.top = dw - crop.right;
3777 crop.right = crop.bottom;
3778 crop.bottom = dw - crop.left;
3779 crop.left = tmp;
3780 } else if (rot == Surface.ROTATION_180) {
3781 int tmp = crop.top;
3782 crop.top = dh - crop.bottom;
3783 crop.bottom = dh - tmp;
3784 tmp = crop.right;
3785 crop.right = dw - crop.left;
3786 crop.left = dw - tmp;
3787 } else if (rot == Surface.ROTATION_270) {
3788 final int tmp = crop.top;
3789 crop.top = crop.left;
3790 crop.left = dh - crop.bottom;
3791 crop.bottom = crop.right;
3792 crop.right = dh - tmp;
3793 }
3794 }
3795
3796 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003797 // Used to indicate the layout is needed.
3798 mTmpWindow = null;
3799
3800 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003801 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003802 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003803 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003804 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003805 w.setDisplayLayoutNeeded();
Vishnu Nairba183352018-11-21 11:16:49 -08003806 w.finishSeamlessRotation(true /* timeout */);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003807 mWmService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003808 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003809
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003810 if (mTmpWindow != null) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003811 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003812 }
3813 }
3814
Wale Ogunwale1666e312016-12-16 11:27:18 -08003815 void setExitingTokensHasVisible(boolean hasVisible) {
3816 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3817 mExitingTokens.get(i).hasVisible = hasVisible;
3818 }
3819
3820 // Initialize state of exiting applications.
3821 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3822 }
3823
3824 void removeExistingTokensIfPossible() {
3825 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3826 final WindowToken token = mExitingTokens.get(i);
3827 if (!token.hasVisible) {
3828 mExitingTokens.remove(i);
3829 }
3830 }
3831
3832 // Time to remove any exiting applications?
3833 mTaskStackContainers.removeExistingAppTokensIfPossible();
3834 }
3835
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003836 @Override
3837 void onDescendantOverrideConfigurationChanged() {
3838 setLayoutNeeded();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003839 mWmService.requestTraversal();
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003840 }
3841
David Stevens9440dc82017-03-16 19:00:20 -07003842 boolean okToDisplay() {
3843 if (mDisplayId == DEFAULT_DISPLAY) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003844 return !mWmService.mDisplayFrozen
3845 && mWmService.mDisplayEnabled && mWmService.mPolicy.isScreenOn();
David Stevens9440dc82017-03-16 19:00:20 -07003846 }
3847 return mDisplayInfo.state == Display.STATE_ON;
3848 }
3849
3850 boolean okToAnimate() {
3851 return okToDisplay() &&
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003852 (mDisplayId != DEFAULT_DISPLAY || mWmService.mPolicy.okToAnimate());
David Stevens9440dc82017-03-16 19:00:20 -07003853 }
3854
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003855 static final class TaskForResizePointSearchResult {
3856 boolean searchDone;
3857 Task taskForResize;
3858
3859 void reset() {
3860 searchDone = false;
3861 taskForResize = null;
3862 }
3863 }
Robert Carr3b716242016-08-16 16:02:21 -07003864
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003865 private static final class ApplySurfaceChangesTransactionState {
3866 boolean displayHasContent;
3867 boolean obscured;
3868 boolean syswin;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003869 float preferredRefreshRate;
3870 int preferredModeId;
3871
3872 void reset() {
3873 displayHasContent = false;
3874 obscured = false;
3875 syswin = false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003876 preferredRefreshRate = 0;
3877 preferredModeId = 0;
3878 }
3879 }
3880
3881 private static final class ScreenshotApplicationState {
3882 WindowState appWin;
3883 int maxLayer;
3884 int minLayer;
3885 boolean screenshotReady;
3886
3887 void reset(boolean screenshotReady) {
3888 appWin = null;
3889 maxLayer = 0;
3890 minLayer = 0;
3891 this.screenshotReady = screenshotReady;
3892 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3893 }
3894 }
3895
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003896 /**
3897 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3898 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3899 * homogeneous children type which is currently required by sub-classes of
3900 * {@link WindowContainer} class.
3901 */
3902 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3903
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003904 DisplayChildWindowContainer(WindowManagerService service) {
3905 super(service);
3906 }
3907
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003908 @Override
3909 boolean fillsParent() {
3910 return true;
3911 }
3912
3913 @Override
3914 boolean isVisible() {
3915 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003916 }
3917 }
3918
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003919 /**
3920 * Window container class that contains all containers on this display relating to Apps.
3921 * I.e Activities.
3922 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003923 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003924 /**
3925 * A control placed at the appropriate level for transitions to occur.
3926 */
chaviw23ee71c2017-12-18 11:29:41 -08003927 SurfaceControl mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02003928 SurfaceControl mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02003929 SurfaceControl mHomeAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003930
Robert Carrf7a7ca82017-12-06 13:17:07 -08003931 /**
3932 * Given that the split-screen divider does not have an AppWindowToken, it
3933 * will have to live inside of a "NonAppWindowContainer", in particular
3934 * {@link DisplayContent#mAboveAppWindowsContainers}. However, in visual Z order
3935 * it will need to be interleaved with some of our children, appearing on top of
3936 * both docked stacks but underneath any assistant stacks.
3937 *
3938 * To solve this problem we have this anchor control, which will always exist so
3939 * we can always assign it the correct value in our {@link #assignChildLayers}.
3940 * Likewise since it always exists, {@link AboveAppWindowContainers} can always
3941 * assign the divider a layer relative to it. This way we prevent linking lifecycle
3942 * events between the two containers.
3943 */
3944 SurfaceControl mSplitScreenDividerAnchor = null;
3945
Wale Ogunwale61911492017-10-11 08:50:50 -07003946 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3947 // through the list to find them.
3948 private TaskStack mHomeStack = null;
3949 private TaskStack mPinnedStack = null;
3950 private TaskStack mSplitScreenPrimaryStack = null;
3951
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003952 TaskStackContainers(WindowManagerService service) {
3953 super(service);
3954 }
3955
Wale Ogunwale61911492017-10-11 08:50:50 -07003956 /**
3957 * Returns the topmost stack on the display that is compatible with the input windowing mode
3958 * and activity type. Null is no compatible stack on the display.
3959 */
3960 TaskStack getStack(int windowingMode, int activityType) {
3961 if (activityType == ACTIVITY_TYPE_HOME) {
3962 return mHomeStack;
3963 }
3964 if (windowingMode == WINDOWING_MODE_PINNED) {
3965 return mPinnedStack;
3966 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3967 return mSplitScreenPrimaryStack;
3968 }
3969 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3970 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003971 if (activityType == ACTIVITY_TYPE_UNDEFINED
3972 && windowingMode == stack.getWindowingMode()) {
3973 // Passing in undefined type means we want to match the topmost stack with the
3974 // windowing mode.
3975 return stack;
3976 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003977 if (stack.isCompatible(windowingMode, activityType)) {
3978 return stack;
3979 }
3980 }
3981 return null;
3982 }
3983
3984 @VisibleForTesting
3985 TaskStack getTopStack() {
3986 return mTaskStackContainers.getChildCount() > 0
3987 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3988 }
3989
3990 TaskStack getHomeStack() {
3991 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3992 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3993 }
3994 return mHomeStack;
3995 }
3996
3997 TaskStack getPinnedStack() {
3998 return mPinnedStack;
3999 }
4000
Matthew Ng64e77cf2017-10-31 14:01:31 -07004001 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07004002 return mSplitScreenPrimaryStack;
4003 }
4004
Winson Chunge2d72172018-01-25 17:46:20 +00004005 ArrayList<Task> getVisibleTasks() {
4006 final ArrayList<Task> visibleTasks = new ArrayList<>();
4007 forAllTasks(task -> {
4008 if (task.isVisible()) {
4009 visibleTasks.add(task);
4010 }
4011 });
4012 return visibleTasks;
4013 }
4014
Andrii Kulian839def92016-11-02 10:58:58 -07004015 /**
4016 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07004017 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07004018 */
4019 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07004020 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004021 addChild(stack, onTop);
4022 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07004023 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004024
Wale Ogunwale61911492017-10-11 08:50:50 -07004025 void onStackWindowingModeChanged(TaskStack stack) {
4026 removeStackReferenceIfNeeded(stack);
4027 addStackReferenceIfNeeded(stack);
4028 if (stack == mPinnedStack && getTopStack() != stack) {
4029 // Looks like this stack changed windowing mode to pinned. Move it to the top.
4030 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
4031 }
4032 }
4033
4034 private void addStackReferenceIfNeeded(TaskStack stack) {
4035 if (stack.isActivityTypeHome()) {
4036 if (mHomeStack != null) {
Louis Chang7d0037c2018-08-13 12:42:06 +08004037 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
Wale Ogunwale61911492017-10-11 08:50:50 -07004038 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
Louis Chang7d0037c2018-08-13 12:42:06 +08004039
Wale Ogunwale61911492017-10-11 08:50:50 -07004040 }
Louis Chang7d0037c2018-08-13 12:42:06 +08004041 mHomeStack = stack;
Wale Ogunwale61911492017-10-11 08:50:50 -07004042 }
4043 final int windowingMode = stack.getWindowingMode();
4044 if (windowingMode == WINDOWING_MODE_PINNED) {
4045 if (mPinnedStack != null) {
4046 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
4047 + mPinnedStack + " already exist on display=" + this
4048 + " stack=" + stack);
4049 }
4050 mPinnedStack = stack;
4051 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
4052 if (mSplitScreenPrimaryStack != null) {
4053 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
4054 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
4055 + " already exist on display=" + this + " stack=" + stack);
4056 }
4057 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004058 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07004059 }
4060 }
4061
4062 private void removeStackReferenceIfNeeded(TaskStack stack) {
4063 if (stack == mHomeStack) {
4064 mHomeStack = null;
4065 } else if (stack == mPinnedStack) {
4066 mPinnedStack = null;
4067 } else if (stack == mSplitScreenPrimaryStack) {
4068 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004069 // Re-set the split-screen create mode whenever the split-screen stack is removed.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004070 mWmService.setDockedStackCreateStateLocked(
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004071 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
4072 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07004073 }
Andrii Kulian839def92016-11-02 10:58:58 -07004074 }
4075
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004076 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004077 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
4078 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004079 addChild(stack, addIndex);
4080 setLayoutNeeded();
4081 }
4082
Wale Ogunwale61911492017-10-11 08:50:50 -07004083 @Override
4084 protected void removeChild(TaskStack stack) {
4085 super.removeChild(stack);
4086 removeStackReferenceIfNeeded(stack);
4087 }
Bryce Lee00d586d2017-07-28 20:48:43 -07004088
4089 @Override
4090 boolean isOnTop() {
4091 // Considered always on top
4092 return true;
4093 }
4094
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004095 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08004096 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07004097 if (child.getWindowConfiguration().isAlwaysOnTop()
4098 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08004099 // This stack is always-on-top, override the default behavior.
4100 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
4101
4102 // Moving to its current position, as we must call super but we don't want to
4103 // perform any meaningful action.
4104 final int currentPosition = mChildren.indexOf(child);
4105 super.positionChildAt(currentPosition, child, false /* includingParents */);
4106 return;
4107 }
4108
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004109 final int targetPosition = findPositionForStack(position, child, false /* adding */);
4110 super.positionChildAt(targetPosition, child, includingParents);
4111
Riddle Hsu2f6e1742018-08-23 22:44:36 +08004112 if (includingParents) {
4113 // We still want to move the display of this stack container to top because even the
4114 // target position is adjusted to non-top, the intention of the condition is to have
4115 // higher z-order to gain focus (e.g. moving a task of a fullscreen stack to front
4116 // in a non-top display which is using picture-in-picture mode).
4117 final int topChildPosition = getChildCount() - 1;
4118 if (targetPosition < topChildPosition && position >= topChildPosition) {
4119 getParent().positionChildAt(POSITION_TOP, this /* child */,
4120 true /* includingParents */);
4121 }
4122 }
4123
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004124 setLayoutNeeded();
4125 }
4126
4127 /**
4128 * When stack is added or repositioned, find a proper position for it.
4129 * This will make sure that pinned stack always stays on top.
4130 * @param requestedPosition Position requested by caller.
4131 * @param stack Stack to be added or positioned.
4132 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
4133 * @return The proper position for the stack.
4134 */
4135 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09004136 if (stack.inPinnedWindowingMode()) {
Kazuki Takisef85197b2018-06-18 18:18:36 +09004137 return POSITION_TOP;
Kazuki Takise148d00a2018-05-31 15:32:19 +09004138 }
4139
Kazuki Takisef85197b2018-06-18 18:18:36 +09004140 final int topChildPosition = mChildren.size() - 1;
4141 int belowAlwaysOnTopPosition = POSITION_BOTTOM;
4142 for (int i = topChildPosition; i >= 0; --i) {
4143 if (getStacks().get(i) != stack && !getStacks().get(i).isAlwaysOnTop()) {
4144 belowAlwaysOnTopPosition = i;
4145 break;
4146 }
4147 }
4148
4149 // The max possible position we can insert the stack at.
4150 int maxPosition = POSITION_TOP;
4151 // The min possible position we can insert the stack at.
4152 int minPosition = POSITION_BOTTOM;
4153
4154 if (stack.isAlwaysOnTop()) {
4155 if (hasPinnedStack()) {
4156 // Always-on-top stacks go below the pinned stack.
4157 maxPosition = getStacks().indexOf(mPinnedStack) - 1;
4158 }
4159 // Always-on-top stacks need to be above all other stacks.
4160 minPosition = belowAlwaysOnTopPosition !=
4161 POSITION_BOTTOM ? belowAlwaysOnTopPosition : topChildPosition;
4162 } else {
4163 // Other stacks need to be below the always-on-top stacks.
4164 maxPosition = belowAlwaysOnTopPosition !=
Arthur Hung928d8dc2018-07-18 15:38:05 +08004165 POSITION_BOTTOM ? belowAlwaysOnTopPosition : 0;
Kazuki Takisef85197b2018-06-18 18:18:36 +09004166 }
4167
4168 int targetPosition = requestedPosition;
4169 targetPosition = Math.min(targetPosition, maxPosition);
4170 targetPosition = Math.max(targetPosition, minPosition);
4171
4172 int prevPosition = getStacks().indexOf(stack);
4173 // The positions we calculated above (maxPosition, minPosition) do not take into
4174 // consideration the following edge cases.
4175 // 1) We need to adjust the position depending on the value "adding".
4176 // 2) When we are moving a stack to another position, we also need to adjust the
4177 // position depending on whether the stack is moving to a higher or lower position.
4178 if ((targetPosition != requestedPosition) &&
4179 (adding || targetPosition < prevPosition)) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09004180 targetPosition++;
4181 }
4182
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004183 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08004184 }
4185
4186 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004187 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
4188 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004189 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004190 if (super.forAllWindows(callback, traverseTopToBottom)) {
4191 return true;
4192 }
4193 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4194 return true;
4195 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004196 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004197 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4198 return true;
4199 }
4200 if (super.forAllWindows(callback, traverseTopToBottom)) {
4201 return true;
4202 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004203 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004204 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004205 }
4206
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004207 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004208 boolean traverseTopToBottom) {
4209 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
4210 // app tokens.
4211 // TODO: Investigate if we need to continue to do this or if we can just process them
4212 // in-order.
4213 if (traverseTopToBottom) {
4214 for (int i = mChildren.size() - 1; i >= 0; --i) {
4215 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4216 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004217 if (appTokens.get(j).forAllWindowsUnchecked(callback,
4218 traverseTopToBottom)) {
4219 return true;
4220 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004221 }
4222 }
4223 } else {
4224 final int count = mChildren.size();
4225 for (int i = 0; i < count; ++i) {
4226 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4227 final int appTokensCount = appTokens.size();
4228 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004229 if (appTokens.get(j).forAllWindowsUnchecked(callback,
4230 traverseTopToBottom)) {
4231 return true;
4232 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004233 }
4234 }
4235 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004236 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004237 }
4238
Wale Ogunwale1666e312016-12-16 11:27:18 -08004239 void setExitingTokensHasVisible(boolean hasVisible) {
4240 for (int i = mChildren.size() - 1; i >= 0; --i) {
4241 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4242 for (int j = appTokens.size() - 1; j >= 0; --j) {
4243 appTokens.get(j).hasVisible = hasVisible;
4244 }
4245 }
4246 }
4247
4248 void removeExistingAppTokensIfPossible() {
4249 for (int i = mChildren.size() - 1; i >= 0; --i) {
4250 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4251 for (int j = appTokens.size() - 1; j >= 0; --j) {
4252 final AppWindowToken token = appTokens.get(j);
lumark588a3e82018-07-20 18:53:54 +08004253 if (!token.hasVisible && !mClosingApps.contains(token)
Wale Ogunwale1666e312016-12-16 11:27:18 -08004254 && (!token.mIsExiting || token.isEmpty())) {
4255 // Make sure there is no animation running on this token, so any windows
4256 // associated with it will be removed as soon as their animations are
4257 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02004258 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004259 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
4260 "performLayout: App token exiting now removed" + token);
4261 token.removeIfPossible();
4262 }
4263 }
4264 }
4265 }
4266
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004267 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004268 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004269 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
4270 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004271 // Apps and their containers are not allowed to specify an orientation while the
Wale Ogunwaled0c225c2018-06-15 14:47:46 -07004272 // docked or freeform stack is visible...except for the home stack if the docked
4273 // stack is minimized and it actually set something and the bounds is different from
4274 // the display.
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004275 if (mHomeStack != null && mHomeStack.isVisible()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004276 && mDividerControllerLocked.isMinimizedDock()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004277 && !(mDividerControllerLocked.isHomeStackResizable()
4278 && mHomeStack.matchParentBounds())) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004279 final int orientation = mHomeStack.getOrientation();
4280 if (orientation != SCREEN_ORIENTATION_UNSET) {
4281 return orientation;
4282 }
4283 }
4284 return SCREEN_ORIENTATION_UNSPECIFIED;
4285 }
4286
4287 final int orientation = super.getOrientation();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004288 boolean isCar = mWmService.mContext.getPackageManager().hasSystemFeature(
Dean Harding3e5a1522017-10-06 09:19:01 -07004289 PackageManager.FEATURE_AUTOMOTIVE);
4290 if (isCar) {
4291 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
4292 // allow anything but the default orientation.
4293 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004294 "Forcing UNSPECIFIED orientation in car for display id=" + mDisplayId
4295 + ". Ignoring " + orientation);
Dean Harding3e5a1522017-10-06 09:19:01 -07004296 return SCREEN_ORIENTATION_UNSPECIFIED;
4297 }
4298
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004299 if (orientation != SCREEN_ORIENTATION_UNSET
4300 && orientation != SCREEN_ORIENTATION_BEHIND) {
4301 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004302 "App is requesting an orientation, return " + orientation
4303 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004304 return orientation;
4305 }
4306
4307 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004308 "No app is requesting an orientation, return " + mLastOrientation
4309 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004310 // The next app has not been requested to be visible, so we keep the current orientation
4311 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08004312 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004313 }
Robert Carrb1579c82017-09-05 14:54:47 -07004314
4315 @Override
4316 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004317 assignStackOrdering(t);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004318
Robert Carr2f8aa392018-01-31 14:46:51 -08004319 for (int i = 0; i < mChildren.size(); i++) {
4320 final TaskStack s = mChildren.get(i);
4321 s.assignChildLayers(t);
4322 }
4323 }
4324
4325 void assignStackOrdering(SurfaceControl.Transaction t) {
Jorim Jaggibe418292018-03-26 16:14:12 +02004326
Robert Carrf7a7ca82017-12-06 13:17:07 -08004327 final int HOME_STACK_STATE = 0;
4328 final int NORMAL_STACK_STATE = 1;
4329 final int ALWAYS_ON_TOP_STATE = 2;
4330
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004331 int layer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08004332 int layerForAnimationLayer = 0;
Jorim Jaggibe418292018-03-26 16:14:12 +02004333 int layerForBoostedAnimationLayer = 0;
Jorim Jaggi391790622018-04-18 15:30:44 +02004334 int layerForHomeAnimationLayer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08004335
Robert Carrf7a7ca82017-12-06 13:17:07 -08004336 for (int state = 0; state <= ALWAYS_ON_TOP_STATE; state++) {
4337 for (int i = 0; i < mChildren.size(); i++) {
4338 final TaskStack s = mChildren.get(i);
Robert Carr2f8aa392018-01-31 14:46:51 -08004339 if (state == HOME_STACK_STATE && !s.isActivityTypeHome()) {
4340 continue;
4341 } else if (state == NORMAL_STACK_STATE && (s.isActivityTypeHome()
4342 || s.isAlwaysOnTop())) {
4343 continue;
4344 } else if (state == ALWAYS_ON_TOP_STATE && !s.isAlwaysOnTop()) {
4345 continue;
4346 }
4347 s.assignLayer(t, layer++);
4348 if (s.inSplitScreenWindowingMode() && mSplitScreenDividerAnchor != null) {
4349 t.setLayer(mSplitScreenDividerAnchor, layer++);
4350 }
Winson Chungd41f71d2018-03-16 15:26:07 -07004351 if ((s.isTaskAnimating() || s.isAppAnimating())
4352 && state != ALWAYS_ON_TOP_STATE) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004353 // Ensure the animation layer ends up above the
4354 // highest animating stack and no higher.
4355 layerForAnimationLayer = layer++;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004356 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004357 if (state != ALWAYS_ON_TOP_STATE) {
4358 layerForBoostedAnimationLayer = layer++;
4359 }
Robert Carrf7a7ca82017-12-06 13:17:07 -08004360 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004361 if (state == HOME_STACK_STATE) {
4362 layerForHomeAnimationLayer = layer++;
4363 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004364 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004365 if (mAppAnimationLayer != null) {
4366 t.setLayer(mAppAnimationLayer, layerForAnimationLayer);
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004367 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004368 if (mBoostedAppAnimationLayer != null) {
4369 t.setLayer(mBoostedAppAnimationLayer, layerForBoostedAnimationLayer);
4370 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004371 if (mHomeAppAnimationLayer != null) {
4372 t.setLayer(mHomeAppAnimationLayer, layerForHomeAnimationLayer);
4373 }
Robert Carrb1579c82017-09-05 14:54:47 -07004374 }
4375
4376 @Override
Jorim Jaggi391790622018-04-18 15:30:44 +02004377 SurfaceControl getAppAnimationLayer(@AnimationLayer int animationLayer) {
4378 switch (animationLayer) {
4379 case ANIMATION_LAYER_BOOSTED:
4380 return mBoostedAppAnimationLayer;
4381 case ANIMATION_LAYER_HOME:
4382 return mHomeAppAnimationLayer;
4383 case ANIMATION_LAYER_STANDARD:
4384 default:
4385 return mAppAnimationLayer;
4386 }
chaviw23ee71c2017-12-18 11:29:41 -08004387 }
4388
Robert Carrf7a7ca82017-12-06 13:17:07 -08004389 SurfaceControl getSplitScreenDividerAnchor() {
4390 return mSplitScreenDividerAnchor;
4391 }
4392
chaviw23ee71c2017-12-18 11:29:41 -08004393 @Override
Robert Carrb1579c82017-09-05 14:54:47 -07004394 void onParentSet() {
4395 super.onParentSet();
4396 if (getParent() != null) {
chaviw23ee71c2017-12-18 11:29:41 -08004397 mAppAnimationLayer = makeChildSurface(null)
4398 .setName("animationLayer")
4399 .build();
Jorim Jaggibe418292018-03-26 16:14:12 +02004400 mBoostedAppAnimationLayer = makeChildSurface(null)
4401 .setName("boostedAnimationLayer")
4402 .build();
Jorim Jaggi391790622018-04-18 15:30:44 +02004403 mHomeAppAnimationLayer = makeChildSurface(null)
4404 .setName("homeAnimationLayer")
4405 .build();
Robert Carrf7a7ca82017-12-06 13:17:07 -08004406 mSplitScreenDividerAnchor = makeChildSurface(null)
4407 .setName("splitScreenDividerAnchor")
4408 .build();
4409 getPendingTransaction()
4410 .show(mAppAnimationLayer)
Jorim Jaggibe418292018-03-26 16:14:12 +02004411 .show(mBoostedAppAnimationLayer)
Jorim Jaggi391790622018-04-18 15:30:44 +02004412 .show(mHomeAppAnimationLayer)
Robert Carrf7a7ca82017-12-06 13:17:07 -08004413 .show(mSplitScreenDividerAnchor);
chaviw23ee71c2017-12-18 11:29:41 -08004414 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07004415 } else {
chaviw23ee71c2017-12-18 11:29:41 -08004416 mAppAnimationLayer.destroy();
4417 mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02004418 mBoostedAppAnimationLayer.destroy();
4419 mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02004420 mHomeAppAnimationLayer.destroy();
4421 mHomeAppAnimationLayer = null;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004422 mSplitScreenDividerAnchor.destroy();
4423 mSplitScreenDividerAnchor = null;
Robert Carrb1579c82017-09-05 14:54:47 -07004424 }
4425 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004426 }
4427
Robert Carree4d4b92017-11-22 12:21:46 -08004428 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004429 AboveAppWindowContainers(String name, WindowManagerService service) {
4430 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08004431 }
4432
Robert Carrb9506032018-02-13 13:54:00 -08004433 @Override
Vishnu Nair83537a72018-07-19 21:27:48 -07004434 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
4435 final SurfaceControl.Builder builder = super.makeChildSurface(child);
4436 if (child instanceof WindowToken && ((WindowToken) child).mRoundedCornerOverlay) {
4437 // To draw above the ColorFade layer during the screen off transition, the
4438 // rounded corner overlays need to be at the root of the surface hierarchy.
4439 // TODO: move the ColorLayer into the display overlay layer such that this is not
4440 // necessary anymore.
4441 builder.setParent(null);
4442 }
4443 return builder;
4444 }
4445
4446 @Override
Robert Carrb9506032018-02-13 13:54:00 -08004447 void assignChildLayers(SurfaceControl.Transaction t) {
4448 assignChildLayers(t, null /* imeContainer */);
4449 }
4450
Robert Carree4d4b92017-11-22 12:21:46 -08004451 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
4452 boolean needAssignIme = imeContainer != null
4453 && imeContainer.getSurfaceControl() != null;
4454 for (int j = 0; j < mChildren.size(); ++j) {
4455 final WindowToken wt = mChildren.get(j);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004456
4457 // See {@link mSplitScreenDividerAnchor}
4458 if (wt.windowType == TYPE_DOCK_DIVIDER) {
4459 wt.assignRelativeLayer(t, mTaskStackContainers.getSplitScreenDividerAnchor(), 1);
4460 continue;
4461 }
Vishnu Nair83537a72018-07-19 21:27:48 -07004462 if (wt.mRoundedCornerOverlay) {
4463 wt.assignLayer(t, WindowManagerPolicy.COLOR_FADE_LAYER + 1);
4464 continue;
4465 }
Robert Carree4d4b92017-11-22 12:21:46 -08004466 wt.assignLayer(t, j);
4467 wt.assignChildLayers(t);
4468
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004469 int layer = mWmService.mPolicy.getWindowLayerFromTypeLw(
Robert Carree4d4b92017-11-22 12:21:46 -08004470 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08004471
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004472 if (needAssignIme && layer >= mWmService.mPolicy.getWindowLayerFromTypeLw(
Robert Carrb8e78fa2017-11-28 13:07:58 -08004473 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004474 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08004475 needAssignIme = false;
4476 }
4477 }
4478 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004479 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08004480 }
4481 }
4482 }
4483
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004484 /**
4485 * Window container class that contains all containers on this display that are not related to
4486 * Apps. E.g. status bar.
4487 */
Robert Carree4d4b92017-11-22 12:21:46 -08004488 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07004489 /**
4490 * Compares two child window tokens returns -1 if the first is lesser than the second in
4491 * terms of z-order and 1 otherwise.
4492 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004493 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07004494 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004495 mWmService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004496 token1.mOwnerCanManageAppTokens)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004497 < mWmService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004498 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004499
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004500 private final Predicate<WindowState> mGetOrientingWindow = w -> {
4501 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
4502 return false;
4503 }
4504 final int req = w.mAttrs.screenOrientation;
4505 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
4506 || req == SCREEN_ORIENTATION_UNSET) {
4507 return false;
4508 }
4509 return true;
4510 };
4511
Wale Ogunwale3a931692016-11-02 16:49:48 -07004512 private final String mName;
chaviwf29223e2018-01-31 13:23:51 -08004513 private final Dimmer mDimmer = new Dimmer(this);
4514 private final Rect mTmpDimBoundsRect = new Rect();
4515
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004516 NonAppWindowContainers(String name, WindowManagerService service) {
4517 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07004518 mName = name;
4519 }
4520
4521 void addChild(WindowToken token) {
4522 addChild(token, mWindowComparator);
4523 }
4524
4525 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004526 int getOrientation() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004527 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004528 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004529 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004530
4531 if (win != null) {
4532 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004533 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004534 mLastKeyguardForcedOrientation = req;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004535 if (mWmService.mKeyguardGoingAway) {
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07004536 // Keyguard can't affect the orientation if it is going away...
4537 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
4538 return SCREEN_ORIENTATION_UNSET;
4539 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004540 }
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004541 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req
4542 + " for display id=" + mDisplayId);
Andrii Kulian8ee72852017-03-10 10:36:45 -08004543 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004544 }
4545
Andrii Kulian8ee72852017-03-10 10:36:45 -08004546 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004547
Jorim Jaggi1c530592018-04-06 15:11:47 +02004548 // Only allow force setting the orientation when all unknown visibilities have been
4549 // resolved, as otherwise we just may be starting another occluding activity.
4550 final boolean isUnoccluding =
lumark588a3e82018-07-20 18:53:54 +08004551 mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
4552 && mUnknownAppVisibilityController.allResolved();
Jorim Jaggi1c530592018-04-06 15:11:47 +02004553 if (policy.isKeyguardShowingAndNotOccluded() || isUnoccluding) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004554 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004555 }
4556
4557 return SCREEN_ORIENTATION_UNSET;
4558 }
4559
4560 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07004561 String getName() {
4562 return mName;
4563 }
chaviwf29223e2018-01-31 13:23:51 -08004564
4565 @Override
4566 Dimmer getDimmer() {
4567 return mDimmer;
4568 }
4569
4570 @Override
4571 void prepareSurfaces() {
4572 mDimmer.resetDimStates();
4573 super.prepareSurfaces();
4574 getBounds(mTmpDimBoundsRect);
4575
4576 if (mDimmer.updateDims(getPendingTransaction(), mTmpDimBoundsRect)) {
4577 scheduleAnimation();
4578 }
4579 }
Robert Carr3b716242016-08-16 16:02:21 -07004580 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01004581
Robert Carr9034d962018-01-04 18:27:42 -08004582 private class NonMagnifiableWindowContainers extends NonAppWindowContainers {
4583 NonMagnifiableWindowContainers(String name, WindowManagerService service) {
4584 super(name, service);
4585 }
4586
4587 @Override
4588 void applyMagnificationSpec(Transaction t, MagnificationSpec spec) {
4589 }
4590 };
4591
Robert Carrb1579c82017-09-05 14:54:47 -07004592 SurfaceControl.Builder makeSurface(SurfaceSession s) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004593 return mWmService.makeSurfaceBuilder(s)
Robert Carrb1579c82017-09-05 14:54:47 -07004594 .setParent(mWindowingLayer);
4595 }
4596
4597 @Override
4598 SurfaceSession getSession() {
4599 return mSession;
4600 }
4601
4602 @Override
4603 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07004604 SurfaceSession s = child != null ? child.getSession() : getSession();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004605 final SurfaceControl.Builder b = mWmService.makeSurfaceBuilder(s);
Robert Carrf59b8dd2017-10-02 18:58:36 -07004606 if (child == null) {
4607 return b;
4608 }
4609
Robert Carree4d4b92017-11-22 12:21:46 -08004610 return b.setName(child.getName())
4611 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07004612 }
4613
4614 /**
4615 * The makeSurface variants are for use by the window-container
4616 * hierarchy. makeOverlay here is a function for various non windowing
4617 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
4618 * and other potpourii.
4619 */
4620 SurfaceControl.Builder makeOverlay() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004621 return mWmService.makeSurfaceBuilder(mSession)
Robert Carrb1579c82017-09-05 14:54:47 -07004622 .setParent(mOverlayLayer);
4623 }
4624
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004625 /**
4626 * Reparents the given surface to mOverlayLayer.
4627 */
4628 void reparentToOverlay(Transaction transaction, SurfaceControl surface) {
4629 transaction.reparent(surface, mOverlayLayer.getHandle());
4630 }
4631
Robert Carrb1579c82017-09-05 14:54:47 -07004632 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carr24be9ab2018-04-30 17:54:53 -07004633 if (spec.scale != 1.0) {
4634 mMagnificationSpec = spec;
4635 } else {
4636 mMagnificationSpec = null;
4637 }
4638
Robert Carrf59b8dd2017-10-02 18:58:36 -07004639 applyMagnificationSpec(getPendingTransaction(), spec);
4640 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07004641 }
4642
Robert Carr24be9ab2018-04-30 17:54:53 -07004643 void reapplyMagnificationSpec() {
4644 if (mMagnificationSpec != null) {
4645 applyMagnificationSpec(getPendingTransaction(), mMagnificationSpec);
4646 }
4647 }
4648
Robert Carrb1579c82017-09-05 14:54:47 -07004649 @Override
4650 void onParentSet() {
4651 // Since we are the top of the SurfaceControl hierarchy here
4652 // we create the root surfaces explicitly rather than chaining
4653 // up as the default implementation in onParentSet does. So we
4654 // explicitly do NOT call super here.
4655 }
4656
4657 @Override
4658 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07004659
4660 // These are layers as children of "mWindowingLayer"
4661 mBelowAppWindowsContainers.assignLayer(t, 0);
4662 mTaskStackContainers.assignLayer(t, 1);
4663 mAboveAppWindowsContainers.assignLayer(t, 2);
4664
lumarkff0ab692018-11-05 20:32:30 +08004665 final WindowState imeTarget = mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08004666 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08004667
Robert Carree4d4b92017-11-22 12:21:46 -08004668 // In the case where we have an IME target that is not in split-screen
4669 // mode IME assignment is easy. We just need the IME to go directly above
4670 // the target. This way children of the target will naturally go above the IME
4671 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08004672 //
Robert Carree4d4b92017-11-22 12:21:46 -08004673 // In the case of split-screen windowing mode, we need to elevate the IME above the
4674 // docked divider while keeping the app itself below the docked divider, so instead
4675 // we use relative layering of the IME targets child windows, and place the
4676 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08004677 //
Robert Carr234b6332018-03-20 13:38:16 -07004678 // In the case the IME target is animating, the animation Z order may be different
4679 // than the WindowContainer Z order, so it's difficult to be sure we have the correct
4680 // IME target. In this case we just layer the IME over all transitions by placing it in the
4681 // above applications layer.
4682 //
Robert Carree4d4b92017-11-22 12:21:46 -08004683 // In the case where we have no IME target we assign it where it's base layer would
4684 // place it in the AboveAppWindowContainers.
Robert Carr234b6332018-03-20 13:38:16 -07004685 if (imeTarget != null && !(imeTarget.inSplitScreenWindowingMode()
4686 || imeTarget.mToken.isAppAnimating())
Robert Carree4d4b92017-11-22 12:21:46 -08004687 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004688 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07004689 // TODO: We need to use an extra level on the app surface to ensure
4690 // this is always above SurfaceView but always below attached window.
4691 1);
Robert Carree4d4b92017-11-22 12:21:46 -08004692 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07004693 }
4694
4695 // Above we have assigned layers to our children, now we ask them to assign
4696 // layers to their children.
4697 mBelowAppWindowsContainers.assignChildLayers(t);
4698 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08004699 mAboveAppWindowsContainers.assignChildLayers(t,
4700 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07004701 mImeWindowsContainers.assignChildLayers(t);
4702 }
4703
4704 /**
4705 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
4706 * that the IME target is one of the docked applications. We'd like the docked divider to be
4707 * above both of the applications, and we'd like the IME to be above the docked divider.
4708 * However we need child windows of the applications to be above the IME (Text drag handles).
4709 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
4710 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
Tiger Huang7c610aa2018-10-27 00:01:01 +08004711 * with {@link WindowState#assignLayer}
Robert Carrb1579c82017-09-05 14:54:47 -07004712 */
4713 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
Robert Carrbb5de662018-04-26 13:29:51 -07004714 child.assignRelativeLayer(t, mImeWindowsContainers.getSurfaceControl(), 1);
Robert Carrb1579c82017-09-05 14:54:47 -07004715 }
4716
4717 @Override
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004718 void prepareSurfaces() {
4719 final ScreenRotationAnimation screenRotationAnimation =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004720 mWmService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004721 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
4722 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
4723 mPendingTransaction.setMatrix(mWindowingLayer,
4724 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
4725 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
4726 mPendingTransaction.setPosition(mWindowingLayer,
4727 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
4728 mPendingTransaction.setAlpha(mWindowingLayer,
4729 screenRotationAnimation.getEnterTransformation().getAlpha());
4730 }
Chavi Weingartenb736e322018-02-23 00:27:54 +00004731
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004732 super.prepareSurfaces();
4733 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004734
Jorim Jaggibe418292018-03-26 16:14:12 +02004735 void assignStackOrdering() {
4736 mTaskStackContainers.assignStackOrdering(getPendingTransaction());
Robert Carr2f8aa392018-01-31 14:46:51 -08004737 }
Chavi Weingarten3a748552018-05-14 17:32:42 +00004738
4739 /**
4740 * Increment the deferral count to determine whether to update the IME target.
4741 */
4742 void deferUpdateImeTarget() {
4743 mDeferUpdateImeTargetCount++;
4744 }
4745
4746 /**
4747 * Decrement the deferral count to determine whether to update the IME target. If the count
4748 * reaches 0, a new ime target will get computed.
4749 */
4750 void continueUpdateImeTarget() {
4751 if (mDeferUpdateImeTargetCount == 0) {
4752 return;
4753 }
4754
4755 mDeferUpdateImeTargetCount--;
4756 if (mDeferUpdateImeTargetCount == 0) {
4757 computeImeTarget(true /* updateImeTarget */);
4758 }
4759 }
4760
4761 /**
4762 * @return Whether a new IME target should be computed.
4763 */
4764 private boolean canUpdateImeTarget() {
4765 return mDeferUpdateImeTargetCount == 0;
4766 }
Arthur Hung95b38a92018-07-20 18:56:12 +08004767
4768 InputMonitor getInputMonitor() {
4769 return mInputMonitor;
4770 }
Jorim Jaggif1292892018-09-10 11:58:13 +02004771
4772 /**
4773 * @return Cached value whether we told display manager that we have content.
4774 */
4775 boolean getLastHasContent() {
4776 return mLastHasContent;
4777 }
Arthur Hungbe5ce212018-09-13 18:41:56 +08004778
4779 void registerPointerEventListener(@NonNull PointerEventListener listener) {
4780 if (mPointerEventDispatcher != null) {
4781 mPointerEventDispatcher.registerInputEventListener(listener);
4782 }
4783 }
4784
4785 void unregisterPointerEventListener(@NonNull PointerEventListener listener) {
4786 if (mPointerEventDispatcher != null) {
4787 mPointerEventDispatcher.unregisterInputEventListener(listener);
4788 }
4789 }
lumark588a3e82018-07-20 18:53:54 +08004790
4791 void prepareAppTransition(@WindowManager.TransitionType int transit,
Wale Ogunwale3a256e62018-12-06 14:41:18 -08004792 boolean alwaysKeepCurrent) {
4793 prepareAppTransition(transit, alwaysKeepCurrent, 0 /* flags */, false /* forceOverride */);
4794 }
4795
4796 void prepareAppTransition(@WindowManager.TransitionType int transit,
lumark588a3e82018-07-20 18:53:54 +08004797 boolean alwaysKeepCurrent, @WindowManager.TransitionFlags int flags,
4798 boolean forceOverride) {
4799 final boolean prepared = mAppTransition.prepareAppTransitionLocked(
4800 transit, alwaysKeepCurrent, flags, forceOverride);
4801 if (prepared && okToAnimate()) {
4802 mSkipAppTransitionAnimation = false;
4803 }
4804 }
4805
4806 void executeAppTransition() {
4807 if (mAppTransition.isTransitionSet()) {
4808 if (DEBUG_APP_TRANSITIONS) {
4809 Slog.w(TAG_WM, "Execute app transition: " + mAppTransition + ", displayId: "
4810 + mDisplayId + " Callers=" + Debug.getCallers(5));
4811 }
4812 mAppTransition.setReady();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004813 mWmService.mWindowPlacerLocked.requestTraversal();
lumark588a3e82018-07-20 18:53:54 +08004814 }
4815 }
4816
4817 /**
4818 * Update pendingLayoutChanges after app transition has finished.
4819 */
4820 void handleAnimatingStoppedAndTransition() {
4821 int changes = 0;
4822
4823 mAppTransition.setIdle();
4824
4825 for (int i = mNoAnimationNotifyOnTransitionFinished.size() - 1; i >= 0; i--) {
4826 final IBinder token = mNoAnimationNotifyOnTransitionFinished.get(i);
4827 mAppTransition.notifyAppTransitionFinishedLocked(token);
4828 }
4829 mNoAnimationNotifyOnTransitionFinished.clear();
4830
4831 mWallpaperController.hideDeferredWallpapersIfNeeded();
4832
4833 onAppTransitionDone();
4834
4835 changes |= FINISH_LAYOUT_REDO_LAYOUT;
4836 if (DEBUG_WALLPAPER_LIGHT) {
4837 Slog.v(TAG_WM, "Wallpaper layer changed: assigning layers + relayout");
4838 }
4839 computeImeTarget(true /* updateImeTarget */);
wilsonshihc32538e2018-11-07 17:27:34 +08004840 mWallpaperMayChange = true;
lumark588a3e82018-07-20 18:53:54 +08004841 // Since the window list has been rebuilt, focus might have to be recomputed since the
4842 // actual order of windows might have changed again.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004843 mWmService.mFocusMayChange = true;
lumark588a3e82018-07-20 18:53:54 +08004844
4845 pendingLayoutChanges |= changes;
4846 }
Andrii Kulian15cfb422018-11-07 13:38:49 -08004847
Wale Ogunwale3a256e62018-12-06 14:41:18 -08004848 /** Check if pending app transition is for activity / task launch. */
4849 boolean isNextTransitionForward() {
4850 final int transit = mAppTransition.getAppTransition();
4851 return transit == TRANSIT_ACTIVITY_OPEN
4852 || transit == TRANSIT_TASK_OPEN
4853 || transit == TRANSIT_TASK_TO_FRONT;
4854 }
4855
Andrii Kulian15cfb422018-11-07 13:38:49 -08004856 /**
4857 * @see Display#FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS
4858 */
4859 boolean supportsSystemDecorations() {
4860 // TODO(b/114338689): Read the setting from DisplaySettings.
4861 return mDisplay.supportsSystemDecorations()
4862 // TODO (b/111363427): Remove this and set the new FLAG_SHOULD_SHOW_LAUNCHER flag
4863 // (b/114338689) whenever vr 2d display id is set.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004864 || mDisplayId == mWmService.mVr2dDisplayId
4865 || mWmService.mForceDesktopModeOnExternalDisplays;
Andrii Kulian15cfb422018-11-07 13:38:49 -08004866 }
chaviwff2e7d82018-11-02 11:11:27 -07004867
4868 /**
4869 * Re-parent the DisplayContent's top surfaces, {@link #mWindowingLayer} and
4870 * {@link #mOverlayLayer} to the specified surfaceControl.
4871 *
4872 * @param surfaceControlHandle The handle for the new SurfaceControl, where the DisplayContent's
4873 * surfaces will be re-parented to.
4874 */
4875 void reparentDisplayContent(IBinder surfaceControlHandle) {
4876 mPendingTransaction.reparent(mWindowingLayer, surfaceControlHandle)
4877 .reparent(mOverlayLayer, surfaceControlHandle);
4878 }
Craig Mautner59c00972012-07-30 12:10:24 -07004879}