blob: bc8e71857af87142832f03c01ceff64592ea7352 [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;
Tarandeep Singh215929b2019-01-11 18:24:37 -080022import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
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;
Tiger Huang86e6d072019-05-02 20:23:47 +080028import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_USER;
Wale Ogunwale687b4272017-07-27 02:56:23 -070029import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
30import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
Jorim Jaggi4981f152019-03-26 18:58:45 +010031import static android.os.Trace.TRACE_TAG_WINDOW_MANAGER;
Adrian Roosbf3bc1b2019-06-18 16:13:53 +020032import static android.util.DisplayMetrics.DENSITY_DEFAULT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070033import static android.view.Display.DEFAULT_DISPLAY;
34import static android.view.Display.FLAG_PRIVATE;
Andrii Kuliandd989612019-02-21 12:13:28 -080035import static android.view.Display.FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS;
Tiger Huang04dc4cc2019-01-17 18:41:41 +080036import static android.view.Display.INVALID_DISPLAY;
Tiger Huang332793b2019-10-29 23:21:27 +080037import static android.view.InsetsState.ITYPE_IME;
38import static android.view.InsetsState.ITYPE_LEFT_GESTURES;
39import static android.view.InsetsState.ITYPE_RIGHT_GESTURES;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070040import static android.view.Surface.ROTATION_0;
41import static android.view.Surface.ROTATION_180;
42import static android.view.Surface.ROTATION_270;
43import static android.view.Surface.ROTATION_90;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070044import static android.view.View.GONE;
Adrian Roosbf3bc1b2019-06-18 16:13:53 +020045import static android.view.View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
46import static android.view.View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
Wale Ogunwale10124582016-09-15 20:25:50 -070047import static android.view.WindowManager.DOCKED_BOTTOM;
48import static android.view.WindowManager.DOCKED_INVALID;
49import static android.view.WindowManager.DOCKED_TOP;
Winson Chungc5fe7ff2019-02-19 14:49:25 -080050import static android.view.WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080051import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
52import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
53import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale494009b82016-10-21 09:01:38 -070054import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070055import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Tiger Huang04dc4cc2019-01-17 18:41:41 +080056import static android.view.WindowManager.LayoutParams.FLAG_SPLIT_TOUCH;
Winson Chungc5fe7ff2019-02-19 14:49:25 -080057import static android.view.WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070058import static android.view.WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE;
59import static android.view.WindowManager.LayoutParams.NEEDS_MENU_UNSET;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070060import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
chaviw8065f442019-11-18 13:20:58 -080061import static android.view.WindowManager.LayoutParams.TYPE_ACCESSIBILITY_MAGNIFICATION_OVERLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070062import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Riddle Hsub2297ad2019-07-26 23:37:25 -060063import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
Wale Ogunwale494009b82016-10-21 09:01:38 -070064import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
Robert Carrf7a7ca82017-12-06 13:17:07 -080065import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070066import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070067import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
68import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Adrian Roos5f2c9a12019-07-03 18:31:46 +020069import static android.view.WindowManager.LayoutParams.TYPE_NAVIGATION_BAR;
Wale Ogunwale494009b82016-10-21 09:01:38 -070070import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070071import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
72import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070073import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070074import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Wale Ogunwale3a256e62018-12-06 14:41:18 -080075import static android.view.WindowManager.TRANSIT_ACTIVITY_OPEN;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080076import static android.view.WindowManager.TRANSIT_KEYGUARD_UNOCCLUDE;
Wale Ogunwale3a256e62018-12-06 14:41:18 -080077import static android.view.WindowManager.TRANSIT_TASK_OPEN;
78import static android.view.WindowManager.TRANSIT_TASK_TO_FRONT;
Tiger Huang51c5a1d2018-12-11 20:24:51 +080079
Adrian Roose99bc052017-11-20 17:55:31 +010080import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
81import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
82import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
83import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080084import static com.android.server.wm.DisplayContentProto.ABOVE_APP_WINDOWS;
lumark588a3e82018-07-20 18:53:54 +080085import static com.android.server.wm.DisplayContentProto.APP_TRANSITION;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080086import static com.android.server.wm.DisplayContentProto.BELOW_APP_WINDOWS;
Louis Changa9350fe2019-04-25 17:14:20 +080087import static com.android.server.wm.DisplayContentProto.CHANGING_APPS;
88import static com.android.server.wm.DisplayContentProto.CLOSING_APPS;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080089import static com.android.server.wm.DisplayContentProto.DISPLAY_FRAMES;
90import static com.android.server.wm.DisplayContentProto.DISPLAY_INFO;
91import static com.android.server.wm.DisplayContentProto.DOCKED_STACK_DIVIDER_CONTROLLER;
92import static com.android.server.wm.DisplayContentProto.DPI;
Tiger Huang1e5b10a2018-07-30 20:19:51 +080093import static com.android.server.wm.DisplayContentProto.FOCUSED_APP;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080094import static com.android.server.wm.DisplayContentProto.ID;
95import static com.android.server.wm.DisplayContentProto.IME_WINDOWS;
Louis Changa9350fe2019-04-25 17:14:20 +080096import static com.android.server.wm.DisplayContentProto.OPENING_APPS;
chaviw8065f442019-11-18 13:20:58 -080097import static com.android.server.wm.DisplayContentProto.OVERLAY_WINDOWS;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080098import static com.android.server.wm.DisplayContentProto.PINNED_STACK_CONTROLLER;
99import static com.android.server.wm.DisplayContentProto.ROTATION;
100import static com.android.server.wm.DisplayContentProto.SCREEN_ROTATION_ANIMATION;
101import static com.android.server.wm.DisplayContentProto.STACKS;
102import static com.android.server.wm.DisplayContentProto.WINDOW_CONTAINER;
Adrian Roosb125e0b2019-10-02 14:55:14 +0200103import static com.android.server.wm.ProtoLogGroup.WM_DEBUG_ADD_REMOVE;
104import static com.android.server.wm.ProtoLogGroup.WM_DEBUG_APP_TRANSITIONS;
105import static com.android.server.wm.ProtoLogGroup.WM_DEBUG_FOCUS;
106import static com.android.server.wm.ProtoLogGroup.WM_DEBUG_FOCUS_LIGHT;
107import static com.android.server.wm.ProtoLogGroup.WM_DEBUG_ORIENTATION;
108import static com.android.server.wm.ProtoLogGroup.WM_DEBUG_SCREEN_ON;
109import static com.android.server.wm.ProtoLogGroup.WM_SHOW_TRANSACTIONS;
lumark9bca6b42019-10-17 18:35:22 +0800110import static com.android.server.wm.WindowContainer.AnimationFlags.PARENTS;
111import static com.android.server.wm.WindowContainer.AnimationFlags.TRANSITION;
Wale Ogunwale10124582016-09-15 20:25:50 -0700112import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700113import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700114import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700115import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700116import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Andrii Kulian839def92016-11-02 10:58:58 -0700117import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700118import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700119import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700120import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800121import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Tiger Huang51c5a1d2018-12-11 20:24:51 +0800122import static com.android.server.wm.WindowManagerService.H.REPORT_FOCUS_CHANGE;
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800123import static com.android.server.wm.WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800124import static com.android.server.wm.WindowManagerService.H.REPORT_LOSING_FOCUS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700125import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700126import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700127import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700128import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800129import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_PLACING_SURFACES;
130import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_REMOVING_FOCUS;
131import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_ASSIGN_LAYERS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700132import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700133import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700134import static com.android.server.wm.WindowManagerService.dipToPixel;
Adrian Roos5f2c9a12019-07-03 18:31:46 +0200135import static com.android.server.wm.WindowState.EXCLUSION_LEFT;
136import static com.android.server.wm.WindowState.EXCLUSION_RIGHT;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700137import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700138import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Adrian Roos2351d5f2019-07-03 15:35:07 +0200139import static com.android.server.wm.utils.RegionUtils.forEachRectReverse;
Adrian Roos4ffc8972019-02-07 20:45:11 +0100140import static com.android.server.wm.utils.RegionUtils.rectListToRegion;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700141
lumark588a3e82018-07-20 18:53:54 +0800142import android.animation.AnimationHandler;
Riddle Hsuf53da812018-08-15 22:00:27 +0800143import android.annotation.IntDef;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700144import android.annotation.NonNull;
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200145import android.annotation.Nullable;
Riddle Hsuccf09402019-08-13 00:33:06 +0800146import android.content.pm.ActivityInfo.ScreenOrientation;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700147import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700148import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700149import android.graphics.Bitmap;
Issei Suzukia5dbf522019-02-01 17:58:15 +0100150import android.graphics.Insets;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700151import android.graphics.Matrix;
Tiger Huangd8ec9382019-04-18 14:35:09 -0700152import android.graphics.Point;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800153import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700154import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700155import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100156import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700157import android.hardware.display.DisplayManagerInternal;
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -0500158import android.metrics.LogMaker;
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800159import android.os.Binder;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700160import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700161import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700162import android.os.IBinder;
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800163import android.os.Process;
Adrian Roos4ffc8972019-02-07 20:45:11 +0100164import android.os.RemoteCallbackList;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700165import android.os.RemoteException;
166import android.os.SystemClock;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100167import android.os.Trace;
Riddle Hsuf53da812018-08-15 22:00:27 +0800168import android.os.UserHandle;
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800169import android.util.ArraySet;
Chong Zhang8e89b312015-09-09 15:09:30 -0700170import android.util.DisplayMetrics;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700171import android.util.Slog;
Riddle Hsub2297ad2019-07-26 23:37:25 -0600172import android.util.SparseBooleanArray;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700173import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700174import android.view.Display;
Adrian Roos1cf585052018-01-03 18:43:27 +0100175import android.view.DisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700176import android.view.DisplayInfo;
Riddle Hsua4d6fa22018-08-11 00:50:39 +0800177import android.view.Gravity;
Adrian Roos4ffc8972019-02-07 20:45:11 +0100178import android.view.ISystemGestureExclusionListener;
Arthur Hungbe5ce212018-09-13 18:41:56 +0800179import android.view.InputChannel;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700180import android.view.InputDevice;
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800181import android.view.InputWindowHandle;
Tiger Huang332793b2019-10-29 23:21:27 +0800182import android.view.InsetsState.InternalInsetsType;
Robert Carrb1579c82017-09-05 14:54:47 -0700183import android.view.MagnificationSpec;
Evan Rosky966759f2019-01-15 10:33:58 -0800184import android.view.RemoteAnimationDefinition;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700185import android.view.Surface;
186import android.view.SurfaceControl;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100187import android.view.SurfaceControl.Transaction;
Robert Carrb1579c82017-09-05 14:54:47 -0700188import android.view.SurfaceSession;
Tiger Huang7c610aa2018-10-27 00:01:01 +0800189import android.view.View;
lumark588a3e82018-07-20 18:53:54 +0800190import android.view.WindowManager;
Evan Rosky39b6f232018-10-30 18:35:41 -0700191import android.view.WindowManagerPolicyConstants.PointerEventListener;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700192
Bryce Lee48f4b572017-04-10 10:54:15 -0700193import com.android.internal.annotations.VisibleForTesting;
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -0500194import com.android.internal.logging.MetricsLogger;
195import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800196import com.android.internal.util.ToBooleanFunction;
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200197import com.android.internal.util.function.TriConsumer;
Winson Chungc5fe7ff2019-02-19 14:49:25 -0800198import com.android.internal.util.function.pooled.PooledConsumer;
Wale Ogunwale85fb19a2019-12-05 10:41:05 +0900199import com.android.internal.util.function.pooled.PooledFunction;
Winson Chungc5fe7ff2019-02-19 14:49:25 -0800200import com.android.internal.util.function.pooled.PooledLambda;
Winson Chung6ba767a2018-12-13 18:20:11 -0800201import com.android.server.AnimationThread;
Adrian Roose99bc052017-11-20 17:55:31 +0100202import com.android.server.policy.WindowManagerPolicy;
Adrian Roosb125e0b2019-10-02 14:55:14 +0200203import com.android.server.protolog.common.ProtoLog;
Issei Suzuki43190bd2018-08-20 17:28:41 +0200204import com.android.server.wm.utils.DisplayRotationUtil;
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100205import com.android.server.wm.utils.RotationCache;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100206import com.android.server.wm.utils.WmDisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700207
208import java.io.PrintWriter;
Riddle Hsuf53da812018-08-15 22:00:27 +0800209import java.lang.annotation.Retention;
210import java.lang.annotation.RetentionPolicy;
Craig Mautner59c00972012-07-30 12:10:24 -0700211import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700212import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700213import java.util.HashMap;
214import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700215import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700216import java.util.List;
Adrian Roos1cf585052018-01-03 18:43:27 +0100217import java.util.Objects;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800218import java.util.function.Consumer;
219import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700220
Craig Mautner59c00972012-07-30 12:10:24 -0700221/**
222 * Utility class for keeping track of the WindowStates and other pertinent contents of a
223 * particular Display.
Craig Mautner59c00972012-07-30 12:10:24 -0700224 */
Riddle Hsuad256a12018-07-18 16:11:30 +0800225class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer>
Louis Chang2453d062019-11-19 22:30:48 +0800226 implements WindowManagerPolicy.DisplayContentInfo {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700227 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700228
Riddle Hsuf53da812018-08-15 22:00:27 +0800229 /** The default scaling mode that scales content automatically. */
230 static final int FORCE_SCALING_MODE_AUTO = 0;
231 /** For {@link #setForcedScalingMode} to apply flag {@link Display#FLAG_SCALING_DISABLED}. */
232 static final int FORCE_SCALING_MODE_DISABLED = 1;
233
234 @IntDef(prefix = { "FORCE_SCALING_MODE_" }, value = {
235 FORCE_SCALING_MODE_AUTO,
236 FORCE_SCALING_MODE_DISABLED
237 })
238 @Retention(RetentionPolicy.SOURCE)
239 @interface ForceScalingMode {}
240
Louis Chang2453d062019-11-19 22:30:48 +0800241 ActivityTaskManagerService mAtmService;
Craig Mautner59c00972012-07-30 12:10:24 -0700242
Louis Chang2453d062019-11-19 22:30:48 +0800243 /** Unique identifier of this display. */
244 private final int mDisplayId;
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800245
chaviw8065f442019-11-18 13:20:58 -0800246 /**
247 * Most surfaces will be a child of this window. There are some special layers and windows
248 * which are always on top of others and omitted from Screen-Magnification, for example the
249 * strict mode flash or the magnification overlay itself. Those layers will be children of
250 * {@link #mOverlayContainers} where mWindowContainers contains everything else.
251 */
252 private final WindowContainers mWindowContainers =
253 new WindowContainers("mWindowContainers", mWmService);
254
255 // Contains some special windows which are always on top of others and omitted from
256 // Screen-Magnification, for example the WindowMagnification windows.
257 private final NonAppWindowContainers mOverlayContainers =
258 new NonAppWindowContainers("mOverlayContainers", mWmService);
259
260 /** The containers below are the only child containers {@link #mWindowContainers} can have. */
Wale Ogunwale3a931692016-11-02 16:49:48 -0700261 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800262 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers(mWmService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700263 // Contains all non-app window containers that should be displayed above the app containers
264 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800265 private final AboveAppWindowContainers mAboveAppWindowsContainers =
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800266 new AboveAppWindowContainers("mAboveAppWindowsContainers", mWmService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700267 // Contains all non-app window containers that should be displayed below the app containers
268 // (e.g. Wallpaper).
269 private final NonAppWindowContainers mBelowAppWindowsContainers =
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800270 new NonAppWindowContainers("mBelowAppWindowsContainers", mWmService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700271 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
272 // 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 -0800273 // window containers together and move them in-sync if/when needed. We use a subclass of
274 // WindowContainer which is omitted from screen magnification, as the IME is never magnified.
Eugene Susla25a24952019-04-08 13:57:44 -0700275 private final NonAppWindowContainers mImeWindowsContainers =
276 new NonAppWindowContainers("mImeWindowsContainers", mWmService);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700277
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000278 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800279 private WindowState mTmpWindow2;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800280 private boolean mUpdateImeTarget;
281 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700282 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700283
lumark588a3e82018-07-20 18:53:54 +0800284 final AppTransition mAppTransition;
285 final AppTransitionController mAppTransitionController;
286 boolean mSkipAppTransitionAnimation = false;
287
Garfield Tane8d84ab2019-10-11 09:49:40 -0700288 final ArraySet<ActivityRecord> mOpeningApps = new ArraySet<>();
289 final ArraySet<ActivityRecord> mClosingApps = new ArraySet<>();
290 final ArraySet<ActivityRecord> mChangingApps = new ArraySet<>();
lumark588a3e82018-07-20 18:53:54 +0800291 final UnknownAppVisibilityController mUnknownAppVisibilityController;
292 BoundsAnimationController mBoundsAnimationController;
293
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -0500294 private MetricsLogger mMetricsLogger;
295
lumark588a3e82018-07-20 18:53:54 +0800296 /**
297 * List of clients without a transtiton animation that we notify once we are done
298 * transitioning since they won't be notified through the app window animator.
299 */
300 final List<IBinder> mNoAnimationNotifyOnTransitionFinished = new ArrayList<>();
301
Wale Ogunwale02319a62016-09-26 15:21:22 -0700302 // Mapping from a token IBinder to a WindowToken object on this display.
303 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
304
Andrii Kuliancd097992017-03-23 18:31:59 -0700305 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700306 int mInitialDisplayWidth = 0;
307 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700308 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700309
Adrian Roos1cf585052018-01-03 18:43:27 +0100310 DisplayCutout mInitialDisplayCutout;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100311 private final RotationCache<DisplayCutout, WmDisplayCutout> mDisplayCutoutCache
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100312 = new RotationCache<>(this::calculateDisplayCutoutForRotationUncached);
Adrian Roos1cf585052018-01-03 18:43:27 +0100313
Andrii Kuliancd097992017-03-23 18:31:59 -0700314 /**
315 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
316 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
317 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
318 */
Craig Mautner59c00972012-07-30 12:10:24 -0700319 int mBaseDisplayWidth = 0;
320 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700321 /**
322 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
323 * but can be set from Settings or via shell command "adb shell wm density".
324 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
325 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700326 int mBaseDisplayDensity = 0;
Riddle Hsuf53da812018-08-15 22:00:27 +0800327
328 /**
329 * Whether to disable display scaling. This can be set via shell command "adb shell wm scaling".
330 * @see WindowManagerService#setForcedDisplayScalingMode(int, int)
331 */
Jeff Brownd46747a2015-04-15 19:02:36 -0700332 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700333 private final DisplayInfo mDisplayInfo = new DisplayInfo();
334 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700335 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800336 private final DisplayPolicy mDisplayPolicy;
Riddle Hsuccf09402019-08-13 00:33:06 +0800337 private final DisplayRotation mDisplayRotation;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000338 DisplayFrames mDisplayFrames;
339
Adrian Roos4ffc8972019-02-07 20:45:11 +0100340 private final RemoteCallbackList<ISystemGestureExclusionListener>
341 mSystemGestureExclusionListeners = new RemoteCallbackList<>();
342 private final Region mSystemGestureExclusion = new Region();
Adrian Roos5f2c9a12019-07-03 18:31:46 +0200343 private boolean mSystemGestureExclusionWasRestricted = false;
344 private final Region mSystemGestureExclusionUnrestricted = new Region();
Adrian Roosbf3bc1b2019-06-18 16:13:53 +0200345 private int mSystemGestureExclusionLimit;
Adrian Roos4ffc8972019-02-07 20:45:11 +0100346
Andrii Kulian06d07d62017-03-14 11:11:47 -0700347 /**
348 * For default display it contains real metrics, empty for others.
chaviw619da692019-06-10 15:39:40 -0700349 * @see WindowManagerService#createWatermark()
Andrii Kulian06d07d62017-03-14 11:11:47 -0700350 */
351 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800352
353 /** @see #computeCompatSmallestWidth(boolean, int, int, int, DisplayCutout) */
Andrii Kulian06d07d62017-03-14 11:11:47 -0700354 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700355
Andrii Kulian06d07d62017-03-14 11:11:47 -0700356 /**
357 * Compat metrics computed based on {@link #mDisplayMetrics}.
358 * @see #updateDisplayAndOrientation(int)
359 */
360 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
361
362 /** The desired scaling factor for compatible apps. */
363 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700364
Riddle Hsu7f704b52019-12-10 23:10:45 +0800365 /** @see #getCurrentOverrideConfigurationChanges */
366 private int mCurrentOverrideConfigurationChanges;
367
Andrii Kulian8ee72852017-03-10 10:36:45 -0800368 /**
Andrii Kulian8ee72852017-03-10 10:36:45 -0800369 * Orientation forced by some window. If there is no visible window that specifies orientation
370 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
371 *
372 * @see NonAppWindowContainers#getOrientation()
373 */
374 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700375
Andrii Kulian8ee72852017-03-10 10:36:45 -0800376 /**
377 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
378 * occluded.
379 *
380 * @see NonAppWindowContainers#getOrientation()
381 */
382 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
383
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700384 /**
Tiger Huang86e6d072019-05-02 20:23:47 +0800385 * The maximum aspect ratio (longerSide/shorterSide) that is treated as close-to-square. The
386 * orientation requests from apps would be ignored if the display is close-to-square.
387 */
388 @VisibleForTesting
389 final float mCloseToSquareMaxAspectRatio;
390
391 /**
392 * If this is true, we would not rotate the display for apps. The rotation would be either the
393 * sensor rotation or the user rotation, controlled by
394 * {@link WindowManagerPolicy.UserRotationMode}.
395 */
396 private boolean mIgnoreRotationForApps;
397
398 /**
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700399 * Keep track of wallpaper visibility to notify changes.
400 */
401 private boolean mLastWallpaperVisible = false;
402
Andrii Kulian06d07d62017-03-14 11:11:47 -0700403 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700404
Craig Mautner39834192012-09-02 07:47:24 -0700405 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700406 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700407 int pendingLayoutChanges;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800408
Vishnu Nairba183352018-11-21 11:16:49 -0800409 /**
410 * Used to gate application window layout until we have sent the complete configuration.
411 * TODO: There are still scenarios where we may be out of sync with the client. Ideally
412 * we want to replace this flag with a mechanism that will confirm the configuration
413 * applied by the client is the one expected by the system server.
414 */
415 boolean mWaitingForConfig;
416
Andrii Kulian839def92016-11-02 10:58:58 -0700417 // TODO(multi-display): remove some of the usages.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800418 @VisibleForTesting
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800419 boolean isDefaultDisplay;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800420
Andrii Kulianf0379de2018-03-14 16:24:07 -0700421 /**
422 * Flag indicating whether WindowManager should override info for this display in
423 * DisplayManager.
424 */
425 boolean mShouldOverrideDisplayConfiguration = true;
Craig Mautner39834192012-09-02 07:47:24 -0700426
Craig Mautnerdc548482014-02-05 13:35:24 -0800427 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700428 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800429
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700430 /** Detect user tapping outside of current focused task bounds .*/
Riddle Hsu2588ab02019-02-25 14:23:56 +0800431 @VisibleForTesting
432 final TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700433
Craig Mautner6601b7b2013-04-29 10:29:11 -0700434 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700435 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700436
Craig Mautner6601b7b2013-04-29 10:29:11 -0700437 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800438 private final Rect mTmpRect = new Rect();
439 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700440 private final RectF mTmpRectF = new RectF();
441 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800442 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700443
Bryce Leef3c6a472017-11-14 14:53:06 -0800444 /** Used for handing back size of display */
445 private final Rect mTmpBounds = new Rect();
446
Garfield Tan90b04282018-12-11 14:04:42 -0800447 private final Configuration mTmpConfiguration = new Configuration();
448
Craig Mautner95da1082014-02-24 17:54:35 -0800449 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700450 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800451
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700452 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700453 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700454
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800455 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800456 /** A collection of windows that provide tap exclude regions inside of them. */
457 final ArraySet<WindowState> mTapExcludeProvidingWindows = new ArraySet<>();
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800458
Garfield Tane8d84ab2019-10-11 09:49:40 -0700459 private final LinkedList<ActivityRecord> mTmpUpdateAllDrawn = new LinkedList();
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700460
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700461 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
462 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000463 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
464 new ApplySurfaceChangesTransactionState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700465
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700466 // True if this display is in the process of being removed. Used to determine if the removal of
467 // the display's direct children should be allowed.
468 private boolean mRemovingDisplay = false;
469
Bryce Leed1871262017-06-12 14:12:29 -0700470 // {@code false} if this display is in the processing of being created.
471 private boolean mDisplayReady = false;
472
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800473 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700474
wilsonshihc32538e2018-11-07 17:27:34 +0800475 boolean mWallpaperMayChange = false;
476
Robert Carrb1579c82017-09-05 14:54:47 -0700477 private final SurfaceSession mSession = new SurfaceSession();
478
479 /**
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800480 * Window that is currently interacting with the user. This window is responsible for receiving
481 * key events and pointer events from the user.
482 */
483 WindowState mCurrentFocus = null;
484
485 /**
486 * The last focused window that we've notified the client that the focus is changed.
487 */
488 WindowState mLastFocus = null;
489
490 /**
491 * Windows that have lost input focus and are waiting for the new focus window to be displayed
492 * before they are told about this.
493 */
494 ArrayList<WindowState> mLosingFocus = new ArrayList<>();
495
496 /**
497 * The foreground app of this display. Windows below this app cannot be the focused window. If
498 * the user taps on the area outside of the task of the focused app, we will notify AM about the
499 * new task the user wants to interact with.
500 */
Garfield Tane8d84ab2019-10-11 09:49:40 -0700501 ActivityRecord mFocusedApp = null;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800502
503 /** Windows added since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
504 final ArrayList<WindowState> mWinAddedSinceNullFocus = new ArrayList<>();
505
506 /** Windows removed since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
507 final ArrayList<WindowState> mWinRemovedSinceNullFocus = new ArrayList<>();
508
Vadim Caenb3715832019-08-13 17:06:38 +0200509 private ScreenRotationAnimation mScreenRotationAnimation;
510
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800511 /**
Adrian Roos5251b1d2018-03-23 18:57:43 +0100512 * Sequence number for the current layout pass.
513 */
514 int mLayoutSeq = 0;
515
Chavi Weingarten3a748552018-05-14 17:32:42 +0000516 /**
517 * Specifies the count to determine whether to defer updating the IME target until ready.
518 */
519 private int mDeferUpdateImeTargetCount;
520
Robert Carr24be9ab2018-04-30 17:54:53 -0700521 private MagnificationSpec mMagnificationSpec;
522
Arthur Hung95b38a92018-07-20 18:56:12 +0800523 private InputMonitor mInputMonitor;
524
Jorim Jaggif1292892018-09-10 11:58:13 +0200525 /** Caches the value whether told display manager that we have content. */
526 private boolean mLastHasContent;
527
Issei Suzuki43190bd2018-08-20 17:28:41 +0200528 private DisplayRotationUtil mRotationUtil = new DisplayRotationUtil();
529
lumark90120a82018-08-15 00:33:03 +0800530 /**
531 * The input method window for this display.
532 */
533 WindowState mInputMethodWindow;
534
lumarkff0ab692018-11-05 20:32:30 +0800535 /**
536 * This just indicates the window the input method is on top of, not
537 * necessarily the window its input is going to.
538 */
539 WindowState mInputMethodTarget;
540
541 /** If true hold off on modifying the animation layer of mInputMethodTarget */
542 boolean mInputMethodTargetWaitingAnim;
543
Arthur Hungbe5ce212018-09-13 18:41:56 +0800544 private final PointerEventDispatcher mPointerEventDispatcher;
545
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200546 private final InsetsStateController mInsetsStateController;
Jorim Jaggi28620472019-01-02 23:21:49 +0100547 private final InsetsPolicy mInsetsPolicy;
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200548
Tiger Huangd8ec9382019-04-18 14:35:09 -0700549 /** @see #getParentWindow() */
550 private WindowState mParentWindow;
551
552 private Point mLocationInParentWindow = new Point();
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800553 private SurfaceControl mParentSurfaceControl;
554 private InputWindowHandle mPortalWindowHandle;
555
Tiger Huang7c610aa2018-10-27 00:01:01 +0800556 // Last systemUiVisibility we received from status bar.
557 private int mLastStatusBarVisibility = 0;
558 // Last systemUiVisibility we dispatched to windows.
559 private int mLastDispatchedSystemUiVisibility = 0;
560
Louis Changdc077272019-11-12 16:52:56 +0800561 private final ArrayList<ActivityStack> mTmpAlwaysOnTopStacks = new ArrayList<>();
562 private final ArrayList<ActivityStack> mTmpNormalStacks = new ArrayList<>();
563 private final ArrayList<ActivityStack> mTmpHomeStacks = new ArrayList<>();
Issei Suzuki25a9e2b2019-08-14 16:46:26 +0200564
Tiger Huang43b8fc22019-04-26 11:49:29 +0800565 /** Corner radius that windows should have in order to match the display. */
566 private final float mWindowCornerRadius;
567
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800568 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
569 WindowStateAnimator winAnimator = w.mWinAnimator;
Garfield Tane8d84ab2019-10-11 09:49:40 -0700570 final ActivityRecord activity = w.mActivityRecord;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800571 if (winAnimator.mDrawState == READY_TO_SHOW) {
Garfield Tane8d84ab2019-10-11 09:49:40 -0700572 if (activity == null || activity.canShowWindows()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800573 if (w.performShowLocked()) {
574 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
575 if (DEBUG_LAYOUT_REPEATS) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800576 mWmService.mWindowPlacerLocked.debugLayoutRepeats(
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800577 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
578 }
579 }
580 }
581 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800582 };
583
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800584 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
585 final int lostFocusUid = mTmpWindow.mOwnerUid;
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800586 final Handler handler = mWmService.mH;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800587 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
588 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
589 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
590 w.mAttrs.hideTimeoutMilliseconds);
591 }
592 }
593 };
594
595 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
Garfield Tane8d84ab2019-10-11 09:49:40 -0700596 final ActivityRecord focusedApp = mFocusedApp;
Adrian Roosb125e0b2019-10-02 14:55:14 +0200597 ProtoLog.v(WM_DEBUG_FOCUS, "Looking for focus: %s, flags=%d, canReceive=%b",
598 w, w.mAttrs.flags, w.canReceiveKeys());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800599
600 if (!w.canReceiveKeys()) {
601 return false;
602 }
603
Garfield Tane8d84ab2019-10-11 09:49:40 -0700604 final ActivityRecord activity = w.mActivityRecord;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800605
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800606 if (focusedApp == null) {
Adrian Roosb125e0b2019-10-02 14:55:14 +0200607 ProtoLog.v(WM_DEBUG_FOCUS_LIGHT,
608 "findFocusedWindow: focusedApp=null using new focus @ %s", w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800609 mTmpWindow = w;
610 return true;
611 }
612
613 if (!focusedApp.windowsAreFocusable()) {
614 // Current focused app windows aren't focusable...
Adrian Roosb125e0b2019-10-02 14:55:14 +0200615 ProtoLog.v(WM_DEBUG_FOCUS_LIGHT, "findFocusedWindow: focusedApp windows not"
616 + " focusable using new focus @ %s", w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800617 mTmpWindow = w;
618 return true;
619 }
620
621 // Descend through all of the app tokens and find the first that either matches
Garfield Tane8d84ab2019-10-11 09:49:40 -0700622 // win.mActivityRecord (return win) or mFocusedApp (return null).
623 if (activity != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
624 if (focusedApp.compareTo(activity) > 0) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800625 // App stack below focused app stack. No focus for you!!!
Adrian Roosb125e0b2019-10-02 14:55:14 +0200626 ProtoLog.v(WM_DEBUG_FOCUS_LIGHT,
627 "findFocusedWindow: Reached focused app=%s", focusedApp);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800628 mTmpWindow = null;
629 return true;
630 }
631 }
632
Adrian Roosb125e0b2019-10-02 14:55:14 +0200633 ProtoLog.v(WM_DEBUG_FOCUS_LIGHT, "findFocusedWindow: Found new focus @ %s", w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800634 mTmpWindow = w;
635 return true;
636 };
637
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800638 private final Consumer<WindowState> mPerformLayout = w -> {
639 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
640 // wasting time and funky changes while a window is animating away.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800641 final boolean gone = (mTmpWindow != null && mWmService.mPolicy.canBeHiddenByKeyguardLw(w))
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800642 || w.isGoneForLayoutLw();
643
644 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
645 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
646 + " mLayoutAttached=" + w.mLayoutAttached
Jorim Jaggi381cd722019-03-27 17:33:02 +0100647 + " config reported=" + w.isLastConfigReportedToClient());
Garfield Tane8d84ab2019-10-11 09:49:40 -0700648 final ActivityRecord activity = w.mActivityRecord;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800649 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Issei Suzukif2f6c912019-11-08 11:24:18 +0100650 + " mRelayoutCalled=" + w.mRelayoutCalled + " visible=" + w.mToken.isVisible()
Issei Suzuki1669ea42019-11-06 14:20:59 +0100651 + " visibleRequested=" + (activity != null && activity.mVisibleRequested)
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800652 + " parentHidden=" + w.isParentWindowHidden());
653 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Issei Suzukif2f6c912019-11-08 11:24:18 +0100654 + " mRelayoutCalled=" + w.mRelayoutCalled + " visible=" + w.mToken.isVisible()
Issei Suzuki1669ea42019-11-06 14:20:59 +0100655 + " visibleRequested=" + (activity != null && activity.mVisibleRequested)
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800656 + " parentHidden=" + w.isParentWindowHidden());
657 }
658
659 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
660 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
661 // since that means "perform layout as normal, just don't display").
Jorim Jaggi91d382a2019-03-27 17:00:48 +0100662 if ((!gone || !w.mHaveFrame || w.mLayoutNeeded) && !w.mLayoutAttached) {
663 if (mTmpInitial) {
664 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800665 }
Jorim Jaggi91d382a2019-03-27 17:00:48 +0100666 if (w.mAttrs.type == TYPE_DREAM) {
667 // Don't layout windows behind a dream, so that if it does stuff like hide
668 // the status bar we won't get a bad transition when it goes away.
669 mTmpWindow = w;
670 }
671 w.mLayoutNeeded = false;
672 w.prelayout();
673 final boolean firstLayout = !w.isLaidOut();
674 getDisplayPolicy().layoutWindowLw(w, null, mDisplayFrames);
675 w.mLayoutSeq = mLayoutSeq;
676
Riddle Hsuf64e1342019-12-05 17:38:41 +0800677 // If this is the first layout, we need to initialize the last frames and inset values,
678 // as otherwise we'd immediately cause an unnecessary resize.
Jorim Jaggi91d382a2019-03-27 17:00:48 +0100679 if (firstLayout) {
Riddle Hsuf64e1342019-12-05 17:38:41 +0800680 w.updateLastFrames();
Jorim Jaggi91d382a2019-03-27 17:00:48 +0100681 w.updateLastInsetValues();
Riddle Hsuf64e1342019-12-05 17:38:41 +0800682 w.updateLocationInParentDisplayIfNeeded();
Jorim Jaggi91d382a2019-03-27 17:00:48 +0100683 }
684
Garfield Tane8d84ab2019-10-11 09:49:40 -0700685 if (w.mActivityRecord != null) {
686 w.mActivityRecord.layoutLetterbox(w);
Jorim Jaggi91d382a2019-03-27 17:00:48 +0100687 }
688
689 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
690 + " mContainingFrame=" + w.getContainingFrame()
691 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800692 }
693 };
694
695 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
696 if (w.mLayoutAttached) {
697 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
698 + " mViewVisibility=" + w.mViewVisibility
699 + " mRelayoutCalled=" + w.mRelayoutCalled);
700 // If this view is GONE, then skip it -- keep the current frame, and let the caller
701 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
702 // windows, since that means "perform layout as normal, just don't display").
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800703 if (mTmpWindow != null && mWmService.mPolicy.canBeHiddenByKeyguardLw(w)) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800704 return;
705 }
706 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
707 || w.mLayoutNeeded) {
708 if (mTmpInitial) {
709 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700710 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800711 }
712 w.mLayoutNeeded = false;
713 w.prelayout();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800714 getDisplayPolicy().layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100715 w.mLayoutSeq = mLayoutSeq;
chaviw492139a2018-07-16 16:07:35 -0700716 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700717 + " mContainingFrame=" + w.getContainingFrame()
718 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800719 }
720 } else if (w.mAttrs.type == TYPE_DREAM) {
721 // Don't layout windows behind a dream, so that if it does stuff like hide the
722 // status bar we won't get a bad transition when it goes away.
723 mTmpWindow = mTmpWindow2;
724 }
725 };
726
727 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
728 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
729 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
730 return w.canBeImeTarget();
731 };
732
733 private final Consumer<WindowState> mApplyPostLayoutPolicy =
Tiger Huang7c610aa2018-10-27 00:01:01 +0800734 w -> getDisplayPolicy().applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
lumarkff0ab692018-11-05 20:32:30 +0800735 mInputMethodTarget);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800736
737 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800738 final WindowSurfacePlacer surfacePlacer = mWmService.mWindowPlacerLocked;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800739 final boolean obscuredChanged = w.mObscured !=
740 mTmpApplySurfaceChangesTransactionState.obscured;
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800741 final RootWindowContainer root = mWmService.mRoot;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800742
743 // Update effect.
744 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
745 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
746 final boolean isDisplayed = w.isDisplayedLw();
747
748 if (isDisplayed && w.isObscuringDisplay()) {
749 // This window completely covers everything behind it, so we want to leave all
750 // of them as undimmed (for performance reasons).
751 root.mObscuringWindow = w;
752 mTmpApplySurfaceChangesTransactionState.obscured = true;
753 }
754
Arthur Hungfbc8f412019-08-01 19:57:54 +0800755 final boolean displayHasContent = root.handleNotObscuredLocked(w,
756 mTmpApplySurfaceChangesTransactionState.obscured,
757 mTmpApplySurfaceChangesTransactionState.syswin);
758
759 if (!mTmpApplySurfaceChangesTransactionState.displayHasContent
760 && !getDisplayPolicy().isWindowExcludedFromContent(w)) {
761 mTmpApplySurfaceChangesTransactionState.displayHasContent |= displayHasContent;
762 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800763
764 if (w.mHasSurface && isDisplayed) {
765 final int type = w.mAttrs.type;
766 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
767 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
768 mTmpApplySurfaceChangesTransactionState.syswin = true;
769 }
770 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
771 && w.mAttrs.preferredRefreshRate != 0) {
772 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
773 = w.mAttrs.preferredRefreshRate;
774 }
Ady Abrahamf3e05312019-05-13 18:04:59 -0700775 final int preferredModeId = getDisplayPolicy().getRefreshRatePolicy()
776 .getPreferredModeId(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800777 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
Ady Abrahamf3e05312019-05-13 18:04:59 -0700778 && preferredModeId != 0) {
779 mTmpApplySurfaceChangesTransactionState.preferredModeId = preferredModeId;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800780 }
781 }
782 }
783
wilsonshihc32538e2018-11-07 17:27:34 +0800784 if (obscuredChanged && w.isVisibleLw() && mWallpaperController.isWallpaperTarget(w)) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800785 // This is the wallpaper target and its obscured state changed... make sure the
786 // current wallpaper's visibility has been updated accordingly.
787 mWallpaperController.updateWallpaperVisibility();
788 }
789
chaviw161ea3e2018-01-31 12:01:12 -0800790 w.handleWindowMovedIfNeeded();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800791
792 final WindowStateAnimator winAnimator = w.mWinAnimator;
793
794 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
chaviw1454b392018-08-06 09:54:04 -0700795 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800796
797 // Moved from updateWindowsAndWallpaperLocked().
798 if (w.mHasSurface) {
799 // Take care of the window being ready to display.
800 final boolean committed = winAnimator.commitFinishDrawingLocked();
801 if (isDefaultDisplay && committed) {
802 if (w.mAttrs.type == TYPE_DREAM) {
803 // HACK: When a dream is shown, it may at that point hide the lock screen.
804 // So we need to redo the layout to let the phone window manager make this
805 // happen.
806 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
807 if (DEBUG_LAYOUT_REPEATS) {
808 surfacePlacer.debugLayoutRepeats(
809 "dream and commitFinishDrawingLocked true",
810 pendingLayoutChanges);
811 }
812 }
813 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
814 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
815 "First draw done in potential wallpaper target " + w);
wilsonshihc32538e2018-11-07 17:27:34 +0800816 mWallpaperMayChange = true;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800817 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
818 if (DEBUG_LAYOUT_REPEATS) {
819 surfacePlacer.debugLayoutRepeats(
820 "wallpaper and commitFinishDrawingLocked true",
821 pendingLayoutChanges);
822 }
823 }
824 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800825 }
826
Garfield Tane8d84ab2019-10-11 09:49:40 -0700827 final ActivityRecord activity = w.mActivityRecord;
828 if (activity != null) {
829 activity.updateLetterboxSurface(w);
830 final boolean updateAllDrawn = activity.updateDrawnWindowStates(w);
831 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(activity)) {
832 mTmpUpdateAllDrawn.add(activity);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800833 }
834 }
835
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800836 if (!mLosingFocus.isEmpty() && w.isFocused() && w.isDisplayedLw()) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800837 mWmService.mH.obtainMessage(REPORT_LOSING_FOCUS, this).sendToTarget();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800838 }
839
840 w.updateResizingWindowIfNeeded();
841 };
842
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800843 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800844 * Create new {@link DisplayContent} instance, add itself to the root window container and
845 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700846 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800847 * @param service You know.
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800848 * @param activityDisplay The ActivityDisplay for the display container.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700849 */
Louis Chang2453d062019-11-19 22:30:48 +0800850 DisplayContent(Display display, WindowManagerService service) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100851 super(service);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800852 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
853 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
854 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
855 + " new=" + display);
856 }
857
Louis Chang2453d062019-11-19 22:30:48 +0800858 mAtmService = mWmService.mAtmService;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700859 mDisplay = display;
860 mDisplayId = display.getDisplayId();
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800861 mWallpaperController = new WallpaperController(mWmService, this);
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700862 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700863 display.getMetrics(mDisplayMetrics);
Adrian Roos1c2e9a12019-08-20 18:23:47 +0200864 mSystemGestureExclusionLimit = mWmService.mConstants.mSystemGestureExclusionLimitDp
Adrian Roosbf3bc1b2019-06-18 16:13:53 +0200865 * mDisplayMetrics.densityDpi / DENSITY_DEFAULT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700866 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100867 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo,
868 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700869 initializeDisplayBaseInfo();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700870
lumark588a3e82018-07-20 18:53:54 +0800871 mAppTransition = new AppTransition(service.mContext, service, this);
872 mAppTransition.registerListenerLocked(service.mActivityManagerAppTransitionNotifier);
873 mAppTransitionController = new AppTransitionController(service, this);
874 mUnknownAppVisibilityController = new UnknownAppVisibilityController(service, this);
875
876 AnimationHandler animationHandler = new AnimationHandler();
877 mBoundsAnimationController = new BoundsAnimationController(service.mContext,
Winson Chung6ba767a2018-12-13 18:20:11 -0800878 mAppTransition, AnimationThread.getHandler(), animationHandler);
lumark588a3e82018-07-20 18:53:54 +0800879
Riddle Hsu2588ab02019-02-25 14:23:56 +0800880 final InputChannel inputChannel = mWmService.mInputManager.monitorInput(
881 "PointerEventDispatcher" + mDisplayId, mDisplayId);
882 mPointerEventDispatcher = new PointerEventDispatcher(inputChannel);
883
884 // Tap Listeners are supported for:
885 // 1. All physical displays (multi-display).
886 // 2. VirtualDisplays on VR, AA (and everything else).
887 mTapDetector = new TaskTapPointerEventListener(mWmService, this);
888 registerPointerEventListener(mTapDetector);
889 registerPointerEventListener(mWmService.mMousePositionTracker);
Winson Chungc5fe7ff2019-02-19 14:49:25 -0800890 if (mWmService.mAtmService.getRecentTasks() != null) {
891 registerPointerEventListener(
892 mWmService.mAtmService.getRecentTasks().getInputListener());
893 }
Riddle Hsu2588ab02019-02-25 14:23:56 +0800894
Tiger Huang7c610aa2018-10-27 00:01:01 +0800895 mDisplayPolicy = new DisplayPolicy(service, this);
896 mDisplayRotation = new DisplayRotation(service, this);
Tiger Huang86e6d072019-05-02 20:23:47 +0800897 mCloseToSquareMaxAspectRatio = service.mContext.getResources().getFloat(
898 com.android.internal.R.dimen.config_closeToSquareDisplayMaxAspectRatio);
Tiger Huang7c610aa2018-10-27 00:01:01 +0800899 if (isDefaultDisplay) {
900 // The policy may be invoked right after here, so it requires the necessary default
901 // fields of this display content.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800902 mWmService.mPolicy.setDefaultDisplay(this);
Tiger Huang7c610aa2018-10-27 00:01:01 +0800903 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800904 if (mWmService.mDisplayReady) {
Tiger Huang7c610aa2018-10-27 00:01:01 +0800905 mDisplayPolicy.onConfigurationChanged();
906 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800907 if (mWmService.mSystemReady) {
Tiger Huang7c610aa2018-10-27 00:01:01 +0800908 mDisplayPolicy.systemReady();
909 }
Tiger Huang43b8fc22019-04-26 11:49:29 +0800910 mWindowCornerRadius = mDisplayPolicy.getWindowCornerRadius();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800911 mDividerControllerLocked = new DockedStackDividerController(service, this);
912 mPinnedStackControllerLocked = new PinnedStackController(service, this);
913
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000914 final SurfaceControl.Builder b = mWmService.makeSurfaceBuilder(mSession)
915 .setOpaque(true)
916 .setContainerLayer();
Vadim Caen1812c762019-08-12 11:38:22 +0200917 mSurfaceControl = b.setName("Root").setContainerLayer().build();
Robert Carrb1579c82017-09-05 14:54:47 -0700918
Vadim Caen1812c762019-08-12 11:38:22 +0200919 getPendingTransaction()
920 .setLayer(mSurfaceControl, 0)
921 .setLayerStack(mSurfaceControl, mDisplayId)
chaviw8065f442019-11-18 13:20:58 -0800922 .show(mSurfaceControl);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700923 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700924
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700925 // These are the only direct children we should ever have and they are permanent.
chaviw8065f442019-11-18 13:20:58 -0800926 super.addChild(mWindowContainers, null);
927 super.addChild(mOverlayContainers, null);
928
929 mWindowContainers.addChildren();
930
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -0700931 // Sets the display content for the children.
932 onDisplayChanged(this);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800933
934 // Add itself as a child to the root container.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800935 mWmService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700936
937 // TODO(b/62541591): evaluate whether this is the best spot to declare the
938 // {@link DisplayContent} ready for use.
939 mDisplayReady = true;
Arthur Hung95b38a92018-07-20 18:56:12 +0800940
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800941 mWmService.mAnimator.addDisplayLocked(mDisplayId);
Arthur Hung39134b22018-08-14 11:58:28 +0800942 mInputMonitor = new InputMonitor(service, mDisplayId);
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200943 mInsetsStateController = new InsetsStateController(this);
Jorim Jaggi28620472019-01-02 23:21:49 +0100944 mInsetsPolicy = new InsetsPolicy(mInsetsStateController, this);
Bryce Leed1871262017-06-12 14:12:29 -0700945 }
946
947 boolean isReady() {
948 // The display is ready when the system and the individual display are both ready.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800949 return mWmService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700950 }
951
952 int getDisplayId() {
953 return mDisplayId;
954 }
955
Tiger Huang43b8fc22019-04-26 11:49:29 +0800956 float getWindowCornerRadius() {
957 return mWindowCornerRadius;
958 }
959
Wale Ogunwale02319a62016-09-26 15:21:22 -0700960 WindowToken getWindowToken(IBinder binder) {
961 return mTokenMap.get(binder);
962 }
963
Garfield Tane8d84ab2019-10-11 09:49:40 -0700964 ActivityRecord getActivityRecord(IBinder binder) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700965 final WindowToken token = getWindowToken(binder);
966 if (token == null) {
967 return null;
968 }
Garfield Tane8d84ab2019-10-11 09:49:40 -0700969 return token.asActivityRecord();
Wale Ogunwale02319a62016-09-26 15:21:22 -0700970 }
971
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700972 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800973 final DisplayContent dc = mWmService.mRoot.getWindowTokenDisplay(token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700974 if (dc != null) {
975 // We currently don't support adding a window token to the display if the display
976 // already has the binder mapped to another token. If there is a use case for supporting
977 // this moving forward we will either need to merge the WindowTokens some how or have
978 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700979 throw new IllegalArgumentException("Can't map token=" + token + " to display="
980 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700981 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700982 if (binder == null) {
983 throw new IllegalArgumentException("Can't map token=" + token + " to display="
984 + getName() + " binder is null");
985 }
986 if (token == null) {
987 throw new IllegalArgumentException("Can't map null token to display="
988 + getName() + " binder=" + binder);
989 }
990
Wale Ogunwale02319a62016-09-26 15:21:22 -0700991 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700992
Garfield Tane8d84ab2019-10-11 09:49:40 -0700993 if (token.asActivityRecord() == null) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700994 // Add non-app token to container hierarchy on the display. App tokens are added through
995 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700996 switch (token.windowType) {
997 case TYPE_WALLPAPER:
998 mBelowAppWindowsContainers.addChild(token);
999 break;
1000 case TYPE_INPUT_METHOD:
1001 case TYPE_INPUT_METHOD_DIALOG:
1002 mImeWindowsContainers.addChild(token);
1003 break;
chaviw8065f442019-11-18 13:20:58 -08001004 case TYPE_ACCESSIBILITY_MAGNIFICATION_OVERLAY:
1005 mOverlayContainers.addChild(token);
1006 break;
Wale Ogunwale3a931692016-11-02 16:49:48 -07001007 default:
1008 mAboveAppWindowsContainers.addChild(token);
1009 break;
1010 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001011 }
Wale Ogunwale02319a62016-09-26 15:21:22 -07001012 }
1013
1014 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001015 final WindowToken token = mTokenMap.remove(binder);
Garfield Tane8d84ab2019-10-11 09:49:40 -07001016 if (token != null && token.asActivityRecord() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -08001017 token.setExiting();
1018 }
1019 return token;
1020 }
1021
1022 /** Changes the display the input window token is housed on to this one. */
1023 void reParentWindowToken(WindowToken token) {
1024 final DisplayContent prevDc = token.getDisplayContent();
1025 if (prevDc == this) {
1026 return;
1027 }
Riddle Hsu85bd04b2018-11-17 00:34:36 +08001028 if (prevDc != null) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07001029 if (prevDc.mTokenMap.remove(token.token) != null && token.asActivityRecord() == null) {
Riddle Hsu85bd04b2018-11-17 00:34:36 +08001030 // Removed the token from the map, but made sure it's not an app token before
1031 // removing from parent.
1032 token.getParent().removeChild(token);
1033 }
lumark280cb212019-09-09 09:47:22 +08001034 if (token.hasChild(prevDc.mLastFocus)) {
1035 // If the reparent window token contains previous display's last focus window, means
1036 // it will end up to gain window focus on the target display, so it should not be
1037 // notified that it lost focus from the previous display.
Riddle Hsu85bd04b2018-11-17 00:34:36 +08001038 prevDc.mLastFocus = null;
1039 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001040 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -08001041
1042 addWindowToken(token.token, token);
RyanlwLin925e8222019-08-30 15:08:48 +08001043
1044 if (mWmService.mAccessibilityController != null) {
1045 final int prevDisplayId = prevDc != null ? prevDc.getDisplayId() : INVALID_DISPLAY;
1046 mWmService.mAccessibilityController.onSomeWindowResizedOrMovedLocked(prevDisplayId,
1047 getDisplayId());
1048 }
Wale Ogunwale02319a62016-09-26 15:21:22 -07001049 }
1050
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001051 void removeAppToken(IBinder binder) {
1052 final WindowToken token = removeWindowToken(binder);
1053 if (token == null) {
1054 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
1055 return;
1056 }
1057
Garfield Tane8d84ab2019-10-11 09:49:40 -07001058 final ActivityRecord activity = token.asActivityRecord();
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001059
Garfield Tane8d84ab2019-10-11 09:49:40 -07001060 if (activity == null) {
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001061 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
1062 return;
1063 }
1064
Garfield Tane8d84ab2019-10-11 09:49:40 -07001065 activity.onRemovedFromDisplay();
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001066 }
1067
Riddle Hsuad256a12018-07-18 16:11:30 +08001068 @Override
1069 public Display getDisplay() {
Craig Mautnerb47bbc32012-08-22 17:41:48 -07001070 return mDisplay;
1071 }
1072
Craig Mautner59c00972012-07-30 12:10:24 -07001073 DisplayInfo getDisplayInfo() {
1074 return mDisplayInfo;
1075 }
1076
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001077 DisplayMetrics getDisplayMetrics() {
1078 return mDisplayMetrics;
1079 }
1080
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001081 DisplayPolicy getDisplayPolicy() {
1082 return mDisplayPolicy;
1083 }
1084
Riddle Hsuad256a12018-07-18 16:11:30 +08001085 @Override
1086 public DisplayRotation getDisplayRotation() {
1087 return mDisplayRotation;
1088 }
1089
Jorim Jaggif96c90a2018-09-26 16:55:15 +02001090 /**
1091 * Marks a window as providing insets for the rest of the windows in the system.
1092 *
1093 * @param type The type of inset this window provides.
1094 * @param win The window.
1095 * @param frameProvider Function to compute the frame, or {@code null} if the just the frame of
1096 * the window should be taken.
1097 */
Tiger Huang332793b2019-10-29 23:21:27 +08001098 void setInsetProvider(@InternalInsetsType int type, WindowState win,
Jorim Jaggif96c90a2018-09-26 16:55:15 +02001099 @Nullable TriConsumer<DisplayFrames, WindowState, Rect> frameProvider) {
1100 mInsetsStateController.getSourceProvider(type).setWindow(win, frameProvider);
1101 }
1102
1103 InsetsStateController getInsetsStateController() {
1104 return mInsetsStateController;
1105 }
1106
Jorim Jaggi28620472019-01-02 23:21:49 +01001107 InsetsPolicy getInsetsPolicy() {
1108 return mInsetsPolicy;
1109 }
1110
Riddle Hsuccf09402019-08-13 00:33:06 +08001111 @Surface.Rotation
Andrii Kulian8ee72852017-03-10 10:36:45 -08001112 int getRotation() {
Riddle Hsuccf09402019-08-13 00:33:06 +08001113 return mDisplayRotation.getRotation();
Andrii Kulian8ee72852017-03-10 10:36:45 -08001114 }
1115
Riddle Hsuccf09402019-08-13 00:33:06 +08001116 @ScreenOrientation
Andrii Kulian8ee72852017-03-10 10:36:45 -08001117 int getLastOrientation() {
Riddle Hsuccf09402019-08-13 00:33:06 +08001118 return mDisplayRotation.getLastOrientation();
Andrii Kulian8ee72852017-03-10 10:36:45 -08001119 }
1120
Riddle Hsuccf09402019-08-13 00:33:06 +08001121 @ScreenOrientation
Andrii Kulian8ee72852017-03-10 10:36:45 -08001122 int getLastWindowForcedOrientation() {
1123 return mLastWindowForcedOrientation;
1124 }
1125
Evan Rosky966759f2019-01-15 10:33:58 -08001126 void registerRemoteAnimations(RemoteAnimationDefinition definition) {
1127 mAppTransitionController.registerRemoteAnimations(definition);
1128 }
1129
Shivam Agrawal6472e0e2019-07-03 16:27:49 -07001130 void reconfigureDisplayLocked() {
1131 if (!isReady()) {
1132 return;
1133 }
1134 configureDisplayPolicy();
1135 setLayoutNeeded();
1136
Riddle Hsuccf09402019-08-13 00:33:06 +08001137 boolean configChanged = updateOrientation();
Shivam Agrawal6472e0e2019-07-03 16:27:49 -07001138 final Configuration currentDisplayConfig = getConfiguration();
1139 mTmpConfiguration.setTo(currentDisplayConfig);
1140 computeScreenConfiguration(mTmpConfiguration);
1141 configChanged |= currentDisplayConfig.diff(mTmpConfiguration) != 0;
1142
1143 if (configChanged) {
1144 mWaitingForConfig = true;
1145 mWmService.startFreezingDisplayLocked(0 /* exitAnim */, 0 /* enterAnim */, this);
1146 sendNewConfiguration();
1147 }
1148
1149 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Riddle Hsu4e611772018-10-31 18:58:28 +08001150 }
1151
Shivam Agrawal1d3db652019-07-01 15:26:11 -07001152 void sendNewConfiguration() {
Louis Chang2453d062019-11-19 22:30:48 +08001153 if (!isReady()) {
Shivam Agrawal6472e0e2019-07-03 16:27:49 -07001154 return;
1155 }
Evan Rosky69cace42019-09-20 16:28:13 -07001156 if (mDisplayRotation.isWaitingForRemoteRotation()) {
1157 return;
1158 }
Louis Chang2453d062019-11-19 22:30:48 +08001159 // TODO(display-merge): Remove cast
1160 final boolean configUpdated =
1161 ((ActivityDisplay) this).updateDisplayOverrideConfigurationLocked();
Shivam Agrawal6472e0e2019-07-03 16:27:49 -07001162 if (configUpdated) {
1163 return;
1164 }
1165 // Something changed (E.g. device rotation), but no configuration update is needed.
1166 // E.g. changing device rotation by 180 degrees. Go ahead and perform surface placement to
1167 // unfreeze the display since we froze it when the rotation was updated in
1168 // DisplayContent#updateRotationUnchecked.
1169 if (mWaitingForConfig) {
1170 mWaitingForConfig = false;
1171 mWmService.mLastFinishedFreezeSource = "config-unchanged";
1172 setLayoutNeeded();
1173 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Shivam Agrawal1d3db652019-07-01 15:26:11 -07001174 }
1175 }
1176
Garfield Tan90b04282018-12-11 14:04:42 -08001177 @Override
1178 boolean onDescendantOrientationChanged(IBinder freezeDisplayToken,
1179 ConfigurationContainer requestingContainer) {
Riddle Hsuccf09402019-08-13 00:33:06 +08001180 final Configuration config = updateOrientation(
1181 getRequestedOverrideConfiguration(), freezeDisplayToken, false /* forceUpdate */);
Garfield Tan49dae102019-02-04 09:51:59 -08001182 // If display rotation class tells us that it doesn't consider app requested orientation,
1183 // this display won't rotate just because of an app changes its requested orientation. Thus
1184 // it indicates that this display chooses not to handle this request.
1185 final boolean handled = getDisplayRotation().respectAppRequestedOrientation();
Garfield Tan90b04282018-12-11 14:04:42 -08001186 if (config == null) {
1187 return handled;
1188 }
1189
1190 if (handled && requestingContainer instanceof ActivityRecord) {
1191 final ActivityRecord activityRecord = (ActivityRecord) requestingContainer;
Louis Chang2453d062019-11-19 22:30:48 +08001192 // TODO(display-merge): Remove cast
1193 final boolean kept = ((ActivityDisplay) this).updateDisplayOverrideConfigurationLocked(
Shivam Agrawal1d3db652019-07-01 15:26:11 -07001194 config, activityRecord, false /* deferResume */, null /* result */);
Garfield Tan90b04282018-12-11 14:04:42 -08001195 activityRecord.frozenBeforeDestroy = true;
1196 if (!kept) {
1197 mWmService.mAtmService.mRootActivityContainer.resumeFocusedStacksTopActivities();
1198 }
1199 } else {
1200 // We have a new configuration to push so we need to update ATMS for now.
1201 // TODO: Clean up display configuration push between ATMS and WMS after unification.
Louis Chang2453d062019-11-19 22:30:48 +08001202 // TODO(display-merge): Remove cast
1203 ((ActivityDisplay) this.mDisplayContent).updateDisplayOverrideConfigurationLocked(
Shivam Agrawal1d3db652019-07-01 15:26:11 -07001204 config, null /* starting */, false /* deferResume */, null);
Garfield Tan90b04282018-12-11 14:04:42 -08001205 }
1206 return handled;
1207 }
1208
Garfield Tan49dae102019-02-04 09:51:59 -08001209 @Override
1210 boolean handlesOrientationChangeFromDescendant() {
1211 return getDisplayRotation().respectAppRequestedOrientation();
1212 }
1213
Riddle Hsu4e611772018-10-31 18:58:28 +08001214 /**
1215 * Determine the new desired orientation of this display.
1216 *
Riddle Hsuccf09402019-08-13 00:33:06 +08001217 * @see #getOrientation()
1218 * @return {@code true} if the orientation is changed and the caller should call
1219 * {@link #sendNewConfiguration} if the method returns {@code true}.
Riddle Hsu4e611772018-10-31 18:58:28 +08001220 */
Riddle Hsuccf09402019-08-13 00:33:06 +08001221 boolean updateOrientation() {
1222 return mDisplayRotation.updateOrientation(getOrientation(), false /* forceUpdate */);
Riddle Hsu4e611772018-10-31 18:58:28 +08001223 }
1224
Garfield Tan90b04282018-12-11 14:04:42 -08001225 /**
Riddle Hsuccf09402019-08-13 00:33:06 +08001226 * Update orientation of the display, returning a non-null new Configuration if it has
Garfield Tan90b04282018-12-11 14:04:42 -08001227 * changed from the current orientation. If a non-null configuration is returned, someone must
1228 * call {@link WindowManagerService#setNewDisplayOverrideConfiguration(Configuration,
1229 * DisplayContent)} to tell the window manager it can unfreeze the screen. This will typically
Riddle Hsuccf09402019-08-13 00:33:06 +08001230 * be done by calling {@link #sendNewConfiguration}.
1231 *
1232 * @param currentConfig The current requested override configuration (it is usually set from
1233 * the last {@link #sendNewConfiguration}) of the display. It is used to
1234 * check if the configuration container has the latest state.
1235 * @param freezeDisplayToken Freeze the app window token if the orientation is changed.
1236 * @param forceUpdate See {@link DisplayRotation#updateRotationUnchecked(boolean)}
Garfield Tan90b04282018-12-11 14:04:42 -08001237 */
Riddle Hsuccf09402019-08-13 00:33:06 +08001238 Configuration updateOrientation(Configuration currentConfig, IBinder freezeDisplayToken,
1239 boolean forceUpdate) {
Garfield Tan90b04282018-12-11 14:04:42 -08001240 if (!mDisplayReady) {
1241 return null;
1242 }
1243
1244 Configuration config = null;
Riddle Hsuccf09402019-08-13 00:33:06 +08001245 if (mDisplayRotation.updateOrientation(getOrientation(), forceUpdate)) {
Garfield Tan90b04282018-12-11 14:04:42 -08001246 // If we changed the orientation but mOrientationChangeComplete is already true,
1247 // we used seamless rotation, and we don't need to freeze the screen.
1248 if (freezeDisplayToken != null && !mWmService.mRoot.mOrientationChangeComplete) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07001249 final ActivityRecord activity = getActivityRecord(freezeDisplayToken);
1250 if (activity != null) {
1251 activity.startFreezingScreen();
Garfield Tan90b04282018-12-11 14:04:42 -08001252 }
1253 }
1254 config = new Configuration();
1255 computeScreenConfiguration(config);
1256 } else if (currentConfig != null) {
1257 // No obvious action we need to take, but if our current state mismatches the
1258 // activity manager's, update it, disregarding font scale, which should remain set
1259 // to the value of the previous configuration.
1260 // Here we're calling Configuration#unset() instead of setToDefaults() because we
1261 // need to keep override configs clear of non-empty values (e.g. fontSize).
1262 mTmpConfiguration.unset();
1263 mTmpConfiguration.updateFrom(currentConfig);
1264 computeScreenConfiguration(mTmpConfiguration);
1265 if (currentConfig.diff(mTmpConfiguration) != 0) {
1266 mWaitingForConfig = true;
1267 setLayoutNeeded();
Riddle Hsuccf09402019-08-13 00:33:06 +08001268 mDisplayRotation.prepareNormalRotationAnimation();
Garfield Tan90b04282018-12-11 14:04:42 -08001269 config = new Configuration(mTmpConfiguration);
1270 }
1271 }
1272
1273 return config;
1274 }
1275
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001276 /**
Andrii Kulian06d07d62017-03-14 11:11:47 -07001277 * Update rotation of the display.
1278 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001279 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
Riddle Hsuccf09402019-08-13 00:33:06 +08001280 * {@link #sendNewConfiguration} TO UNFREEZE THE SCREEN.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001281 */
Robert Carrae606b42018-02-15 15:36:23 -08001282 boolean updateRotationUnchecked() {
Riddle Hsuccf09402019-08-13 00:33:06 +08001283 return mDisplayRotation.updateRotationUnchecked(false /* forceUpdate */);
Evan Roskye747c3e2018-10-30 20:06:41 -07001284 }
1285
1286 /**
1287 * Applies the rotation transaction. This must be called after {@link #updateRotationUnchecked}
1288 * (if it returned {@code true}) to actually finish the rotation.
1289 *
1290 * @param oldRotation the rotation we are coming from.
1291 * @param rotation the rotation to apply.
1292 */
1293 void applyRotationLocked(final int oldRotation, final int rotation) {
Riddle Hsuccf09402019-08-13 00:33:06 +08001294 mDisplayRotation.applyCurrentRotation(rotation);
1295 final boolean rotateSeamlessly = mDisplayRotation.isRotatingSeamlessly();
Riddle Hsuccf09402019-08-13 00:33:06 +08001296 final Transaction transaction = getPendingTransaction();
Vadim Caenb3715832019-08-13 17:06:38 +02001297 ScreenRotationAnimation screenRotationAnimation = rotateSeamlessly
1298 ? null : getRotationAnimation();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001299 // We need to update our screen size information to match the new rotation. If the rotation
1300 // has actually changed then this method will return true and, according to the comment at
1301 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1302 // By updating the Display info here it will be available to
1303 // #computeScreenConfiguration() later.
Tiger Huanga817b1f2019-05-09 20:04:17 +08001304 updateDisplayAndOrientation(getConfiguration().uiMode, null /* outConfig */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001305
Robert Carrae606b42018-02-15 15:36:23 -08001306 // NOTE: We disable the rotation in the emulator because
1307 // it doesn't support hardware OpenGL emulation yet.
Vadim Caenba4fd6c2019-08-05 16:45:46 +02001308 if (screenRotationAnimation != null && screenRotationAnimation.hasScreenshot()) {
Riddle Hsuccf09402019-08-13 00:33:06 +08001309 screenRotationAnimation.setRotation(transaction, rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001310 }
1311
Vishnu Nair83537a72018-07-19 21:27:48 -07001312 forAllWindows(w -> {
Riddle Hsuccf09402019-08-13 00:33:06 +08001313 w.seamlesslyRotateIfAllowed(transaction, oldRotation, rotation, rotateSeamlessly);
Vishnu Nair83537a72018-07-19 21:27:48 -07001314 }, true /* traverseTopToBottom */);
1315
Riddle Hsuccf09402019-08-13 00:33:06 +08001316 mWmService.mDisplayManagerInternal.performTraversal(transaction);
Robert Carrae606b42018-02-15 15:36:23 -08001317 scheduleAnimation();
1318
Andrii Kulian06d07d62017-03-14 11:11:47 -07001319 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001320 if (w.mHasSurface && !rotateSeamlessly) {
Adrian Roosb125e0b2019-10-02 14:55:14 +02001321 ProtoLog.v(WM_DEBUG_ORIENTATION, "Set mOrientationChanging of %s", w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001322 w.setOrientationChanging(true);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001323 mWmService.mRoot.mOrientationChangeComplete = false;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001324 w.mLastFreezeDuration = 0;
1325 }
1326 w.mReportOrientationChanged = true;
1327 }, true /* traverseTopToBottom */);
1328
1329 if (rotateSeamlessly) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001330 mWmService.mH.sendNewMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
Riddle Hsu654a6f92018-07-13 22:59:36 +08001331 this, SEAMLESS_ROTATION_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001332 }
1333
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001334 for (int i = mWmService.mRotationWatchers.size() - 1; i >= 0; i--) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001335 final WindowManagerService.RotationWatcher rotationWatcher
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001336 = mWmService.mRotationWatchers.get(i);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001337 if (rotationWatcher.mDisplayId == mDisplayId) {
1338 try {
1339 rotationWatcher.mWatcher.onRotationChanged(rotation);
1340 } catch (RemoteException e) {
1341 // Ignore
1342 }
1343 }
1344 }
1345
Andrii Kulian06d07d62017-03-14 11:11:47 -07001346 // Announce rotation only if we will not animate as we already have the
1347 // windows in final state. Otherwise, we make this call at the rotation end.
Rhed Jao02655dc2018-10-30 20:44:52 +08001348 if (screenRotationAnimation == null && mWmService.mAccessibilityController != null) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001349 mWmService.mAccessibilityController.onRotationChangedLocked(this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001350 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001351 }
1352
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001353 void configureDisplayPolicy() {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001354 final int width = mBaseDisplayWidth;
1355 final int height = mBaseDisplayHeight;
1356 final int shortSize;
1357 final int longSize;
1358 if (width > height) {
1359 shortSize = height;
1360 longSize = width;
1361 } else {
1362 shortSize = width;
1363 longSize = height;
1364 }
1365
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02001366 final int shortSizeDp = shortSize * DENSITY_DEFAULT / mBaseDisplayDensity;
1367 final int longSizeDp = longSize * DENSITY_DEFAULT / mBaseDisplayDensity;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001368
Winson Chung4723b4e2019-03-25 16:49:36 -07001369 mDisplayPolicy.updateConfigurationAndScreenSizeDependentBehaviors();
Tiger Huang3d2b8982019-01-29 22:56:48 +08001370 mDisplayRotation.configure(width, height, shortSizeDp, longSizeDp);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001371
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001372 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
1373 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Tiger Huang86e6d072019-05-02 20:23:47 +08001374
1375 // Not much of use to rotate the display for apps since it's close to square.
1376 mIgnoreRotationForApps = isNonDecorDisplayCloseToSquare(Surface.ROTATION_0, width, height);
1377 }
1378
1379 private boolean isNonDecorDisplayCloseToSquare(int rotation, int width, int height) {
1380 final DisplayCutout displayCutout =
1381 calculateDisplayCutoutForRotation(rotation).getDisplayCutout();
1382 final int uiMode = mWmService.mPolicy.getUiMode();
1383 final int w = mDisplayPolicy.getNonDecorDisplayWidth(
1384 width, height, rotation, uiMode, displayCutout);
1385 final int h = mDisplayPolicy.getNonDecorDisplayHeight(
1386 width, height, rotation, uiMode, displayCutout);
1387 final float aspectRatio = Math.max(w, h) / (float) Math.min(w, h);
1388 return aspectRatio <= mCloseToSquareMaxAspectRatio;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001389 }
1390
Andrii Kulian06d07d62017-03-14 11:11:47 -07001391 /**
1392 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1393 * changed.
1394 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1395 */
Tiger Huanga817b1f2019-05-09 20:04:17 +08001396 private DisplayInfo updateDisplayAndOrientation(int uiMode, Configuration outConfig) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001397 // Use the effective "visual" dimensions based on current rotation
Riddle Hsuccf09402019-08-13 00:33:06 +08001398 final int rotation = getRotation();
1399 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Garfield Tan4bb83472019-01-16 14:37:04 -08001400 final int dw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1401 final int dh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001402
1403 // Update application display metrics.
Riddle Hsuccf09402019-08-13 00:33:06 +08001404 final WmDisplayCutout wmDisplayCutout = calculateDisplayCutoutForRotation(rotation);
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001405 final DisplayCutout displayCutout = wmDisplayCutout.getDisplayCutout();
1406
Riddle Hsuccf09402019-08-13 00:33:06 +08001407 final int appWidth = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001408 displayCutout);
Riddle Hsuccf09402019-08-13 00:33:06 +08001409 final int appHeight = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001410 displayCutout);
Riddle Hsuccf09402019-08-13 00:33:06 +08001411 mDisplayInfo.rotation = rotation;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001412 mDisplayInfo.logicalWidth = dw;
1413 mDisplayInfo.logicalHeight = dh;
1414 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1415 mDisplayInfo.appWidth = appWidth;
1416 mDisplayInfo.appHeight = appHeight;
1417 if (isDefaultDisplay) {
1418 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1419 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1420 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001421 mDisplayInfo.displayCutout = displayCutout.isEmpty() ? null : displayCutout;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001422 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1423 if (mDisplayScalingDisabled) {
1424 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1425 } else {
1426 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1427 }
1428
Tiger Huanga817b1f2019-05-09 20:04:17 +08001429 computeSizeRangesAndScreenLayout(mDisplayInfo, rotated, uiMode, dw, dh,
1430 mDisplayMetrics.density, outConfig);
1431
Andrii Kulianf0379de2018-03-14 16:24:07 -07001432 // We usually set the override info in DisplayManager so that we get consistent display
1433 // metrics values when displays are changing and don't send out new values until WM is aware
1434 // of them. However, we don't do this for displays that serve as containers for ActivityView
1435 // because we don't want letter-/pillar-boxing during resize.
1436 final DisplayInfo overrideDisplayInfo = mShouldOverrideDisplayConfiguration
1437 ? mDisplayInfo : null;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001438 mWmService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
Andrii Kulianf0379de2018-03-14 16:24:07 -07001439 overrideDisplayInfo);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001440
1441 mBaseDisplayRect.set(0, 0, dw, dh);
1442
1443 if (isDefaultDisplay) {
1444 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1445 mCompatDisplayMetrics);
1446 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001447
Andrii Kulian06d07d62017-03-14 11:11:47 -07001448 return mDisplayInfo;
1449 }
1450
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001451 WmDisplayCutout calculateDisplayCutoutForRotation(int rotation) {
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001452 return mDisplayCutoutCache.getOrCompute(mInitialDisplayCutout, rotation);
1453 }
1454
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001455 private WmDisplayCutout calculateDisplayCutoutForRotationUncached(
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001456 DisplayCutout cutout, int rotation) {
Adrian Roos24264212018-02-19 16:26:15 +01001457 if (cutout == null || cutout == DisplayCutout.NO_CUTOUT) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001458 return WmDisplayCutout.NO_CUTOUT;
Adrian Roos1cf585052018-01-03 18:43:27 +01001459 }
Adrian Roos11c25582018-02-19 18:06:36 +01001460 if (rotation == ROTATION_0) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001461 return WmDisplayCutout.computeSafeInsets(
1462 cutout, mInitialDisplayWidth, mInitialDisplayHeight);
Adrian Roos24264212018-02-19 16:26:15 +01001463 }
Adrian Roosbed538e2018-02-21 17:50:07 +01001464 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Issei Suzuki43190bd2018-08-20 17:28:41 +02001465 final Rect[] newBounds = mRotationUtil.getRotatedBounds(
1466 WmDisplayCutout.computeSafeInsets(
Jorim Jaggi60640512018-06-29 01:14:31 +02001467 cutout, mInitialDisplayWidth, mInitialDisplayHeight)
Issei Suzuki43190bd2018-08-20 17:28:41 +02001468 .getDisplayCutout().getBoundingRectsAll(),
1469 rotation, mInitialDisplayWidth, mInitialDisplayHeight);
1470 return WmDisplayCutout.computeSafeInsets(DisplayCutout.fromBounds(newBounds),
Adrian Roos24264212018-02-19 16:26:15 +01001471 rotated ? mInitialDisplayHeight : mInitialDisplayWidth,
1472 rotated ? mInitialDisplayWidth : mInitialDisplayHeight);
Adrian Roos1cf585052018-01-03 18:43:27 +01001473 }
1474
Andrii Kulian06d07d62017-03-14 11:11:47 -07001475 /**
1476 * Compute display configuration based on display properties and policy settings.
1477 * Do not call if mDisplayReady == false.
1478 */
1479 void computeScreenConfiguration(Configuration config) {
Tiger Huanga817b1f2019-05-09 20:04:17 +08001480 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode, config);
Evan Roskye747c3e2018-10-30 20:06:41 -07001481 calculateBounds(displayInfo, mTmpBounds);
1482 config.windowConfiguration.setBounds(mTmpBounds);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001483
1484 final int dw = displayInfo.logicalWidth;
1485 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001486 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
Garfield Tane0846042018-07-26 13:42:04 -07001487 config.windowConfiguration.setWindowingMode(getWindowingMode());
Yunfan Chen7daa6ac2018-11-29 18:16:44 -08001488 config.windowConfiguration.setDisplayWindowingMode(getWindowingMode());
Evan Roskye747c3e2018-10-30 20:06:41 -07001489 config.windowConfiguration.setRotation(displayInfo.rotation);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001490
Adrian Roos11c25582018-02-19 18:06:36 +01001491 final float density = mDisplayMetrics.density;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001492 config.screenWidthDp =
Tiger Huang7c610aa2018-10-27 00:01:01 +08001493 (int)(mDisplayPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1494 config.uiMode, displayInfo.displayCutout) / density);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001495 config.screenHeightDp =
Tiger Huang7c610aa2018-10-27 00:01:01 +08001496 (int)(mDisplayPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1497 config.uiMode, displayInfo.displayCutout) / density);
Bryce Lee7566d762017-03-30 09:34:15 -07001498
Tiger Huang7c610aa2018-10-27 00:01:01 +08001499 mDisplayPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh,
Adrian Roos11c25582018-02-19 18:06:36 +01001500 displayInfo.displayCutout, mTmpRect);
Bryce Lee7566d762017-03-30 09:34:15 -07001501 final int leftInset = mTmpRect.left;
1502 final int topInset = mTmpRect.top;
1503 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001504 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1505 leftInset + displayInfo.appWidth /* right */,
1506 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001507 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1508 || displayInfo.rotation == Surface.ROTATION_270);
1509
Andrii Kulian06d07d62017-03-14 11:11:47 -07001510 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1511 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1512 ? Configuration.SCREENLAYOUT_ROUND_YES
1513 : Configuration.SCREENLAYOUT_ROUND_NO);
1514
1515 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1516 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1517 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001518 dh, displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001519 config.densityDpi = displayInfo.logicalDensityDpi;
1520
1521 config.colorMode =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001522 ((displayInfo.isHdr() && mWmService.hasHdrSupport())
Andrii Kulian06d07d62017-03-14 11:11:47 -07001523 ? Configuration.COLOR_MODE_HDR_YES
1524 : Configuration.COLOR_MODE_HDR_NO)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001525 | (displayInfo.isWideColorGamut() && mWmService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001526 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1527 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1528
1529 // Update the configuration based on available input devices, lid switch,
1530 // and platform configuration.
1531 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1532 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1533 config.navigation = Configuration.NAVIGATION_NONAV;
1534
1535 int keyboardPresence = 0;
1536 int navigationPresence = 0;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001537 final InputDevice[] devices = mWmService.mInputManager.getInputDevices();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001538 final int len = devices != null ? devices.length : 0;
1539 for (int i = 0; i < len; i++) {
1540 InputDevice device = devices[i];
Arthur Hung82bbfc32018-11-29 20:24:51 +08001541 // Ignore virtual input device.
1542 if (device.isVirtual()) {
1543 continue;
1544 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001545
Arthur Hung82bbfc32018-11-29 20:24:51 +08001546 // Check if input device can dispatch events to current display.
1547 // If display type is virtual, will follow the default display.
1548 if (!mWmService.mInputManager.canDispatchToDisplay(device.getId(),
1549 displayInfo.type == Display.TYPE_VIRTUAL ? DEFAULT_DISPLAY : mDisplayId)) {
1550 continue;
1551 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001552
Arthur Hung82bbfc32018-11-29 20:24:51 +08001553 final int sources = device.getSources();
1554 final int presenceFlag = device.isExternal()
1555 ? WindowManagerPolicy.PRESENCE_EXTERNAL : WindowManagerPolicy.PRESENCE_INTERNAL;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001556
Arthur Hung82bbfc32018-11-29 20:24:51 +08001557 if (mWmService.mIsTouchDevice) {
1558 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) == InputDevice.SOURCE_TOUCHSCREEN) {
1559 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001560 }
Arthur Hung82bbfc32018-11-29 20:24:51 +08001561 } else {
1562 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1563 }
1564
1565 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1566 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1567 navigationPresence |= presenceFlag;
1568 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1569 && config.navigation == Configuration.NAVIGATION_NONAV) {
1570 config.navigation = Configuration.NAVIGATION_DPAD;
1571 navigationPresence |= presenceFlag;
1572 }
1573
1574 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1575 config.keyboard = Configuration.KEYBOARD_QWERTY;
1576 keyboardPresence |= presenceFlag;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001577 }
1578 }
1579
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001580 if (config.navigation == Configuration.NAVIGATION_NONAV && mWmService.mHasPermanentDpad) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001581 config.navigation = Configuration.NAVIGATION_DPAD;
1582 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1583 }
1584
1585 // Determine whether a hard keyboard is available and enabled.
1586 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1587 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001588 if (hardKeyboardAvailable != mWmService.mHardKeyboardAvailable) {
1589 mWmService.mHardKeyboardAvailable = hardKeyboardAvailable;
1590 mWmService.mH.removeMessages(REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1591 mWmService.mH.sendEmptyMessage(REPORT_HARD_KEYBOARD_STATUS_CHANGE);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001592 }
1593
Winson Chung4723b4e2019-03-25 16:49:36 -07001594 mDisplayPolicy.updateConfigurationAndScreenSizeDependentBehaviors();
Tiger Huang7c610aa2018-10-27 00:01:01 +08001595
Andrii Kulian06d07d62017-03-14 11:11:47 -07001596 // Let the policy update hidden states.
1597 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1598 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1599 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001600 mWmService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001601 }
1602
1603 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001604 DisplayCutout displayCutout) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001605 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1606 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1607 final int unrotDw, unrotDh;
1608 if (rotated) {
1609 unrotDw = dh;
1610 unrotDh = dw;
1611 } else {
1612 unrotDw = dw;
1613 unrotDh = dh;
1614 }
1615 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001616 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001617 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001618 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001619 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001620 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001621 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001622 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001623 return sw;
1624 }
1625
1626 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001627 DisplayMetrics dm, int dw, int dh, DisplayCutout displayCutout) {
1628 dm.noncompatWidthPixels = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1629 displayCutout);
1630 dm.noncompatHeightPixels = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode,
1631 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001632 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1633 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1634 if (curSize == 0 || size < curSize) {
1635 curSize = size;
1636 }
1637 return curSize;
1638 }
1639
Tiger Huang7c610aa2018-10-27 00:01:01 +08001640 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, boolean rotated,
1641 int uiMode, int dw, int dh, float density, Configuration outConfig) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001642
1643 // We need to determine the smallest width that will occur under normal
1644 // operation. To this, start with the base screen size and compute the
1645 // width under the different possible rotations. We need to un-rotate
1646 // the current screen dimensions before doing this.
1647 int unrotDw, unrotDh;
1648 if (rotated) {
1649 unrotDw = dh;
1650 unrotDh = dw;
1651 } else {
1652 unrotDw = dw;
1653 unrotDh = dh;
1654 }
1655 displayInfo.smallestNominalAppWidth = 1<<30;
1656 displayInfo.smallestNominalAppHeight = 1<<30;
1657 displayInfo.largestNominalAppWidth = 0;
1658 displayInfo.largestNominalAppHeight = 0;
Tiger Huang7c610aa2018-10-27 00:01:01 +08001659 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_0, uiMode, unrotDw, unrotDh);
1660 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_90, uiMode, unrotDh, unrotDw);
1661 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_180, uiMode, unrotDw, unrotDh);
1662 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_270, uiMode, unrotDh, unrotDw);
Tiger Huanga817b1f2019-05-09 20:04:17 +08001663
1664 if (outConfig == null) {
1665 return;
1666 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001667 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1668 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001669 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001670 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001671 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001672 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001673 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001674 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001675 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001676 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1677 outConfig.screenLayout = sl;
1678 }
1679
1680 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001681 int uiMode, DisplayCutout displayCutout) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001682 // Get the app screen size at this rotation.
Tiger Huang7c610aa2018-10-27 00:01:01 +08001683 int w = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayCutout);
1684 int h = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001685
1686 // Compute the screen layout size class for this rotation.
1687 int longSize = w;
1688 int shortSize = h;
1689 if (longSize < shortSize) {
1690 int tmp = longSize;
1691 longSize = shortSize;
1692 shortSize = tmp;
1693 }
1694 longSize = (int)(longSize/density);
1695 shortSize = (int)(shortSize/density);
1696 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1697 }
1698
Tiger Huang7c610aa2018-10-27 00:01:01 +08001699 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int rotation,
Andrii Kulian06d07d62017-03-14 11:11:47 -07001700 int uiMode, int dw, int dh) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001701 final DisplayCutout displayCutout = calculateDisplayCutoutForRotation(
1702 rotation).getDisplayCutout();
Tiger Huang7c610aa2018-10-27 00:01:01 +08001703 final int width = mDisplayPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1704 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001705 if (width < displayInfo.smallestNominalAppWidth) {
1706 displayInfo.smallestNominalAppWidth = width;
1707 }
1708 if (width > displayInfo.largestNominalAppWidth) {
1709 displayInfo.largestNominalAppWidth = width;
1710 }
Tiger Huang7c610aa2018-10-27 00:01:01 +08001711 final int height = mDisplayPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1712 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001713 if (height < displayInfo.smallestNominalAppHeight) {
1714 displayInfo.smallestNominalAppHeight = height;
1715 }
1716 if (height > displayInfo.largestNominalAppHeight) {
1717 displayInfo.largestNominalAppHeight = height;
1718 }
1719 }
1720
Riddle Hsua4d6fa22018-08-11 00:50:39 +08001721 /**
1722 * Apps that use the compact menu panel (as controlled by the panelMenuIsCompact
1723 * theme attribute) on devices that feature a physical options menu key attempt to position
1724 * their menu panel window along the edge of the screen nearest the physical menu key.
1725 * This lowers the travel distance between invoking the menu panel and selecting
1726 * a menu option.
1727 *
1728 * This method helps control where that menu is placed. Its current implementation makes
1729 * assumptions about the menu key and its relationship to the screen based on whether
1730 * the device's natural orientation is portrait (width < height) or landscape.
1731 *
1732 * The menu key is assumed to be located along the bottom edge of natural-portrait
1733 * devices and along the right edge of natural-landscape devices. If these assumptions
1734 * do not hold for the target device, this method should be changed to reflect that.
1735 *
1736 * @return A {@link Gravity} value for placing the options menu window.
1737 */
1738 int getPreferredOptionsPanelGravity() {
1739 final int rotation = getRotation();
1740 if (mInitialDisplayWidth < mInitialDisplayHeight) {
1741 // On devices with a natural orientation of portrait.
1742 switch (rotation) {
1743 default:
1744 case Surface.ROTATION_0:
1745 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1746 case Surface.ROTATION_90:
1747 return Gravity.RIGHT | Gravity.BOTTOM;
1748 case Surface.ROTATION_180:
1749 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1750 case Surface.ROTATION_270:
1751 return Gravity.START | Gravity.BOTTOM;
1752 }
1753 }
1754
1755 // On devices with a natural orientation of landscape.
1756 switch (rotation) {
1757 default:
1758 case Surface.ROTATION_0:
1759 return Gravity.RIGHT | Gravity.BOTTOM;
1760 case Surface.ROTATION_90:
1761 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1762 case Surface.ROTATION_180:
1763 return Gravity.START | Gravity.BOTTOM;
1764 case Surface.ROTATION_270:
1765 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1766 }
1767 }
1768
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001769 DockedStackDividerController getDockedDividerController() {
1770 return mDividerControllerLocked;
1771 }
1772
Winson Chung655332c2016-10-31 13:14:28 -07001773 PinnedStackController getPinnedStackController() {
1774 return mPinnedStackControllerLocked;
1775 }
1776
Jeff Browna506a6e2013-06-04 00:02:38 -07001777 /**
1778 * Returns true if the specified UID has access to this display.
1779 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001780 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001781 return mDisplay.hasAccess(uid);
1782 }
1783
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001784 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001785 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001786 }
1787
Louis Changdc077272019-11-12 16:52:56 +08001788 ActivityStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001789 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001790 }
1791
Louis Chang2453d062019-11-19 22:30:48 +08001792 ActivityStack getRecentsStack() {
1793 return mTaskStackContainers.getRecentsStack();
1794 }
1795
Wale Ogunwale61911492017-10-11 08:50:50 -07001796 /**
Louis Chang2453d062019-11-19 22:30:48 +08001797 * @return The primary split-screen stack, and {@code null} otherwise.
Wale Ogunwale61911492017-10-11 08:50:50 -07001798 */
Louis Changdc077272019-11-12 16:52:56 +08001799 ActivityStack getSplitScreenPrimaryStack() {
Louis Chang2453d062019-11-19 22:30:48 +08001800 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001801 }
1802
Robert Carr9785cf32018-04-25 15:06:07 -07001803 boolean hasSplitScreenPrimaryStack() {
1804 return getSplitScreenPrimaryStack() != null;
1805 }
1806
Wale Ogunwale61911492017-10-11 08:50:50 -07001807 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001808 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001809 * not visible.
1810 */
Louis Changdc077272019-11-12 16:52:56 +08001811 ActivityStack getSplitScreenPrimaryStackIgnoringVisibility() {
Matthew Ng64e77cf2017-10-31 14:01:31 -07001812 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001813 }
1814
Louis Changdc077272019-11-12 16:52:56 +08001815 ActivityStack getPinnedStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001816 return mTaskStackContainers.getPinnedStack();
1817 }
1818
Riddle Hsuccf09402019-08-13 00:33:06 +08001819 boolean hasPinnedStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001820 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001821 }
1822
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001823 /**
1824 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1825 * Null is no compatible stack on the display.
1826 */
Louis Changdc077272019-11-12 16:52:56 +08001827 ActivityStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001828 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1829 }
1830
1831 /**
1832 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1833 * activity type. Null is no compatible stack on the display.
1834 */
Louis Changdc077272019-11-12 16:52:56 +08001835 ActivityStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001836 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001837 }
1838
Louis Chang2453d062019-11-19 22:30:48 +08001839 protected int getStackCount() {
1840 return mTaskStackContainers.mChildren.size();
1841 }
1842
1843 protected ActivityStack getStackAt(int index) {
1844 return mTaskStackContainers.mChildren.get(index);
1845 }
1846
1847 int getIndexOf(ActivityStack stack) {
1848 return mTaskStackContainers.getIndexOf(stack);
1849 }
1850
1851 void removeStack(ActivityStack stack) {
1852 mTaskStackContainers.removeChild(stack);
1853 }
1854
Bryce Lee48f4b572017-04-10 10:54:15 -07001855 @VisibleForTesting
Louis Changdc077272019-11-12 16:52:56 +08001856 WindowList<ActivityStack> getStacks() {
Kazuki Takise148d00a2018-05-31 15:32:19 +09001857 return mTaskStackContainers.mChildren;
1858 }
1859
1860 @VisibleForTesting
Louis Changdc077272019-11-12 16:52:56 +08001861 ActivityStack getTopStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001862 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001863 }
1864
Winson Chunge2d72172018-01-25 17:46:20 +00001865 ArrayList<Task> getVisibleTasks() {
1866 return mTaskStackContainers.getVisibleTasks();
1867 }
1868
Louis Changdc077272019-11-12 16:52:56 +08001869 void onStackWindowingModeChanged(ActivityStack stack) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001870 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001871 }
1872
Riddle Hsu7f704b52019-12-10 23:10:45 +08001873 /**
1874 * The value is only valid in the scope {@link #onRequestedOverrideConfigurationChanged} of the
1875 * changing hierarchy and the {@link #onConfigurationChanged} of its children.
1876 *
1877 * @return The current changes ({@link android.content.pm.ActivityInfo.Config}) of requested
1878 * override configuration.
1879 */
1880 int getCurrentOverrideConfigurationChanges() {
1881 return mCurrentOverrideConfigurationChanges;
1882 }
1883
1884 @Override
1885 public void onRequestedOverrideConfigurationChanged(Configuration overrideConfiguration) {
1886 mCurrentOverrideConfigurationChanges =
1887 getRequestedOverrideConfiguration().diff(overrideConfiguration);
1888 super.onRequestedOverrideConfigurationChanged(overrideConfiguration);
1889 mCurrentOverrideConfigurationChanges = 0;
1890 }
1891
Andrii Kulian441e4492016-09-29 15:25:00 -07001892 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001893 public void onConfigurationChanged(Configuration newParentConfig) {
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -05001894 final int lastOrientation = getConfiguration().orientation;
Andrii Kulian441e4492016-09-29 15:25:00 -07001895 super.onConfigurationChanged(newParentConfig);
Tiger Huang7c610aa2018-10-27 00:01:01 +08001896 if (mDisplayPolicy != null) {
1897 mDisplayPolicy.onConfigurationChanged();
1898 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001899
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -05001900 if (lastOrientation != getConfiguration().orientation) {
1901 getMetricsLogger().write(
1902 new LogMaker(MetricsEvent.ACTION_PHONE_ORIENTATION_CHANGED)
chaviw8065f442019-11-18 13:20:58 -08001903 .setSubtype(getConfiguration().orientation)
1904 .addTaggedData(MetricsEvent.FIELD_DISPLAY_ID, getDisplayId()));
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -05001905 }
1906
Evan Roskye747c3e2018-10-30 20:06:41 -07001907 // If there was no pinned stack, we still need to notify the controller of the display info
1908 // update as a result of the config change.
1909 if (mPinnedStackControllerLocked != null && !hasPinnedStack()) {
1910 mPinnedStackControllerLocked.onDisplayInfoChanged(getDisplayInfo());
1911 }
Evan Roskye747c3e2018-10-30 20:06:41 -07001912 }
1913
1914 /**
1915 * Updates the resources used by docked/pinned controllers. This needs to be called at the
1916 * beginning of a configuration update cascade since the metrics from these resources are used
1917 * for bounds calculations. Since ActivityDisplay initiates the configuration update, this
1918 * should be called from there instead of DisplayContent's onConfigurationChanged.
1919 */
1920 void preOnConfigurationChanged() {
Bryce Leef3c6a472017-11-14 14:53:06 -08001921 final DockedStackDividerController dividerController = getDockedDividerController();
1922
1923 if (dividerController != null) {
1924 getDockedDividerController().onConfigurationChanged();
1925 }
1926
1927 final PinnedStackController pinnedStackController = getPinnedStackController();
1928
1929 if (pinnedStackController != null) {
1930 getPinnedStackController().onConfigurationChanged();
1931 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001932 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001933
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001934 @Override
1935 boolean fillsParent() {
1936 return true;
1937 }
1938
1939 @Override
1940 boolean isVisible() {
1941 return true;
1942 }
1943
1944 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001945 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001946 super.onAppTransitionDone();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001947 mWmService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001948 }
1949
Yunfan Chen7daa6ac2018-11-29 18:16:44 -08001950 @Override
1951 public void setWindowingMode(int windowingMode) {
1952 super.setWindowingMode(windowingMode);
1953 super.setDisplayWindowingMode(windowingMode);
1954 }
1955
1956 @Override
1957 void setDisplayWindowingMode(int windowingMode) {
1958 setWindowingMode(windowingMode);
1959 }
1960
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001961 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1962 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1963 }
1964
Riddle Hsuccf09402019-08-13 00:33:06 +08001965 /**
1966 * In the general case, the orientation is computed from the above app windows first. If none of
1967 * the above app windows specify orientation, the orientation is computed from the child window
1968 * container, e.g. {@link AppWindowToken#getOrientation(int)}.
1969 */
Wale Ogunwale399c8692017-05-08 14:22:42 -07001970 @Override
1971 int getOrientation() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001972 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001973
Tiger Huang86e6d072019-05-02 20:23:47 +08001974 if (mIgnoreRotationForApps) {
1975 return SCREEN_ORIENTATION_USER;
1976 }
1977
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001978 if (mWmService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001979 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Adrian Roosb125e0b2019-10-02 14:55:14 +02001980 ProtoLog.v(WM_DEBUG_ORIENTATION,
1981 "Display id=%d is frozen, return %d", mDisplayId,
chaviw8065f442019-11-18 13:20:58 -08001982 mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001983 // If the display is frozen, some activities may be in the middle of restarting, and
1984 // thus have removed their old window. If the window has the flag to hide the lock
1985 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1986 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001987 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001988 } else if (policy.isKeyguardLocked()) {
1989 // Use the last orientation the while the display is frozen with the keyguard
1990 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1991 // window. We don't want to check the show when locked window directly though as
1992 // things aren't stable while the display is frozen, for example the window could be
1993 // momentarily unavailable due to activity relaunch.
Adrian Roosb125e0b2019-10-02 14:55:14 +02001994 ProtoLog.v(WM_DEBUG_ORIENTATION,
1995 "Display id=%d is frozen while keyguard locked, return %d",
chaviw8065f442019-11-18 13:20:58 -08001996 mDisplayId, getLastOrientation());
Riddle Hsuccf09402019-08-13 00:33:06 +08001997 return getLastOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001998 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001999 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002000 final int orientation = mAboveAppWindowsContainers.getOrientation();
2001 if (orientation != SCREEN_ORIENTATION_UNSET) {
2002 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002003 }
Wale Ogunwale51362492016-09-08 17:49:17 -07002004 }
2005
Wale Ogunwale399c8692017-05-08 14:22:42 -07002006 // Top system windows are not requesting an orientation. Start searching from apps.
2007 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07002008 }
2009
Craig Mautner46ac6fa2013-08-01 10:06:34 -07002010 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07002011 // Check if display metrics changed and update base values if needed.
2012 updateBaseDisplayMetricsIfNeeded();
2013
Craig Mautner722285e2012-09-07 13:55:58 -07002014 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07002015 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07002016
Garfield Tan2f145f22018-11-01 15:27:03 -07002017 onDisplayChanged(this);
Craig Mautner722285e2012-09-07 13:55:58 -07002018 }
2019
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02002020 @Override
2021 void onDisplayChanged(DisplayContent dc) {
2022 super.onDisplayChanged(dc);
2023 updateSystemGestureExclusionLimit();
2024 }
2025
2026 void updateSystemGestureExclusionLimit() {
Adrian Roos1c2e9a12019-08-20 18:23:47 +02002027 mSystemGestureExclusionLimit = mWmService.mConstants.mSystemGestureExclusionLimitDp
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02002028 * mDisplayMetrics.densityDpi / DENSITY_DEFAULT;
2029 updateSystemGestureExclusion();
2030 }
2031
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002032 void initializeDisplayBaseInfo() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002033 final DisplayManagerInternal displayManagerInternal = mWmService.mDisplayManagerInternal;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002034 if (displayManagerInternal != null) {
2035 // Bootstrap the default logical display from the display manager.
2036 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
2037 if (newDisplayInfo != null) {
2038 mDisplayInfo.copyFrom(newDisplayInfo);
2039 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002040 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002041
Andrii Kuliancd097992017-03-23 18:31:59 -07002042 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
2043 mDisplayInfo.logicalDensityDpi);
2044 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
2045 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
2046 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002047 mInitialDisplayCutout = mDisplayInfo.displayCutout;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002048 }
2049
Andrii Kuliancd097992017-03-23 18:31:59 -07002050 /**
2051 * If display metrics changed, overrides are not set and it's not just a rotation - update base
2052 * values.
2053 */
2054 private void updateBaseDisplayMetricsIfNeeded() {
2055 // Get real display metrics without overrides from WM.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002056 mWmService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
Andrii Kuliancd097992017-03-23 18:31:59 -07002057 final int orientation = mDisplayInfo.rotation;
2058 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2059 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
2060 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
2061 final int newDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002062 final DisplayCutout newCutout = mDisplayInfo.displayCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07002063
2064 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
2065 || mInitialDisplayHeight != newHeight
Adrian Roos1cf585052018-01-03 18:43:27 +01002066 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi
2067 || !Objects.equals(mInitialDisplayCutout, newCutout);
Andrii Kuliancd097992017-03-23 18:31:59 -07002068
2069 if (displayMetricsChanged) {
2070 // Check if display size or density is forced.
2071 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
2072 || mBaseDisplayHeight != mInitialDisplayHeight;
2073 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
2074
2075 // If there is an override set for base values - use it, otherwise use new values.
2076 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
2077 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
2078 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
2079
2080 // Real display metrics changed, so we should also update initial values.
2081 mInitialDisplayWidth = newWidth;
2082 mInitialDisplayHeight = newHeight;
2083 mInitialDisplayDensity = newDensity;
Adrian Roos1cf585052018-01-03 18:43:27 +01002084 mInitialDisplayCutout = newCutout;
Shivam Agrawal6472e0e2019-07-03 16:27:49 -07002085 reconfigureDisplayLocked();
Andrii Kuliancd097992017-03-23 18:31:59 -07002086 }
2087 }
2088
Bryce Lee27cec322017-03-21 09:41:37 -07002089 /** Sets the maximum width the screen resolution can be */
2090 void setMaxUiWidth(int width) {
2091 if (DEBUG_DISPLAY) {
2092 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
2093 }
2094
2095 mMaxUiWidth = width;
2096
2097 // Update existing metrics.
2098 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
2099 }
2100
2101 /** Update base (override) display metrics. */
2102 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
2103 mBaseDisplayWidth = baseWidth;
2104 mBaseDisplayHeight = baseHeight;
2105 mBaseDisplayDensity = baseDensity;
2106
2107 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
2108 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
2109 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
2110 mBaseDisplayWidth = mMaxUiWidth;
2111
2112 if (DEBUG_DISPLAY) {
2113 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
2114 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
2115 + " on display:" + getDisplayId());
2116 }
2117 }
2118
2119 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08002120
2121 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07002122 }
2123
Riddle Hsuf53da812018-08-15 22:00:27 +08002124 /**
2125 * Forces this display to use the specified density.
2126 *
2127 * @param density The density in DPI to use. If the value equals to initial density, the setting
2128 * will be cleared.
2129 * @param userId The target user to apply. Only meaningful when this is default display. If the
2130 * user id is {@link UserHandle#USER_CURRENT}, it means to apply current settings
2131 * so only need to configure display.
2132 */
2133 void setForcedDensity(int density, int userId) {
Riddle Hsuf53da812018-08-15 22:00:27 +08002134 final boolean updateCurrent = userId == UserHandle.USER_CURRENT;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002135 if (mWmService.mCurrentUserId == userId || updateCurrent) {
Riddle Hsuf53da812018-08-15 22:00:27 +08002136 mBaseDisplayDensity = density;
Shivam Agrawal6472e0e2019-07-03 16:27:49 -07002137 reconfigureDisplayLocked();
Riddle Hsuf53da812018-08-15 22:00:27 +08002138 }
2139 if (updateCurrent) {
2140 // We are applying existing settings so no need to save it again.
2141 return;
2142 }
2143
2144 if (density == mInitialDisplayDensity) {
2145 density = 0;
2146 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002147 mWmService.mDisplayWindowSettings.setForcedDensity(this, density, userId);
Riddle Hsuf53da812018-08-15 22:00:27 +08002148 }
2149
2150 /** @param mode {@link #FORCE_SCALING_MODE_AUTO} or {@link #FORCE_SCALING_MODE_DISABLED}. */
2151 void setForcedScalingMode(@ForceScalingMode int mode) {
2152 if (mode != FORCE_SCALING_MODE_DISABLED) {
2153 mode = FORCE_SCALING_MODE_AUTO;
2154 }
2155
2156 mDisplayScalingDisabled = (mode != FORCE_SCALING_MODE_AUTO);
2157 Slog.i(TAG_WM, "Using display scaling mode: " + (mDisplayScalingDisabled ? "off" : "auto"));
Shivam Agrawal6472e0e2019-07-03 16:27:49 -07002158 reconfigureDisplayLocked();
Riddle Hsuf53da812018-08-15 22:00:27 +08002159
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002160 mWmService.mDisplayWindowSettings.setForcedScalingMode(this, mode);
Riddle Hsuf53da812018-08-15 22:00:27 +08002161 }
2162
2163 /** If the given width and height equal to initial size, the setting will be cleared. */
2164 void setForcedSize(int width, int height) {
2165 final boolean clear = mInitialDisplayWidth == width && mInitialDisplayHeight == height;
2166 if (!clear) {
2167 // Set some sort of reasonable bounds on the size of the display that we will try
2168 // to emulate.
2169 final int minSize = 200;
2170 final int maxScale = 2;
2171 width = Math.min(Math.max(width, minSize), mInitialDisplayWidth * maxScale);
2172 height = Math.min(Math.max(height, minSize), mInitialDisplayHeight * maxScale);
2173 }
2174
2175 Slog.i(TAG_WM, "Using new display size: " + width + "x" + height);
2176 updateBaseDisplayMetrics(width, height, mBaseDisplayDensity);
Shivam Agrawal6472e0e2019-07-03 16:27:49 -07002177 reconfigureDisplayLocked();
Riddle Hsuf53da812018-08-15 22:00:27 +08002178
2179 if (clear) {
2180 width = height = 0;
2181 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002182 mWmService.mDisplayWindowSettings.setForcedSize(this, width, height);
Riddle Hsuf53da812018-08-15 22:00:27 +08002183 }
2184
Wale Ogunwaledb506192017-12-08 10:57:32 -08002185 void getStableRect(Rect out) {
2186 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08002187 }
2188
Louis Changdc077272019-11-12 16:52:56 +08002189 void setStackOnDisplay(ActivityStack stack, int position) {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07002190 if (DEBUG_STACK) Slog.d(TAG_WM, "Set stack=" + stack + " on displayId=" + mDisplayId);
2191 mTaskStackContainers.addChild(stack, position);
Andrii Kulian839def92016-11-02 10:58:58 -07002192 }
2193
Louis Changdc077272019-11-12 16:52:56 +08002194 void moveStackToDisplay(ActivityStack stack, boolean onTop) {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07002195 stack.reparent(mTaskStackContainers, onTop ? POSITION_TOP: POSITION_BOTTOM);
2196 }
Andrii Kulian839def92016-11-02 10:58:58 -07002197
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002198 @Override
2199 protected void addChild(DisplayChildWindowContainer child,
2200 Comparator<DisplayChildWindowContainer> comparator) {
2201 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2202 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002203
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002204 @Override
2205 protected void addChild(DisplayChildWindowContainer child, int index) {
2206 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2207 }
2208
2209 @Override
2210 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002211 // Only allow removal of direct children from this display if the display is in the process
2212 // of been removed.
2213 if (mRemovingDisplay) {
2214 super.removeChild(child);
2215 return;
2216 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002217 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002218 }
2219
Andrii Kuliand2765632016-12-12 22:26:34 -08002220 @Override
2221 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
2222 // Children of the display are statically ordered, so the real intention here is to perform
2223 // the operation on the display and not the static direct children.
2224 getParent().positionChildAt(position, this, includingParents);
2225 }
2226
Louis Changdc077272019-11-12 16:52:56 +08002227 void positionStackAt(int position, ActivityStack child, boolean includingParents) {
Riddle Hsu57831b52018-07-27 00:31:48 +08002228 mTaskStackContainers.positionChildAt(position, child, includingParents);
Winson Chung59a47ded2018-01-25 17:46:06 +00002229 layoutAndAssignWindowLayersIfNeeded();
2230 }
2231
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002232 /**
Winson Chungc5fe7ff2019-02-19 14:49:25 -08002233 * Returns true if the input point is within an app window.
2234 */
2235 boolean pointWithinAppWindow(int x, int y) {
2236 final int[] targetWindowType = {-1};
Wale Ogunwalef5f3f1b2019-11-12 09:47:30 -08002237 final PooledConsumer fn = PooledLambda.obtainConsumer((w, nonArg) -> {
Winson Chungc5fe7ff2019-02-19 14:49:25 -08002238 if (targetWindowType[0] != -1) {
2239 return;
2240 }
2241
2242 if (w.isOnScreen() && w.isVisibleLw() && w.getFrameLw().contains(x, y)) {
2243 targetWindowType[0] = w.mAttrs.type;
2244 return;
2245 }
2246 }, PooledLambda.__(WindowState.class), mTmpRect);
2247 forAllWindows(fn, true /* traverseTopToBottom */);
Wale Ogunwalef5f3f1b2019-11-12 09:47:30 -08002248 fn.recycle();
Winson Chungc5fe7ff2019-02-19 14:49:25 -08002249 return FIRST_APPLICATION_WINDOW <= targetWindowType[0]
chaviw8065f442019-11-18 13:20:58 -08002250 && targetWindowType[0] <= LAST_APPLICATION_WINDOW;
Winson Chungc5fe7ff2019-02-19 14:49:25 -08002251 }
2252
2253 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08002254 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07002255 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07002256 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07002257 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002258 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale85fb19a2019-12-05 10:41:05 +09002259 return mTmpTaskForResizePointSearchResult.process(mTaskStackContainers, x, y, delta);
Chong Zhang8e89b312015-09-09 15:09:30 -07002260 }
2261
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002262 void updateTouchExcludeRegion() {
2263 final Task focusedTask = (mFocusedApp != null ? mFocusedApp.getTask() : null);
David Stevensee9e2772017-02-09 16:30:27 -08002264 if (focusedTask == null) {
2265 mTouchExcludeRegion.setEmpty();
2266 } else {
2267 mTouchExcludeRegion.set(mBaseDisplayRect);
2268 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale85fb19a2019-12-05 10:41:05 +09002269 mTmpRect.setEmpty();
David Stevensee9e2772017-02-09 16:30:27 -08002270 mTmpRect2.setEmpty();
Wale Ogunwale85fb19a2019-12-05 10:41:05 +09002271
2272 final PooledConsumer c = PooledLambda.obtainConsumer(
2273 DisplayContent::processTaskForTouchExcludeRegion, this,
2274 PooledLambda.__(Task.class), focusedTask, delta);
2275 mTaskStackContainers.forAllTasks(c);
2276 c.recycle();
2277
David Stevensee9e2772017-02-09 16:30:27 -08002278 // If we removed the focused task above, add it back and only leave its
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002279 // outside touch area in the exclusion. TapDetector is not interested in
David Stevensee9e2772017-02-09 16:30:27 -08002280 // any touch inside the focused task itself.
2281 if (!mTmpRect2.isEmpty()) {
2282 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
2283 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08002284 }
lumark90120a82018-08-15 00:33:03 +08002285 if (mInputMethodWindow != null && mInputMethodWindow.isVisibleLw()) {
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002286 // If the input method is visible and the user is typing, we don't want these touch
2287 // events to be intercepted and used to change focus. This would likely cause a
2288 // disappearance of the input method.
lumark90120a82018-08-15 00:33:03 +08002289 mInputMethodWindow.getTouchableRegion(mTmpRegion);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002290 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002291 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002292 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002293 final WindowState win = mTapExcludedWindows.get(i);
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002294 win.getTouchableRegion(mTmpRegion);
2295 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
2296 }
Tiger Huang04dc4cc2019-01-17 18:41:41 +08002297 amendWindowTapExcludeRegion(mTouchExcludeRegion);
Andrii Kulian03c403d2016-11-11 11:14:12 -08002298 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07002299 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002300 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07002301 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002302 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
2303 }
Riddle Hsu2588ab02019-02-25 14:23:56 +08002304 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner6601b7b2013-04-29 10:29:11 -07002305 }
2306
Wale Ogunwale85fb19a2019-12-05 10:41:05 +09002307 private void processTaskForTouchExcludeRegion(Task task, Task focusedTask, int delta) {
2308 final ActivityRecord topVisibleActivity = task.getTopVisibleActivity();
2309
2310 if (topVisibleActivity == null || !topVisibleActivity.hasContentToDisplay()) {
2311 return;
2312 }
2313
2314 // Exclusion region is the region that TapDetector doesn't care about.
2315 // Here we want to remove all non-focused tasks from the exclusion region.
2316 // We also remove the outside touch area for resizing for all freeform
2317 // tasks (including the focused).
2318 // We save the focused task region once we find it, and add it back at the end.
2319 // If the task is home stack and it is resizable in the minimized state, we want to
2320 // exclude the docked stack from touch so we need the entire screen area and not just a
2321 // small portion which the home stack currently is resized to.
2322 if (task.isActivityTypeHome() && task.getStack().isMinimizedDockAndHomeStackResizable()) {
2323 mDisplayContent.getBounds(mTmpRect);
2324 } else {
2325 task.getDimBounds(mTmpRect);
2326 }
2327
2328 if (task == focusedTask) {
2329 // Add the focused task rect back into the exclude region once we are done
2330 // processing stacks.
2331 mTmpRect2.set(mTmpRect);
2332 }
2333
2334 final boolean isFreeformed = task.inFreeformWindowingMode();
2335 if (task != focusedTask || isFreeformed) {
2336 if (isFreeformed) {
2337 // If the task is freeformed, enlarge the area to account for outside
2338 // touch area for resize.
2339 mTmpRect.inset(-delta, -delta);
2340 // Intersect with display content rect. If we have system decor (status bar/
2341 // navigation bar), we want to exclude that from the tap detection.
2342 // Otherwise, if the app is partially placed under some system button (eg.
2343 // Recents, Home), pressing that button would cause a full series of
2344 // unwanted transfer focus/resume/pause, before we could go home.
2345 mTmpRect.intersect(mDisplayFrames.mContent);
2346 }
2347 mTouchExcludeRegion.op(mTmpRect, Region.Op.DIFFERENCE);
2348 }
2349 }
2350
Tiger Huang04dc4cc2019-01-17 18:41:41 +08002351 /**
2352 * Union the region with all the tap exclude region provided by windows on this display.
2353 *
2354 * @param inOutRegion The region to be amended.
2355 */
Wale Ogunwale85fb19a2019-12-05 10:41:05 +09002356 private void amendWindowTapExcludeRegion(Region inOutRegion) {
Tiger Huang04dc4cc2019-01-17 18:41:41 +08002357 for (int i = mTapExcludeProvidingWindows.size() - 1; i >= 0; i--) {
2358 final WindowState win = mTapExcludeProvidingWindows.valueAt(i);
2359 win.amendTapExcludeRegion(inOutRegion);
2360 }
2361 }
2362
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002363 @Override
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07002364 void switchUser(int userId) {
2365 super.switchUser(userId);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002366 mWmService.mWindowsChanged = true;
Winson Chungda20fec2019-04-10 12:19:59 -07002367 mDisplayPolicy.switchUser();
Craig Mautner858d8a62013-04-23 17:08:34 -07002368 }
2369
Wale Ogunwale10124582016-09-15 20:25:50 -07002370 @Override
2371 void removeIfPossible() {
lumark9bca6b42019-10-17 18:35:22 +08002372 if (isAnimating(TRANSITION | PARENTS)) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002373 mDeferredRemoval = true;
2374 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07002375 }
Wale Ogunwale10124582016-09-15 20:25:50 -07002376 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07002377 }
2378
Wale Ogunwale10124582016-09-15 20:25:50 -07002379 @Override
2380 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002381 mRemovingDisplay = true;
2382 try {
Jackal Guoc43a0a62019-04-23 09:15:14 +08002383 if (mParentWindow != null) {
2384 mParentWindow.removeEmbeddedDisplayContent(this);
2385 }
lumark588a3e82018-07-20 18:53:54 +08002386 // Clear all transitions & screen frozen states when removing display.
2387 mOpeningApps.clear();
2388 mClosingApps.clear();
Evan Rosky2289ba12018-11-19 18:28:18 -08002389 mChangingApps.clear();
lumark588a3e82018-07-20 18:53:54 +08002390 mUnknownAppVisibilityController.clear();
2391 mAppTransition.removeAppTransitionTimeoutCallbacks();
2392 handleAnimatingStoppedAndTransition();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002393 mWmService.stopFreezingDisplayLocked();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002394 super.removeImmediately();
2395 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Riddle Hsu2588ab02019-02-25 14:23:56 +08002396 mPointerEventDispatcher.dispose();
Vadim Caenb3715832019-08-13 17:06:38 +02002397 setRotationAnimation(null);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002398 mWmService.mAnimator.removeDisplayLocked(mDisplayId);
Jorim Jaggiae962e62018-12-27 17:23:48 +01002399 mInputMonitor.onDisplayRemoved();
Louis Chang2453d062019-11-19 22:30:48 +08002400 // TODO(display-merge): Remove cast
2401 mWmService.mDisplayNotificationController
2402 .dispatchDisplayRemoved((ActivityDisplay) this);
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002403 } finally {
Riddle Hsu4e611772018-10-31 18:58:28 +08002404 mDisplayReady = false;
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002405 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08002406 }
Robert Carr679ccb02018-08-08 15:32:35 -07002407
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002408 mWmService.mWindowPlacerLocked.requestTraversal();
Craig Mautner95da1082014-02-24 17:54:35 -08002409 }
2410
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002411 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07002412 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002413 boolean checkCompleteDeferredRemoval() {
Riddle Hsu793442c52019-11-05 01:04:55 +08002414 boolean stillDeferringRemoval = false;
2415
2416 for (int i = getChildCount() - 1; i >= 0; --i) {
2417 final DisplayChildWindowContainer child = getChildAt(i);
2418 stillDeferringRemoval |= child.checkCompleteDeferredRemoval();
2419 if (getChildCount() == 0) {
2420 // If this display is pending to be removed because it contains an activity with
2421 // {@link ActivityRecord#mIsExiting} is true, this display may be removed when
2422 // completing the removal of the last activity from
2423 // {@link ActivityRecord#checkCompleteDeferredRemoval}.
2424 return false;
2425 }
2426 }
Wale Ogunwale10124582016-09-15 20:25:50 -07002427
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002428 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002429 removeImmediately();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002430 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08002431 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002432 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08002433 }
2434
Andrii Kulian0214ed92017-05-16 13:44:05 -07002435 /** @return 'true' if removal of this display content is deferred due to active animation. */
2436 boolean isRemovalDeferred() {
2437 return mDeferredRemoval;
2438 }
2439
Wale Ogunwale10124582016-09-15 20:25:50 -07002440 boolean animateForIme(float interpolatedValue, float animationTarget,
2441 float dividerAnimationTarget) {
2442 boolean updated = false;
2443
Wale Ogunwale61911492017-10-11 08:50:50 -07002444 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
Louis Changdc077272019-11-12 16:52:56 +08002445 final ActivityStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002446 if (stack == null || !stack.isAdjustedForIme()) {
2447 continue;
2448 }
2449
2450 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
2451 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2452 updated = true;
2453 } else {
2454 mDividerControllerLocked.mLastAnimationProgress =
2455 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
2456 mDividerControllerLocked.mLastDividerProgress =
2457 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
2458 updated |= stack.updateAdjustForIme(
2459 mDividerControllerLocked.mLastAnimationProgress,
2460 mDividerControllerLocked.mLastDividerProgress,
2461 false /* force */);
2462 }
2463 if (interpolatedValue >= 1f) {
2464 stack.endImeAdjustAnimation();
2465 }
2466 }
2467
2468 return updated;
2469 }
2470
2471 boolean clearImeAdjustAnimation() {
2472 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002473 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
Louis Changdc077272019-11-12 16:52:56 +08002474 final ActivityStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002475 if (stack != null && stack.isAdjustedForIme()) {
2476 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2477 changed = true;
2478 }
2479 }
2480 return changed;
2481 }
2482
2483 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002484 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
Louis Changdc077272019-11-12 16:52:56 +08002485 final ActivityStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002486 if (stack.isVisible() && stack.isAdjustedForIme()) {
2487 stack.beginImeAdjustAnimation();
2488 }
2489 }
2490 }
2491
2492 void adjustForImeIfNeeded() {
lumark90120a82018-08-15 00:33:03 +08002493 final WindowState imeWin = mInputMethodWindow;
Wale Ogunwale10124582016-09-15 20:25:50 -07002494 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2495 && !mDividerControllerLocked.isImeHideRequested();
Louis Changdc077272019-11-12 16:52:56 +08002496 final ActivityStack dockedStack = getSplitScreenPrimaryStack();
Riddle Hsuc6814252019-05-16 17:06:52 +08002497 final boolean dockVisible = dockedStack != null;
Wale Ogunwalea38654f2019-11-17 20:37:15 -08002498 final Task topDockedTask = dockVisible ? dockedStack.getTask((t) -> true): null;
Louis Changdc077272019-11-12 16:52:56 +08002499 final ActivityStack imeTargetStack = mWmService.getImeFocusStackLocked();
Wale Ogunwale10124582016-09-15 20:25:50 -07002500 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2501 imeTargetStack.getDockSide() : DOCKED_INVALID;
2502 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2503 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002504 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002505 final boolean imeHeightChanged = imeVisible &&
2506 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2507
Riddle Hsuc6814252019-05-16 17:06:52 +08002508 // This includes a case where the docked stack is unminimizing and IME is visible for the
2509 // bottom side stack. The condition prevents adjusting the override task bounds for IME to
2510 // the minimized docked stack bounds.
2511 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock()
2512 || (topDockedTask != null && imeOnBottom && !dockedStack.isAdjustedForIme()
chaviw8065f442019-11-18 13:20:58 -08002513 && dockedStack.getBounds().height() < topDockedTask.getBounds().height());
Riddle Hsuc6814252019-05-16 17:06:52 +08002514
Wale Ogunwale10124582016-09-15 20:25:50 -07002515 // The divider could be adjusted for IME position, or be thinner than usual,
2516 // or both. There are three possible cases:
2517 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2518 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2519 // - If IME is not visible, divider is not moved and is normal width.
2520
2521 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002522 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
Louis Changdc077272019-11-12 16:52:56 +08002523 final ActivityStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002524 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002525 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2526 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002527 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2528 } else {
2529 stack.resetAdjustedForIme(false);
2530 }
2531 }
2532 mDividerControllerLocked.setAdjustedForIme(
2533 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2534 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002535 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
Louis Changdc077272019-11-12 16:52:56 +08002536 final ActivityStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002537 stack.resetAdjustedForIme(!dockVisible);
2538 }
2539 mDividerControllerLocked.setAdjustedForIme(
2540 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2541 }
Winson Chung655332c2016-10-31 13:14:28 -07002542 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002543 }
2544
2545 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002546 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
Louis Changdc077272019-11-12 16:52:56 +08002547 final ActivityStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002548 stack.prepareFreezingTaskBounds();
2549 }
2550 }
2551
Wale Ogunwale94744212015-09-21 19:01:47 -07002552 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002553 getBounds(mTmpRect, newRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002554 rotateBounds(mTmpRect, oldRotation, newRotation, bounds);
2555 }
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002556
Evan Rosky39b6f232018-10-30 18:35:41 -07002557 void rotateBounds(Rect parentBounds, int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002558 // Compute a transform matrix to undo the coordinate space transformation,
2559 // and present the window at the same physical position it previously occupied.
2560 final int deltaRotation = deltaRotation(newRotation, oldRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002561 createRotationMatrix(
2562 deltaRotation, parentBounds.width(), parentBounds.height(), mTmpMatrix);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002563
2564 mTmpRectF.set(bounds);
2565 mTmpMatrix.mapRect(mTmpRectF);
2566 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002567 }
2568
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002569 static int deltaRotation(int oldRotation, int newRotation) {
2570 int delta = newRotation - oldRotation;
2571 if (delta < 0) delta += 4;
2572 return delta;
2573 }
2574
Vadim Caenb3715832019-08-13 17:06:38 +02002575 public void setRotationAnimation(ScreenRotationAnimation screenRotationAnimation) {
2576 if (mScreenRotationAnimation != null) {
2577 mScreenRotationAnimation.kill();
2578 }
2579 mScreenRotationAnimation = screenRotationAnimation;
2580 }
2581
2582 public ScreenRotationAnimation getRotationAnimation() {
2583 return mScreenRotationAnimation;
2584 }
2585
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002586 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002587 Matrix outMatrix) {
2588 // For rotations without Z-ordering we don't need the target rectangle's position.
2589 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2590 displayHeight, outMatrix);
2591 }
2592
2593 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2594 float displayWidth, float displayHeight, Matrix outMatrix) {
2595 switch (rotation) {
2596 case ROTATION_0:
2597 outMatrix.reset();
2598 break;
2599 case ROTATION_270:
2600 outMatrix.setRotate(270, 0, 0);
2601 outMatrix.postTranslate(0, displayHeight);
2602 outMatrix.postTranslate(rectTop, 0);
2603 break;
2604 case ROTATION_180:
2605 outMatrix.reset();
2606 break;
2607 case ROTATION_90:
2608 outMatrix.setRotate(90, 0, 0);
2609 outMatrix.postTranslate(displayWidth, 0);
2610 outMatrix.postTranslate(-rectTop, rectLeft);
2611 break;
2612 }
2613 }
2614
Louis Chang2453d062019-11-19 22:30:48 +08002615 // TODO(proto-merge): Remove once protos for ActivityDisplay and DisplayContent are merged.
Jeffrey Huangcb782852019-12-05 11:28:11 -08002616 public void dumpDebugInner(ProtoOutputStream proto, long fieldId,
Nataniel Borges023ecb52019-01-16 14:15:43 -08002617 @WindowTraceLogLevel int logLevel) {
2618 // Critical log level logs only visible elements to mitigate performance overheard
2619 if (logLevel == WindowTraceLogLevel.CRITICAL && !isVisible()) {
2620 return;
2621 }
2622
Steven Timotiusaf03df62017-07-18 16:56:43 -07002623 final long token = proto.start(fieldId);
Jeffrey Huangcb782852019-12-05 11:28:11 -08002624 super.dumpDebug(proto, WINDOW_CONTAINER, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002625 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002626 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
Louis Changdc077272019-11-12 16:52:56 +08002627 final ActivityStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jeffrey Huangcb782852019-12-05 11:28:11 -08002628 stack.dumpDebugInnerStackOnly(proto, STACKS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002629 }
Jeffrey Huangcb782852019-12-05 11:28:11 -08002630 mDividerControllerLocked.dumpDebug(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2631 mPinnedStackControllerLocked.dumpDebug(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002632 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2633 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Jeffrey Huangcb782852019-12-05 11:28:11 -08002634 windowToken.dumpDebug(proto, ABOVE_APP_WINDOWS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002635 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002636 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2637 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Jeffrey Huangcb782852019-12-05 11:28:11 -08002638 windowToken.dumpDebug(proto, BELOW_APP_WINDOWS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002639 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002640 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2641 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Jeffrey Huangcb782852019-12-05 11:28:11 -08002642 windowToken.dumpDebug(proto, IME_WINDOWS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002643 }
chaviw8065f442019-11-18 13:20:58 -08002644 for (int i = mOverlayContainers.getChildCount() - 1; i >= 0; --i) {
2645 final WindowToken windowToken = mOverlayContainers.getChildAt(i);
2646 windowToken.dumpDebug(proto, OVERLAY_WINDOWS, logLevel);
2647 }
Steven Timotiusaf03df62017-07-18 16:56:43 -07002648 proto.write(DPI, mBaseDisplayDensity);
Jeffrey Huangcb782852019-12-05 11:28:11 -08002649 mDisplayInfo.dumpDebug(proto, DISPLAY_INFO);
Riddle Hsuccf09402019-08-13 00:33:06 +08002650 proto.write(ROTATION, getRotation());
Vadim Caenb3715832019-08-13 17:06:38 +02002651 final ScreenRotationAnimation screenRotationAnimation = getRotationAnimation();
Steven Timotiusf2d68892017-08-28 17:00:01 -07002652 if (screenRotationAnimation != null) {
Jeffrey Huangcb782852019-12-05 11:28:11 -08002653 screenRotationAnimation.dumpDebug(proto, SCREEN_ROTATION_ANIMATION);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002654 }
Jeffrey Huangcb782852019-12-05 11:28:11 -08002655 mDisplayFrames.dumpDebug(proto, DISPLAY_FRAMES);
2656 mAppTransition.dumpDebug(proto, APP_TRANSITION);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002657 if (mFocusedApp != null) {
2658 mFocusedApp.writeNameToProto(proto, FOCUSED_APP);
2659 }
Jorim Jaggi1112fed2019-04-15 13:32:14 +02002660 for (int i = mOpeningApps.size() - 1; i >= 0; i--) {
Wale Ogunwaleda8b8272018-11-29 19:37:37 -08002661 mOpeningApps.valueAt(i).writeIdentifierToProto(proto, OPENING_APPS);
Jorim Jaggi1112fed2019-04-15 13:32:14 +02002662 }
2663 for (int i = mClosingApps.size() - 1; i >= 0; i--) {
Wale Ogunwaleda8b8272018-11-29 19:37:37 -08002664 mClosingApps.valueAt(i).writeIdentifierToProto(proto, CLOSING_APPS);
Jorim Jaggi1112fed2019-04-15 13:32:14 +02002665 }
2666 for (int i = mChangingApps.size() - 1; i >= 0; i--) {
Wale Ogunwaleda8b8272018-11-29 19:37:37 -08002667 mChangingApps.valueAt(i).writeIdentifierToProto(proto, CHANGING_APPS);
Jorim Jaggi1112fed2019-04-15 13:32:14 +02002668 }
Steven Timotiusaf03df62017-07-18 16:56:43 -07002669 proto.end(token);
2670 }
2671
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002672 @Override
2673 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2674 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002675 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2676 final String subPrefix = " " + prefix;
2677 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
chaviw8065f442019-11-18 13:20:58 -08002678 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2679 pw.print("dpi");
2680 if (mInitialDisplayWidth != mBaseDisplayWidth
2681 || mInitialDisplayHeight != mBaseDisplayHeight
2682 || mInitialDisplayDensity != mBaseDisplayDensity) {
2683 pw.print(" base=");
2684 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2685 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2686 }
2687 if (mDisplayScalingDisabled) {
2688 pw.println(" noscale");
2689 }
2690 pw.print(" cur=");
2691 pw.print(mDisplayInfo.logicalWidth);
2692 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2693 pw.print(" app=");
2694 pw.print(mDisplayInfo.appWidth);
2695 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2696 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2697 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2698 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2699 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
2700 pw.print(subPrefix + "deferred=" + mDeferredRemoval
2701 + " mLayoutNeeded=" + mLayoutNeeded);
2702 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002703
Craig Mautnerdc548482014-02-05 13:35:24 -08002704 pw.println();
Adrian Roos5251b1d2018-03-23 18:57:43 +01002705 pw.print(prefix); pw.print("mLayoutSeq="); pw.println(mLayoutSeq);
2706
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002707 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
2708 if (mLastFocus != mCurrentFocus) {
2709 pw.print(" mLastFocus="); pw.println(mLastFocus);
2710 }
2711 if (mLosingFocus.size() > 0) {
2712 pw.println();
2713 pw.println(" Windows losing focus:");
2714 for (int i = mLosingFocus.size() - 1; i >= 0; i--) {
2715 final WindowState w = mLosingFocus.get(i);
2716 pw.print(" Losing #"); pw.print(i); pw.print(' ');
2717 pw.print(w);
2718 if (dumpAll) {
2719 pw.println(":");
2720 w.dump(pw, " ", true);
2721 } else {
2722 pw.println();
2723 }
2724 }
2725 }
2726 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
Tiger Huang7c610aa2018-10-27 00:01:01 +08002727 if (mLastStatusBarVisibility != 0) {
2728 pw.print(" mLastStatusBarVisibility=0x");
2729 pw.println(Integer.toHexString(mLastStatusBarVisibility));
2730 }
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002731
Adrian Roos5251b1d2018-03-23 18:57:43 +01002732 pw.println();
wilsonshihc32538e2018-11-07 17:27:34 +08002733 mWallpaperController.dump(pw, " ");
2734
2735 pw.println();
Adrian Roos4ffc8972019-02-07 20:45:11 +01002736 pw.print("mSystemGestureExclusion=");
2737 if (mSystemGestureExclusionListeners.getRegisteredCallbackCount() > 0) {
2738 pw.println(mSystemGestureExclusion);
2739 } else {
2740 pw.println("<no lstnrs>");
2741 }
2742
2743 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002744 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002745 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
Louis Changdc077272019-11-12 16:52:56 +08002746 final ActivityStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002747 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002748 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002749
Craig Mautnerdc548482014-02-05 13:35:24 -08002750 pw.println();
2751 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002752 pw.println();
2753 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002754 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002755 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002756 pw.print(" Exiting #"); pw.print(i);
2757 pw.print(' '); pw.print(token);
2758 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002759 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002760 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002761 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002762
Vadim Caenb3715832019-08-13 17:06:38 +02002763 final ScreenRotationAnimation rotationAnimation = getRotationAnimation();
2764 if (rotationAnimation != null) {
2765 pw.print(subPrefix);
2766 pw.println(" mScreenRotationAnimation:");
2767 rotationAnimation.printTo(" ", pw);
2768 } else if (dumpAll) {
2769 pw.print(subPrefix);
2770 pw.println(" no ScreenRotationAnimation ");
2771 }
2772
Jorim Jaggi31f71702016-05-04 16:43:04 -07002773 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002774
2775 // Dump stack references
Louis Changdc077272019-11-12 16:52:56 +08002776 final ActivityStack homeStack = getHomeStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002777 if (homeStack != null) {
2778 pw.println(prefix + "homeStack=" + homeStack.getName());
2779 }
Louis Changdc077272019-11-12 16:52:56 +08002780 final ActivityStack pinnedStack = getPinnedStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002781 if (pinnedStack != null) {
2782 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2783 }
Louis Changdc077272019-11-12 16:52:56 +08002784 final ActivityStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002785 if (splitScreenPrimaryStack != null) {
2786 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2787 }
2788
2789 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002790 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002791 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002792 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002793
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002794 pw.println();
2795 mDisplayFrames.dump(prefix, pw);
Arthur Hung95b38a92018-07-20 18:56:12 +08002796 pw.println();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08002797 mDisplayPolicy.dump(prefix, pw);
2798 pw.println();
Riddle Hsuad256a12018-07-18 16:11:30 +08002799 mDisplayRotation.dump(prefix, pw);
2800 pw.println();
Arthur Hung95b38a92018-07-20 18:56:12 +08002801 mInputMonitor.dump(pw, " ");
Jorim Jaggif96c90a2018-09-26 16:55:15 +02002802 pw.println();
2803 mInsetsStateController.dump(prefix, pw);
Craig Mautner59c00972012-07-30 12:10:24 -07002804 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002805
2806 @Override
2807 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002808 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002809 }
2810
2811 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002812 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002813 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002814
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002815 /** Returns true if the stack in the windowing mode is visible. */
2816 boolean isStackVisible(int windowingMode) {
Louis Changdc077272019-11-12 16:52:56 +08002817 final ActivityStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002818 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002819 }
2820
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002821 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002822 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002823 final int x = (int) xf;
2824 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002825 final WindowState touchedWin = getWindow(w -> {
2826 final int flags = w.mAttrs.flags;
2827 if (!w.isVisibleLw()) {
2828 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002829 }
2830 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002831 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002832 }
2833
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002834 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002835 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002836 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002837 }
2838
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002839 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002840
2841 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002842 return mTmpRegion.contains(x, y) || touchFlags == 0;
2843 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002844
2845 return touchedWin;
2846 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002847
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002848 boolean canAddToastWindowForUid(int uid) {
2849 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002850 // Also if the app is focused adding more than one toast at
2851 // a time for better backwards compatibility.
2852 final WindowState focusedWindowForUid = getWindow(w ->
2853 w.mOwnerUid == uid && w.isFocused());
2854 if (focusedWindowForUid != null) {
2855 return true;
2856 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002857 final WindowState win = getWindow(w ->
2858 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
chaviw8065f442019-11-18 13:20:58 -08002859 && !w.mWindowRemovalAllowed);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002860 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002861 }
2862
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002863 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002864 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2865 return;
2866 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002867
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002868 // Used to communicate the old focus to the callback method.
2869 mTmpWindow = oldFocus;
2870
2871 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002872 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002873
Louis Changa9350fe2019-04-25 17:14:20 +08002874 /**
2875 * Looking for the focused window on this display if the top focused display hasn't been
2876 * found yet (topFocusedDisplayId is INVALID_DISPLAY) or per-display focused was allowed.
2877 *
2878 * @param topFocusedDisplayId Id of the top focused display.
2879 * @return The focused window or null if there isn't any or no need to seek.
2880 */
2881 WindowState findFocusedWindowIfNeeded(int topFocusedDisplayId) {
2882 return (mWmService.mPerDisplayFocusEnabled || topFocusedDisplayId == INVALID_DISPLAY)
2883 ? findFocusedWindow() : null;
Tiger Huang51c5a1d2018-12-11 20:24:51 +08002884 }
2885
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002886 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002887 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002888
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002889 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002890
2891 if (mTmpWindow == null) {
Adrian Roosb125e0b2019-10-02 14:55:14 +02002892 ProtoLog.v(WM_DEBUG_FOCUS_LIGHT, "findFocusedWindow: No focusable windows.");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002893 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002894 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002895 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002896 }
2897
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002898 /**
2899 * Update the focused window and make some adjustments if the focus has changed.
2900 *
2901 * @param mode Indicates the situation we are in. Possible modes are:
2902 * {@link WindowManagerService#UPDATE_FOCUS_NORMAL},
2903 * {@link WindowManagerService#UPDATE_FOCUS_PLACING_SURFACES},
2904 * {@link WindowManagerService#UPDATE_FOCUS_WILL_PLACE_SURFACES},
2905 * {@link WindowManagerService#UPDATE_FOCUS_REMOVING_FOCUS}
2906 * @param updateInputWindows Whether to sync the window information to the input module.
Louis Changa9350fe2019-04-25 17:14:20 +08002907 * @param topFocusedDisplayId Display id of current top focused display.
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002908 * @return {@code true} if the focused window has changed.
2909 */
Louis Changa9350fe2019-04-25 17:14:20 +08002910 boolean updateFocusedWindowLocked(int mode, boolean updateInputWindows,
2911 int topFocusedDisplayId) {
2912 WindowState newFocus = findFocusedWindowIfNeeded(topFocusedDisplayId);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002913 if (mCurrentFocus == newFocus) {
2914 return false;
2915 }
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002916 boolean imWindowChanged = false;
Tiger Huang51c5a1d2018-12-11 20:24:51 +08002917 final WindowState imWindow = mInputMethodWindow;
2918 if (imWindow != null) {
2919 final WindowState prevTarget = mInputMethodTarget;
2920 final WindowState newTarget = computeImeTarget(true /* updateImeTarget*/);
2921 imWindowChanged = prevTarget != newTarget;
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002922
Tiger Huang51c5a1d2018-12-11 20:24:51 +08002923 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS
2924 && mode != UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2925 assignWindowLayers(false /* setLayoutNeeded */);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002926 }
2927 }
2928
2929 if (imWindowChanged) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002930 mWmService.mWindowsChanged = true;
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002931 setLayoutNeeded();
Louis Changa9350fe2019-04-25 17:14:20 +08002932 newFocus = findFocusedWindowIfNeeded(topFocusedDisplayId);
Tiger Huang51c5a1d2018-12-11 20:24:51 +08002933 }
2934 if (mCurrentFocus != newFocus) {
2935 mWmService.mH.obtainMessage(REPORT_FOCUS_CHANGE, this).sendToTarget();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002936 }
2937
Adrian Roosb125e0b2019-10-02 14:55:14 +02002938 ProtoLog.v(WM_DEBUG_FOCUS_LIGHT, "Changing focus from %s to %s displayId=%d Callers=%s",
chaviw8065f442019-11-18 13:20:58 -08002939 mCurrentFocus, newFocus, getDisplayId(), Debug.getCallers(4));
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002940 final WindowState oldFocus = mCurrentFocus;
2941 mCurrentFocus = newFocus;
2942 mLosingFocus.remove(newFocus);
2943
2944 if (newFocus != null) {
2945 mWinAddedSinceNullFocus.clear();
2946 mWinRemovedSinceNullFocus.clear();
2947
2948 if (newFocus.canReceiveKeys()) {
2949 // Displaying a window implicitly causes dispatching to be unpaused.
2950 // This is to protect against bugs if someone pauses dispatching but
2951 // forgets to resume.
2952 newFocus.mToken.paused = false;
2953 }
2954 }
2955
Tiger Huang7c610aa2018-10-27 00:01:01 +08002956 int focusChanged = getDisplayPolicy().focusChangedLw(oldFocus, newFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002957
2958 if (imWindowChanged && oldFocus != mInputMethodWindow) {
2959 // Focus of the input method window changed. Perform layout if needed.
2960 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2961 performLayout(true /*initial*/, updateInputWindows);
2962 focusChanged &= ~FINISH_LAYOUT_REDO_LAYOUT;
2963 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2964 // Client will do the layout, but we need to assign layers
2965 // for handleNewWindowLocked() below.
2966 assignWindowLayers(false /* setLayoutNeeded */);
2967 }
2968 }
2969
2970 if ((focusChanged & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2971 // The change in focus caused us to need to do a layout. Okay.
2972 setLayoutNeeded();
2973 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2974 performLayout(true /*initial*/, updateInputWindows);
2975 } else if (mode == UPDATE_FOCUS_REMOVING_FOCUS) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002976 mWmService.mRoot.performSurfacePlacement(false);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002977 }
2978 }
2979
2980 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
2981 // If we defer assigning layers, then the caller is responsible for doing this part.
2982 getInputMonitor().setInputFocusLw(newFocus, updateInputWindows);
2983 }
2984
2985 adjustForImeIfNeeded();
2986
2987 // We may need to schedule some toast windows to be removed. The toasts for an app that
2988 // does not have input focus are removed within a timeout to prevent apps to redress
2989 // other apps' UI.
2990 scheduleToastWindowsTimeoutIfNeededLocked(oldFocus, newFocus);
2991
2992 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2993 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
2994 }
2995 return true;
2996 }
2997
2998 /**
2999 * Set the new focused app to this display.
3000 *
Garfield Tane8d84ab2019-10-11 09:49:40 -07003001 * @param newFocus the new focused {@link ActivityRecord}.
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003002 * @return true if the focused app is changed.
3003 */
Garfield Tane8d84ab2019-10-11 09:49:40 -07003004 boolean setFocusedApp(ActivityRecord newFocus) {
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003005 if (newFocus != null) {
3006 final DisplayContent appDisplay = newFocus.getDisplayContent();
3007 if (appDisplay != this) {
3008 throw new IllegalStateException(newFocus + " is not on " + getName()
3009 + " but " + ((appDisplay != null) ? appDisplay.getName() : "none"));
3010 }
3011 }
3012 if (mFocusedApp == newFocus) {
3013 return false;
3014 }
3015 mFocusedApp = newFocus;
3016 getInputMonitor().setFocusedAppLw(newFocus);
3017 updateTouchExcludeRegion();
3018 return true;
3019 }
3020
Wale Ogunwalec69694a2016-10-18 13:51:15 -07003021 /** Updates the layer assignment of windows on this display. */
3022 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggi4981f152019-03-26 18:58:45 +01003023 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07003024 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07003025 if (setLayoutNeeded) {
3026 setLayoutNeeded();
3027 }
Robert Carrb1579c82017-09-05 14:54:47 -07003028
Robert Carrf59b8dd2017-10-02 18:58:36 -07003029 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07003030 // the application of this transaction until the animation pass triggers
3031 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
3032 // the hiding and showing of surfaces.
3033 scheduleAnimation();
Jorim Jaggi4981f152019-03-26 18:58:45 +01003034 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07003035 }
3036
Wale Ogunwale1666e312016-12-16 11:27:18 -08003037 // TODO: This should probably be called any time a visual change is made to the hierarchy like
3038 // moving containers or resizing them. Need to investigate the best way to have it automatically
3039 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003040 void layoutAndAssignWindowLayersIfNeeded() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003041 mWmService.mWindowsChanged = true;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003042 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003043
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003044 if (!mWmService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003045 false /*updateInputWindows*/)) {
3046 assignWindowLayers(false /* setLayoutNeeded */);
3047 }
3048
Arthur Hung95b38a92018-07-20 18:56:12 +08003049 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003050 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Arthur Hung95b38a92018-07-20 18:56:12 +08003051 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003052 }
3053
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003054 /** Returns true if a leaked surface was destroyed */
3055 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003056 // Used to indicate that a surface was leaked.
3057 mTmpWindow = null;
3058 forAllWindows(w -> {
3059 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003060 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003061 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003062 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003063 if (!mWmService.mSessions.contains(wsa.mSession)) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003064 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003065 + w + " surface=" + wsa.mSurfaceController
3066 + " token=" + w.mToken
3067 + " pid=" + w.mSession.mPid
3068 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003069 wsa.destroySurface();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003070 mWmService.mForceRemoves.add(w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003071 mTmpWindow = w;
Issei Suzukid4ee1cc2019-11-08 13:20:14 +01003072 } else if (w.mActivityRecord != null && !w.mActivityRecord.isClientVisible()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003073 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003074 + w + " surface=" + wsa.mSurfaceController
Garfield Tane8d84ab2019-10-11 09:49:40 -07003075 + " token=" + w.mActivityRecord);
Adrian Roosb125e0b2019-10-02 14:55:14 +02003076 ProtoLog.i(WM_SHOW_TRANSACTIONS, "SURFACE LEAK DESTROY: %s", w);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003077 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003078 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003079 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003080 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003081
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003082 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003083 }
3084
Riddle Hsuccf09402019-08-13 00:33:06 +08003085 boolean hasAlertWindowSurfaces() {
3086 for (int i = mWmService.mSessions.size() - 1; i >= 0; --i) {
3087 if (mWmService.mSessions.valueAt(i).hasAlertWindowSurfaces(this)) {
3088 return true;
3089 }
3090 }
3091 return false;
3092 }
3093
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003094 /**
lumark90120a82018-08-15 00:33:03 +08003095 * Set input method window for the display.
3096 * @param win Set when window added or Null when destroyed.
3097 */
3098 void setInputMethodWindowLocked(WindowState win) {
3099 mInputMethodWindow = win;
lumark70865a82018-09-17 16:56:26 +08003100 // Update display configuration for IME process.
3101 if (mInputMethodWindow != null) {
3102 final int imePid = mInputMethodWindow.mSession.mPid;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003103 mWmService.mAtmInternal.onImeWindowSetOnDisplay(imePid,
lumark70865a82018-09-17 16:56:26 +08003104 mInputMethodWindow.getDisplayId());
3105 }
lumark90120a82018-08-15 00:33:03 +08003106 computeImeTarget(true /* updateImeTarget */);
Tiger Huang332793b2019-10-29 23:21:27 +08003107 mInsetsStateController.getSourceProvider(ITYPE_IME).setWindow(win,
Jorim Jaggif96c90a2018-09-26 16:55:15 +02003108 null /* frameProvider */);
lumark90120a82018-08-15 00:33:03 +08003109 }
3110
3111 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003112 * Determine and return the window that should be the IME target.
3113 * @param updateImeTarget If true the system IME target will be updated to match what we found.
3114 * @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 +00003115 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003116 WindowState computeImeTarget(boolean updateImeTarget) {
lumark90120a82018-08-15 00:33:03 +08003117 if (mInputMethodWindow == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003118 // There isn't an IME so there shouldn't be a target...That was easy!
3119 if (updateImeTarget) {
3120 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
lumarkff0ab692018-11-05 20:32:30 +08003121 + mInputMethodTarget + " to null since mInputMethodWindow is null");
3122 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003123 }
3124 return null;
3125 }
3126
lumarkff0ab692018-11-05 20:32:30 +08003127 final WindowState curTarget = mInputMethodTarget;
Chavi Weingarten3a748552018-05-14 17:32:42 +00003128 if (!canUpdateImeTarget()) {
3129 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Defer updating IME target");
3130 return curTarget;
3131 }
3132
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003133 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
3134 // same display. Or even when the current IME/target are not on the same screen as the next
3135 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08003136 mUpdateImeTarget = updateImeTarget;
3137 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003138
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003139
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003140 // Yet more tricksyness! If this window is a "starting" window, we do actually want
3141 // to be on top of it, but it is not -really- where input will go. So look down below
3142 // for a real window to target...
3143 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07003144 final ActivityRecord activity = target.mActivityRecord;
3145 if (activity != null) {
3146 final WindowState betterTarget = activity.getImeTargetBelowWindow(target);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003147 if (betterTarget != null) {
3148 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003149 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003150 }
3151 }
3152
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003153 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
lumarkff0ab692018-11-05 20:32:30 +08003154 "Proposed new IME target: " + target + " for display: " + getDisplayId());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003155
chaviw1c13ad32018-06-12 16:44:23 -07003156 // Now, a special case -- if the last target's window is in the process of exiting, but
Tarandeep Singh2d0aca42019-03-28 14:08:58 -07003157 // not removed, keep on the last target to avoid IME flicker.
chaviw1c13ad32018-06-12 16:44:23 -07003158 if (curTarget != null && !curTarget.mRemoved && curTarget.isDisplayedLw()
Tarandeep Singh2d0aca42019-03-28 14:08:58 -07003159 && curTarget.isClosing()) {
3160 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Not changing target till current window is"
3161 + " closing and not removed");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003162 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003163 }
3164
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003165 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
3166 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003167
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003168 if (target == null) {
3169 if (updateImeTarget) {
3170 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
3171 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
3172 + Debug.getCallers(4) : ""));
lumarkff0ab692018-11-05 20:32:30 +08003173 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003174 }
3175
3176 return null;
3177 }
3178
3179 if (updateImeTarget) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07003180 ActivityRecord activity = curTarget == null ? null : curTarget.mActivityRecord;
3181 if (activity != null) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003182
3183 // Now some fun for dealing with window animations that modify the Z order. We need
3184 // to look at all windows below the current target that are in this app, finding the
3185 // highest visible one in layering.
3186 WindowState highestTarget = null;
lumark9bca6b42019-10-17 18:35:22 +08003187 if (activity.isAnimating(TRANSITION)) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07003188 highestTarget = activity.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003189 }
3190
3191 if (highestTarget != null) {
lumark9bca6b42019-10-17 18:35:22 +08003192 if (DEBUG_INPUT_METHOD) {
3193 Slog.v(TAG_WM, mAppTransition + " " + highestTarget + " animating="
3194 + highestTarget.isAnimating(TRANSITION | PARENTS));
3195 }
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003196
lumark588a3e82018-07-20 18:53:54 +08003197 if (mAppTransition.isTransitionSet()) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003198 // If we are currently setting up for an animation, hold everything until we
3199 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003200 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003201 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003202 }
3203 }
3204 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003205
3206 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
3207 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003208 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003209 }
3210
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003211 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003212 }
3213
lumarkff0ab692018-11-05 20:32:30 +08003214 /**
3215 * Calling {@link #computeImeTarget(boolean)} to update the input method target window in
3216 * the candidate app window token if needed.
3217 */
Garfield Tane8d84ab2019-10-11 09:49:40 -07003218 void computeImeTargetIfNeeded(ActivityRecord candidate) {
3219 if (mInputMethodTarget != null && mInputMethodTarget.mActivityRecord == candidate) {
lumarkff0ab692018-11-05 20:32:30 +08003220 computeImeTarget(true /* updateImeTarget */);
3221 }
3222 }
3223
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003224 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
lumarkff0ab692018-11-05 20:32:30 +08003225 if (target == mInputMethodTarget && mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003226 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003227 }
3228
lumarkff0ab692018-11-05 20:32:30 +08003229 mInputMethodTarget = target;
3230 mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003231 assignWindowLayers(false /* setLayoutNeeded */);
Tarandeep Singh215929b2019-01-11 18:24:37 -08003232 mInsetsStateController.onImeTargetChanged(target);
Tarandeep Singha6f35612019-01-11 19:50:46 -08003233 updateImeParent();
3234 }
3235
3236 private void updateImeParent() {
Jackal Guo818c3f12019-03-08 18:00:39 +08003237 // Force attaching IME to the display when magnifying, or it would be magnified with
3238 // target app together.
3239 final boolean shouldAttachToDisplay = (mMagnificationSpec != null);
3240 final SurfaceControl newParent =
chaviw8065f442019-11-18 13:20:58 -08003241 shouldAttachToDisplay ? mWindowContainers.getSurfaceControl() : computeImeParent();
Tarandeep Singha6f35612019-01-11 19:50:46 -08003242 if (newParent != null) {
Tiger Huanged6794e2019-05-07 20:07:59 +08003243 getPendingTransaction().reparent(mImeWindowsContainers.mSurfaceControl, newParent);
Tarandeep Singha6f35612019-01-11 19:50:46 -08003244 scheduleAnimation();
3245 }
3246 }
3247
3248 /**
3249 * Computes the window the IME should be attached to.
3250 */
3251 @VisibleForTesting
3252 SurfaceControl computeImeParent() {
3253
3254 // Attach it to app if the target is part of an app and such app is covering the entire
3255 // screen. If it's not covering the entire screen the IME might extend beyond the apps
3256 // bounds.
Garfield Tane8d84ab2019-10-11 09:49:40 -07003257 if (mInputMethodTarget != null && mInputMethodTarget.mActivityRecord != null
Riddle Hsu6d6f67c2019-03-14 16:54:26 +08003258 && mInputMethodTarget.getWindowingMode() == WINDOWING_MODE_FULLSCREEN
3259 // An activity with override bounds should be letterboxed inside its parent bounds,
3260 // so it doesn't fill the screen.
Garfield Tane8d84ab2019-10-11 09:49:40 -07003261 && mInputMethodTarget.mActivityRecord.matchParentBounds()) {
3262 return mInputMethodTarget.mActivityRecord.getSurfaceControl();
Tarandeep Singha6f35612019-01-11 19:50:46 -08003263 }
3264
3265 // Otherwise, we just attach it to the display.
chaviw8065f442019-11-18 13:20:58 -08003266 return mWindowContainers.getSurfaceControl();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003267 }
3268
3269 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
3270 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3271 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
3272 }
3273
3274 // Used to indicate we have reached the first window in the range we are interested in.
3275 mTmpWindow = null;
3276
3277 // TODO: Figure-out a more efficient way to do this.
3278 final WindowState candidate = getWindow(w -> {
3279 if (w == top) {
3280 // Reached the first window in the range we are interested in.
3281 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003282 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003283 if (mTmpWindow == null) {
3284 return false;
3285 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003286
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003287 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3288 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003289 }
3290 // If we reached the bottom of the range of windows we are considering,
3291 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003292 if (w == bottom) {
3293 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003294 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003295 return false;
3296 });
3297
3298 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003299 }
3300
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003301 void setLayoutNeeded() {
3302 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
3303 mLayoutNeeded = true;
3304 }
3305
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003306 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003307 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
3308 mLayoutNeeded = false;
3309 }
3310
3311 boolean isLayoutNeeded() {
3312 return mLayoutNeeded;
3313 }
3314
Wale Ogunwale02319a62016-09-26 15:21:22 -07003315 void dumpTokens(PrintWriter pw, boolean dumpAll) {
3316 if (mTokenMap.isEmpty()) {
3317 return;
3318 }
3319 pw.println(" Display #" + mDisplayId);
3320 final Iterator<WindowToken> it = mTokenMap.values().iterator();
3321 while (it.hasNext()) {
3322 final WindowToken token = it.next();
3323 pw.print(" ");
3324 pw.print(token);
3325 if (dumpAll) {
3326 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003327 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07003328 } else {
3329 pw.println();
3330 }
3331 }
lumark588a3e82018-07-20 18:53:54 +08003332
Evan Rosky2289ba12018-11-19 18:28:18 -08003333 if (!mOpeningApps.isEmpty() || !mClosingApps.isEmpty() || !mChangingApps.isEmpty()) {
lumark588a3e82018-07-20 18:53:54 +08003334 pw.println();
3335 if (mOpeningApps.size() > 0) {
3336 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
3337 }
3338 if (mClosingApps.size() > 0) {
3339 pw.print(" mClosingApps="); pw.println(mClosingApps);
3340 }
Evan Rosky2289ba12018-11-19 18:28:18 -08003341 if (mChangingApps.size() > 0) {
3342 pw.print(" mChangingApps="); pw.println(mChangingApps);
3343 }
lumark588a3e82018-07-20 18:53:54 +08003344 }
3345
3346 mUnknownAppVisibilityController.dump(pw, " ");
Wale Ogunwale02319a62016-09-26 15:21:22 -07003347 }
3348
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003349 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003350 final int[] index = new int[1];
3351 forAllWindows(w -> {
3352 final WindowStateAnimator wAnim = w.mWinAnimator;
3353 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
3354 index[0] = index[0] + 1;
3355 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003356 }
3357
Jorim Jaggife762342016-10-13 14:33:27 +02003358 /**
3359 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
3360 */
Issei Suzuki5609ccb2019-06-13 15:04:08 +02003361 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade,
3362 boolean subtle) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003363 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003364 forAllWindows(w -> {
Garfield Tane8d84ab2019-10-11 09:49:40 -07003365 if (w.mActivityRecord == null && policy.canBeHiddenByKeyguardLw(w)
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02003366 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Issei Suzuki5609ccb2019-06-13 15:04:08 +02003367 w.startAnimation(policy.createHiddenByKeyguardExit(
3368 onWallpaper, goingToShade, subtle));
Jorim Jaggife762342016-10-13 14:33:27 +02003369 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003370 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02003371 }
3372
Riddle Hsub2297ad2019-07-26 23:37:25 -06003373 /** @return {@code true} if there is window to wait before enabling the screen. */
3374 boolean shouldWaitForSystemDecorWindowsOnBoot() {
3375 if (!isDefaultDisplay && !supportsSystemDecorations()) {
3376 // Nothing to wait because the secondary display doesn't support system decorations,
3377 // there is no wallpaper, keyguard (status bar) or application (home) window to show
3378 // during booting.
3379 return false;
3380 }
Wale Ogunwale494009b82016-10-21 09:01:38 -07003381
Riddle Hsub2297ad2019-07-26 23:37:25 -06003382 final SparseBooleanArray drawnWindowTypes = new SparseBooleanArray();
3383 // Presuppose keyguard is drawn because if its window isn't attached, we don't know if it
3384 // wants to be shown or hidden, then it should not delay enabling the screen.
3385 drawnWindowTypes.put(TYPE_STATUS_BAR, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003386
Riddle Hsub2297ad2019-07-26 23:37:25 -06003387 final WindowState visibleNotDrawnWindow = getWindow(w -> {
3388 if (w.mViewVisibility == View.VISIBLE && !w.mObscured && !w.isDrawnLw()) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003389 return true;
3390 }
3391 if (w.isDrawnLw()) {
Riddle Hsub2297ad2019-07-26 23:37:25 -06003392 final int type = w.mAttrs.type;
3393 if (type == TYPE_BOOT_PROGRESS || type == TYPE_BASE_APPLICATION
3394 || type == TYPE_WALLPAPER) {
3395 drawnWindowTypes.put(type, true);
3396 } else if (type == TYPE_STATUS_BAR) {
3397 drawnWindowTypes.put(TYPE_STATUS_BAR, mWmService.mPolicy.isKeyguardDrawnLw());
Wale Ogunwale494009b82016-10-21 09:01:38 -07003398 }
3399 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003400 return false;
3401 });
3402
Riddle Hsub2297ad2019-07-26 23:37:25 -06003403 if (visibleNotDrawnWindow != null) {
3404 // Wait for the visible window to be drawn.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003405 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003406 }
3407
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003408 // if the wallpaper service is disabled on the device, we're never going to have
3409 // wallpaper, don't bother waiting for it
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003410 boolean wallpaperEnabled = mWmService.mContext.getResources().getBoolean(
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003411 com.android.internal.R.bool.config_enableWallpaperService)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003412 && mWmService.mContext.getResources().getBoolean(
chaviw8065f442019-11-18 13:20:58 -08003413 com.android.internal.R.bool.config_checkWallpaperAtBoot)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003414 && !mWmService.mOnlyCore;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003415
Riddle Hsub2297ad2019-07-26 23:37:25 -06003416 final boolean haveBootMsg = drawnWindowTypes.get(TYPE_BOOT_PROGRESS);
3417 final boolean haveApp = drawnWindowTypes.get(TYPE_BASE_APPLICATION);
3418 final boolean haveWallpaper = drawnWindowTypes.get(TYPE_WALLPAPER);
3419 final boolean haveKeyguard = drawnWindowTypes.get(TYPE_STATUS_BAR);
3420
Adrian Roosb125e0b2019-10-02 14:55:14 +02003421 ProtoLog.i(WM_DEBUG_SCREEN_ON,
Riddle Hsub2297ad2019-07-26 23:37:25 -06003422 "******** booted=%b msg=%b haveBoot=%b haveApp=%b haveWall=%b "
3423 + "wallEnabled=%b haveKeyguard=%b",
3424 mWmService.mSystemBooted, mWmService.mShowingBootMessages, haveBootMsg,
3425 haveApp, haveWallpaper, wallpaperEnabled, haveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003426
3427 // If we are turning on the screen to show the boot message, don't do it until the boot
3428 // message is actually displayed.
Riddle Hsub2297ad2019-07-26 23:37:25 -06003429 if (!mWmService.mSystemBooted && !haveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003430 return true;
3431 }
3432
3433 // If we are turning on the screen after the boot is completed normally, don't do so until
3434 // we have the application and wallpaper.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003435 if (mWmService.mSystemBooted
Riddle Hsub2297ad2019-07-26 23:37:25 -06003436 && ((!haveApp && !haveKeyguard) || (wallpaperEnabled && !haveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003437 return true;
3438 }
3439
3440 return false;
3441 }
3442
Jorim Jaggi8f520872018-08-14 17:00:20 +02003443 void updateWindowsForAnimator() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003444 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003445 }
3446
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003447 boolean isInputMethodClientFocus(int uid, int pid) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003448 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003449 if (imFocus == null) {
3450 return false;
3451 }
3452
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003453 if (DEBUG_INPUT_METHOD) {
3454 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003455 Slog.i(TAG_WM, "Current focus: " + mCurrentFocus + " displayId=" + mDisplayId);
3456 Slog.i(TAG_WM, "Last focus: " + mLastFocus + " displayId=" + mDisplayId);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003457 }
3458
Wale Ogunwale494009b82016-10-21 09:01:38 -07003459 if (DEBUG_INPUT_METHOD) {
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003460 Slog.i(TAG_WM, "IM target uid/pid: " + imFocus.mSession.mUid
3461 + "/" + imFocus.mSession.mPid);
3462 Slog.i(TAG_WM, "Requesting client uid/pid: " + uid + "/" + pid);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003463 }
3464
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003465 return imFocus.mSession.mUid == uid && imFocus.mSession.mPid == pid;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003466 }
3467
3468 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003469 final WindowState win = getWindow(
3470 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
3471 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003472 }
3473
Jorim Jaggi956ca412019-01-07 14:49:14 +01003474 void hideTransientBars() {
3475 // TODO(b/118118435): Remove this after migration
3476 final int transientFlags = View.STATUS_BAR_TRANSIENT | View.NAVIGATION_BAR_TRANSIENT;
3477 statusBarVisibilityChanged(mLastStatusBarVisibility & ~transientFlags);
3478
3479 getInsetsPolicy().hideTransient();
3480 }
3481
Tiger Huang7c610aa2018-10-27 00:01:01 +08003482 void statusBarVisibilityChanged(int visibility) {
3483 mLastStatusBarVisibility = visibility;
3484 visibility = getDisplayPolicy().adjustSystemUiVisibilityLw(visibility);
3485 updateStatusBarVisibilityLocked(visibility);
3486 }
3487
3488 private boolean updateStatusBarVisibilityLocked(int visibility) {
3489 if (mLastDispatchedSystemUiVisibility == visibility) {
3490 return false;
3491 }
3492 final int globalDiff = (visibility ^ mLastDispatchedSystemUiVisibility)
3493 // We are only interested in differences of one of the
3494 // clearable flags...
3495 & View.SYSTEM_UI_CLEARABLE_FLAGS
3496 // ...if it has actually been cleared.
3497 & ~visibility;
3498
3499 mLastDispatchedSystemUiVisibility = visibility;
3500 if (isDefaultDisplay) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003501 mWmService.mInputManager.setSystemUiVisibility(visibility);
Tiger Huang7c610aa2018-10-27 00:01:01 +08003502 }
3503 updateSystemUiVisibility(visibility, globalDiff);
3504 return true;
3505 }
3506
Wale Ogunwale494009b82016-10-21 09:01:38 -07003507 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003508 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003509 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003510 final int curValue = w.mSystemUiVisibility;
3511 final int diff = (curValue ^ visibility) & globalDiff;
3512 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003513 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003514 w.mSeq++;
3515 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003516 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003517 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
3518 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07003519 visibility, newValue, diff);
3520 }
3521 } catch (RemoteException e) {
3522 // so sorry
3523 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003524 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003525 }
3526
Tiger Huang7c610aa2018-10-27 00:01:01 +08003527 void reevaluateStatusBarVisibility() {
3528 int visibility = getDisplayPolicy().adjustSystemUiVisibilityLw(mLastStatusBarVisibility);
3529 if (updateStatusBarVisibilityLocked(visibility)) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003530 mWmService.mWindowPlacerLocked.requestTraversal();
Tiger Huang7c610aa2018-10-27 00:01:01 +08003531 }
3532 }
3533
Wale Ogunwale494009b82016-10-21 09:01:38 -07003534 void onWindowFreezeTimeout() {
3535 Slog.w(TAG_WM, "Window freeze timeout expired.");
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003536 mWmService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003537
3538 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07003539 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003540 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003541 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07003542 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003543 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003544 - mWmService.mDisplayFreezeTime);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003545 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003546 }, true /* traverseTopToBottom */);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003547 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003548 }
3549
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003550 // TODO: Super crazy long method that should be broken down...
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003551 void applySurfaceChangesTransaction(boolean recoveringMemory) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003552 final WindowSurfacePlacer surfacePlacer = mWmService.mWindowPlacerLocked;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003553
3554 mTmpUpdateAllDrawn.clear();
3555
3556 int repeats = 0;
3557 do {
3558 repeats++;
3559 if (repeats > 6) {
3560 Slog.w(TAG, "Animation repeat aborted after too many iterations");
3561 clearLayoutNeeded();
3562 break;
3563 }
3564
3565 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
3566 pendingLayoutChanges);
3567
wilsonshihc32538e2018-11-07 17:27:34 +08003568 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
3569 mWallpaperController.adjustWallpaperWindows();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003570 }
3571
Riddle Hsu654a6f92018-07-13 22:59:36 +08003572 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003573 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
Riddle Hsuccf09402019-08-13 00:33:06 +08003574 if (updateOrientation()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003575 setLayoutNeeded();
Shivam Agrawal6472e0e2019-07-03 16:27:49 -07003576 sendNewConfiguration();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003577 }
3578 }
3579
3580 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
3581 setLayoutNeeded();
3582 }
3583
3584 // FIRST LOOP: Perform a layout, if needed.
3585 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
3586 performLayout(repeats == 1, false /* updateInputWindows */);
3587 } else {
3588 Slog.w(TAG, "Layout repeat skipped after too many iterations");
3589 }
3590
3591 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
3592 pendingLayoutChanges = 0;
3593
Jorim Jaggi4981f152019-03-26 18:58:45 +01003594 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "applyPostLayoutPolicy");
3595 try {
3596 mDisplayPolicy.beginPostLayoutPolicyLw();
3597 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
3598 pendingLayoutChanges |= mDisplayPolicy.finishPostLayoutPolicyLw();
3599 } finally {
3600 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
3601 }
Tiger Huang7c610aa2018-10-27 00:01:01 +08003602 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
3603 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
chaviw8065f442019-11-18 13:20:58 -08003604 mInsetsStateController.onPostLayout();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003605 } while (pendingLayoutChanges != 0);
3606
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003607 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003608
Jorim Jaggi4981f152019-03-26 18:58:45 +01003609 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "applyWindowSurfaceChanges");
3610 try {
3611 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
3612 } finally {
3613 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
3614 }
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01003615 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003616
Jorim Jaggif1292892018-09-10 11:58:13 +02003617 mLastHasContent = mTmpApplySurfaceChangesTransactionState.displayHasContent;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003618 mWmService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Jorim Jaggif1292892018-09-10 11:58:13 +02003619 mLastHasContent,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003620 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
3621 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003622 true /* inTraversal, must call performTraversalInTrans... below */);
3623
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003624 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
3625 if (wallpaperVisible != mLastWallpaperVisible) {
3626 mLastWallpaperVisible = wallpaperVisible;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003627 mWmService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003628 }
3629
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003630 while (!mTmpUpdateAllDrawn.isEmpty()) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07003631 final ActivityRecord activity = mTmpUpdateAllDrawn.removeLast();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003632 // See if any windows have been drawn, so they (and others associated with them)
3633 // can now be shown.
Garfield Tane8d84ab2019-10-11 09:49:40 -07003634 activity.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003635 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003636 }
3637
Bryce Leef3c6a472017-11-14 14:53:06 -08003638 private void updateBounds() {
Evan Rosky39b6f232018-10-30 18:35:41 -07003639 calculateBounds(mDisplayInfo, mTmpBounds);
Bryce Leef3c6a472017-11-14 14:53:06 -08003640 setBounds(mTmpBounds);
Tiger Huang04dc4cc2019-01-17 18:41:41 +08003641 if (mPortalWindowHandle != null && mParentSurfaceControl != null) {
3642 mPortalWindowHandle.touchableRegion.getBounds(mTmpRect);
3643 if (!mTmpBounds.equals(mTmpRect)) {
3644 mPortalWindowHandle.touchableRegion.set(mTmpBounds);
Tiger Huanged6794e2019-05-07 20:07:59 +08003645 getPendingTransaction().setInputWindowInfo(
3646 mParentSurfaceControl, mPortalWindowHandle);
Tiger Huang04dc4cc2019-01-17 18:41:41 +08003647 }
3648 }
Bryce Leef3c6a472017-11-14 14:53:06 -08003649 }
3650
3651 // Determines the current display bounds based on the current state
Evan Rosky39b6f232018-10-30 18:35:41 -07003652 private void calculateBounds(DisplayInfo displayInfo, Rect out) {
Bryce Leef3c6a472017-11-14 14:53:06 -08003653 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Evan Rosky39b6f232018-10-30 18:35:41 -07003654 final int rotation = displayInfo.rotation;
3655 boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Bryce Leef3c6a472017-11-14 14:53:06 -08003656 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
3657 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Evan Rosky39b6f232018-10-30 18:35:41 -07003658 int width = displayInfo.logicalWidth;
Bryce Leef3c6a472017-11-14 14:53:06 -08003659 int left = (physWidth - width) / 2;
Evan Rosky39b6f232018-10-30 18:35:41 -07003660 int height = displayInfo.logicalHeight;
Bryce Leef3c6a472017-11-14 14:53:06 -08003661 int top = (physHeight - height) / 2;
3662 out.set(left, top, left + width, top + height);
3663 }
3664
Bryce Leef3c6a472017-11-14 14:53:06 -08003665 private void getBounds(Rect out, int orientation) {
3666 getBounds(out);
3667
3668 // Rotate the Rect if needed.
3669 final int currentRotation = mDisplayInfo.rotation;
3670 final int rotationDelta = deltaRotation(currentRotation, orientation);
3671 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
3672 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
3673 mTmpRectF.set(out);
3674 mTmpMatrix.mapRect(mTmpRectF);
3675 mTmpRectF.round(out);
3676 }
3677 }
3678
Evan Rosky730f6e82018-12-03 17:40:11 -08003679 /** @returns the orientation of the display when it's rotation is ROTATION_0. */
3680 int getNaturalOrientation() {
3681 return mBaseDisplayWidth < mBaseDisplayHeight
3682 ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
3683 }
3684
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003685 void performLayout(boolean initial, boolean updateInputWindows) {
Jorim Jaggi4981f152019-03-26 18:58:45 +01003686 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "performLayout");
3687 try {
3688 performLayoutNoTrace(initial, updateInputWindows);
3689 } finally {
3690 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
3691 }
3692 }
3693
3694 private void performLayoutNoTrace(boolean initial, boolean updateInputWindows) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003695 if (!isLayoutNeeded()) {
3696 return;
3697 }
3698 clearLayoutNeeded();
3699
3700 final int dw = mDisplayInfo.logicalWidth;
3701 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003702 if (DEBUG_LAYOUT) {
3703 Slog.v(TAG, "-------------------------------------");
Jorim Jaggi4981f152019-03-26 18:58:45 +01003704 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw
3705 + " dh=" + dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003706 }
3707
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01003708 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
3709 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Jorim Jaggi4981f152019-03-26 18:58:45 +01003710 // TODO: Not sure if we really need to set the rotation here since we are updating from
3711 // the display info above...
Riddle Hsuccf09402019-08-13 00:33:06 +08003712 mDisplayFrames.mRotation = getRotation();
Tiger Huang7c610aa2018-10-27 00:01:01 +08003713 mDisplayPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003714
Adrian Roos5251b1d2018-03-23 18:57:43 +01003715 int seq = mLayoutSeq + 1;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003716 if (seq < 0) seq = 0;
Adrian Roos5251b1d2018-03-23 18:57:43 +01003717 mLayoutSeq = seq;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003718
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003719 // Used to indicate that we have processed the dream window and all additional windows are
3720 // behind it.
3721 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003722 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003723
3724 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003725 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003726
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003727 // Used to indicate that we have processed the dream window and all additional attached
3728 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003729 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003730 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003731
3732 // Now perform layout of attached windows, which usually depend on the position of the
3733 // window they are attached to. XXX does not deal with windows that are attached to windows
3734 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003735 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003736
3737 // Window frames may have changed. Tell the input dispatcher about it.
Arthur Hung95b38a92018-07-20 18:56:12 +08003738 mInputMonitor.layoutInputConsumers(dw, dh);
3739 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003740 if (updateInputWindows) {
Arthur Hung95b38a92018-07-20 18:56:12 +08003741 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003742 }
3743
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003744 mWmService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003745 }
3746
3747 /**
3748 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
3749 * In portrait mode, it grabs the full screenshot.
3750 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003751 * @param config of the output bitmap
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003752 */
chaviw0315a1a2018-03-05 15:28:35 -08003753 Bitmap screenshotDisplayLocked(Bitmap.Config config) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003754 if (!mWmService.mPolicy.isScreenOn()) {
chaviw0315a1a2018-03-05 15:28:35 -08003755 if (DEBUG_SCREENSHOT) {
3756 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003757 }
chaviw0315a1a2018-03-05 15:28:35 -08003758 return null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003759 }
chaviwfbe47df2017-11-10 16:14:49 -08003760
chaviw0315a1a2018-03-05 15:28:35 -08003761 int dw = mDisplayInfo.logicalWidth;
3762 int dh = mDisplayInfo.logicalHeight;
chaviwfbe47df2017-11-10 16:14:49 -08003763
chaviw0315a1a2018-03-05 15:28:35 -08003764 if (dw <= 0 || dh <= 0) {
3765 return null;
3766 }
chaviwfbe47df2017-11-10 16:14:49 -08003767
chaviw0315a1a2018-03-05 15:28:35 -08003768 final Rect frame = new Rect(0, 0, dw, dh);
chaviwfbe47df2017-11-10 16:14:49 -08003769
chaviw0315a1a2018-03-05 15:28:35 -08003770 // The screenshot API does not apply the current screen rotation.
3771 int rot = mDisplay.getRotation();
chaviwfbe47df2017-11-10 16:14:49 -08003772
chaviw0315a1a2018-03-05 15:28:35 -08003773 if (rot == ROTATION_90 || rot == ROTATION_270) {
3774 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3775 }
chaviwfbe47df2017-11-10 16:14:49 -08003776
chaviw0315a1a2018-03-05 15:28:35 -08003777 // SurfaceFlinger is not aware of orientation, so convert our logical
3778 // crop to SurfaceFlinger's portrait orientation.
3779 convertCropForSurfaceFlinger(frame, rot, dw, dh);
3780
3781 final ScreenRotationAnimation screenRotationAnimation =
Vadim Caenb3715832019-08-13 17:06:38 +02003782 mWmService.mRoot.getDisplayContent(DEFAULT_DISPLAY).getRotationAnimation();
chaviw0315a1a2018-03-05 15:28:35 -08003783 final boolean inRotation = screenRotationAnimation != null &&
3784 screenRotationAnimation.isAnimating();
3785 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
3786
3787 // TODO(b/68392460): We should screenshot Task controls directly
3788 // but it's difficult at the moment as the Task doesn't have the
3789 // correct size set.
chaviw08520a02018-09-10 16:44:56 -07003790 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, inRotation, rot);
chaviw0315a1a2018-03-05 15:28:35 -08003791 if (bitmap == null) {
3792 Slog.w(TAG_WM, "Failed to take screenshot");
3793 return null;
3794 }
3795
3796 // Create a copy of the screenshot that is immutable and backed in ashmem.
3797 // This greatly reduces the overhead of passing the bitmap between processes.
3798 final Bitmap ret = bitmap.createAshmemBitmap(config);
3799 bitmap.recycle();
3800 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003801 }
3802
3803 // TODO: Can this use createRotationMatrix()?
3804 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3805 if (rot == Surface.ROTATION_90) {
3806 final int tmp = crop.top;
3807 crop.top = dw - crop.right;
3808 crop.right = crop.bottom;
3809 crop.bottom = dw - crop.left;
3810 crop.left = tmp;
3811 } else if (rot == Surface.ROTATION_180) {
3812 int tmp = crop.top;
3813 crop.top = dh - crop.bottom;
3814 crop.bottom = dh - tmp;
3815 tmp = crop.right;
3816 crop.right = dw - crop.left;
3817 crop.left = dw - tmp;
3818 } else if (rot == Surface.ROTATION_270) {
3819 final int tmp = crop.top;
3820 crop.top = crop.left;
3821 crop.left = dh - crop.bottom;
3822 crop.bottom = crop.right;
3823 crop.right = dh - tmp;
3824 }
3825 }
3826
Wale Ogunwale1666e312016-12-16 11:27:18 -08003827 void setExitingTokensHasVisible(boolean hasVisible) {
3828 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3829 mExitingTokens.get(i).hasVisible = hasVisible;
3830 }
3831
3832 // Initialize state of exiting applications.
3833 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3834 }
3835
3836 void removeExistingTokensIfPossible() {
3837 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3838 final WindowToken token = mExitingTokens.get(i);
3839 if (!token.hasVisible) {
3840 mExitingTokens.remove(i);
3841 }
3842 }
3843
3844 // Time to remove any exiting applications?
3845 mTaskStackContainers.removeExistingAppTokensIfPossible();
3846 }
3847
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003848 @Override
3849 void onDescendantOverrideConfigurationChanged() {
3850 setLayoutNeeded();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003851 mWmService.requestTraversal();
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003852 }
3853
David Stevens9440dc82017-03-16 19:00:20 -07003854 boolean okToDisplay() {
3855 if (mDisplayId == DEFAULT_DISPLAY) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003856 return !mWmService.mDisplayFrozen
3857 && mWmService.mDisplayEnabled && mWmService.mPolicy.isScreenOn();
David Stevens9440dc82017-03-16 19:00:20 -07003858 }
3859 return mDisplayInfo.state == Display.STATE_ON;
3860 }
3861
3862 boolean okToAnimate() {
3863 return okToDisplay() &&
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003864 (mDisplayId != DEFAULT_DISPLAY || mWmService.mPolicy.okToAnimate());
David Stevens9440dc82017-03-16 19:00:20 -07003865 }
3866
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003867 static final class TaskForResizePointSearchResult {
Wale Ogunwale85fb19a2019-12-05 10:41:05 +09003868 private Task taskForResize;
3869 private int x;
3870 private int y;
3871 private int delta;
3872 private Rect mTmpRect = new Rect();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003873
Wale Ogunwale85fb19a2019-12-05 10:41:05 +09003874 Task process(WindowContainer root, int x, int y, int delta) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003875 taskForResize = null;
Wale Ogunwale85fb19a2019-12-05 10:41:05 +09003876 this.x = x;
3877 this.y = y;
3878 this.delta = delta;
3879 mTmpRect.setEmpty();
3880
3881 final PooledFunction f = PooledLambda.obtainFunction(
3882 TaskForResizePointSearchResult::processTask, this, PooledLambda.__(Task.class));
3883 root.forAllTasks(f);
3884 f.recycle();
3885
3886 return taskForResize;
3887 }
3888
3889 private boolean processTask(Task task) {
3890 if (!task.getStack().getWindowConfiguration().canResizeTask()) {
3891 return true;
3892 }
3893
3894 if (task.getWindowingMode() == WINDOWING_MODE_FULLSCREEN) {
3895 return true;
3896 }
3897
3898 // We need to use the task's dim bounds (which is derived from the visible bounds of
3899 // its apps windows) for any touch-related tests. Can't use the task's original
3900 // bounds because it might be adjusted to fit the content frame. One example is when
3901 // the task is put to top-left quadrant, the actual visible area would not start at
3902 // (0,0) after it's adjusted for the status bar.
3903 task.getDimBounds(mTmpRect);
3904 mTmpRect.inset(-delta, -delta);
3905 if (mTmpRect.contains(x, y)) {
3906 mTmpRect.inset(delta, delta);
3907
3908 if (!mTmpRect.contains(x, y)) {
3909 taskForResize = task;
3910 return true;
3911 }
3912 // User touched inside the task. No need to look further,
3913 // focus transfer will be handled in ACTION_UP.
3914 return true;
3915 }
3916
3917 return false;
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003918 }
3919 }
Robert Carr3b716242016-08-16 16:02:21 -07003920
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003921 private static final class ApplySurfaceChangesTransactionState {
3922 boolean displayHasContent;
3923 boolean obscured;
3924 boolean syswin;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003925 float preferredRefreshRate;
3926 int preferredModeId;
3927
3928 void reset() {
3929 displayHasContent = false;
3930 obscured = false;
3931 syswin = false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003932 preferredRefreshRate = 0;
3933 preferredModeId = 0;
3934 }
3935 }
3936
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003937 /**
3938 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3939 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3940 * homogeneous children type which is currently required by sub-classes of
3941 * {@link WindowContainer} class.
3942 */
3943 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3944
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003945 DisplayChildWindowContainer(WindowManagerService service) {
3946 super(service);
3947 }
3948
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003949 @Override
3950 boolean fillsParent() {
3951 return true;
3952 }
3953
3954 @Override
3955 boolean isVisible() {
3956 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003957 }
3958 }
3959
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003960 /**
3961 * Window container class that contains all containers on this display relating to Apps.
3962 * I.e Activities.
3963 */
Louis Changdc077272019-11-12 16:52:56 +08003964 private final class TaskStackContainers extends DisplayChildWindowContainer<ActivityStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003965 /**
3966 * A control placed at the appropriate level for transitions to occur.
3967 */
chaviw23ee71c2017-12-18 11:29:41 -08003968 SurfaceControl mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02003969 SurfaceControl mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02003970 SurfaceControl mHomeAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003971
Robert Carrf7a7ca82017-12-06 13:17:07 -08003972 /**
3973 * Given that the split-screen divider does not have an AppWindowToken, it
3974 * will have to live inside of a "NonAppWindowContainer", in particular
3975 * {@link DisplayContent#mAboveAppWindowsContainers}. However, in visual Z order
3976 * it will need to be interleaved with some of our children, appearing on top of
3977 * both docked stacks but underneath any assistant stacks.
3978 *
3979 * To solve this problem we have this anchor control, which will always exist so
3980 * we can always assign it the correct value in our {@link #assignChildLayers}.
3981 * Likewise since it always exists, {@link AboveAppWindowContainers} can always
3982 * assign the divider a layer relative to it. This way we prevent linking lifecycle
3983 * events between the two containers.
3984 */
3985 SurfaceControl mSplitScreenDividerAnchor = null;
3986
Wale Ogunwale61911492017-10-11 08:50:50 -07003987 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3988 // through the list to find them.
Louis Changdc077272019-11-12 16:52:56 +08003989 private ActivityStack mHomeStack = null;
Louis Chang2453d062019-11-19 22:30:48 +08003990 private ActivityStack mRecentsStack = null;
Louis Changdc077272019-11-12 16:52:56 +08003991 private ActivityStack mPinnedStack = null;
3992 private ActivityStack mSplitScreenPrimaryStack = null;
Wale Ogunwale61911492017-10-11 08:50:50 -07003993
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003994 TaskStackContainers(WindowManagerService service) {
3995 super(service);
3996 }
3997
Wale Ogunwale61911492017-10-11 08:50:50 -07003998 /**
3999 * Returns the topmost stack on the display that is compatible with the input windowing mode
4000 * and activity type. Null is no compatible stack on the display.
4001 */
Louis Changdc077272019-11-12 16:52:56 +08004002 ActivityStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07004003 if (activityType == ACTIVITY_TYPE_HOME) {
4004 return mHomeStack;
4005 }
4006 if (windowingMode == WINDOWING_MODE_PINNED) {
4007 return mPinnedStack;
4008 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
4009 return mSplitScreenPrimaryStack;
4010 }
4011 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
Louis Changdc077272019-11-12 16:52:56 +08004012 final ActivityStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004013 if (activityType == ACTIVITY_TYPE_UNDEFINED
4014 && windowingMode == stack.getWindowingMode()) {
4015 // Passing in undefined type means we want to match the topmost stack with the
4016 // windowing mode.
4017 return stack;
4018 }
Wale Ogunwale61911492017-10-11 08:50:50 -07004019 if (stack.isCompatible(windowingMode, activityType)) {
4020 return stack;
4021 }
4022 }
4023 return null;
4024 }
4025
4026 @VisibleForTesting
Louis Changdc077272019-11-12 16:52:56 +08004027 ActivityStack getTopStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07004028 return mTaskStackContainers.getChildCount() > 0
4029 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
4030 }
4031
Louis Chang2453d062019-11-19 22:30:48 +08004032 int getIndexOf(ActivityStack stack) {
4033 return mTaskStackContainers.mChildren.indexOf(stack);
4034 }
4035
Louis Changdc077272019-11-12 16:52:56 +08004036 ActivityStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07004037 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
4038 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
4039 }
4040 return mHomeStack;
4041 }
4042
Louis Chang2453d062019-11-19 22:30:48 +08004043 ActivityStack getRecentsStack() {
4044 return mRecentsStack;
4045 }
4046
Louis Changdc077272019-11-12 16:52:56 +08004047 ActivityStack getPinnedStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07004048 return mPinnedStack;
4049 }
4050
Louis Changdc077272019-11-12 16:52:56 +08004051 ActivityStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07004052 return mSplitScreenPrimaryStack;
4053 }
4054
Winson Chunge2d72172018-01-25 17:46:20 +00004055 ArrayList<Task> getVisibleTasks() {
4056 final ArrayList<Task> visibleTasks = new ArrayList<>();
4057 forAllTasks(task -> {
4058 if (task.isVisible()) {
4059 visibleTasks.add(task);
4060 }
4061 });
4062 return visibleTasks;
4063 }
4064
Louis Changdc077272019-11-12 16:52:56 +08004065 void onStackWindowingModeChanged(ActivityStack stack) {
Wale Ogunwale61911492017-10-11 08:50:50 -07004066 removeStackReferenceIfNeeded(stack);
4067 addStackReferenceIfNeeded(stack);
4068 if (stack == mPinnedStack && getTopStack() != stack) {
4069 // Looks like this stack changed windowing mode to pinned. Move it to the top.
4070 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
4071 }
4072 }
4073
Louis Changdc077272019-11-12 16:52:56 +08004074 private void addStackReferenceIfNeeded(ActivityStack stack) {
Wale Ogunwale61911492017-10-11 08:50:50 -07004075 if (stack.isActivityTypeHome()) {
4076 if (mHomeStack != null) {
Louis Chang7d0037c2018-08-13 12:42:06 +08004077 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
Wale Ogunwale61911492017-10-11 08:50:50 -07004078 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
Louis Chang7d0037c2018-08-13 12:42:06 +08004079
Wale Ogunwale61911492017-10-11 08:50:50 -07004080 }
Louis Chang7d0037c2018-08-13 12:42:06 +08004081 mHomeStack = stack;
Louis Chang2453d062019-11-19 22:30:48 +08004082 } else if (stack.isActivityTypeRecents()) {
4083 if (mRecentsStack != null && mRecentsStack != stack) {
4084 throw new IllegalArgumentException(
4085 "addStackReferenceIfNeeded: recents stack=" + mRecentsStack
4086 + " already exist on display=" + this + " stack=" + stack);
4087 }
4088 mRecentsStack = stack;
Wale Ogunwale61911492017-10-11 08:50:50 -07004089 }
4090 final int windowingMode = stack.getWindowingMode();
4091 if (windowingMode == WINDOWING_MODE_PINNED) {
4092 if (mPinnedStack != null) {
4093 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
4094 + mPinnedStack + " already exist on display=" + this
4095 + " stack=" + stack);
4096 }
4097 mPinnedStack = stack;
4098 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
4099 if (mSplitScreenPrimaryStack != null) {
4100 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
4101 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
4102 + " already exist on display=" + this + " stack=" + stack);
4103 }
4104 mSplitScreenPrimaryStack = stack;
Louis Chang2453d062019-11-19 22:30:48 +08004105 // TODO(display-merge): Remove cast
4106 ((ActivityDisplay) this.mDisplayContent).onSplitScreenModeActivated();
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004107 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07004108 }
4109 }
4110
Louis Chang2453d062019-11-19 22:30:48 +08004111 void removeStackReferenceIfNeeded(ActivityStack stack) {
Wale Ogunwale61911492017-10-11 08:50:50 -07004112 if (stack == mHomeStack) {
4113 mHomeStack = null;
Louis Chang2453d062019-11-19 22:30:48 +08004114 } else if (stack == mRecentsStack) {
4115 mRecentsStack = null;
Wale Ogunwale61911492017-10-11 08:50:50 -07004116 } else if (stack == mPinnedStack) {
4117 mPinnedStack = null;
4118 } else if (stack == mSplitScreenPrimaryStack) {
4119 mSplitScreenPrimaryStack = null;
Louis Chang2453d062019-11-19 22:30:48 +08004120 // TODO(display-merge): Remove cast
4121 ((ActivityDisplay) this.mDisplayContent).onSplitScreenModeDismissed();
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004122 // Re-set the split-screen create mode whenever the split-screen stack is removed.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004123 mWmService.setDockedStackCreateStateLocked(
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004124 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
4125 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07004126 }
Andrii Kulian839def92016-11-02 10:58:58 -07004127 }
4128
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07004129 @Override
Louis Changdc077272019-11-12 16:52:56 +08004130 void addChild(ActivityStack stack, int position) {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07004131 addStackReferenceIfNeeded(stack);
4132 position = findPositionForStack(position, stack, true /* adding */);
4133
4134 super.addChild(stack, position);
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07004135
4136 // The reparenting case is handled in WindowContainer.
4137 if (!stack.mReparenting) {
4138 setLayoutNeeded();
4139 stack.onDisplayChanged(DisplayContent.this);
4140 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004141 }
4142
Wale Ogunwale61911492017-10-11 08:50:50 -07004143 @Override
Louis Changdc077272019-11-12 16:52:56 +08004144 protected void removeChild(ActivityStack stack) {
Wale Ogunwale61911492017-10-11 08:50:50 -07004145 super.removeChild(stack);
Louis Chang2453d062019-11-19 22:30:48 +08004146 // TODO(display-merge): Remove cast
4147 ((ActivityDisplay) this.mDisplayContent).onStackRemoved(stack);
Wale Ogunwale61911492017-10-11 08:50:50 -07004148 removeStackReferenceIfNeeded(stack);
4149 }
Bryce Lee00d586d2017-07-28 20:48:43 -07004150
4151 @Override
4152 boolean isOnTop() {
4153 // Considered always on top
4154 return true;
4155 }
4156
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004157 @Override
Louis Changdc077272019-11-12 16:52:56 +08004158 void positionChildAt(int position, ActivityStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07004159 if (child.getWindowConfiguration().isAlwaysOnTop()
Louis Chang2453d062019-11-19 22:30:48 +08004160 && position != POSITION_TOP && position != mChildren.size()) {
Andrii Kuliand2765632016-12-12 22:26:34 -08004161 // This stack is always-on-top, override the default behavior.
4162 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
4163
4164 // Moving to its current position, as we must call super but we don't want to
4165 // perform any meaningful action.
4166 final int currentPosition = mChildren.indexOf(child);
4167 super.positionChildAt(currentPosition, child, false /* includingParents */);
4168 return;
4169 }
lumarkd0b5c8f2019-09-29 11:30:37 +08004170 // We don't allow untrusted display to top when task stack moves to top,
4171 // until user tapping this display to change display position as top intentionally.
4172 if (isUntrustedVirtualDisplay() && !getParent().isOnTop()) {
4173 includingParents = false;
4174 }
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004175 final int targetPosition = findPositionForStack(position, child, false /* adding */);
4176 super.positionChildAt(targetPosition, child, includingParents);
4177
Riddle Hsu2f6e1742018-08-23 22:44:36 +08004178 if (includingParents) {
4179 // We still want to move the display of this stack container to top because even the
4180 // target position is adjusted to non-top, the intention of the condition is to have
4181 // higher z-order to gain focus (e.g. moving a task of a fullscreen stack to front
4182 // in a non-top display which is using picture-in-picture mode).
4183 final int topChildPosition = getChildCount() - 1;
4184 if (targetPosition < topChildPosition && position >= topChildPosition) {
4185 getParent().positionChildAt(POSITION_TOP, this /* child */,
4186 true /* includingParents */);
4187 }
4188 }
4189
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004190 setLayoutNeeded();
4191 }
4192
4193 /**
4194 * When stack is added or repositioned, find a proper position for it.
4195 * This will make sure that pinned stack always stays on top.
4196 * @param requestedPosition Position requested by caller.
4197 * @param stack Stack to be added or positioned.
4198 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
4199 * @return The proper position for the stack.
4200 */
Louis Changdc077272019-11-12 16:52:56 +08004201 private int findPositionForStack(int requestedPosition, ActivityStack stack,
4202 boolean adding) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09004203 if (stack.inPinnedWindowingMode()) {
Kazuki Takisef85197b2018-06-18 18:18:36 +09004204 return POSITION_TOP;
Kazuki Takise148d00a2018-05-31 15:32:19 +09004205 }
4206
Kazuki Takisef85197b2018-06-18 18:18:36 +09004207 final int topChildPosition = mChildren.size() - 1;
4208 int belowAlwaysOnTopPosition = POSITION_BOTTOM;
4209 for (int i = topChildPosition; i >= 0; --i) {
Louis Chang2453d062019-11-19 22:30:48 +08004210 // Since a stack could be repositioned while being one of the child, return
4211 // current index if that's the same stack we are positioning and it is always on
4212 // top.
4213 final boolean sameStack = getStacks().get(i) == stack;
4214 if ((sameStack && stack.isAlwaysOnTop())
4215 || (!sameStack && !getStacks().get(i).isAlwaysOnTop())) {
Kazuki Takisef85197b2018-06-18 18:18:36 +09004216 belowAlwaysOnTopPosition = i;
4217 break;
4218 }
4219 }
4220
4221 // The max possible position we can insert the stack at.
4222 int maxPosition = POSITION_TOP;
4223 // The min possible position we can insert the stack at.
4224 int minPosition = POSITION_BOTTOM;
4225
4226 if (stack.isAlwaysOnTop()) {
4227 if (hasPinnedStack()) {
4228 // Always-on-top stacks go below the pinned stack.
4229 maxPosition = getStacks().indexOf(mPinnedStack) - 1;
4230 }
4231 // Always-on-top stacks need to be above all other stacks.
4232 minPosition = belowAlwaysOnTopPosition !=
4233 POSITION_BOTTOM ? belowAlwaysOnTopPosition : topChildPosition;
4234 } else {
4235 // Other stacks need to be below the always-on-top stacks.
4236 maxPosition = belowAlwaysOnTopPosition !=
Arthur Hung928d8dc2018-07-18 15:38:05 +08004237 POSITION_BOTTOM ? belowAlwaysOnTopPosition : 0;
Kazuki Takisef85197b2018-06-18 18:18:36 +09004238 }
4239
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07004240 // Cap the requested position to something reasonable for the previous position check
4241 // below.
4242 if (requestedPosition == POSITION_TOP) {
4243 requestedPosition = mChildren.size();
4244 } else if (requestedPosition == POSITION_BOTTOM) {
4245 requestedPosition = 0;
4246 }
4247
Louis Chang2453d062019-11-19 22:30:48 +08004248 int targetPosition = requestedPosition;
4249 targetPosition = Math.min(targetPosition, maxPosition);
4250 targetPosition = Math.max(targetPosition, minPosition);
4251
Kazuki Takisef85197b2018-06-18 18:18:36 +09004252 int prevPosition = getStacks().indexOf(stack);
4253 // The positions we calculated above (maxPosition, minPosition) do not take into
4254 // consideration the following edge cases.
4255 // 1) We need to adjust the position depending on the value "adding".
4256 // 2) When we are moving a stack to another position, we also need to adjust the
4257 // position depending on whether the stack is moving to a higher or lower position.
4258 if ((targetPosition != requestedPosition) &&
4259 (adding || targetPosition < prevPosition)) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09004260 targetPosition++;
4261 }
4262
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004263 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08004264 }
4265
4266 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004267 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
4268 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004269 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004270 if (super.forAllWindows(callback, traverseTopToBottom)) {
4271 return true;
4272 }
4273 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4274 return true;
4275 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004276 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004277 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4278 return true;
4279 }
4280 if (super.forAllWindows(callback, traverseTopToBottom)) {
4281 return true;
4282 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004283 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004284 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004285 }
4286
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004287 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004288 boolean traverseTopToBottom) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07004289 // For legacy reasons we process the TaskStack.mExitingActivities first here before the
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004290 // app tokens.
4291 // TODO: Investigate if we need to continue to do this or if we can just process them
4292 // in-order.
4293 if (traverseTopToBottom) {
4294 for (int i = mChildren.size() - 1; i >= 0; --i) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07004295 final List<ActivityRecord> activities = mChildren.get(i).mExitingActivities;
4296 for (int j = activities.size() - 1; j >= 0; --j) {
4297 if (activities.get(j).forAllWindowsUnchecked(callback,
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004298 traverseTopToBottom)) {
4299 return true;
4300 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004301 }
4302 }
4303 } else {
4304 final int count = mChildren.size();
4305 for (int i = 0; i < count; ++i) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07004306 final List<ActivityRecord> activities = mChildren.get(i).mExitingActivities;
4307 final int appTokensCount = activities.size();
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004308 for (int j = 0; j < appTokensCount; j++) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07004309 if (activities.get(j).forAllWindowsUnchecked(callback,
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004310 traverseTopToBottom)) {
4311 return true;
4312 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004313 }
4314 }
4315 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004316 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004317 }
4318
Wale Ogunwale1666e312016-12-16 11:27:18 -08004319 void setExitingTokensHasVisible(boolean hasVisible) {
4320 for (int i = mChildren.size() - 1; i >= 0; --i) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07004321 final ArrayList<ActivityRecord> activities = mChildren.get(i).mExitingActivities;
4322 for (int j = activities.size() - 1; j >= 0; --j) {
4323 activities.get(j).hasVisible = hasVisible;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004324 }
4325 }
4326 }
4327
4328 void removeExistingAppTokensIfPossible() {
4329 for (int i = mChildren.size() - 1; i >= 0; --i) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07004330 final ArrayList<ActivityRecord> activities = mChildren.get(i).mExitingActivities;
4331 for (int j = activities.size() - 1; j >= 0; --j) {
4332 final ActivityRecord activity = activities.get(j);
4333 if (!activity.hasVisible && !mClosingApps.contains(activity)
4334 && (!activity.mIsExiting || activity.isEmpty())) {
4335 // Make sure there is no animation running on this activity, so any windows
Wale Ogunwale1666e312016-12-16 11:27:18 -08004336 // associated with it will be removed as soon as their animations are
4337 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02004338 cancelAnimation();
Adrian Roosb125e0b2019-10-02 14:55:14 +02004339 ProtoLog.v(WM_DEBUG_ADD_REMOVE,
Garfield Tane8d84ab2019-10-11 09:49:40 -07004340 "performLayout: Activity exiting now removed %s", activity);
4341 activity.removeIfPossible();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004342 }
4343 }
4344 }
4345 }
4346
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004347 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004348 int getOrientation() {
Garfield Tan98eead32019-11-18 13:24:53 -08004349 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004350 // Apps and their containers are not allowed to specify an orientation while the
Garfield Tan98eead32019-11-18 13:24:53 -08004351 // docked stack is visible...except for the home stack if the docked stack is
4352 // minimized and it actually set something and the bounds is different from the
4353 // display.
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004354 if (mHomeStack != null && mHomeStack.isVisible()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004355 && mDividerControllerLocked.isMinimizedDock()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004356 && !(mDividerControllerLocked.isHomeStackResizable()
chaviw8065f442019-11-18 13:20:58 -08004357 && mHomeStack.matchParentBounds())) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004358 final int orientation = mHomeStack.getOrientation();
4359 if (orientation != SCREEN_ORIENTATION_UNSET) {
4360 return orientation;
4361 }
4362 }
4363 return SCREEN_ORIENTATION_UNSPECIFIED;
4364 }
4365
4366 final int orientation = super.getOrientation();
4367 if (orientation != SCREEN_ORIENTATION_UNSET
4368 && orientation != SCREEN_ORIENTATION_BEHIND) {
Adrian Roosb125e0b2019-10-02 14:55:14 +02004369 ProtoLog.v(WM_DEBUG_ORIENTATION,
chaviw8065f442019-11-18 13:20:58 -08004370 "App is requesting an orientation, return %d for display id=%d",
4371 orientation, mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004372 return orientation;
4373 }
4374
Adrian Roosb125e0b2019-10-02 14:55:14 +02004375 ProtoLog.v(WM_DEBUG_ORIENTATION,
chaviw8065f442019-11-18 13:20:58 -08004376 "No app is requesting an orientation, return %d for display id=%d",
4377 getLastOrientation(), mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004378 // The next app has not been requested to be visible, so we keep the current orientation
4379 // to prevent freezing/unfreezing the display too early.
Riddle Hsuccf09402019-08-13 00:33:06 +08004380 return getLastOrientation();
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004381 }
Robert Carrb1579c82017-09-05 14:54:47 -07004382
4383 @Override
4384 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004385 assignStackOrdering(t);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004386
Robert Carr2f8aa392018-01-31 14:46:51 -08004387 for (int i = 0; i < mChildren.size(); i++) {
Louis Changdc077272019-11-12 16:52:56 +08004388 final ActivityStack s = mChildren.get(i);
Robert Carr2f8aa392018-01-31 14:46:51 -08004389 s.assignChildLayers(t);
4390 }
4391 }
4392
4393 void assignStackOrdering(SurfaceControl.Transaction t) {
Issei Suzuki25a9e2b2019-08-14 16:46:26 +02004394 if (getParent() == null) {
4395 return;
4396 }
4397 mTmpAlwaysOnTopStacks.clear();
4398 mTmpHomeStacks.clear();
4399 mTmpNormalStacks.clear();
4400 for (int i = 0; i < mChildren.size(); ++i) {
Louis Changdc077272019-11-12 16:52:56 +08004401 final ActivityStack s = mChildren.get(i);
Issei Suzuki25a9e2b2019-08-14 16:46:26 +02004402 if (s.isAlwaysOnTop()) {
4403 mTmpAlwaysOnTopStacks.add(s);
4404 } else if (s.isActivityTypeHome()) {
4405 mTmpHomeStacks.add(s);
4406 } else {
4407 mTmpNormalStacks.add(s);
4408 }
4409 }
Robert Carrf7a7ca82017-12-06 13:17:07 -08004410
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004411 int layer = 0;
Issei Suzuki25a9e2b2019-08-14 16:46:26 +02004412 // Place home stacks to the bottom.
4413 for (int i = 0; i < mTmpHomeStacks.size(); i++) {
4414 mTmpHomeStacks.get(i).assignLayer(t, layer++);
4415 }
4416 // The home animation layer is between the home stacks and the normal stacks.
4417 final int layerForHomeAnimationLayer = layer++;
4418 int layerForSplitScreenDividerAnchor = layer++;
4419 int layerForAnimationLayer = layer++;
4420 for (int i = 0; i < mTmpNormalStacks.size(); i++) {
Louis Changdc077272019-11-12 16:52:56 +08004421 final ActivityStack s = mTmpNormalStacks.get(i);
Issei Suzuki25a9e2b2019-08-14 16:46:26 +02004422 s.assignLayer(t, layer++);
4423 if (s.inSplitScreenWindowingMode()) {
4424 // The split screen divider anchor is located above the split screen window.
4425 layerForSplitScreenDividerAnchor = layer++;
4426 }
lumark9bca6b42019-10-17 18:35:22 +08004427 if (s.isTaskAnimating() || s.isAppTransitioning()) {
Issei Suzuki25a9e2b2019-08-14 16:46:26 +02004428 // The animation layer is located above the highest animating stack and no
4429 // higher.
4430 layerForAnimationLayer = layer++;
4431 }
4432 }
4433 // The boosted animation layer is between the normal stacks and the always on top
4434 // stacks.
4435 final int layerForBoostedAnimationLayer = layer++;
4436 for (int i = 0; i < mTmpAlwaysOnTopStacks.size(); i++) {
4437 mTmpAlwaysOnTopStacks.get(i).assignLayer(t, layer++);
4438 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004439
Issei Suzuki25a9e2b2019-08-14 16:46:26 +02004440 t.setLayer(mHomeAppAnimationLayer, layerForHomeAnimationLayer);
4441 t.setLayer(mAppAnimationLayer, layerForAnimationLayer);
4442 t.setLayer(mSplitScreenDividerAnchor, layerForSplitScreenDividerAnchor);
4443 t.setLayer(mBoostedAppAnimationLayer, layerForBoostedAnimationLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07004444 }
4445
4446 @Override
Jorim Jaggi391790622018-04-18 15:30:44 +02004447 SurfaceControl getAppAnimationLayer(@AnimationLayer int animationLayer) {
4448 switch (animationLayer) {
4449 case ANIMATION_LAYER_BOOSTED:
4450 return mBoostedAppAnimationLayer;
4451 case ANIMATION_LAYER_HOME:
4452 return mHomeAppAnimationLayer;
4453 case ANIMATION_LAYER_STANDARD:
4454 default:
4455 return mAppAnimationLayer;
4456 }
chaviw23ee71c2017-12-18 11:29:41 -08004457 }
4458
Robert Carrf7a7ca82017-12-06 13:17:07 -08004459 SurfaceControl getSplitScreenDividerAnchor() {
4460 return mSplitScreenDividerAnchor;
4461 }
4462
chaviw23ee71c2017-12-18 11:29:41 -08004463 @Override
Wale Ogunwalec17418e2019-10-13 23:00:40 +02004464 void onParentChanged(ConfigurationContainer newParent, ConfigurationContainer oldParent) {
Robert Carrb1579c82017-09-05 14:54:47 -07004465 if (getParent() != null) {
Wale Ogunwalec17418e2019-10-13 23:00:40 +02004466 super.onParentChanged(newParent, oldParent, () -> {
Issei Suzuki25a9e2b2019-08-14 16:46:26 +02004467 mAppAnimationLayer = makeChildSurface(null)
4468 .setName("animationLayer")
4469 .build();
4470 mBoostedAppAnimationLayer = makeChildSurface(null)
4471 .setName("boostedAnimationLayer")
4472 .build();
4473 mHomeAppAnimationLayer = makeChildSurface(null)
4474 .setName("homeAnimationLayer")
4475 .build();
4476 mSplitScreenDividerAnchor = makeChildSurface(null)
4477 .setName("splitScreenDividerAnchor")
4478 .build();
4479 getPendingTransaction()
4480 .show(mAppAnimationLayer)
4481 .show(mBoostedAppAnimationLayer)
4482 .show(mHomeAppAnimationLayer)
4483 .show(mSplitScreenDividerAnchor);
4484 });
Robert Carrb1579c82017-09-05 14:54:47 -07004485 } else {
Wale Ogunwalec17418e2019-10-13 23:00:40 +02004486 super.onParentChanged(newParent, oldParent);
Vishnu Nair33197392019-08-30 10:29:37 -07004487 mWmService.mTransactionFactory.get()
chaviw9f6171e2019-06-07 16:33:50 -07004488 .remove(mAppAnimationLayer)
4489 .remove(mBoostedAppAnimationLayer)
4490 .remove(mHomeAppAnimationLayer)
4491 .remove(mSplitScreenDividerAnchor)
4492 .apply();
chaviw23ee71c2017-12-18 11:29:41 -08004493 mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02004494 mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02004495 mHomeAppAnimationLayer = null;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004496 mSplitScreenDividerAnchor = null;
Robert Carrb1579c82017-09-05 14:54:47 -07004497 }
4498 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004499 }
4500
Robert Carree4d4b92017-11-22 12:21:46 -08004501 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004502 AboveAppWindowContainers(String name, WindowManagerService service) {
4503 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08004504 }
4505
Robert Carrb9506032018-02-13 13:54:00 -08004506 @Override
Vishnu Nair83537a72018-07-19 21:27:48 -07004507 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
4508 final SurfaceControl.Builder builder = super.makeChildSurface(child);
4509 if (child instanceof WindowToken && ((WindowToken) child).mRoundedCornerOverlay) {
4510 // To draw above the ColorFade layer during the screen off transition, the
4511 // rounded corner overlays need to be at the root of the surface hierarchy.
4512 // TODO: move the ColorLayer into the display overlay layer such that this is not
4513 // necessary anymore.
4514 builder.setParent(null);
4515 }
4516 return builder;
4517 }
4518
4519 @Override
Robert Carrb9506032018-02-13 13:54:00 -08004520 void assignChildLayers(SurfaceControl.Transaction t) {
4521 assignChildLayers(t, null /* imeContainer */);
4522 }
4523
Robert Carree4d4b92017-11-22 12:21:46 -08004524 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
4525 boolean needAssignIme = imeContainer != null
4526 && imeContainer.getSurfaceControl() != null;
4527 for (int j = 0; j < mChildren.size(); ++j) {
4528 final WindowToken wt = mChildren.get(j);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004529
4530 // See {@link mSplitScreenDividerAnchor}
4531 if (wt.windowType == TYPE_DOCK_DIVIDER) {
4532 wt.assignRelativeLayer(t, mTaskStackContainers.getSplitScreenDividerAnchor(), 1);
4533 continue;
4534 }
Vishnu Nair83537a72018-07-19 21:27:48 -07004535 if (wt.mRoundedCornerOverlay) {
4536 wt.assignLayer(t, WindowManagerPolicy.COLOR_FADE_LAYER + 1);
4537 continue;
4538 }
Robert Carree4d4b92017-11-22 12:21:46 -08004539 wt.assignLayer(t, j);
4540 wt.assignChildLayers(t);
4541
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004542 int layer = mWmService.mPolicy.getWindowLayerFromTypeLw(
Robert Carree4d4b92017-11-22 12:21:46 -08004543 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08004544
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004545 if (needAssignIme && layer >= mWmService.mPolicy.getWindowLayerFromTypeLw(
chaviw8065f442019-11-18 13:20:58 -08004546 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004547 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08004548 needAssignIme = false;
4549 }
4550 }
4551 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004552 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08004553 }
4554 }
4555 }
4556
chaviw8065f442019-11-18 13:20:58 -08004557 private class WindowContainers extends DisplayChildWindowContainer<WindowContainer> {
4558 private final String mName;
4559
4560 WindowContainers(String name, WindowManagerService service) {
4561 super(service);
4562 mName = name;
4563 }
4564
4565 @Override
4566 void assignChildLayers(SurfaceControl.Transaction t) {
4567 mBelowAppWindowsContainers.assignLayer(t, 0);
4568 mTaskStackContainers.assignLayer(t, 1);
4569 mAboveAppWindowsContainers.assignLayer(t, 2);
4570
4571 final WindowState imeTarget = mInputMethodTarget;
4572 boolean needAssignIme = true;
4573
4574 // In the case where we have an IME target that is not in split-screen mode IME
4575 // assignment is easy. We just need the IME to go directly above the target. This way
4576 // children of the target will naturally go above the IME and everyone is happy.
4577 //
4578 // In the case of split-screen windowing mode, we need to elevate the IME above the
4579 // docked divider while keeping the app itself below the docked divider, so instead
4580 // we use relative layering of the IME targets child windows, and place the IME in
4581 // the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
4582 //
4583 // In the case the IME target is animating, the animation Z order may be different
4584 // than the WindowContainer Z order, so it's difficult to be sure we have the correct
4585 // IME target. In this case we just layer the IME over all transitions by placing it
4586 // in the above applications layer.
4587 //
4588 // In the case where we have no IME target we assign it where its base layer would
4589 // place it in the AboveAppWindowContainers.
4590 //
4591 // Keep IME window in mAboveAppWindowsContainers as long as app's starting window
4592 // exists so it get's layered above the starting window.
4593 if (imeTarget != null && !(imeTarget.mActivityRecord != null
4594 && imeTarget.mActivityRecord.hasStartingWindow()) && (
4595 !(imeTarget.inSplitScreenWindowingMode()
4596 || imeTarget.mToken.isAppTransitioning()) && (
4597 imeTarget.getSurfaceControl() != null))) {
4598 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
4599 // TODO: We need to use an extra level on the app surface to ensure
4600 // this is always above SurfaceView but always below attached window.
4601 1);
4602 needAssignIme = false;
4603 }
4604
4605 // Above we have assigned layers to our children, now we ask them to assign
4606 // layers to their children.
4607 mBelowAppWindowsContainers.assignChildLayers(t);
4608 mTaskStackContainers.assignChildLayers(t);
4609 mAboveAppWindowsContainers.assignChildLayers(t,
4610 needAssignIme ? mImeWindowsContainers : null);
4611 mImeWindowsContainers.assignChildLayers(t);
4612 }
4613
4614 @Override
4615 String getName() {
4616 return mName;
4617 }
4618
4619 void addChildren() {
4620 addChild(mBelowAppWindowsContainers, null);
4621 addChild(mTaskStackContainers, null);
4622 addChild(mAboveAppWindowsContainers, null);
4623 addChild(mImeWindowsContainers, null);
4624 }
4625
4626 /**
4627 * In split-screen mode we process the IME containers above the docked divider
4628 * rather than directly above their target.
4629 */
4630 private boolean skipTraverseChild(WindowContainer child) {
4631 return child == mImeWindowsContainers && mInputMethodTarget != null
4632 && !hasSplitScreenPrimaryStack();
4633 }
4634
4635 @Override
4636 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
4637 boolean traverseTopToBottom) {
4638 // Special handling so we can process IME windows with #forAllImeWindows above their IME
4639 // target, or here in order if there isn't an IME target.
4640 if (traverseTopToBottom) {
4641 for (int i = mChildren.size() - 1; i >= 0; --i) {
4642 final WindowContainer child = mChildren.get(i);
4643 if (skipTraverseChild(child)) {
4644 continue;
4645 }
4646
4647 if (child.forAllWindows(callback, traverseTopToBottom)) {
4648 return true;
4649 }
4650 }
4651 } else {
4652 final int count = mChildren.size();
4653 for (int i = 0; i < count; i++) {
4654 Slog.d(TAG, "child " + mChildren.get(i));
4655 final WindowContainer child = mChildren.get(i);
4656 if (skipTraverseChild(child)) {
4657 Slog.d(TAG, "child skipped");
4658 continue;
4659 }
4660
4661 if (child.forAllWindows(callback, traverseTopToBottom)) {
4662 return true;
4663 }
4664 }
4665 }
4666 return false;
4667 }
4668
4669 @Override
4670 void positionChildAt(int position, WindowContainer child, boolean includingParents) {
4671 // Children of the WindowContainers are statically ordered, so the real intention here
4672 // is to perform the operation on the display and not the static direct children.
4673 getParent().positionChildAt(position, this, includingParents);
4674 }
4675 }
4676
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004677 /**
4678 * Window container class that contains all containers on this display that are not related to
4679 * Apps. E.g. status bar.
4680 */
Robert Carree4d4b92017-11-22 12:21:46 -08004681 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07004682 /**
4683 * Compares two child window tokens returns -1 if the first is lesser than the second in
4684 * terms of z-order and 1 otherwise.
4685 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004686 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07004687 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004688 mWmService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004689 token1.mOwnerCanManageAppTokens)
chaviw8065f442019-11-18 13:20:58 -08004690 < mWmService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004691 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004692
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004693 private final Predicate<WindowState> mGetOrientingWindow = w -> {
chaviw15ad49f2019-04-24 15:05:39 -07004694 if (!w.isVisibleLw() || !w.mLegacyPolicyVisibilityAfterAnim) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004695 return false;
4696 }
4697 final int req = w.mAttrs.screenOrientation;
4698 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
4699 || req == SCREEN_ORIENTATION_UNSET) {
4700 return false;
4701 }
4702 return true;
4703 };
4704
Wale Ogunwale3a931692016-11-02 16:49:48 -07004705 private final String mName;
chaviwf29223e2018-01-31 13:23:51 -08004706 private final Dimmer mDimmer = new Dimmer(this);
4707 private final Rect mTmpDimBoundsRect = new Rect();
4708
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004709 NonAppWindowContainers(String name, WindowManagerService service) {
4710 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07004711 mName = name;
4712 }
4713
Wale Ogunwaledfbeed72019-11-20 08:57:39 -08004714 @Override
4715 boolean hasActivity() {
4716 // I am a non-app-window-container :P
4717 return false;
4718 }
4719
Wale Ogunwale3a931692016-11-02 16:49:48 -07004720 void addChild(WindowToken token) {
4721 addChild(token, mWindowComparator);
4722 }
4723
4724 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004725 int getOrientation() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004726 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004727 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004728 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004729
4730 if (win != null) {
4731 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004732 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004733 mLastKeyguardForcedOrientation = req;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004734 if (mWmService.mKeyguardGoingAway) {
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07004735 // Keyguard can't affect the orientation if it is going away...
4736 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
4737 return SCREEN_ORIENTATION_UNSET;
4738 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004739 }
Adrian Roosb125e0b2019-10-02 14:55:14 +02004740 ProtoLog.v(WM_DEBUG_ORIENTATION,
4741 "%s forcing orientation to %d for display id=%d", win, req,
chaviw8065f442019-11-18 13:20:58 -08004742 mDisplayId);
Andrii Kulian8ee72852017-03-10 10:36:45 -08004743 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004744 }
4745
Andrii Kulian8ee72852017-03-10 10:36:45 -08004746 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004747
Jorim Jaggi1c530592018-04-06 15:11:47 +02004748 // Only allow force setting the orientation when all unknown visibilities have been
4749 // resolved, as otherwise we just may be starting another occluding activity.
4750 final boolean isUnoccluding =
lumark588a3e82018-07-20 18:53:54 +08004751 mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
4752 && mUnknownAppVisibilityController.allResolved();
Jorim Jaggi1c530592018-04-06 15:11:47 +02004753 if (policy.isKeyguardShowingAndNotOccluded() || isUnoccluding) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004754 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004755 }
4756
4757 return SCREEN_ORIENTATION_UNSET;
4758 }
4759
4760 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07004761 String getName() {
4762 return mName;
4763 }
chaviwf29223e2018-01-31 13:23:51 -08004764
4765 @Override
4766 Dimmer getDimmer() {
4767 return mDimmer;
4768 }
4769
4770 @Override
4771 void prepareSurfaces() {
4772 mDimmer.resetDimStates();
4773 super.prepareSurfaces();
4774 getBounds(mTmpDimBoundsRect);
4775
4776 if (mDimmer.updateDims(getPendingTransaction(), mTmpDimBoundsRect)) {
4777 scheduleAnimation();
4778 }
4779 }
Robert Carr3b716242016-08-16 16:02:21 -07004780 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01004781
Robert Carrb1579c82017-09-05 14:54:47 -07004782 @Override
4783 SurfaceSession getSession() {
4784 return mSession;
4785 }
4786
4787 @Override
4788 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07004789 SurfaceSession s = child != null ? child.getSession() : getSession();
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +00004790 final SurfaceControl.Builder b = mWmService.makeSurfaceBuilder(s).setContainerLayer();
Robert Carrf59b8dd2017-10-02 18:58:36 -07004791 if (child == null) {
4792 return b;
4793 }
4794
Robert Carree4d4b92017-11-22 12:21:46 -08004795 return b.setName(child.getName())
chaviw8065f442019-11-18 13:20:58 -08004796 .setParent(mSurfaceControl);
Robert Carrb1579c82017-09-05 14:54:47 -07004797 }
4798
4799 /**
4800 * The makeSurface variants are for use by the window-container
4801 * hierarchy. makeOverlay here is a function for various non windowing
4802 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
4803 * and other potpourii.
4804 */
4805 SurfaceControl.Builder makeOverlay() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004806 return mWmService.makeSurfaceBuilder(mSession)
chaviw8065f442019-11-18 13:20:58 -08004807 .setParent(mOverlayContainers.getSurfaceControl());
Robert Carrb1579c82017-09-05 14:54:47 -07004808 }
4809
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004810 /**
chaviw8065f442019-11-18 13:20:58 -08004811 * Reparents the given surface to {@link #mOverlayContainers}' SurfaceControl.
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004812 */
4813 void reparentToOverlay(Transaction transaction, SurfaceControl surface) {
chaviw8065f442019-11-18 13:20:58 -08004814 transaction.reparent(surface, mOverlayContainers.getSurfaceControl());
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004815 }
4816
Robert Carrb1579c82017-09-05 14:54:47 -07004817 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carr24be9ab2018-04-30 17:54:53 -07004818 if (spec.scale != 1.0) {
4819 mMagnificationSpec = spec;
4820 } else {
4821 mMagnificationSpec = null;
4822 }
Jackal Guo818c3f12019-03-08 18:00:39 +08004823 // Re-parent IME's SurfaceControl when MagnificationSpec changed.
4824 updateImeParent();
Robert Carr24be9ab2018-04-30 17:54:53 -07004825
Robert Carrf59b8dd2017-10-02 18:58:36 -07004826 applyMagnificationSpec(getPendingTransaction(), spec);
4827 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07004828 }
4829
Robert Carr24be9ab2018-04-30 17:54:53 -07004830 void reapplyMagnificationSpec() {
4831 if (mMagnificationSpec != null) {
4832 applyMagnificationSpec(getPendingTransaction(), mMagnificationSpec);
4833 }
4834 }
4835
Robert Carrb1579c82017-09-05 14:54:47 -07004836 @Override
Wale Ogunwalec17418e2019-10-13 23:00:40 +02004837 void onParentChanged(ConfigurationContainer newParent, ConfigurationContainer oldParent) {
Robert Carrb1579c82017-09-05 14:54:47 -07004838 // Since we are the top of the SurfaceControl hierarchy here
4839 // we create the root surfaces explicitly rather than chaining
Riddle Hsu3a4bb612019-01-31 00:02:22 +08004840 // up as the default implementation in onParentChanged does. So we
Robert Carrb1579c82017-09-05 14:54:47 -07004841 // explicitly do NOT call super here.
4842 }
4843
4844 @Override
4845 void assignChildLayers(SurfaceControl.Transaction t) {
chaviw8065f442019-11-18 13:20:58 -08004846 mWindowContainers.assignLayer(t, 0);
4847 mOverlayContainers.assignLayer(t, 1);
Robert Carrb1579c82017-09-05 14:54:47 -07004848
chaviw8065f442019-11-18 13:20:58 -08004849 mWindowContainers.assignChildLayers(t);
4850 mOverlayContainers.assignChildLayers(t);
Robert Carrb1579c82017-09-05 14:54:47 -07004851 }
4852
4853 /**
4854 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
4855 * that the IME target is one of the docked applications. We'd like the docked divider to be
4856 * above both of the applications, and we'd like the IME to be above the docked divider.
4857 * However we need child windows of the applications to be above the IME (Text drag handles).
4858 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
4859 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
Tiger Huang7c610aa2018-10-27 00:01:01 +08004860 * with {@link WindowState#assignLayer}
Robert Carrb1579c82017-09-05 14:54:47 -07004861 */
4862 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
Robert Carrbb5de662018-04-26 13:29:51 -07004863 child.assignRelativeLayer(t, mImeWindowsContainers.getSurfaceControl(), 1);
Robert Carrb1579c82017-09-05 14:54:47 -07004864 }
4865
4866 @Override
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004867 void prepareSurfaces() {
Jorim Jaggi4981f152019-03-26 18:58:45 +01004868 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "prepareSurfaces");
4869 try {
Tiger Huanged6794e2019-05-07 20:07:59 +08004870 final Transaction transaction = getPendingTransaction();
Jorim Jaggi4981f152019-03-26 18:58:45 +01004871 super.prepareSurfaces();
Tiger Huanged6794e2019-05-07 20:07:59 +08004872
4873 // TODO: Once we totally eliminate global transaction we will pass transaction in here
4874 // rather than merging to global.
4875 SurfaceControl.mergeToGlobalTransaction(transaction);
Jorim Jaggi4981f152019-03-26 18:58:45 +01004876 } finally {
4877 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
4878 }
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004879 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004880
Jorim Jaggibe418292018-03-26 16:14:12 +02004881 void assignStackOrdering() {
4882 mTaskStackContainers.assignStackOrdering(getPendingTransaction());
Robert Carr2f8aa392018-01-31 14:46:51 -08004883 }
Chavi Weingarten3a748552018-05-14 17:32:42 +00004884
4885 /**
4886 * Increment the deferral count to determine whether to update the IME target.
4887 */
4888 void deferUpdateImeTarget() {
4889 mDeferUpdateImeTargetCount++;
4890 }
4891
4892 /**
4893 * Decrement the deferral count to determine whether to update the IME target. If the count
4894 * reaches 0, a new ime target will get computed.
4895 */
4896 void continueUpdateImeTarget() {
4897 if (mDeferUpdateImeTargetCount == 0) {
4898 return;
4899 }
4900
4901 mDeferUpdateImeTargetCount--;
4902 if (mDeferUpdateImeTargetCount == 0) {
4903 computeImeTarget(true /* updateImeTarget */);
4904 }
4905 }
4906
4907 /**
4908 * @return Whether a new IME target should be computed.
4909 */
4910 private boolean canUpdateImeTarget() {
4911 return mDeferUpdateImeTargetCount == 0;
4912 }
Arthur Hung95b38a92018-07-20 18:56:12 +08004913
4914 InputMonitor getInputMonitor() {
4915 return mInputMonitor;
4916 }
Jorim Jaggif1292892018-09-10 11:58:13 +02004917
4918 /**
4919 * @return Cached value whether we told display manager that we have content.
4920 */
4921 boolean getLastHasContent() {
4922 return mLastHasContent;
4923 }
Arthur Hungbe5ce212018-09-13 18:41:56 +08004924
4925 void registerPointerEventListener(@NonNull PointerEventListener listener) {
Riddle Hsu2588ab02019-02-25 14:23:56 +08004926 mPointerEventDispatcher.registerInputEventListener(listener);
Arthur Hungbe5ce212018-09-13 18:41:56 +08004927 }
4928
4929 void unregisterPointerEventListener(@NonNull PointerEventListener listener) {
Riddle Hsu2588ab02019-02-25 14:23:56 +08004930 mPointerEventDispatcher.unregisterInputEventListener(listener);
Arthur Hungbe5ce212018-09-13 18:41:56 +08004931 }
lumark588a3e82018-07-20 18:53:54 +08004932
4933 void prepareAppTransition(@WindowManager.TransitionType int transit,
Wale Ogunwale3a256e62018-12-06 14:41:18 -08004934 boolean alwaysKeepCurrent) {
4935 prepareAppTransition(transit, alwaysKeepCurrent, 0 /* flags */, false /* forceOverride */);
4936 }
4937
4938 void prepareAppTransition(@WindowManager.TransitionType int transit,
lumark588a3e82018-07-20 18:53:54 +08004939 boolean alwaysKeepCurrent, @WindowManager.TransitionFlags int flags,
4940 boolean forceOverride) {
4941 final boolean prepared = mAppTransition.prepareAppTransitionLocked(
4942 transit, alwaysKeepCurrent, flags, forceOverride);
4943 if (prepared && okToAnimate()) {
4944 mSkipAppTransitionAnimation = false;
4945 }
4946 }
4947
4948 void executeAppTransition() {
4949 if (mAppTransition.isTransitionSet()) {
Adrian Roosb125e0b2019-10-02 14:55:14 +02004950 ProtoLog.w(WM_DEBUG_APP_TRANSITIONS,
4951 "Execute app transition: %s, displayId: %d Callers=%s",
chaviw8065f442019-11-18 13:20:58 -08004952 mAppTransition, mDisplayId, Debug.getCallers(5));
lumark588a3e82018-07-20 18:53:54 +08004953 mAppTransition.setReady();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004954 mWmService.mWindowPlacerLocked.requestTraversal();
lumark588a3e82018-07-20 18:53:54 +08004955 }
4956 }
4957
4958 /**
4959 * Update pendingLayoutChanges after app transition has finished.
4960 */
4961 void handleAnimatingStoppedAndTransition() {
4962 int changes = 0;
4963
4964 mAppTransition.setIdle();
4965
4966 for (int i = mNoAnimationNotifyOnTransitionFinished.size() - 1; i >= 0; i--) {
4967 final IBinder token = mNoAnimationNotifyOnTransitionFinished.get(i);
4968 mAppTransition.notifyAppTransitionFinishedLocked(token);
4969 }
4970 mNoAnimationNotifyOnTransitionFinished.clear();
4971
4972 mWallpaperController.hideDeferredWallpapersIfNeeded();
4973
4974 onAppTransitionDone();
4975
4976 changes |= FINISH_LAYOUT_REDO_LAYOUT;
4977 if (DEBUG_WALLPAPER_LIGHT) {
4978 Slog.v(TAG_WM, "Wallpaper layer changed: assigning layers + relayout");
4979 }
4980 computeImeTarget(true /* updateImeTarget */);
wilsonshihc32538e2018-11-07 17:27:34 +08004981 mWallpaperMayChange = true;
lumark588a3e82018-07-20 18:53:54 +08004982 // Since the window list has been rebuilt, focus might have to be recomputed since the
4983 // actual order of windows might have changed again.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004984 mWmService.mFocusMayChange = true;
lumark588a3e82018-07-20 18:53:54 +08004985
4986 pendingLayoutChanges |= changes;
4987 }
Andrii Kulian15cfb422018-11-07 13:38:49 -08004988
Wale Ogunwale3a256e62018-12-06 14:41:18 -08004989 /** Check if pending app transition is for activity / task launch. */
4990 boolean isNextTransitionForward() {
4991 final int transit = mAppTransition.getAppTransition();
4992 return transit == TRANSIT_ACTIVITY_OPEN
4993 || transit == TRANSIT_TASK_OPEN
4994 || transit == TRANSIT_TASK_TO_FRONT;
4995 }
4996
Andrii Kulian15cfb422018-11-07 13:38:49 -08004997 /**
4998 * @see Display#FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS
4999 */
5000 boolean supportsSystemDecorations() {
Arthur Hung879a8ab2019-03-13 19:51:10 +08005001 return (mWmService.mDisplayWindowSettings.shouldShowSystemDecorsLocked(this)
Andrii Kuliandd989612019-02-21 12:13:28 -08005002 || (mDisplay.getFlags() & FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS) != 0
Chilun891aed22019-05-02 18:08:47 +08005003 || (mWmService.mForceDesktopModeOnExternalDisplays && !isUntrustedVirtualDisplay()))
Arthur Hung879a8ab2019-03-13 19:51:10 +08005004 // VR virtual display will be used to run and render 2D app within a VR experience.
5005 && mDisplayId != mWmService.mVr2dDisplayId;
Andrii Kulian15cfb422018-11-07 13:38:49 -08005006 }
chaviwff2e7d82018-11-02 11:11:27 -07005007
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005008 /**
Chilun891aed22019-05-02 18:08:47 +08005009 * @return {@code true} if the display is non-system created virtual display.
5010 */
5011 boolean isUntrustedVirtualDisplay() {
5012 return mDisplay.getType() == Display.TYPE_VIRTUAL
5013 && mDisplay.getOwnerUid() != Process.SYSTEM_UID;
5014 }
5015
5016 /**
chaviw8065f442019-11-18 13:20:58 -08005017 * Re-parent the DisplayContent's top surface, {@link #mSurfaceControl} to the specified
5018 * SurfaceControl.
chaviwff2e7d82018-11-02 11:11:27 -07005019 *
Tiger Huangd8ec9382019-04-18 14:35:09 -07005020 * @param win The window which owns the SurfaceControl. This indicates the z-order of the
5021 * windows of this display against the windows on the parent display.
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005022 * @param sc The new SurfaceControl, where the DisplayContent's surfaces will be re-parented to.
chaviwff2e7d82018-11-02 11:11:27 -07005023 */
Tiger Huangd8ec9382019-04-18 14:35:09 -07005024 void reparentDisplayContent(WindowState win, SurfaceControl sc) {
5025 mParentWindow = win;
Jackal Guoc43a0a62019-04-23 09:15:14 +08005026 mParentWindow.addEmbeddedDisplayContent(this);
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005027 mParentSurfaceControl = sc;
5028 if (mPortalWindowHandle == null) {
5029 mPortalWindowHandle = createPortalWindowHandle(sc.toString());
5030 }
Tiger Huanged6794e2019-05-07 20:07:59 +08005031 getPendingTransaction().setInputWindowInfo(sc, mPortalWindowHandle)
Vadim Caen1812c762019-08-12 11:38:22 +02005032 .reparent(mSurfaceControl, sc);
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005033 }
5034
Tiger Huangd8ec9382019-04-18 14:35:09 -07005035 /**
5036 * Get the window which owns the surface that this DisplayContent is re-parented to.
5037 *
5038 * @return the parent window.
5039 */
5040 WindowState getParentWindow() {
5041 return mParentWindow;
5042 }
5043
5044 /**
5045 * Update the location of this display in the parent window. This enables windows in this
5046 * display to compute the global transformation matrix.
5047 *
5048 * @param win The parent window of this display.
5049 * @param x The x coordinate in the parent window.
5050 * @param y The y coordinate in the parent window.
5051 */
5052 void updateLocation(WindowState win, int x, int y) {
5053 if (mParentWindow != win) {
5054 throw new IllegalArgumentException(
5055 "The given window is not the parent window of this display.");
5056 }
Jackal Guoc43a0a62019-04-23 09:15:14 +08005057 if (!mLocationInParentWindow.equals(x, y)) {
5058 mLocationInParentWindow.set(x, y);
Tiger Huangd8ec9382019-04-18 14:35:09 -07005059 if (mWmService.mAccessibilityController != null) {
Jacky Kaof93252b2019-07-18 15:19:52 +08005060 mWmService.mAccessibilityController.onSomeWindowResizedOrMovedLocked(mDisplayId);
Tiger Huangd8ec9382019-04-18 14:35:09 -07005061 }
Jackal Guoc43a0a62019-04-23 09:15:14 +08005062 notifyLocationInParentDisplayChanged();
Tiger Huangd8ec9382019-04-18 14:35:09 -07005063 }
5064 }
5065
5066 Point getLocationInParentWindow() {
5067 return mLocationInParentWindow;
5068 }
5069
Jackal Guoc43a0a62019-04-23 09:15:14 +08005070 Point getLocationInParentDisplay() {
5071 final Point location = new Point();
5072 if (mParentWindow != null) {
5073 // LocationInParentWindow indicates the offset to (0,0) of window, but what we need is
5074 // the offset to (0,0) of display.
5075 DisplayContent dc = this;
5076 do {
5077 final WindowState displayParent = dc.getParentWindow();
5078 location.x += displayParent.getFrameLw().left
5079 + (dc.getLocationInParentWindow().x * displayParent.mGlobalScale + 0.5f);
5080 location.y += displayParent.getFrameLw().top
5081 + (dc.getLocationInParentWindow().y * displayParent.mGlobalScale + 0.5f);
5082 dc = displayParent.getDisplayContent();
5083 } while (dc != null && dc.getParentWindow() != null);
5084 }
5085 return location;
5086 }
5087
5088 void notifyLocationInParentDisplayChanged() {
5089 forAllWindows(w -> {
5090 w.updateLocationInParentDisplayIfNeeded();
5091 }, false /* traverseTopToBottom */);
5092 }
5093
Tarandeep Singha6f35612019-01-11 19:50:46 -08005094 @VisibleForTesting
5095 SurfaceControl getWindowingLayer() {
chaviw8065f442019-11-18 13:20:58 -08005096 return mWindowContainers.getSurfaceControl();
Tarandeep Singha6f35612019-01-11 19:50:46 -08005097 }
5098
Vadim Caenf8474262019-08-12 17:52:01 +02005099 SurfaceControl getOverlayLayer() {
chaviw8065f442019-11-18 13:20:58 -08005100 return mOverlayContainers.getSurfaceControl();
Vadim Caenf8474262019-08-12 17:52:01 +02005101 }
5102
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005103 /**
Adrian Roos4ffc8972019-02-07 20:45:11 +01005104 * Updates the display's system gesture exclusion.
5105 *
5106 * @return true, if the exclusion changed.
5107 */
5108 boolean updateSystemGestureExclusion() {
5109 if (mSystemGestureExclusionListeners.getRegisteredCallbackCount() == 0) {
5110 // No one's interested anyways.
5111 return false;
5112 }
5113
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005114 final Region systemGestureExclusion = Region.obtain();
5115 mSystemGestureExclusionWasRestricted = calculateSystemGestureExclusion(
5116 systemGestureExclusion, mSystemGestureExclusionUnrestricted);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005117 try {
5118 if (mSystemGestureExclusion.equals(systemGestureExclusion)) {
5119 return false;
5120 }
5121 mSystemGestureExclusion.set(systemGestureExclusion);
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005122 final Region unrestrictedOrNull = mSystemGestureExclusionWasRestricted
5123 ? mSystemGestureExclusionUnrestricted : null;
Adrian Roos4ffc8972019-02-07 20:45:11 +01005124 for (int i = mSystemGestureExclusionListeners.beginBroadcast() - 1; i >= 0; --i) {
5125 try {
5126 mSystemGestureExclusionListeners.getBroadcastItem(i)
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005127 .onSystemGestureExclusionChanged(mDisplayId, systemGestureExclusion,
5128 unrestrictedOrNull);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005129 } catch (RemoteException e) {
5130 Slog.e(TAG, "Failed to notify SystemGestureExclusionListener", e);
5131 }
5132 }
5133 mSystemGestureExclusionListeners.finishBroadcast();
5134 return true;
5135 } finally {
5136 systemGestureExclusion.recycle();
5137 }
5138 }
5139
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005140 /**
5141 * Calculates the system gesture exclusion.
5142 *
5143 * @param outExclusion will be set to the gesture exclusion region
5144 * @param outExclusionUnrestricted will be set to the gesture exclusion region without
5145 * any restrictions applied.
5146 * @return whether any restrictions were applied, i.e. outExclusion and outExclusionUnrestricted
5147 * differ.
5148 */
Adrian Roos4ffc8972019-02-07 20:45:11 +01005149 @VisibleForTesting
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005150 boolean calculateSystemGestureExclusion(Region outExclusion, @Nullable
5151 Region outExclusionUnrestricted) {
5152 outExclusion.setEmpty();
5153 if (outExclusionUnrestricted != null) {
5154 outExclusionUnrestricted.setEmpty();
5155 }
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005156 final Region unhandled = Region.obtain();
5157 unhandled.set(0, 0, mDisplayFrames.mDisplayWidth, mDisplayFrames.mDisplayHeight);
5158
Tiger Huang332793b2019-10-29 23:21:27 +08005159 final Rect leftEdge = mInsetsStateController.getSourceProvider(ITYPE_LEFT_GESTURES)
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005160 .getSource().getFrame();
Tiger Huang332793b2019-10-29 23:21:27 +08005161 final Rect rightEdge = mInsetsStateController.getSourceProvider(ITYPE_RIGHT_GESTURES)
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005162 .getSource().getFrame();
5163
Adrian Roos4ffc8972019-02-07 20:45:11 +01005164 final Region touchableRegion = Region.obtain();
5165 final Region local = Region.obtain();
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005166 final int[] remainingLeftRight =
5167 {mSystemGestureExclusionLimit, mSystemGestureExclusionLimit};
Adrian Roos4ffc8972019-02-07 20:45:11 +01005168
Adrian Roosb1063792019-06-28 12:10:51 +02005169 // Traverse all windows top down to assemble the gesture exclusion rects.
Adrian Roos4ffc8972019-02-07 20:45:11 +01005170 // For each window, we only take the rects that fall within its touchable region.
5171 forAllWindows(w -> {
5172 if (w.cantReceiveTouchInput() || !w.isVisible()
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005173 || (w.getAttrs().flags & FLAG_NOT_TOUCHABLE) != 0
5174 || unhandled.isEmpty()) {
Adrian Roos4ffc8972019-02-07 20:45:11 +01005175 return;
5176 }
Adrian Roos4ffc8972019-02-07 20:45:11 +01005177
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005178 // Get the touchable region of the window, and intersect with where the screen is still
5179 // touchable, i.e. touchable regions on top are not covering it yet.
Adrian Roosb1063792019-06-28 12:10:51 +02005180 w.getEffectiveTouchableRegion(touchableRegion);
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005181 touchableRegion.op(unhandled, Op.INTERSECT);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005182
Adrian Roos019a52b2019-07-02 16:47:44 +02005183 if (w.isImplicitlyExcludingAllSystemGestures()) {
5184 local.set(touchableRegion);
5185 } else {
5186 rectListToRegion(w.getSystemGestureExclusion(), local);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005187
Adrian Roos019a52b2019-07-02 16:47:44 +02005188 // Transform to display coordinates
5189 local.scale(w.mGlobalScale);
5190 final Rect frame = w.getWindowFrames().mFrame;
5191 local.translate(frame.left, frame.top);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005192
Adrian Roos019a52b2019-07-02 16:47:44 +02005193 // A window can only exclude system gestures where it is actually touchable
5194 local.op(touchableRegion, Op.INTERSECT);
5195 }
Adrian Roos4ffc8972019-02-07 20:45:11 +01005196
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005197 // Apply restriction if necessary.
5198 if (needsGestureExclusionRestrictions(w, mLastDispatchedSystemUiVisibility)) {
5199
5200 // Processes the region along the left edge.
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005201 remainingLeftRight[0] = addToGlobalAndConsumeLimit(local, outExclusion, leftEdge,
5202 remainingLeftRight[0], w, EXCLUSION_LEFT);
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005203
5204 // Processes the region along the right edge.
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005205 remainingLeftRight[1] = addToGlobalAndConsumeLimit(local, outExclusion, rightEdge,
5206 remainingLeftRight[1], w, EXCLUSION_RIGHT);
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005207
5208 // Adds the middle (unrestricted area)
5209 final Region middle = Region.obtain(local);
5210 middle.op(leftEdge, Op.DIFFERENCE);
5211 middle.op(rightEdge, Op.DIFFERENCE);
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005212 outExclusion.op(middle, Op.UNION);
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005213 middle.recycle();
5214 } else {
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005215 boolean loggable = needsGestureExclusionRestrictions(w, 0 /* lastSysUiVis */);
5216 if (loggable) {
5217 addToGlobalAndConsumeLimit(local, outExclusion, leftEdge,
5218 Integer.MAX_VALUE, w, EXCLUSION_LEFT);
5219 addToGlobalAndConsumeLimit(local, outExclusion, rightEdge,
5220 Integer.MAX_VALUE, w, EXCLUSION_RIGHT);
5221 }
5222 outExclusion.op(local, Op.UNION);
5223 }
5224 if (outExclusionUnrestricted != null) {
5225 outExclusionUnrestricted.op(local, Op.UNION);
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005226 }
5227 unhandled.op(touchableRegion, Op.DIFFERENCE);
5228 }, true /* topToBottom */);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005229 local.recycle();
5230 touchableRegion.recycle();
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005231 unhandled.recycle();
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005232 return remainingLeftRight[0] < mSystemGestureExclusionLimit
5233 || remainingLeftRight[1] < mSystemGestureExclusionLimit;
Adrian Roos4ffc8972019-02-07 20:45:11 +01005234 }
5235
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005236 /**
5237 * @return Whether gesture exclusion area should be restricted from the window depending on the
5238 * current SystemUI visibility flags.
5239 */
5240 private static boolean needsGestureExclusionRestrictions(WindowState win, int sysUiVisibility) {
5241 final int type = win.mAttrs.type;
5242 final int stickyHideNavFlags =
5243 SYSTEM_UI_FLAG_HIDE_NAVIGATION | SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
5244 final boolean stickyHideNav =
5245 (sysUiVisibility & stickyHideNavFlags) == stickyHideNavFlags;
5246 return !stickyHideNav && type != TYPE_INPUT_METHOD && type != TYPE_STATUS_BAR
5247 && win.getActivityType() != ACTIVITY_TYPE_HOME;
5248 }
5249
5250 /**
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005251 * @return Whether gesture exclusion area should be logged for the given window
5252 */
5253 static boolean logsGestureExclusionRestrictions(WindowState win) {
Adrian Roos1c2e9a12019-08-20 18:23:47 +02005254 if (win.mWmService.mConstants.mSystemGestureExclusionLogDebounceTimeoutMillis <= 0) {
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005255 return false;
5256 }
5257 final WindowManager.LayoutParams attrs = win.getAttrs();
5258 final int type = attrs.type;
5259 return type != TYPE_WALLPAPER
5260 && type != TYPE_APPLICATION_STARTING
5261 && type != TYPE_NAVIGATION_BAR
5262 && (attrs.flags & FLAG_NOT_TOUCHABLE) == 0
5263 && needsGestureExclusionRestrictions(win, 0 /* sysUiVisibility */)
5264 && win.getDisplayContent().mDisplayPolicy.hasSideGestures();
5265 }
5266
5267 /**
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005268 * Adds a local gesture exclusion area to the global area while applying a limit per edge.
5269 *
5270 * @param local The gesture exclusion area to add.
5271 * @param global The destination.
5272 * @param edge Only processes the part in that region.
5273 * @param limit How much limit in pixels we have.
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005274 * @param win The WindowState that is being processed
5275 * @param side The side that is being processed, either {@link WindowState#EXCLUSION_LEFT} or
5276 * {@link WindowState#EXCLUSION_RIGHT}
5277 * @return How much of the limit is remaining.
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005278 */
5279 private static int addToGlobalAndConsumeLimit(Region local, Region global, Rect edge,
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005280 int limit, WindowState win, int side) {
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005281 final Region r = Region.obtain(local);
5282 r.op(edge, Op.INTERSECT);
5283
5284 final int[] remaining = {limit};
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005285 final int[] requestedExclusion = {0};
Adrian Roos2351d5f2019-07-03 15:35:07 +02005286 forEachRectReverse(r, rect -> {
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005287 if (remaining[0] <= 0) {
5288 return;
5289 }
5290 final int height = rect.height();
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005291 requestedExclusion[0] += height;
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005292 if (height > remaining[0]) {
Adrian Roos2351d5f2019-07-03 15:35:07 +02005293 rect.top = rect.bottom - remaining[0];
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005294 }
5295 remaining[0] -= height;
5296 global.op(rect, Op.UNION);
5297 });
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005298
5299 final int grantedExclusion = limit - remaining[0];
5300 win.setLastExclusionHeights(side, requestedExclusion[0], grantedExclusion);
5301
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005302 r.recycle();
5303 return remaining[0];
5304 }
5305
Adrian Roos4ffc8972019-02-07 20:45:11 +01005306 void registerSystemGestureExclusionListener(ISystemGestureExclusionListener listener) {
5307 mSystemGestureExclusionListeners.register(listener);
5308 final boolean changed;
5309 if (mSystemGestureExclusionListeners.getRegisteredCallbackCount() == 1) {
5310 changed = updateSystemGestureExclusion();
5311 } else {
5312 changed = false;
5313 }
5314
5315 if (!changed) {
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005316 final Region unrestrictedOrNull = mSystemGestureExclusionWasRestricted
5317 ? mSystemGestureExclusionUnrestricted : null;
Adrian Roos4ffc8972019-02-07 20:45:11 +01005318 // If updateSystemGestureExclusion changed the exclusion, it will already have
5319 // notified the listener. Otherwise, we'll do it here.
5320 try {
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005321 listener.onSystemGestureExclusionChanged(mDisplayId, mSystemGestureExclusion,
5322 unrestrictedOrNull);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005323 } catch (RemoteException e) {
5324 Slog.e(TAG, "Failed to notify SystemGestureExclusionListener during register", e);
5325 }
5326 }
5327 }
5328
5329 void unregisterSystemGestureExclusionListener(ISystemGestureExclusionListener listener) {
5330 mSystemGestureExclusionListeners.unregister(listener);
5331 }
5332
5333 /**
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005334 * Create a portal window handle for input. This window transports any touch to the display
5335 * indicated by {@link InputWindowHandle#portalToDisplayId} if the touch hits this window.
5336 *
5337 * @param name The name of the portal window handle.
5338 * @return the new portal window handle.
5339 */
5340 private InputWindowHandle createPortalWindowHandle(String name) {
5341 // Let surface flinger to set the display ID of this input window handle because we don't
5342 // know which display the parent surface control is on.
5343 final InputWindowHandle portalWindowHandle = new InputWindowHandle(
Vishnu Nair18782162019-10-08 14:57:16 -07005344 null /* inputApplicationHandle */, INVALID_DISPLAY);
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005345 portalWindowHandle.name = name;
5346 portalWindowHandle.token = new Binder();
5347 portalWindowHandle.layoutParamsFlags =
5348 FLAG_SPLIT_TOUCH | FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL;
5349 getBounds(mTmpBounds);
5350 portalWindowHandle.touchableRegion.set(mTmpBounds);
5351 portalWindowHandle.scaleFactor = 1f;
5352 portalWindowHandle.ownerPid = Process.myPid();
5353 portalWindowHandle.ownerUid = Process.myUid();
5354 portalWindowHandle.portalToDisplayId = mDisplayId;
5355 return portalWindowHandle;
chaviwff2e7d82018-11-02 11:11:27 -07005356 }
Issei Suzukia5dbf522019-02-01 17:58:15 +01005357
5358 /**
5359 * @see IWindowManager#setForwardedInsets
5360 */
5361 public void setForwardedInsets(Insets insets) {
5362 if (insets == null) {
5363 insets = Insets.NONE;
5364 }
5365 if (mDisplayPolicy.getForwardedInsets().equals(insets)) {
5366 return;
5367 }
5368 mDisplayPolicy.setForwardedInsets(insets);
5369 setLayoutNeeded();
5370 mWmService.mWindowPlacerLocked.requestTraversal();
5371 }
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -05005372
5373 protected MetricsLogger getMetricsLogger() {
5374 if (mMetricsLogger == null) {
5375 mMetricsLogger = new MetricsLogger();
5376 }
5377 return mMetricsLogger;
5378 }
Craig Mautner59c00972012-07-30 12:10:24 -07005379}