blob: 983f87398c0208627782457cb0f106ff2a347f95 [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;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070061import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Riddle Hsub2297ad2019-07-26 23:37:25 -060062import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
Wale Ogunwale494009b82016-10-21 09:01:38 -070063import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
Robert Carrf7a7ca82017-12-06 13:17:07 -080064import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070065import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070066import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
67import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Adrian Roos5f2c9a12019-07-03 18:31:46 +020068import static android.view.WindowManager.LayoutParams.TYPE_NAVIGATION_BAR;
Wale Ogunwale494009b82016-10-21 09:01:38 -070069import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070070import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
71import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070072import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070073import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Wale Ogunwale3a256e62018-12-06 14:41:18 -080074import static android.view.WindowManager.TRANSIT_ACTIVITY_OPEN;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080075import static android.view.WindowManager.TRANSIT_KEYGUARD_UNOCCLUDE;
Wale Ogunwale3a256e62018-12-06 14:41:18 -080076import static android.view.WindowManager.TRANSIT_TASK_OPEN;
77import static android.view.WindowManager.TRANSIT_TASK_TO_FRONT;
Tiger Huang51c5a1d2018-12-11 20:24:51 +080078
Adrian Roose99bc052017-11-20 17:55:31 +010079import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
80import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
81import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
82import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080083import static com.android.server.wm.DisplayContentProto.ABOVE_APP_WINDOWS;
lumark588a3e82018-07-20 18:53:54 +080084import static com.android.server.wm.DisplayContentProto.APP_TRANSITION;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080085import static com.android.server.wm.DisplayContentProto.BELOW_APP_WINDOWS;
Louis Changa9350fe2019-04-25 17:14:20 +080086import static com.android.server.wm.DisplayContentProto.CHANGING_APPS;
87import static com.android.server.wm.DisplayContentProto.CLOSING_APPS;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080088import static com.android.server.wm.DisplayContentProto.DISPLAY_FRAMES;
89import static com.android.server.wm.DisplayContentProto.DISPLAY_INFO;
90import static com.android.server.wm.DisplayContentProto.DOCKED_STACK_DIVIDER_CONTROLLER;
91import static com.android.server.wm.DisplayContentProto.DPI;
Tiger Huang1e5b10a2018-07-30 20:19:51 +080092import static com.android.server.wm.DisplayContentProto.FOCUSED_APP;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080093import static com.android.server.wm.DisplayContentProto.ID;
94import static com.android.server.wm.DisplayContentProto.IME_WINDOWS;
Louis Changa9350fe2019-04-25 17:14:20 +080095import static com.android.server.wm.DisplayContentProto.OPENING_APPS;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080096import static com.android.server.wm.DisplayContentProto.PINNED_STACK_CONTROLLER;
97import static com.android.server.wm.DisplayContentProto.ROTATION;
98import static com.android.server.wm.DisplayContentProto.SCREEN_ROTATION_ANIMATION;
99import static com.android.server.wm.DisplayContentProto.STACKS;
100import static com.android.server.wm.DisplayContentProto.WINDOW_CONTAINER;
Adrian Roosb125e0b2019-10-02 14:55:14 +0200101import static com.android.server.wm.ProtoLogGroup.WM_DEBUG_ADD_REMOVE;
102import static com.android.server.wm.ProtoLogGroup.WM_DEBUG_APP_TRANSITIONS;
103import static com.android.server.wm.ProtoLogGroup.WM_DEBUG_FOCUS;
104import static com.android.server.wm.ProtoLogGroup.WM_DEBUG_FOCUS_LIGHT;
105import static com.android.server.wm.ProtoLogGroup.WM_DEBUG_ORIENTATION;
106import static com.android.server.wm.ProtoLogGroup.WM_DEBUG_SCREEN_ON;
107import static com.android.server.wm.ProtoLogGroup.WM_SHOW_TRANSACTIONS;
lumark9bca6b42019-10-17 18:35:22 +0800108import static com.android.server.wm.WindowContainer.AnimationFlags.PARENTS;
109import static com.android.server.wm.WindowContainer.AnimationFlags.TRANSITION;
Wale Ogunwale10124582016-09-15 20:25:50 -0700110import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700111import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700112import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700113import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700114import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Andrii Kulian839def92016-11-02 10:58:58 -0700115import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700116import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700117import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700118import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800119import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Tiger Huang51c5a1d2018-12-11 20:24:51 +0800120import static com.android.server.wm.WindowManagerService.H.REPORT_FOCUS_CHANGE;
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800121import static com.android.server.wm.WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800122import static com.android.server.wm.WindowManagerService.H.REPORT_LOSING_FOCUS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700123import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700124import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700125import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700126import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800127import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_PLACING_SURFACES;
128import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_REMOVING_FOCUS;
129import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_ASSIGN_LAYERS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700130import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700131import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700132import static com.android.server.wm.WindowManagerService.dipToPixel;
Adrian Roos5f2c9a12019-07-03 18:31:46 +0200133import static com.android.server.wm.WindowState.EXCLUSION_LEFT;
134import static com.android.server.wm.WindowState.EXCLUSION_RIGHT;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700135import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700136import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Adrian Roos2351d5f2019-07-03 15:35:07 +0200137import static com.android.server.wm.utils.RegionUtils.forEachRectReverse;
Adrian Roos4ffc8972019-02-07 20:45:11 +0100138import static com.android.server.wm.utils.RegionUtils.rectListToRegion;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700139
lumark588a3e82018-07-20 18:53:54 +0800140import android.animation.AnimationHandler;
Riddle Hsuf53da812018-08-15 22:00:27 +0800141import android.annotation.IntDef;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700142import android.annotation.NonNull;
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200143import android.annotation.Nullable;
Riddle Hsuccf09402019-08-13 00:33:06 +0800144import android.content.pm.ActivityInfo.ScreenOrientation;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700145import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700146import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700147import android.graphics.Bitmap;
Issei Suzukia5dbf522019-02-01 17:58:15 +0100148import android.graphics.Insets;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700149import android.graphics.Matrix;
Tiger Huangd8ec9382019-04-18 14:35:09 -0700150import android.graphics.Point;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800151import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700152import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700153import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100154import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700155import android.hardware.display.DisplayManagerInternal;
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -0500156import android.metrics.LogMaker;
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800157import android.os.Binder;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700158import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700159import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700160import android.os.IBinder;
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800161import android.os.Process;
Adrian Roos4ffc8972019-02-07 20:45:11 +0100162import android.os.RemoteCallbackList;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700163import android.os.RemoteException;
164import android.os.SystemClock;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100165import android.os.Trace;
Riddle Hsuf53da812018-08-15 22:00:27 +0800166import android.os.UserHandle;
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800167import android.util.ArraySet;
Chong Zhang8e89b312015-09-09 15:09:30 -0700168import android.util.DisplayMetrics;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700169import android.util.Slog;
Riddle Hsub2297ad2019-07-26 23:37:25 -0600170import android.util.SparseBooleanArray;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700171import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700172import android.view.Display;
Adrian Roos1cf585052018-01-03 18:43:27 +0100173import android.view.DisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700174import android.view.DisplayInfo;
Riddle Hsua4d6fa22018-08-11 00:50:39 +0800175import android.view.Gravity;
Adrian Roos4ffc8972019-02-07 20:45:11 +0100176import android.view.ISystemGestureExclusionListener;
Arthur Hungbe5ce212018-09-13 18:41:56 +0800177import android.view.InputChannel;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700178import android.view.InputDevice;
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800179import android.view.InputWindowHandle;
Tiger Huang332793b2019-10-29 23:21:27 +0800180import android.view.InsetsState.InternalInsetsType;
Robert Carrb1579c82017-09-05 14:54:47 -0700181import android.view.MagnificationSpec;
Evan Rosky966759f2019-01-15 10:33:58 -0800182import android.view.RemoteAnimationDefinition;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700183import android.view.Surface;
184import android.view.SurfaceControl;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100185import android.view.SurfaceControl.Transaction;
Robert Carrb1579c82017-09-05 14:54:47 -0700186import android.view.SurfaceSession;
Tiger Huang7c610aa2018-10-27 00:01:01 +0800187import android.view.View;
lumark588a3e82018-07-20 18:53:54 +0800188import android.view.WindowManager;
Evan Rosky39b6f232018-10-30 18:35:41 -0700189import android.view.WindowManagerPolicyConstants.PointerEventListener;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700190
Bryce Lee48f4b572017-04-10 10:54:15 -0700191import com.android.internal.annotations.VisibleForTesting;
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -0500192import com.android.internal.logging.MetricsLogger;
193import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800194import com.android.internal.util.ToBooleanFunction;
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200195import com.android.internal.util.function.TriConsumer;
Winson Chungc5fe7ff2019-02-19 14:49:25 -0800196import com.android.internal.util.function.pooled.PooledConsumer;
Wale Ogunwale85fb19a2019-12-05 10:41:05 +0900197import com.android.internal.util.function.pooled.PooledFunction;
Winson Chungc5fe7ff2019-02-19 14:49:25 -0800198import com.android.internal.util.function.pooled.PooledLambda;
Winson Chung6ba767a2018-12-13 18:20:11 -0800199import com.android.server.AnimationThread;
Adrian Roose99bc052017-11-20 17:55:31 +0100200import com.android.server.policy.WindowManagerPolicy;
Adrian Roosb125e0b2019-10-02 14:55:14 +0200201import com.android.server.protolog.common.ProtoLog;
Issei Suzuki43190bd2018-08-20 17:28:41 +0200202import com.android.server.wm.utils.DisplayRotationUtil;
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100203import com.android.server.wm.utils.RotationCache;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100204import com.android.server.wm.utils.WmDisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700205
206import java.io.PrintWriter;
Riddle Hsuf53da812018-08-15 22:00:27 +0800207import java.lang.annotation.Retention;
208import java.lang.annotation.RetentionPolicy;
Craig Mautner59c00972012-07-30 12:10:24 -0700209import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700210import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700211import java.util.HashMap;
212import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700213import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700214import java.util.List;
Adrian Roos1cf585052018-01-03 18:43:27 +0100215import java.util.Objects;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800216import java.util.function.Consumer;
217import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700218
Craig Mautner59c00972012-07-30 12:10:24 -0700219/**
220 * Utility class for keeping track of the WindowStates and other pertinent contents of a
221 * particular Display.
Craig Mautner59c00972012-07-30 12:10:24 -0700222 */
Riddle Hsuad256a12018-07-18 16:11:30 +0800223class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer>
Louis Chang2453d062019-11-19 22:30:48 +0800224 implements WindowManagerPolicy.DisplayContentInfo {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700225 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700226
Riddle Hsuf53da812018-08-15 22:00:27 +0800227 /** The default scaling mode that scales content automatically. */
228 static final int FORCE_SCALING_MODE_AUTO = 0;
229 /** For {@link #setForcedScalingMode} to apply flag {@link Display#FLAG_SCALING_DISABLED}. */
230 static final int FORCE_SCALING_MODE_DISABLED = 1;
231
232 @IntDef(prefix = { "FORCE_SCALING_MODE_" }, value = {
233 FORCE_SCALING_MODE_AUTO,
234 FORCE_SCALING_MODE_DISABLED
235 })
236 @Retention(RetentionPolicy.SOURCE)
237 @interface ForceScalingMode {}
238
Louis Chang2453d062019-11-19 22:30:48 +0800239 ActivityTaskManagerService mAtmService;
Craig Mautner59c00972012-07-30 12:10:24 -0700240
Louis Chang2453d062019-11-19 22:30:48 +0800241 /** Unique identifier of this display. */
242 private final int mDisplayId;
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800243
Wale Ogunwale3a931692016-11-02 16:49:48 -0700244 /** The containers below are the only child containers the display can have. */
245 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800246 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers(mWmService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700247 // Contains all non-app window containers that should be displayed above the app containers
248 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800249 private final AboveAppWindowContainers mAboveAppWindowsContainers =
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800250 new AboveAppWindowContainers("mAboveAppWindowsContainers", mWmService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700251 // Contains all non-app window containers that should be displayed below the app containers
252 // (e.g. Wallpaper).
253 private final NonAppWindowContainers mBelowAppWindowsContainers =
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800254 new NonAppWindowContainers("mBelowAppWindowsContainers", mWmService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700255 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
256 // 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 -0800257 // window containers together and move them in-sync if/when needed. We use a subclass of
258 // WindowContainer which is omitted from screen magnification, as the IME is never magnified.
Eugene Susla25a24952019-04-08 13:57:44 -0700259 private final NonAppWindowContainers mImeWindowsContainers =
260 new NonAppWindowContainers("mImeWindowsContainers", mWmService);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700261
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000262 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800263 private WindowState mTmpWindow2;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800264 private boolean mTmpRecoveringMemory;
265 private boolean mUpdateImeTarget;
266 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700267 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700268
lumark588a3e82018-07-20 18:53:54 +0800269 final AppTransition mAppTransition;
270 final AppTransitionController mAppTransitionController;
271 boolean mSkipAppTransitionAnimation = false;
272
Garfield Tane8d84ab2019-10-11 09:49:40 -0700273 final ArraySet<ActivityRecord> mOpeningApps = new ArraySet<>();
274 final ArraySet<ActivityRecord> mClosingApps = new ArraySet<>();
275 final ArraySet<ActivityRecord> mChangingApps = new ArraySet<>();
lumark588a3e82018-07-20 18:53:54 +0800276 final UnknownAppVisibilityController mUnknownAppVisibilityController;
277 BoundsAnimationController mBoundsAnimationController;
278
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -0500279 private MetricsLogger mMetricsLogger;
280
lumark588a3e82018-07-20 18:53:54 +0800281 /**
282 * List of clients without a transtiton animation that we notify once we are done
283 * transitioning since they won't be notified through the app window animator.
284 */
285 final List<IBinder> mNoAnimationNotifyOnTransitionFinished = new ArrayList<>();
286
Wale Ogunwale02319a62016-09-26 15:21:22 -0700287 // Mapping from a token IBinder to a WindowToken object on this display.
288 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
289
Andrii Kuliancd097992017-03-23 18:31:59 -0700290 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700291 int mInitialDisplayWidth = 0;
292 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700293 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700294
Adrian Roos1cf585052018-01-03 18:43:27 +0100295 DisplayCutout mInitialDisplayCutout;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100296 private final RotationCache<DisplayCutout, WmDisplayCutout> mDisplayCutoutCache
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100297 = new RotationCache<>(this::calculateDisplayCutoutForRotationUncached);
Adrian Roos1cf585052018-01-03 18:43:27 +0100298
Andrii Kuliancd097992017-03-23 18:31:59 -0700299 /**
300 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
301 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
302 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
303 */
Craig Mautner59c00972012-07-30 12:10:24 -0700304 int mBaseDisplayWidth = 0;
305 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700306 /**
307 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
308 * but can be set from Settings or via shell command "adb shell wm density".
309 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
310 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700311 int mBaseDisplayDensity = 0;
Riddle Hsuf53da812018-08-15 22:00:27 +0800312
313 /**
314 * Whether to disable display scaling. This can be set via shell command "adb shell wm scaling".
315 * @see WindowManagerService#setForcedDisplayScalingMode(int, int)
316 */
Jeff Brownd46747a2015-04-15 19:02:36 -0700317 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700318 private final DisplayInfo mDisplayInfo = new DisplayInfo();
319 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700320 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800321 private final DisplayPolicy mDisplayPolicy;
Riddle Hsuccf09402019-08-13 00:33:06 +0800322 private final DisplayRotation mDisplayRotation;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000323 DisplayFrames mDisplayFrames;
324
Adrian Roos4ffc8972019-02-07 20:45:11 +0100325 private final RemoteCallbackList<ISystemGestureExclusionListener>
326 mSystemGestureExclusionListeners = new RemoteCallbackList<>();
327 private final Region mSystemGestureExclusion = new Region();
Adrian Roos5f2c9a12019-07-03 18:31:46 +0200328 private boolean mSystemGestureExclusionWasRestricted = false;
329 private final Region mSystemGestureExclusionUnrestricted = new Region();
Adrian Roosbf3bc1b2019-06-18 16:13:53 +0200330 private int mSystemGestureExclusionLimit;
Adrian Roos4ffc8972019-02-07 20:45:11 +0100331
Andrii Kulian06d07d62017-03-14 11:11:47 -0700332 /**
333 * For default display it contains real metrics, empty for others.
chaviw619da692019-06-10 15:39:40 -0700334 * @see WindowManagerService#createWatermark()
Andrii Kulian06d07d62017-03-14 11:11:47 -0700335 */
336 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800337
338 /** @see #computeCompatSmallestWidth(boolean, int, int, int, DisplayCutout) */
Andrii Kulian06d07d62017-03-14 11:11:47 -0700339 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700340
Andrii Kulian06d07d62017-03-14 11:11:47 -0700341 /**
342 * Compat metrics computed based on {@link #mDisplayMetrics}.
343 * @see #updateDisplayAndOrientation(int)
344 */
345 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
346
347 /** The desired scaling factor for compatible apps. */
348 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700349
Andrii Kulian8ee72852017-03-10 10:36:45 -0800350 /**
Andrii Kulian8ee72852017-03-10 10:36:45 -0800351 * Orientation forced by some window. If there is no visible window that specifies orientation
352 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
353 *
354 * @see NonAppWindowContainers#getOrientation()
355 */
356 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700357
Andrii Kulian8ee72852017-03-10 10:36:45 -0800358 /**
359 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
360 * occluded.
361 *
362 * @see NonAppWindowContainers#getOrientation()
363 */
364 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
365
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700366 /**
Tiger Huang86e6d072019-05-02 20:23:47 +0800367 * The maximum aspect ratio (longerSide/shorterSide) that is treated as close-to-square. The
368 * orientation requests from apps would be ignored if the display is close-to-square.
369 */
370 @VisibleForTesting
371 final float mCloseToSquareMaxAspectRatio;
372
373 /**
374 * If this is true, we would not rotate the display for apps. The rotation would be either the
375 * sensor rotation or the user rotation, controlled by
376 * {@link WindowManagerPolicy.UserRotationMode}.
377 */
378 private boolean mIgnoreRotationForApps;
379
380 /**
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700381 * Keep track of wallpaper visibility to notify changes.
382 */
383 private boolean mLastWallpaperVisible = false;
384
Andrii Kulian06d07d62017-03-14 11:11:47 -0700385 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700386
Craig Mautner39834192012-09-02 07:47:24 -0700387 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700388 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700389 int pendingLayoutChanges;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800390
Vishnu Nairba183352018-11-21 11:16:49 -0800391 /**
392 * Used to gate application window layout until we have sent the complete configuration.
393 * TODO: There are still scenarios where we may be out of sync with the client. Ideally
394 * we want to replace this flag with a mechanism that will confirm the configuration
395 * applied by the client is the one expected by the system server.
396 */
397 boolean mWaitingForConfig;
398
Andrii Kulian839def92016-11-02 10:58:58 -0700399 // TODO(multi-display): remove some of the usages.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800400 @VisibleForTesting
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800401 boolean isDefaultDisplay;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800402
Andrii Kulianf0379de2018-03-14 16:24:07 -0700403 /**
404 * Flag indicating whether WindowManager should override info for this display in
405 * DisplayManager.
406 */
407 boolean mShouldOverrideDisplayConfiguration = true;
Craig Mautner39834192012-09-02 07:47:24 -0700408
Craig Mautnerdc548482014-02-05 13:35:24 -0800409 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700410 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800411
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700412 /** Detect user tapping outside of current focused task bounds .*/
Riddle Hsu2588ab02019-02-25 14:23:56 +0800413 @VisibleForTesting
414 final TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700415
Craig Mautner6601b7b2013-04-29 10:29:11 -0700416 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700417 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700418
Craig Mautner6601b7b2013-04-29 10:29:11 -0700419 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800420 private final Rect mTmpRect = new Rect();
421 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700422 private final RectF mTmpRectF = new RectF();
423 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800424 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700425
Bryce Leef3c6a472017-11-14 14:53:06 -0800426 /** Used for handing back size of display */
427 private final Rect mTmpBounds = new Rect();
428
Garfield Tan90b04282018-12-11 14:04:42 -0800429 private final Configuration mTmpConfiguration = new Configuration();
430
Craig Mautner95da1082014-02-24 17:54:35 -0800431 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700432 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800433
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700434 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700435 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700436
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800437 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800438 /** A collection of windows that provide tap exclude regions inside of them. */
439 final ArraySet<WindowState> mTapExcludeProvidingWindows = new ArraySet<>();
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800440
Garfield Tane8d84ab2019-10-11 09:49:40 -0700441 private final LinkedList<ActivityRecord> mTmpUpdateAllDrawn = new LinkedList();
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700442
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700443 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
444 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000445 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
446 new ApplySurfaceChangesTransactionState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700447
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700448 // True if this display is in the process of being removed. Used to determine if the removal of
449 // the display's direct children should be allowed.
450 private boolean mRemovingDisplay = false;
451
Bryce Leed1871262017-06-12 14:12:29 -0700452 // {@code false} if this display is in the processing of being created.
453 private boolean mDisplayReady = false;
454
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800455 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700456
wilsonshihc32538e2018-11-07 17:27:34 +0800457 boolean mWallpaperMayChange = false;
458
Robert Carrb1579c82017-09-05 14:54:47 -0700459 private final SurfaceSession mSession = new SurfaceSession();
460
461 /**
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800462 * Window that is currently interacting with the user. This window is responsible for receiving
463 * key events and pointer events from the user.
464 */
465 WindowState mCurrentFocus = null;
466
467 /**
468 * The last focused window that we've notified the client that the focus is changed.
469 */
470 WindowState mLastFocus = null;
471
472 /**
473 * Windows that have lost input focus and are waiting for the new focus window to be displayed
474 * before they are told about this.
475 */
476 ArrayList<WindowState> mLosingFocus = new ArrayList<>();
477
478 /**
479 * The foreground app of this display. Windows below this app cannot be the focused window. If
480 * the user taps on the area outside of the task of the focused app, we will notify AM about the
481 * new task the user wants to interact with.
482 */
Garfield Tane8d84ab2019-10-11 09:49:40 -0700483 ActivityRecord mFocusedApp = null;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800484
485 /** Windows added since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
486 final ArrayList<WindowState> mWinAddedSinceNullFocus = new ArrayList<>();
487
488 /** Windows removed since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
489 final ArrayList<WindowState> mWinRemovedSinceNullFocus = new ArrayList<>();
490
Vadim Caenb3715832019-08-13 17:06:38 +0200491 private ScreenRotationAnimation mScreenRotationAnimation;
492
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800493 /**
Robert Carrb1579c82017-09-05 14:54:47 -0700494 * We organize all top-level Surfaces in to the following layers.
495 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800496 * and omitted from Screen-Magnification, for example the strict mode flash or
497 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700498 * {@link #mWindowingLayer} contains everything else.
499 */
500 private SurfaceControl mOverlayLayer;
501
502 /**
503 * See {@link #mOverlayLayer}
504 */
505 private SurfaceControl mWindowingLayer;
506
507 /**
Adrian Roos5251b1d2018-03-23 18:57:43 +0100508 * Sequence number for the current layout pass.
509 */
510 int mLayoutSeq = 0;
511
Chavi Weingarten3a748552018-05-14 17:32:42 +0000512 /**
513 * Specifies the count to determine whether to defer updating the IME target until ready.
514 */
515 private int mDeferUpdateImeTargetCount;
516
Robert Carr24be9ab2018-04-30 17:54:53 -0700517 private MagnificationSpec mMagnificationSpec;
518
Arthur Hung95b38a92018-07-20 18:56:12 +0800519 private InputMonitor mInputMonitor;
520
Jorim Jaggif1292892018-09-10 11:58:13 +0200521 /** Caches the value whether told display manager that we have content. */
522 private boolean mLastHasContent;
523
Issei Suzuki43190bd2018-08-20 17:28:41 +0200524 private DisplayRotationUtil mRotationUtil = new DisplayRotationUtil();
525
lumark90120a82018-08-15 00:33:03 +0800526 /**
527 * The input method window for this display.
528 */
529 WindowState mInputMethodWindow;
530
lumarkff0ab692018-11-05 20:32:30 +0800531 /**
532 * This just indicates the window the input method is on top of, not
533 * necessarily the window its input is going to.
534 */
535 WindowState mInputMethodTarget;
536
537 /** If true hold off on modifying the animation layer of mInputMethodTarget */
538 boolean mInputMethodTargetWaitingAnim;
539
Arthur Hungbe5ce212018-09-13 18:41:56 +0800540 private final PointerEventDispatcher mPointerEventDispatcher;
541
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200542 private final InsetsStateController mInsetsStateController;
Jorim Jaggi28620472019-01-02 23:21:49 +0100543 private final InsetsPolicy mInsetsPolicy;
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200544
Tiger Huangd8ec9382019-04-18 14:35:09 -0700545 /** @see #getParentWindow() */
546 private WindowState mParentWindow;
547
548 private Point mLocationInParentWindow = new Point();
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800549 private SurfaceControl mParentSurfaceControl;
550 private InputWindowHandle mPortalWindowHandle;
551
Tiger Huang7c610aa2018-10-27 00:01:01 +0800552 // Last systemUiVisibility we received from status bar.
553 private int mLastStatusBarVisibility = 0;
554 // Last systemUiVisibility we dispatched to windows.
555 private int mLastDispatchedSystemUiVisibility = 0;
556
Louis Changdc077272019-11-12 16:52:56 +0800557 private final ArrayList<ActivityStack> mTmpAlwaysOnTopStacks = new ArrayList<>();
558 private final ArrayList<ActivityStack> mTmpNormalStacks = new ArrayList<>();
559 private final ArrayList<ActivityStack> mTmpHomeStacks = new ArrayList<>();
Issei Suzuki25a9e2b2019-08-14 16:46:26 +0200560
Tiger Huang43b8fc22019-04-26 11:49:29 +0800561 /** Corner radius that windows should have in order to match the display. */
562 private final float mWindowCornerRadius;
563
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800564 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
565 WindowStateAnimator winAnimator = w.mWinAnimator;
Garfield Tane8d84ab2019-10-11 09:49:40 -0700566 final ActivityRecord activity = w.mActivityRecord;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800567 if (winAnimator.mDrawState == READY_TO_SHOW) {
Garfield Tane8d84ab2019-10-11 09:49:40 -0700568 if (activity == null || activity.canShowWindows()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800569 if (w.performShowLocked()) {
570 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
571 if (DEBUG_LAYOUT_REPEATS) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800572 mWmService.mWindowPlacerLocked.debugLayoutRepeats(
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800573 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
574 }
575 }
576 }
577 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800578 };
579
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800580 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
581 final int lostFocusUid = mTmpWindow.mOwnerUid;
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800582 final Handler handler = mWmService.mH;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800583 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
584 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
585 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
586 w.mAttrs.hideTimeoutMilliseconds);
587 }
588 }
589 };
590
591 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
Garfield Tane8d84ab2019-10-11 09:49:40 -0700592 final ActivityRecord focusedApp = mFocusedApp;
Adrian Roosb125e0b2019-10-02 14:55:14 +0200593 ProtoLog.v(WM_DEBUG_FOCUS, "Looking for focus: %s, flags=%d, canReceive=%b",
594 w, w.mAttrs.flags, w.canReceiveKeys());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800595
596 if (!w.canReceiveKeys()) {
597 return false;
598 }
599
Garfield Tane8d84ab2019-10-11 09:49:40 -0700600 final ActivityRecord activity = w.mActivityRecord;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800601
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800602 if (focusedApp == null) {
Adrian Roosb125e0b2019-10-02 14:55:14 +0200603 ProtoLog.v(WM_DEBUG_FOCUS_LIGHT,
604 "findFocusedWindow: focusedApp=null using new focus @ %s", w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800605 mTmpWindow = w;
606 return true;
607 }
608
609 if (!focusedApp.windowsAreFocusable()) {
610 // Current focused app windows aren't focusable...
Adrian Roosb125e0b2019-10-02 14:55:14 +0200611 ProtoLog.v(WM_DEBUG_FOCUS_LIGHT, "findFocusedWindow: focusedApp windows not"
612 + " focusable using new focus @ %s", w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800613 mTmpWindow = w;
614 return true;
615 }
616
617 // Descend through all of the app tokens and find the first that either matches
Garfield Tane8d84ab2019-10-11 09:49:40 -0700618 // win.mActivityRecord (return win) or mFocusedApp (return null).
619 if (activity != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
620 if (focusedApp.compareTo(activity) > 0) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800621 // App stack below focused app stack. No focus for you!!!
Adrian Roosb125e0b2019-10-02 14:55:14 +0200622 ProtoLog.v(WM_DEBUG_FOCUS_LIGHT,
623 "findFocusedWindow: Reached focused app=%s", focusedApp);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800624 mTmpWindow = null;
625 return true;
626 }
627 }
628
Adrian Roosb125e0b2019-10-02 14:55:14 +0200629 ProtoLog.v(WM_DEBUG_FOCUS_LIGHT, "findFocusedWindow: Found new focus @ %s", w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800630 mTmpWindow = w;
631 return true;
632 };
633
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800634 private final Consumer<WindowState> mPerformLayout = w -> {
635 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
636 // wasting time and funky changes while a window is animating away.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800637 final boolean gone = (mTmpWindow != null && mWmService.mPolicy.canBeHiddenByKeyguardLw(w))
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800638 || w.isGoneForLayoutLw();
639
640 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
641 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
642 + " mLayoutAttached=" + w.mLayoutAttached
Jorim Jaggi381cd722019-03-27 17:33:02 +0100643 + " config reported=" + w.isLastConfigReportedToClient());
Garfield Tane8d84ab2019-10-11 09:49:40 -0700644 final ActivityRecord activity = w.mActivityRecord;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800645 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Issei Suzukif2f6c912019-11-08 11:24:18 +0100646 + " mRelayoutCalled=" + w.mRelayoutCalled + " visible=" + w.mToken.isVisible()
Issei Suzuki1669ea42019-11-06 14:20:59 +0100647 + " visibleRequested=" + (activity != null && activity.mVisibleRequested)
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800648 + " parentHidden=" + w.isParentWindowHidden());
649 else Slog.v(TAG, " VIS: 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 }
654
655 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
656 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
657 // since that means "perform layout as normal, just don't display").
Jorim Jaggi91d382a2019-03-27 17:00:48 +0100658 if ((!gone || !w.mHaveFrame || w.mLayoutNeeded) && !w.mLayoutAttached) {
659 if (mTmpInitial) {
660 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800661 }
Jorim Jaggi91d382a2019-03-27 17:00:48 +0100662 if (w.mAttrs.type == TYPE_DREAM) {
663 // Don't layout windows behind a dream, so that if it does stuff like hide
664 // the status bar we won't get a bad transition when it goes away.
665 mTmpWindow = w;
666 }
667 w.mLayoutNeeded = false;
668 w.prelayout();
669 final boolean firstLayout = !w.isLaidOut();
670 getDisplayPolicy().layoutWindowLw(w, null, mDisplayFrames);
671 w.mLayoutSeq = mLayoutSeq;
672
Riddle Hsuf64e1342019-12-05 17:38:41 +0800673 // If this is the first layout, we need to initialize the last frames and inset values,
674 // as otherwise we'd immediately cause an unnecessary resize.
Jorim Jaggi91d382a2019-03-27 17:00:48 +0100675 if (firstLayout) {
Riddle Hsuf64e1342019-12-05 17:38:41 +0800676 w.updateLastFrames();
Jorim Jaggi91d382a2019-03-27 17:00:48 +0100677 w.updateLastInsetValues();
Riddle Hsuf64e1342019-12-05 17:38:41 +0800678 w.updateLocationInParentDisplayIfNeeded();
Jorim Jaggi91d382a2019-03-27 17:00:48 +0100679 }
680
Garfield Tane8d84ab2019-10-11 09:49:40 -0700681 if (w.mActivityRecord != null) {
682 w.mActivityRecord.layoutLetterbox(w);
Jorim Jaggi91d382a2019-03-27 17:00:48 +0100683 }
684
685 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
686 + " mContainingFrame=" + w.getContainingFrame()
687 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800688 }
689 };
690
691 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
692 if (w.mLayoutAttached) {
693 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
694 + " mViewVisibility=" + w.mViewVisibility
695 + " mRelayoutCalled=" + w.mRelayoutCalled);
696 // If this view is GONE, then skip it -- keep the current frame, and let the caller
697 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
698 // windows, since that means "perform layout as normal, just don't display").
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800699 if (mTmpWindow != null && mWmService.mPolicy.canBeHiddenByKeyguardLw(w)) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800700 return;
701 }
702 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
703 || w.mLayoutNeeded) {
704 if (mTmpInitial) {
705 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700706 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800707 }
708 w.mLayoutNeeded = false;
709 w.prelayout();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800710 getDisplayPolicy().layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100711 w.mLayoutSeq = mLayoutSeq;
chaviw492139a2018-07-16 16:07:35 -0700712 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700713 + " mContainingFrame=" + w.getContainingFrame()
714 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800715 }
716 } else if (w.mAttrs.type == TYPE_DREAM) {
717 // Don't layout windows behind a dream, so that if it does stuff like hide the
718 // status bar we won't get a bad transition when it goes away.
719 mTmpWindow = mTmpWindow2;
720 }
721 };
722
723 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
724 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
725 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
726 return w.canBeImeTarget();
727 };
728
729 private final Consumer<WindowState> mApplyPostLayoutPolicy =
Tiger Huang7c610aa2018-10-27 00:01:01 +0800730 w -> getDisplayPolicy().applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
lumarkff0ab692018-11-05 20:32:30 +0800731 mInputMethodTarget);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800732
733 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800734 final WindowSurfacePlacer surfacePlacer = mWmService.mWindowPlacerLocked;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800735 final boolean obscuredChanged = w.mObscured !=
736 mTmpApplySurfaceChangesTransactionState.obscured;
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800737 final RootWindowContainer root = mWmService.mRoot;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800738
739 // Update effect.
740 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
741 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
742 final boolean isDisplayed = w.isDisplayedLw();
743
744 if (isDisplayed && w.isObscuringDisplay()) {
745 // This window completely covers everything behind it, so we want to leave all
746 // of them as undimmed (for performance reasons).
747 root.mObscuringWindow = w;
748 mTmpApplySurfaceChangesTransactionState.obscured = true;
749 }
750
Arthur Hungfbc8f412019-08-01 19:57:54 +0800751 final boolean displayHasContent = root.handleNotObscuredLocked(w,
752 mTmpApplySurfaceChangesTransactionState.obscured,
753 mTmpApplySurfaceChangesTransactionState.syswin);
754
755 if (!mTmpApplySurfaceChangesTransactionState.displayHasContent
756 && !getDisplayPolicy().isWindowExcludedFromContent(w)) {
757 mTmpApplySurfaceChangesTransactionState.displayHasContent |= displayHasContent;
758 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800759
760 if (w.mHasSurface && isDisplayed) {
761 final int type = w.mAttrs.type;
762 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
763 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
764 mTmpApplySurfaceChangesTransactionState.syswin = true;
765 }
766 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
767 && w.mAttrs.preferredRefreshRate != 0) {
768 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
769 = w.mAttrs.preferredRefreshRate;
770 }
Ady Abrahamf3e05312019-05-13 18:04:59 -0700771 final int preferredModeId = getDisplayPolicy().getRefreshRatePolicy()
772 .getPreferredModeId(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800773 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
Ady Abrahamf3e05312019-05-13 18:04:59 -0700774 && preferredModeId != 0) {
775 mTmpApplySurfaceChangesTransactionState.preferredModeId = preferredModeId;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800776 }
777 }
778 }
779
wilsonshihc32538e2018-11-07 17:27:34 +0800780 if (obscuredChanged && w.isVisibleLw() && mWallpaperController.isWallpaperTarget(w)) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800781 // This is the wallpaper target and its obscured state changed... make sure the
782 // current wallpaper's visibility has been updated accordingly.
783 mWallpaperController.updateWallpaperVisibility();
784 }
785
chaviw161ea3e2018-01-31 12:01:12 -0800786 w.handleWindowMovedIfNeeded();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800787
788 final WindowStateAnimator winAnimator = w.mWinAnimator;
789
790 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
chaviw1454b392018-08-06 09:54:04 -0700791 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800792
793 // Moved from updateWindowsAndWallpaperLocked().
794 if (w.mHasSurface) {
795 // Take care of the window being ready to display.
796 final boolean committed = winAnimator.commitFinishDrawingLocked();
797 if (isDefaultDisplay && committed) {
798 if (w.mAttrs.type == TYPE_DREAM) {
799 // HACK: When a dream is shown, it may at that point hide the lock screen.
800 // So we need to redo the layout to let the phone window manager make this
801 // happen.
802 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
803 if (DEBUG_LAYOUT_REPEATS) {
804 surfacePlacer.debugLayoutRepeats(
805 "dream and commitFinishDrawingLocked true",
806 pendingLayoutChanges);
807 }
808 }
809 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
810 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
811 "First draw done in potential wallpaper target " + w);
wilsonshihc32538e2018-11-07 17:27:34 +0800812 mWallpaperMayChange = true;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800813 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
814 if (DEBUG_LAYOUT_REPEATS) {
815 surfacePlacer.debugLayoutRepeats(
816 "wallpaper and commitFinishDrawingLocked true",
817 pendingLayoutChanges);
818 }
819 }
820 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800821 }
822
Garfield Tane8d84ab2019-10-11 09:49:40 -0700823 final ActivityRecord activity = w.mActivityRecord;
824 if (activity != null) {
825 activity.updateLetterboxSurface(w);
826 final boolean updateAllDrawn = activity.updateDrawnWindowStates(w);
827 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(activity)) {
828 mTmpUpdateAllDrawn.add(activity);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800829 }
830 }
831
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800832 if (!mLosingFocus.isEmpty() && w.isFocused() && w.isDisplayedLw()) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800833 mWmService.mH.obtainMessage(REPORT_LOSING_FOCUS, this).sendToTarget();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800834 }
835
836 w.updateResizingWindowIfNeeded();
837 };
838
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800839 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800840 * Create new {@link DisplayContent} instance, add itself to the root window container and
841 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700842 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800843 * @param service You know.
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800844 * @param activityDisplay The ActivityDisplay for the display container.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700845 */
Louis Chang2453d062019-11-19 22:30:48 +0800846 DisplayContent(Display display, WindowManagerService service) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100847 super(service);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800848 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
849 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
850 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
851 + " new=" + display);
852 }
853
Louis Chang2453d062019-11-19 22:30:48 +0800854 mAtmService = mWmService.mAtmService;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700855 mDisplay = display;
856 mDisplayId = display.getDisplayId();
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800857 mWallpaperController = new WallpaperController(mWmService, this);
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700858 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700859 display.getMetrics(mDisplayMetrics);
Adrian Roos1c2e9a12019-08-20 18:23:47 +0200860 mSystemGestureExclusionLimit = mWmService.mConstants.mSystemGestureExclusionLimitDp
Adrian Roosbf3bc1b2019-06-18 16:13:53 +0200861 * mDisplayMetrics.densityDpi / DENSITY_DEFAULT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700862 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100863 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo,
864 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700865 initializeDisplayBaseInfo();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700866
lumark588a3e82018-07-20 18:53:54 +0800867 mAppTransition = new AppTransition(service.mContext, service, this);
868 mAppTransition.registerListenerLocked(service.mActivityManagerAppTransitionNotifier);
869 mAppTransitionController = new AppTransitionController(service, this);
870 mUnknownAppVisibilityController = new UnknownAppVisibilityController(service, this);
871
872 AnimationHandler animationHandler = new AnimationHandler();
873 mBoundsAnimationController = new BoundsAnimationController(service.mContext,
Winson Chung6ba767a2018-12-13 18:20:11 -0800874 mAppTransition, AnimationThread.getHandler(), animationHandler);
lumark588a3e82018-07-20 18:53:54 +0800875
Riddle Hsu2588ab02019-02-25 14:23:56 +0800876 final InputChannel inputChannel = mWmService.mInputManager.monitorInput(
877 "PointerEventDispatcher" + mDisplayId, mDisplayId);
878 mPointerEventDispatcher = new PointerEventDispatcher(inputChannel);
879
880 // Tap Listeners are supported for:
881 // 1. All physical displays (multi-display).
882 // 2. VirtualDisplays on VR, AA (and everything else).
883 mTapDetector = new TaskTapPointerEventListener(mWmService, this);
884 registerPointerEventListener(mTapDetector);
885 registerPointerEventListener(mWmService.mMousePositionTracker);
Winson Chungc5fe7ff2019-02-19 14:49:25 -0800886 if (mWmService.mAtmService.getRecentTasks() != null) {
887 registerPointerEventListener(
888 mWmService.mAtmService.getRecentTasks().getInputListener());
889 }
Riddle Hsu2588ab02019-02-25 14:23:56 +0800890
Tiger Huang7c610aa2018-10-27 00:01:01 +0800891 mDisplayPolicy = new DisplayPolicy(service, this);
892 mDisplayRotation = new DisplayRotation(service, this);
Tiger Huang86e6d072019-05-02 20:23:47 +0800893 mCloseToSquareMaxAspectRatio = service.mContext.getResources().getFloat(
894 com.android.internal.R.dimen.config_closeToSquareDisplayMaxAspectRatio);
Tiger Huang7c610aa2018-10-27 00:01:01 +0800895 if (isDefaultDisplay) {
896 // The policy may be invoked right after here, so it requires the necessary default
897 // fields of this display content.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800898 mWmService.mPolicy.setDefaultDisplay(this);
Tiger Huang7c610aa2018-10-27 00:01:01 +0800899 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800900 if (mWmService.mDisplayReady) {
Tiger Huang7c610aa2018-10-27 00:01:01 +0800901 mDisplayPolicy.onConfigurationChanged();
902 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800903 if (mWmService.mSystemReady) {
Tiger Huang7c610aa2018-10-27 00:01:01 +0800904 mDisplayPolicy.systemReady();
905 }
Tiger Huang43b8fc22019-04-26 11:49:29 +0800906 mWindowCornerRadius = mDisplayPolicy.getWindowCornerRadius();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800907 mDividerControllerLocked = new DockedStackDividerController(service, this);
908 mPinnedStackControllerLocked = new PinnedStackController(service, this);
909
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000910 final SurfaceControl.Builder b = mWmService.makeSurfaceBuilder(mSession)
911 .setOpaque(true)
912 .setContainerLayer();
Vadim Caen1812c762019-08-12 11:38:22 +0200913 mSurfaceControl = b.setName("Root").setContainerLayer().build();
914 mWindowingLayer = b.setName("Display Windows").setParent(mSurfaceControl).build();
915 mOverlayLayer = b.setName("Display Overlays").setParent(mSurfaceControl).build();
Robert Carrb1579c82017-09-05 14:54:47 -0700916
Vadim Caen1812c762019-08-12 11:38:22 +0200917 getPendingTransaction()
918 .setLayer(mSurfaceControl, 0)
919 .setLayerStack(mSurfaceControl, mDisplayId)
920 .show(mSurfaceControl)
921 .setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700922 .show(mWindowingLayer)
923 .setLayer(mOverlayLayer, 1)
Robert Carrb1579c82017-09-05 14:54:47 -0700924 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700925 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700926
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700927 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700928 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700929 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700930 super.addChild(mAboveAppWindowsContainers, null);
931 super.addChild(mImeWindowsContainers, null);
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -0700932 // Sets the display content for the children.
933 onDisplayChanged(this);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800934
935 // Add itself as a child to the root container.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800936 mWmService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700937
938 // TODO(b/62541591): evaluate whether this is the best spot to declare the
939 // {@link DisplayContent} ready for use.
940 mDisplayReady = true;
Arthur Hung95b38a92018-07-20 18:56:12 +0800941
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800942 mWmService.mAnimator.addDisplayLocked(mDisplayId);
Arthur Hung39134b22018-08-14 11:58:28 +0800943 mInputMonitor = new InputMonitor(service, mDisplayId);
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200944 mInsetsStateController = new InsetsStateController(this);
Jorim Jaggi28620472019-01-02 23:21:49 +0100945 mInsetsPolicy = new InsetsPolicy(mInsetsStateController, this);
Bryce Leed1871262017-06-12 14:12:29 -0700946 }
947
948 boolean isReady() {
949 // The display is ready when the system and the individual display are both ready.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800950 return mWmService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700951 }
952
953 int getDisplayId() {
954 return mDisplayId;
955 }
956
Tiger Huang43b8fc22019-04-26 11:49:29 +0800957 float getWindowCornerRadius() {
958 return mWindowCornerRadius;
959 }
960
Wale Ogunwale02319a62016-09-26 15:21:22 -0700961 WindowToken getWindowToken(IBinder binder) {
962 return mTokenMap.get(binder);
963 }
964
Garfield Tane8d84ab2019-10-11 09:49:40 -0700965 ActivityRecord getActivityRecord(IBinder binder) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700966 final WindowToken token = getWindowToken(binder);
967 if (token == null) {
968 return null;
969 }
Garfield Tane8d84ab2019-10-11 09:49:40 -0700970 return token.asActivityRecord();
Wale Ogunwale02319a62016-09-26 15:21:22 -0700971 }
972
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700973 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800974 final DisplayContent dc = mWmService.mRoot.getWindowTokenDisplay(token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700975 if (dc != null) {
976 // We currently don't support adding a window token to the display if the display
977 // already has the binder mapped to another token. If there is a use case for supporting
978 // this moving forward we will either need to merge the WindowTokens some how or have
979 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700980 throw new IllegalArgumentException("Can't map token=" + token + " to display="
981 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700982 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700983 if (binder == null) {
984 throw new IllegalArgumentException("Can't map token=" + token + " to display="
985 + getName() + " binder is null");
986 }
987 if (token == null) {
988 throw new IllegalArgumentException("Can't map null token to display="
989 + getName() + " binder=" + binder);
990 }
991
Wale Ogunwale02319a62016-09-26 15:21:22 -0700992 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700993
Garfield Tane8d84ab2019-10-11 09:49:40 -0700994 if (token.asActivityRecord() == null) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700995 // Add non-app token to container hierarchy on the display. App tokens are added through
996 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700997 switch (token.windowType) {
998 case TYPE_WALLPAPER:
999 mBelowAppWindowsContainers.addChild(token);
1000 break;
1001 case TYPE_INPUT_METHOD:
1002 case TYPE_INPUT_METHOD_DIALOG:
1003 mImeWindowsContainers.addChild(token);
1004 break;
1005 default:
1006 mAboveAppWindowsContainers.addChild(token);
1007 break;
1008 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001009 }
Wale Ogunwale02319a62016-09-26 15:21:22 -07001010 }
1011
1012 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001013 final WindowToken token = mTokenMap.remove(binder);
Garfield Tane8d84ab2019-10-11 09:49:40 -07001014 if (token != null && token.asActivityRecord() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -08001015 token.setExiting();
1016 }
1017 return token;
1018 }
1019
1020 /** Changes the display the input window token is housed on to this one. */
1021 void reParentWindowToken(WindowToken token) {
1022 final DisplayContent prevDc = token.getDisplayContent();
1023 if (prevDc == this) {
1024 return;
1025 }
Riddle Hsu85bd04b2018-11-17 00:34:36 +08001026 if (prevDc != null) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07001027 if (prevDc.mTokenMap.remove(token.token) != null && token.asActivityRecord() == null) {
Riddle Hsu85bd04b2018-11-17 00:34:36 +08001028 // Removed the token from the map, but made sure it's not an app token before
1029 // removing from parent.
1030 token.getParent().removeChild(token);
1031 }
lumark280cb212019-09-09 09:47:22 +08001032 if (token.hasChild(prevDc.mLastFocus)) {
1033 // If the reparent window token contains previous display's last focus window, means
1034 // it will end up to gain window focus on the target display, so it should not be
1035 // notified that it lost focus from the previous display.
Riddle Hsu85bd04b2018-11-17 00:34:36 +08001036 prevDc.mLastFocus = null;
1037 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001038 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -08001039
1040 addWindowToken(token.token, token);
RyanlwLin925e8222019-08-30 15:08:48 +08001041
1042 if (mWmService.mAccessibilityController != null) {
1043 final int prevDisplayId = prevDc != null ? prevDc.getDisplayId() : INVALID_DISPLAY;
1044 mWmService.mAccessibilityController.onSomeWindowResizedOrMovedLocked(prevDisplayId,
1045 getDisplayId());
1046 }
Wale Ogunwale02319a62016-09-26 15:21:22 -07001047 }
1048
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001049 void removeAppToken(IBinder binder) {
1050 final WindowToken token = removeWindowToken(binder);
1051 if (token == null) {
1052 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
1053 return;
1054 }
1055
Garfield Tane8d84ab2019-10-11 09:49:40 -07001056 final ActivityRecord activity = token.asActivityRecord();
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001057
Garfield Tane8d84ab2019-10-11 09:49:40 -07001058 if (activity == null) {
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001059 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
1060 return;
1061 }
1062
Garfield Tane8d84ab2019-10-11 09:49:40 -07001063 activity.onRemovedFromDisplay();
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001064 }
1065
Riddle Hsuad256a12018-07-18 16:11:30 +08001066 @Override
1067 public Display getDisplay() {
Craig Mautnerb47bbc32012-08-22 17:41:48 -07001068 return mDisplay;
1069 }
1070
Craig Mautner59c00972012-07-30 12:10:24 -07001071 DisplayInfo getDisplayInfo() {
1072 return mDisplayInfo;
1073 }
1074
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001075 DisplayMetrics getDisplayMetrics() {
1076 return mDisplayMetrics;
1077 }
1078
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001079 DisplayPolicy getDisplayPolicy() {
1080 return mDisplayPolicy;
1081 }
1082
Riddle Hsuad256a12018-07-18 16:11:30 +08001083 @Override
1084 public DisplayRotation getDisplayRotation() {
1085 return mDisplayRotation;
1086 }
1087
Jorim Jaggif96c90a2018-09-26 16:55:15 +02001088 /**
1089 * Marks a window as providing insets for the rest of the windows in the system.
1090 *
1091 * @param type The type of inset this window provides.
1092 * @param win The window.
1093 * @param frameProvider Function to compute the frame, or {@code null} if the just the frame of
1094 * the window should be taken.
1095 */
Tiger Huang332793b2019-10-29 23:21:27 +08001096 void setInsetProvider(@InternalInsetsType int type, WindowState win,
Jorim Jaggif96c90a2018-09-26 16:55:15 +02001097 @Nullable TriConsumer<DisplayFrames, WindowState, Rect> frameProvider) {
1098 mInsetsStateController.getSourceProvider(type).setWindow(win, frameProvider);
1099 }
1100
1101 InsetsStateController getInsetsStateController() {
1102 return mInsetsStateController;
1103 }
1104
Jorim Jaggi28620472019-01-02 23:21:49 +01001105 InsetsPolicy getInsetsPolicy() {
1106 return mInsetsPolicy;
1107 }
1108
Riddle Hsuccf09402019-08-13 00:33:06 +08001109 @Surface.Rotation
Andrii Kulian8ee72852017-03-10 10:36:45 -08001110 int getRotation() {
Riddle Hsuccf09402019-08-13 00:33:06 +08001111 return mDisplayRotation.getRotation();
Andrii Kulian8ee72852017-03-10 10:36:45 -08001112 }
1113
Riddle Hsuccf09402019-08-13 00:33:06 +08001114 @ScreenOrientation
Andrii Kulian8ee72852017-03-10 10:36:45 -08001115 int getLastOrientation() {
Riddle Hsuccf09402019-08-13 00:33:06 +08001116 return mDisplayRotation.getLastOrientation();
Andrii Kulian8ee72852017-03-10 10:36:45 -08001117 }
1118
Riddle Hsuccf09402019-08-13 00:33:06 +08001119 @ScreenOrientation
Andrii Kulian8ee72852017-03-10 10:36:45 -08001120 int getLastWindowForcedOrientation() {
1121 return mLastWindowForcedOrientation;
1122 }
1123
Evan Rosky966759f2019-01-15 10:33:58 -08001124 void registerRemoteAnimations(RemoteAnimationDefinition definition) {
1125 mAppTransitionController.registerRemoteAnimations(definition);
1126 }
1127
Shivam Agrawal6472e0e2019-07-03 16:27:49 -07001128 void reconfigureDisplayLocked() {
1129 if (!isReady()) {
1130 return;
1131 }
1132 configureDisplayPolicy();
1133 setLayoutNeeded();
1134
Riddle Hsuccf09402019-08-13 00:33:06 +08001135 boolean configChanged = updateOrientation();
Shivam Agrawal6472e0e2019-07-03 16:27:49 -07001136 final Configuration currentDisplayConfig = getConfiguration();
1137 mTmpConfiguration.setTo(currentDisplayConfig);
1138 computeScreenConfiguration(mTmpConfiguration);
1139 configChanged |= currentDisplayConfig.diff(mTmpConfiguration) != 0;
1140
1141 if (configChanged) {
1142 mWaitingForConfig = true;
1143 mWmService.startFreezingDisplayLocked(0 /* exitAnim */, 0 /* enterAnim */, this);
1144 sendNewConfiguration();
1145 }
1146
1147 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Riddle Hsu4e611772018-10-31 18:58:28 +08001148 }
1149
Shivam Agrawal1d3db652019-07-01 15:26:11 -07001150 void sendNewConfiguration() {
Louis Chang2453d062019-11-19 22:30:48 +08001151 if (!isReady()) {
Shivam Agrawal6472e0e2019-07-03 16:27:49 -07001152 return;
1153 }
Evan Rosky69cace42019-09-20 16:28:13 -07001154 if (mDisplayRotation.isWaitingForRemoteRotation()) {
1155 return;
1156 }
Louis Chang2453d062019-11-19 22:30:48 +08001157 // TODO(display-merge): Remove cast
1158 final boolean configUpdated =
1159 ((ActivityDisplay) this).updateDisplayOverrideConfigurationLocked();
Shivam Agrawal6472e0e2019-07-03 16:27:49 -07001160 if (configUpdated) {
1161 return;
1162 }
1163 // Something changed (E.g. device rotation), but no configuration update is needed.
1164 // E.g. changing device rotation by 180 degrees. Go ahead and perform surface placement to
1165 // unfreeze the display since we froze it when the rotation was updated in
1166 // DisplayContent#updateRotationUnchecked.
1167 if (mWaitingForConfig) {
1168 mWaitingForConfig = false;
1169 mWmService.mLastFinishedFreezeSource = "config-unchanged";
1170 setLayoutNeeded();
1171 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Shivam Agrawal1d3db652019-07-01 15:26:11 -07001172 }
1173 }
1174
Garfield Tan90b04282018-12-11 14:04:42 -08001175 @Override
1176 boolean onDescendantOrientationChanged(IBinder freezeDisplayToken,
1177 ConfigurationContainer requestingContainer) {
Riddle Hsuccf09402019-08-13 00:33:06 +08001178 final Configuration config = updateOrientation(
1179 getRequestedOverrideConfiguration(), freezeDisplayToken, false /* forceUpdate */);
Garfield Tan49dae102019-02-04 09:51:59 -08001180 // If display rotation class tells us that it doesn't consider app requested orientation,
1181 // this display won't rotate just because of an app changes its requested orientation. Thus
1182 // it indicates that this display chooses not to handle this request.
1183 final boolean handled = getDisplayRotation().respectAppRequestedOrientation();
Garfield Tan90b04282018-12-11 14:04:42 -08001184 if (config == null) {
1185 return handled;
1186 }
1187
1188 if (handled && requestingContainer instanceof ActivityRecord) {
1189 final ActivityRecord activityRecord = (ActivityRecord) requestingContainer;
Louis Chang2453d062019-11-19 22:30:48 +08001190 // TODO(display-merge): Remove cast
1191 final boolean kept = ((ActivityDisplay) this).updateDisplayOverrideConfigurationLocked(
Shivam Agrawal1d3db652019-07-01 15:26:11 -07001192 config, activityRecord, false /* deferResume */, null /* result */);
Garfield Tan90b04282018-12-11 14:04:42 -08001193 activityRecord.frozenBeforeDestroy = true;
1194 if (!kept) {
1195 mWmService.mAtmService.mRootActivityContainer.resumeFocusedStacksTopActivities();
1196 }
1197 } else {
1198 // We have a new configuration to push so we need to update ATMS for now.
1199 // TODO: Clean up display configuration push between ATMS and WMS after unification.
Louis Chang2453d062019-11-19 22:30:48 +08001200 // TODO(display-merge): Remove cast
1201 ((ActivityDisplay) this.mDisplayContent).updateDisplayOverrideConfigurationLocked(
Shivam Agrawal1d3db652019-07-01 15:26:11 -07001202 config, null /* starting */, false /* deferResume */, null);
Garfield Tan90b04282018-12-11 14:04:42 -08001203 }
1204 return handled;
1205 }
1206
Garfield Tan49dae102019-02-04 09:51:59 -08001207 @Override
1208 boolean handlesOrientationChangeFromDescendant() {
1209 return getDisplayRotation().respectAppRequestedOrientation();
1210 }
1211
Riddle Hsu4e611772018-10-31 18:58:28 +08001212 /**
1213 * Determine the new desired orientation of this display.
1214 *
Riddle Hsuccf09402019-08-13 00:33:06 +08001215 * @see #getOrientation()
1216 * @return {@code true} if the orientation is changed and the caller should call
1217 * {@link #sendNewConfiguration} if the method returns {@code true}.
Riddle Hsu4e611772018-10-31 18:58:28 +08001218 */
Riddle Hsuccf09402019-08-13 00:33:06 +08001219 boolean updateOrientation() {
1220 return mDisplayRotation.updateOrientation(getOrientation(), false /* forceUpdate */);
Riddle Hsu4e611772018-10-31 18:58:28 +08001221 }
1222
Garfield Tan90b04282018-12-11 14:04:42 -08001223 /**
Riddle Hsuccf09402019-08-13 00:33:06 +08001224 * Update orientation of the display, returning a non-null new Configuration if it has
Garfield Tan90b04282018-12-11 14:04:42 -08001225 * changed from the current orientation. If a non-null configuration is returned, someone must
1226 * call {@link WindowManagerService#setNewDisplayOverrideConfiguration(Configuration,
1227 * DisplayContent)} to tell the window manager it can unfreeze the screen. This will typically
Riddle Hsuccf09402019-08-13 00:33:06 +08001228 * be done by calling {@link #sendNewConfiguration}.
1229 *
1230 * @param currentConfig The current requested override configuration (it is usually set from
1231 * the last {@link #sendNewConfiguration}) of the display. It is used to
1232 * check if the configuration container has the latest state.
1233 * @param freezeDisplayToken Freeze the app window token if the orientation is changed.
1234 * @param forceUpdate See {@link DisplayRotation#updateRotationUnchecked(boolean)}
Garfield Tan90b04282018-12-11 14:04:42 -08001235 */
Riddle Hsuccf09402019-08-13 00:33:06 +08001236 Configuration updateOrientation(Configuration currentConfig, IBinder freezeDisplayToken,
1237 boolean forceUpdate) {
Garfield Tan90b04282018-12-11 14:04:42 -08001238 if (!mDisplayReady) {
1239 return null;
1240 }
1241
1242 Configuration config = null;
Riddle Hsuccf09402019-08-13 00:33:06 +08001243 if (mDisplayRotation.updateOrientation(getOrientation(), forceUpdate)) {
Garfield Tan90b04282018-12-11 14:04:42 -08001244 // If we changed the orientation but mOrientationChangeComplete is already true,
1245 // we used seamless rotation, and we don't need to freeze the screen.
1246 if (freezeDisplayToken != null && !mWmService.mRoot.mOrientationChangeComplete) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07001247 final ActivityRecord activity = getActivityRecord(freezeDisplayToken);
1248 if (activity != null) {
1249 activity.startFreezingScreen();
Garfield Tan90b04282018-12-11 14:04:42 -08001250 }
1251 }
1252 config = new Configuration();
1253 computeScreenConfiguration(config);
1254 } else if (currentConfig != null) {
1255 // No obvious action we need to take, but if our current state mismatches the
1256 // activity manager's, update it, disregarding font scale, which should remain set
1257 // to the value of the previous configuration.
1258 // Here we're calling Configuration#unset() instead of setToDefaults() because we
1259 // need to keep override configs clear of non-empty values (e.g. fontSize).
1260 mTmpConfiguration.unset();
1261 mTmpConfiguration.updateFrom(currentConfig);
1262 computeScreenConfiguration(mTmpConfiguration);
1263 if (currentConfig.diff(mTmpConfiguration) != 0) {
1264 mWaitingForConfig = true;
1265 setLayoutNeeded();
Riddle Hsuccf09402019-08-13 00:33:06 +08001266 mDisplayRotation.prepareNormalRotationAnimation();
Garfield Tan90b04282018-12-11 14:04:42 -08001267 config = new Configuration(mTmpConfiguration);
1268 }
1269 }
1270
1271 return config;
1272 }
1273
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001274 /**
Andrii Kulian06d07d62017-03-14 11:11:47 -07001275 * Update rotation of the display.
1276 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001277 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
Riddle Hsuccf09402019-08-13 00:33:06 +08001278 * {@link #sendNewConfiguration} TO UNFREEZE THE SCREEN.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001279 */
Robert Carrae606b42018-02-15 15:36:23 -08001280 boolean updateRotationUnchecked() {
Riddle Hsuccf09402019-08-13 00:33:06 +08001281 return mDisplayRotation.updateRotationUnchecked(false /* forceUpdate */);
Evan Roskye747c3e2018-10-30 20:06:41 -07001282 }
1283
1284 /**
1285 * Applies the rotation transaction. This must be called after {@link #updateRotationUnchecked}
1286 * (if it returned {@code true}) to actually finish the rotation.
1287 *
1288 * @param oldRotation the rotation we are coming from.
1289 * @param rotation the rotation to apply.
1290 */
1291 void applyRotationLocked(final int oldRotation, final int rotation) {
Riddle Hsuccf09402019-08-13 00:33:06 +08001292 mDisplayRotation.applyCurrentRotation(rotation);
1293 final boolean rotateSeamlessly = mDisplayRotation.isRotatingSeamlessly();
Riddle Hsuccf09402019-08-13 00:33:06 +08001294 final Transaction transaction = getPendingTransaction();
Vadim Caenb3715832019-08-13 17:06:38 +02001295 ScreenRotationAnimation screenRotationAnimation = rotateSeamlessly
1296 ? null : getRotationAnimation();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001297 // We need to update our screen size information to match the new rotation. If the rotation
1298 // has actually changed then this method will return true and, according to the comment at
1299 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1300 // By updating the Display info here it will be available to
1301 // #computeScreenConfiguration() later.
Tiger Huanga817b1f2019-05-09 20:04:17 +08001302 updateDisplayAndOrientation(getConfiguration().uiMode, null /* outConfig */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001303
Robert Carrae606b42018-02-15 15:36:23 -08001304 // NOTE: We disable the rotation in the emulator because
1305 // it doesn't support hardware OpenGL emulation yet.
Vadim Caenba4fd6c2019-08-05 16:45:46 +02001306 if (screenRotationAnimation != null && screenRotationAnimation.hasScreenshot()) {
Riddle Hsuccf09402019-08-13 00:33:06 +08001307 screenRotationAnimation.setRotation(transaction, rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001308 }
1309
Vishnu Nair83537a72018-07-19 21:27:48 -07001310 forAllWindows(w -> {
Riddle Hsuccf09402019-08-13 00:33:06 +08001311 w.seamlesslyRotateIfAllowed(transaction, oldRotation, rotation, rotateSeamlessly);
Vishnu Nair83537a72018-07-19 21:27:48 -07001312 }, true /* traverseTopToBottom */);
1313
Riddle Hsuccf09402019-08-13 00:33:06 +08001314 mWmService.mDisplayManagerInternal.performTraversal(transaction);
Robert Carrae606b42018-02-15 15:36:23 -08001315 scheduleAnimation();
1316
Andrii Kulian06d07d62017-03-14 11:11:47 -07001317 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001318 if (w.mHasSurface && !rotateSeamlessly) {
Adrian Roosb125e0b2019-10-02 14:55:14 +02001319 ProtoLog.v(WM_DEBUG_ORIENTATION, "Set mOrientationChanging of %s", w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001320 w.setOrientationChanging(true);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001321 mWmService.mRoot.mOrientationChangeComplete = false;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001322 w.mLastFreezeDuration = 0;
1323 }
1324 w.mReportOrientationChanged = true;
1325 }, true /* traverseTopToBottom */);
1326
1327 if (rotateSeamlessly) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001328 mWmService.mH.sendNewMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
Riddle Hsu654a6f92018-07-13 22:59:36 +08001329 this, SEAMLESS_ROTATION_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001330 }
1331
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001332 for (int i = mWmService.mRotationWatchers.size() - 1; i >= 0; i--) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001333 final WindowManagerService.RotationWatcher rotationWatcher
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001334 = mWmService.mRotationWatchers.get(i);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001335 if (rotationWatcher.mDisplayId == mDisplayId) {
1336 try {
1337 rotationWatcher.mWatcher.onRotationChanged(rotation);
1338 } catch (RemoteException e) {
1339 // Ignore
1340 }
1341 }
1342 }
1343
Andrii Kulian06d07d62017-03-14 11:11:47 -07001344 // Announce rotation only if we will not animate as we already have the
1345 // windows in final state. Otherwise, we make this call at the rotation end.
Rhed Jao02655dc2018-10-30 20:44:52 +08001346 if (screenRotationAnimation == null && mWmService.mAccessibilityController != null) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001347 mWmService.mAccessibilityController.onRotationChangedLocked(this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001348 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001349 }
1350
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001351 void configureDisplayPolicy() {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001352 final int width = mBaseDisplayWidth;
1353 final int height = mBaseDisplayHeight;
1354 final int shortSize;
1355 final int longSize;
1356 if (width > height) {
1357 shortSize = height;
1358 longSize = width;
1359 } else {
1360 shortSize = width;
1361 longSize = height;
1362 }
1363
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02001364 final int shortSizeDp = shortSize * DENSITY_DEFAULT / mBaseDisplayDensity;
1365 final int longSizeDp = longSize * DENSITY_DEFAULT / mBaseDisplayDensity;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001366
Winson Chung4723b4e2019-03-25 16:49:36 -07001367 mDisplayPolicy.updateConfigurationAndScreenSizeDependentBehaviors();
Tiger Huang3d2b8982019-01-29 22:56:48 +08001368 mDisplayRotation.configure(width, height, shortSizeDp, longSizeDp);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001369
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001370 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
1371 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Tiger Huang86e6d072019-05-02 20:23:47 +08001372
1373 // Not much of use to rotate the display for apps since it's close to square.
1374 mIgnoreRotationForApps = isNonDecorDisplayCloseToSquare(Surface.ROTATION_0, width, height);
1375 }
1376
1377 private boolean isNonDecorDisplayCloseToSquare(int rotation, int width, int height) {
1378 final DisplayCutout displayCutout =
1379 calculateDisplayCutoutForRotation(rotation).getDisplayCutout();
1380 final int uiMode = mWmService.mPolicy.getUiMode();
1381 final int w = mDisplayPolicy.getNonDecorDisplayWidth(
1382 width, height, rotation, uiMode, displayCutout);
1383 final int h = mDisplayPolicy.getNonDecorDisplayHeight(
1384 width, height, rotation, uiMode, displayCutout);
1385 final float aspectRatio = Math.max(w, h) / (float) Math.min(w, h);
1386 return aspectRatio <= mCloseToSquareMaxAspectRatio;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001387 }
1388
Andrii Kulian06d07d62017-03-14 11:11:47 -07001389 /**
1390 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1391 * changed.
1392 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1393 */
Tiger Huanga817b1f2019-05-09 20:04:17 +08001394 private DisplayInfo updateDisplayAndOrientation(int uiMode, Configuration outConfig) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001395 // Use the effective "visual" dimensions based on current rotation
Riddle Hsuccf09402019-08-13 00:33:06 +08001396 final int rotation = getRotation();
1397 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Garfield Tan4bb83472019-01-16 14:37:04 -08001398 final int dw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1399 final int dh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001400
1401 // Update application display metrics.
Riddle Hsuccf09402019-08-13 00:33:06 +08001402 final WmDisplayCutout wmDisplayCutout = calculateDisplayCutoutForRotation(rotation);
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001403 final DisplayCutout displayCutout = wmDisplayCutout.getDisplayCutout();
1404
Riddle Hsuccf09402019-08-13 00:33:06 +08001405 final int appWidth = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001406 displayCutout);
Riddle Hsuccf09402019-08-13 00:33:06 +08001407 final int appHeight = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001408 displayCutout);
Riddle Hsuccf09402019-08-13 00:33:06 +08001409 mDisplayInfo.rotation = rotation;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001410 mDisplayInfo.logicalWidth = dw;
1411 mDisplayInfo.logicalHeight = dh;
1412 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1413 mDisplayInfo.appWidth = appWidth;
1414 mDisplayInfo.appHeight = appHeight;
1415 if (isDefaultDisplay) {
1416 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1417 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1418 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001419 mDisplayInfo.displayCutout = displayCutout.isEmpty() ? null : displayCutout;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001420 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1421 if (mDisplayScalingDisabled) {
1422 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1423 } else {
1424 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1425 }
1426
Tiger Huanga817b1f2019-05-09 20:04:17 +08001427 computeSizeRangesAndScreenLayout(mDisplayInfo, rotated, uiMode, dw, dh,
1428 mDisplayMetrics.density, outConfig);
1429
Andrii Kulianf0379de2018-03-14 16:24:07 -07001430 // We usually set the override info in DisplayManager so that we get consistent display
1431 // metrics values when displays are changing and don't send out new values until WM is aware
1432 // of them. However, we don't do this for displays that serve as containers for ActivityView
1433 // because we don't want letter-/pillar-boxing during resize.
1434 final DisplayInfo overrideDisplayInfo = mShouldOverrideDisplayConfiguration
1435 ? mDisplayInfo : null;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001436 mWmService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
Andrii Kulianf0379de2018-03-14 16:24:07 -07001437 overrideDisplayInfo);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001438
1439 mBaseDisplayRect.set(0, 0, dw, dh);
1440
1441 if (isDefaultDisplay) {
1442 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1443 mCompatDisplayMetrics);
1444 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001445
Andrii Kulian06d07d62017-03-14 11:11:47 -07001446 return mDisplayInfo;
1447 }
1448
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001449 WmDisplayCutout calculateDisplayCutoutForRotation(int rotation) {
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001450 return mDisplayCutoutCache.getOrCompute(mInitialDisplayCutout, rotation);
1451 }
1452
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001453 private WmDisplayCutout calculateDisplayCutoutForRotationUncached(
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001454 DisplayCutout cutout, int rotation) {
Adrian Roos24264212018-02-19 16:26:15 +01001455 if (cutout == null || cutout == DisplayCutout.NO_CUTOUT) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001456 return WmDisplayCutout.NO_CUTOUT;
Adrian Roos1cf585052018-01-03 18:43:27 +01001457 }
Adrian Roos11c25582018-02-19 18:06:36 +01001458 if (rotation == ROTATION_0) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001459 return WmDisplayCutout.computeSafeInsets(
1460 cutout, mInitialDisplayWidth, mInitialDisplayHeight);
Adrian Roos24264212018-02-19 16:26:15 +01001461 }
Adrian Roosbed538e2018-02-21 17:50:07 +01001462 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Issei Suzuki43190bd2018-08-20 17:28:41 +02001463 final Rect[] newBounds = mRotationUtil.getRotatedBounds(
1464 WmDisplayCutout.computeSafeInsets(
Jorim Jaggi60640512018-06-29 01:14:31 +02001465 cutout, mInitialDisplayWidth, mInitialDisplayHeight)
Issei Suzuki43190bd2018-08-20 17:28:41 +02001466 .getDisplayCutout().getBoundingRectsAll(),
1467 rotation, mInitialDisplayWidth, mInitialDisplayHeight);
1468 return WmDisplayCutout.computeSafeInsets(DisplayCutout.fromBounds(newBounds),
Adrian Roos24264212018-02-19 16:26:15 +01001469 rotated ? mInitialDisplayHeight : mInitialDisplayWidth,
1470 rotated ? mInitialDisplayWidth : mInitialDisplayHeight);
Adrian Roos1cf585052018-01-03 18:43:27 +01001471 }
1472
Andrii Kulian06d07d62017-03-14 11:11:47 -07001473 /**
1474 * Compute display configuration based on display properties and policy settings.
1475 * Do not call if mDisplayReady == false.
1476 */
1477 void computeScreenConfiguration(Configuration config) {
Tiger Huanga817b1f2019-05-09 20:04:17 +08001478 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode, config);
Evan Roskye747c3e2018-10-30 20:06:41 -07001479 calculateBounds(displayInfo, mTmpBounds);
1480 config.windowConfiguration.setBounds(mTmpBounds);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001481
1482 final int dw = displayInfo.logicalWidth;
1483 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001484 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
Garfield Tane0846042018-07-26 13:42:04 -07001485 config.windowConfiguration.setWindowingMode(getWindowingMode());
Yunfan Chen7daa6ac2018-11-29 18:16:44 -08001486 config.windowConfiguration.setDisplayWindowingMode(getWindowingMode());
Evan Roskye747c3e2018-10-30 20:06:41 -07001487 config.windowConfiguration.setRotation(displayInfo.rotation);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001488
Adrian Roos11c25582018-02-19 18:06:36 +01001489 final float density = mDisplayMetrics.density;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001490 config.screenWidthDp =
Tiger Huang7c610aa2018-10-27 00:01:01 +08001491 (int)(mDisplayPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1492 config.uiMode, displayInfo.displayCutout) / density);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001493 config.screenHeightDp =
Tiger Huang7c610aa2018-10-27 00:01:01 +08001494 (int)(mDisplayPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1495 config.uiMode, displayInfo.displayCutout) / density);
Bryce Lee7566d762017-03-30 09:34:15 -07001496
Tiger Huang7c610aa2018-10-27 00:01:01 +08001497 mDisplayPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh,
Adrian Roos11c25582018-02-19 18:06:36 +01001498 displayInfo.displayCutout, mTmpRect);
Bryce Lee7566d762017-03-30 09:34:15 -07001499 final int leftInset = mTmpRect.left;
1500 final int topInset = mTmpRect.top;
1501 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001502 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1503 leftInset + displayInfo.appWidth /* right */,
1504 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001505 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1506 || displayInfo.rotation == Surface.ROTATION_270);
1507
Andrii Kulian06d07d62017-03-14 11:11:47 -07001508 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1509 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1510 ? Configuration.SCREENLAYOUT_ROUND_YES
1511 : Configuration.SCREENLAYOUT_ROUND_NO);
1512
1513 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1514 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1515 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001516 dh, displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001517 config.densityDpi = displayInfo.logicalDensityDpi;
1518
1519 config.colorMode =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001520 ((displayInfo.isHdr() && mWmService.hasHdrSupport())
Andrii Kulian06d07d62017-03-14 11:11:47 -07001521 ? Configuration.COLOR_MODE_HDR_YES
1522 : Configuration.COLOR_MODE_HDR_NO)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001523 | (displayInfo.isWideColorGamut() && mWmService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001524 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1525 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1526
1527 // Update the configuration based on available input devices, lid switch,
1528 // and platform configuration.
1529 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1530 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1531 config.navigation = Configuration.NAVIGATION_NONAV;
1532
1533 int keyboardPresence = 0;
1534 int navigationPresence = 0;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001535 final InputDevice[] devices = mWmService.mInputManager.getInputDevices();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001536 final int len = devices != null ? devices.length : 0;
1537 for (int i = 0; i < len; i++) {
1538 InputDevice device = devices[i];
Arthur Hung82bbfc32018-11-29 20:24:51 +08001539 // Ignore virtual input device.
1540 if (device.isVirtual()) {
1541 continue;
1542 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001543
Arthur Hung82bbfc32018-11-29 20:24:51 +08001544 // Check if input device can dispatch events to current display.
1545 // If display type is virtual, will follow the default display.
1546 if (!mWmService.mInputManager.canDispatchToDisplay(device.getId(),
1547 displayInfo.type == Display.TYPE_VIRTUAL ? DEFAULT_DISPLAY : mDisplayId)) {
1548 continue;
1549 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001550
Arthur Hung82bbfc32018-11-29 20:24:51 +08001551 final int sources = device.getSources();
1552 final int presenceFlag = device.isExternal()
1553 ? WindowManagerPolicy.PRESENCE_EXTERNAL : WindowManagerPolicy.PRESENCE_INTERNAL;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001554
Arthur Hung82bbfc32018-11-29 20:24:51 +08001555 if (mWmService.mIsTouchDevice) {
1556 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) == InputDevice.SOURCE_TOUCHSCREEN) {
1557 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001558 }
Arthur Hung82bbfc32018-11-29 20:24:51 +08001559 } else {
1560 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1561 }
1562
1563 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1564 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1565 navigationPresence |= presenceFlag;
1566 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1567 && config.navigation == Configuration.NAVIGATION_NONAV) {
1568 config.navigation = Configuration.NAVIGATION_DPAD;
1569 navigationPresence |= presenceFlag;
1570 }
1571
1572 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1573 config.keyboard = Configuration.KEYBOARD_QWERTY;
1574 keyboardPresence |= presenceFlag;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001575 }
1576 }
1577
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001578 if (config.navigation == Configuration.NAVIGATION_NONAV && mWmService.mHasPermanentDpad) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001579 config.navigation = Configuration.NAVIGATION_DPAD;
1580 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1581 }
1582
1583 // Determine whether a hard keyboard is available and enabled.
1584 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1585 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001586 if (hardKeyboardAvailable != mWmService.mHardKeyboardAvailable) {
1587 mWmService.mHardKeyboardAvailable = hardKeyboardAvailable;
1588 mWmService.mH.removeMessages(REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1589 mWmService.mH.sendEmptyMessage(REPORT_HARD_KEYBOARD_STATUS_CHANGE);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001590 }
1591
Winson Chung4723b4e2019-03-25 16:49:36 -07001592 mDisplayPolicy.updateConfigurationAndScreenSizeDependentBehaviors();
Tiger Huang7c610aa2018-10-27 00:01:01 +08001593
Andrii Kulian06d07d62017-03-14 11:11:47 -07001594 // Let the policy update hidden states.
1595 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1596 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1597 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001598 mWmService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001599 }
1600
1601 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001602 DisplayCutout displayCutout) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001603 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1604 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1605 final int unrotDw, unrotDh;
1606 if (rotated) {
1607 unrotDw = dh;
1608 unrotDh = dw;
1609 } else {
1610 unrotDw = dw;
1611 unrotDh = dh;
1612 }
1613 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001614 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001615 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001616 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001617 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001618 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001619 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001620 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001621 return sw;
1622 }
1623
1624 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001625 DisplayMetrics dm, int dw, int dh, DisplayCutout displayCutout) {
1626 dm.noncompatWidthPixels = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1627 displayCutout);
1628 dm.noncompatHeightPixels = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode,
1629 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001630 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1631 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1632 if (curSize == 0 || size < curSize) {
1633 curSize = size;
1634 }
1635 return curSize;
1636 }
1637
Tiger Huang7c610aa2018-10-27 00:01:01 +08001638 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, boolean rotated,
1639 int uiMode, int dw, int dh, float density, Configuration outConfig) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001640
1641 // We need to determine the smallest width that will occur under normal
1642 // operation. To this, start with the base screen size and compute the
1643 // width under the different possible rotations. We need to un-rotate
1644 // the current screen dimensions before doing this.
1645 int unrotDw, unrotDh;
1646 if (rotated) {
1647 unrotDw = dh;
1648 unrotDh = dw;
1649 } else {
1650 unrotDw = dw;
1651 unrotDh = dh;
1652 }
1653 displayInfo.smallestNominalAppWidth = 1<<30;
1654 displayInfo.smallestNominalAppHeight = 1<<30;
1655 displayInfo.largestNominalAppWidth = 0;
1656 displayInfo.largestNominalAppHeight = 0;
Tiger Huang7c610aa2018-10-27 00:01:01 +08001657 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_0, uiMode, unrotDw, unrotDh);
1658 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_90, uiMode, unrotDh, unrotDw);
1659 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_180, uiMode, unrotDw, unrotDh);
1660 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_270, uiMode, unrotDh, unrotDw);
Tiger Huanga817b1f2019-05-09 20:04:17 +08001661
1662 if (outConfig == null) {
1663 return;
1664 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001665 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1666 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001667 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001668 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001669 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001670 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001671 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001672 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001673 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001674 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1675 outConfig.screenLayout = sl;
1676 }
1677
1678 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001679 int uiMode, DisplayCutout displayCutout) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001680 // Get the app screen size at this rotation.
Tiger Huang7c610aa2018-10-27 00:01:01 +08001681 int w = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayCutout);
1682 int h = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001683
1684 // Compute the screen layout size class for this rotation.
1685 int longSize = w;
1686 int shortSize = h;
1687 if (longSize < shortSize) {
1688 int tmp = longSize;
1689 longSize = shortSize;
1690 shortSize = tmp;
1691 }
1692 longSize = (int)(longSize/density);
1693 shortSize = (int)(shortSize/density);
1694 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1695 }
1696
Tiger Huang7c610aa2018-10-27 00:01:01 +08001697 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int rotation,
Andrii Kulian06d07d62017-03-14 11:11:47 -07001698 int uiMode, int dw, int dh) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001699 final DisplayCutout displayCutout = calculateDisplayCutoutForRotation(
1700 rotation).getDisplayCutout();
Tiger Huang7c610aa2018-10-27 00:01:01 +08001701 final int width = mDisplayPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1702 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001703 if (width < displayInfo.smallestNominalAppWidth) {
1704 displayInfo.smallestNominalAppWidth = width;
1705 }
1706 if (width > displayInfo.largestNominalAppWidth) {
1707 displayInfo.largestNominalAppWidth = width;
1708 }
Tiger Huang7c610aa2018-10-27 00:01:01 +08001709 final int height = mDisplayPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1710 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001711 if (height < displayInfo.smallestNominalAppHeight) {
1712 displayInfo.smallestNominalAppHeight = height;
1713 }
1714 if (height > displayInfo.largestNominalAppHeight) {
1715 displayInfo.largestNominalAppHeight = height;
1716 }
1717 }
1718
Riddle Hsua4d6fa22018-08-11 00:50:39 +08001719 /**
1720 * Apps that use the compact menu panel (as controlled by the panelMenuIsCompact
1721 * theme attribute) on devices that feature a physical options menu key attempt to position
1722 * their menu panel window along the edge of the screen nearest the physical menu key.
1723 * This lowers the travel distance between invoking the menu panel and selecting
1724 * a menu option.
1725 *
1726 * This method helps control where that menu is placed. Its current implementation makes
1727 * assumptions about the menu key and its relationship to the screen based on whether
1728 * the device's natural orientation is portrait (width < height) or landscape.
1729 *
1730 * The menu key is assumed to be located along the bottom edge of natural-portrait
1731 * devices and along the right edge of natural-landscape devices. If these assumptions
1732 * do not hold for the target device, this method should be changed to reflect that.
1733 *
1734 * @return A {@link Gravity} value for placing the options menu window.
1735 */
1736 int getPreferredOptionsPanelGravity() {
1737 final int rotation = getRotation();
1738 if (mInitialDisplayWidth < mInitialDisplayHeight) {
1739 // On devices with a natural orientation of portrait.
1740 switch (rotation) {
1741 default:
1742 case Surface.ROTATION_0:
1743 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1744 case Surface.ROTATION_90:
1745 return Gravity.RIGHT | Gravity.BOTTOM;
1746 case Surface.ROTATION_180:
1747 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1748 case Surface.ROTATION_270:
1749 return Gravity.START | Gravity.BOTTOM;
1750 }
1751 }
1752
1753 // On devices with a natural orientation of landscape.
1754 switch (rotation) {
1755 default:
1756 case Surface.ROTATION_0:
1757 return Gravity.RIGHT | Gravity.BOTTOM;
1758 case Surface.ROTATION_90:
1759 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1760 case Surface.ROTATION_180:
1761 return Gravity.START | Gravity.BOTTOM;
1762 case Surface.ROTATION_270:
1763 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1764 }
1765 }
1766
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001767 DockedStackDividerController getDockedDividerController() {
1768 return mDividerControllerLocked;
1769 }
1770
Winson Chung655332c2016-10-31 13:14:28 -07001771 PinnedStackController getPinnedStackController() {
1772 return mPinnedStackControllerLocked;
1773 }
1774
Jeff Browna506a6e2013-06-04 00:02:38 -07001775 /**
1776 * Returns true if the specified UID has access to this display.
1777 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001778 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001779 return mDisplay.hasAccess(uid);
1780 }
1781
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001782 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001783 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001784 }
1785
Louis Changdc077272019-11-12 16:52:56 +08001786 ActivityStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001787 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001788 }
1789
Louis Chang2453d062019-11-19 22:30:48 +08001790 ActivityStack getRecentsStack() {
1791 return mTaskStackContainers.getRecentsStack();
1792 }
1793
Wale Ogunwale61911492017-10-11 08:50:50 -07001794 /**
Louis Chang2453d062019-11-19 22:30:48 +08001795 * @return The primary split-screen stack, and {@code null} otherwise.
Wale Ogunwale61911492017-10-11 08:50:50 -07001796 */
Louis Changdc077272019-11-12 16:52:56 +08001797 ActivityStack getSplitScreenPrimaryStack() {
Louis Chang2453d062019-11-19 22:30:48 +08001798 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001799 }
1800
Robert Carr9785cf32018-04-25 15:06:07 -07001801 boolean hasSplitScreenPrimaryStack() {
1802 return getSplitScreenPrimaryStack() != null;
1803 }
1804
Wale Ogunwale61911492017-10-11 08:50:50 -07001805 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001806 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001807 * not visible.
1808 */
Louis Changdc077272019-11-12 16:52:56 +08001809 ActivityStack getSplitScreenPrimaryStackIgnoringVisibility() {
Matthew Ng64e77cf2017-10-31 14:01:31 -07001810 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001811 }
1812
Louis Changdc077272019-11-12 16:52:56 +08001813 ActivityStack getPinnedStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001814 return mTaskStackContainers.getPinnedStack();
1815 }
1816
Riddle Hsuccf09402019-08-13 00:33:06 +08001817 boolean hasPinnedStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001818 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001819 }
1820
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001821 /**
1822 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1823 * Null is no compatible stack on the display.
1824 */
Louis Changdc077272019-11-12 16:52:56 +08001825 ActivityStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001826 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1827 }
1828
1829 /**
1830 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1831 * activity type. Null is no compatible stack on the display.
1832 */
Louis Changdc077272019-11-12 16:52:56 +08001833 ActivityStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001834 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001835 }
1836
Louis Chang2453d062019-11-19 22:30:48 +08001837 protected int getStackCount() {
1838 return mTaskStackContainers.mChildren.size();
1839 }
1840
1841 protected ActivityStack getStackAt(int index) {
1842 return mTaskStackContainers.mChildren.get(index);
1843 }
1844
1845 int getIndexOf(ActivityStack stack) {
1846 return mTaskStackContainers.getIndexOf(stack);
1847 }
1848
1849 void removeStack(ActivityStack stack) {
1850 mTaskStackContainers.removeChild(stack);
1851 }
1852
Bryce Lee48f4b572017-04-10 10:54:15 -07001853 @VisibleForTesting
Louis Changdc077272019-11-12 16:52:56 +08001854 WindowList<ActivityStack> getStacks() {
Kazuki Takise148d00a2018-05-31 15:32:19 +09001855 return mTaskStackContainers.mChildren;
1856 }
1857
1858 @VisibleForTesting
Louis Changdc077272019-11-12 16:52:56 +08001859 ActivityStack getTopStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001860 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001861 }
1862
Winson Chunge2d72172018-01-25 17:46:20 +00001863 ArrayList<Task> getVisibleTasks() {
1864 return mTaskStackContainers.getVisibleTasks();
1865 }
1866
Louis Changdc077272019-11-12 16:52:56 +08001867 void onStackWindowingModeChanged(ActivityStack stack) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001868 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001869 }
1870
Andrii Kulian441e4492016-09-29 15:25:00 -07001871 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001872 public void onConfigurationChanged(Configuration newParentConfig) {
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -05001873 final int lastOrientation = getConfiguration().orientation;
Andrii Kulian441e4492016-09-29 15:25:00 -07001874 super.onConfigurationChanged(newParentConfig);
Tiger Huang7c610aa2018-10-27 00:01:01 +08001875 if (mDisplayPolicy != null) {
1876 mDisplayPolicy.onConfigurationChanged();
1877 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001878
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -05001879 if (lastOrientation != getConfiguration().orientation) {
1880 getMetricsLogger().write(
1881 new LogMaker(MetricsEvent.ACTION_PHONE_ORIENTATION_CHANGED)
1882 .setSubtype(getConfiguration().orientation)
1883 .addTaggedData(MetricsEvent.FIELD_DISPLAY_ID, getDisplayId()));
1884 }
1885
Evan Roskye747c3e2018-10-30 20:06:41 -07001886 // If there was no pinned stack, we still need to notify the controller of the display info
1887 // update as a result of the config change.
1888 if (mPinnedStackControllerLocked != null && !hasPinnedStack()) {
1889 mPinnedStackControllerLocked.onDisplayInfoChanged(getDisplayInfo());
1890 }
Evan Roskye747c3e2018-10-30 20:06:41 -07001891 }
1892
1893 /**
1894 * Updates the resources used by docked/pinned controllers. This needs to be called at the
1895 * beginning of a configuration update cascade since the metrics from these resources are used
1896 * for bounds calculations. Since ActivityDisplay initiates the configuration update, this
1897 * should be called from there instead of DisplayContent's onConfigurationChanged.
1898 */
1899 void preOnConfigurationChanged() {
Bryce Leef3c6a472017-11-14 14:53:06 -08001900 final DockedStackDividerController dividerController = getDockedDividerController();
1901
1902 if (dividerController != null) {
1903 getDockedDividerController().onConfigurationChanged();
1904 }
1905
1906 final PinnedStackController pinnedStackController = getPinnedStackController();
1907
1908 if (pinnedStackController != null) {
1909 getPinnedStackController().onConfigurationChanged();
1910 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001911 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001912
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001913 @Override
1914 boolean fillsParent() {
1915 return true;
1916 }
1917
1918 @Override
1919 boolean isVisible() {
1920 return true;
1921 }
1922
1923 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001924 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001925 super.onAppTransitionDone();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001926 mWmService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001927 }
1928
Yunfan Chen7daa6ac2018-11-29 18:16:44 -08001929 @Override
1930 public void setWindowingMode(int windowingMode) {
1931 super.setWindowingMode(windowingMode);
1932 super.setDisplayWindowingMode(windowingMode);
1933 }
1934
1935 @Override
1936 void setDisplayWindowingMode(int windowingMode) {
1937 setWindowingMode(windowingMode);
1938 }
1939
Robert Carr9785cf32018-04-25 15:06:07 -07001940 /**
1941 * In split-screen mode we process the IME containers above the docked divider
1942 * rather than directly above their target.
1943 */
1944 private boolean skipTraverseChild(WindowContainer child) {
lumarkff0ab692018-11-05 20:32:30 +08001945 if (child == mImeWindowsContainers && mInputMethodTarget != null
Robert Carr9785cf32018-04-25 15:06:07 -07001946 && !hasSplitScreenPrimaryStack()) {
1947 return true;
1948 }
1949 return false;
1950 }
1951
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001952 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001953 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1954 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1955 // target, or here in order if there isn't an IME target.
1956 if (traverseTopToBottom) {
1957 for (int i = mChildren.size() - 1; i >= 0; --i) {
1958 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001959 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001960 continue;
1961 }
Robert Carr9785cf32018-04-25 15:06:07 -07001962
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001963 if (child.forAllWindows(callback, traverseTopToBottom)) {
1964 return true;
1965 }
1966 }
1967 } else {
1968 final int count = mChildren.size();
1969 for (int i = 0; i < count; i++) {
1970 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001971 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001972 continue;
1973 }
Robert Carr9785cf32018-04-25 15:06:07 -07001974
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001975 if (child.forAllWindows(callback, traverseTopToBottom)) {
1976 return true;
1977 }
1978 }
1979 }
1980 return false;
1981 }
1982
1983 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1984 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1985 }
1986
Riddle Hsuccf09402019-08-13 00:33:06 +08001987 /**
1988 * In the general case, the orientation is computed from the above app windows first. If none of
1989 * the above app windows specify orientation, the orientation is computed from the child window
1990 * container, e.g. {@link AppWindowToken#getOrientation(int)}.
1991 */
Wale Ogunwale399c8692017-05-08 14:22:42 -07001992 @Override
1993 int getOrientation() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001994 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001995
Tiger Huang86e6d072019-05-02 20:23:47 +08001996 if (mIgnoreRotationForApps) {
1997 return SCREEN_ORIENTATION_USER;
1998 }
1999
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002000 if (mWmService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08002001 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Adrian Roosb125e0b2019-10-02 14:55:14 +02002002 ProtoLog.v(WM_DEBUG_ORIENTATION,
2003 "Display id=%d is frozen, return %d", mDisplayId,
2004 mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002005 // If the display is frozen, some activities may be in the middle of restarting, and
2006 // thus have removed their old window. If the window has the flag to hide the lock
2007 // screen, then the lock screen can re-appear and inflict its own orientation on us.
2008 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08002009 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002010 } else if (policy.isKeyguardLocked()) {
2011 // Use the last orientation the while the display is frozen with the keyguard
2012 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
2013 // window. We don't want to check the show when locked window directly though as
2014 // things aren't stable while the display is frozen, for example the window could be
2015 // momentarily unavailable due to activity relaunch.
Adrian Roosb125e0b2019-10-02 14:55:14 +02002016 ProtoLog.v(WM_DEBUG_ORIENTATION,
2017 "Display id=%d is frozen while keyguard locked, return %d",
2018 mDisplayId, getLastOrientation());
Riddle Hsuccf09402019-08-13 00:33:06 +08002019 return getLastOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07002020 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002021 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002022 final int orientation = mAboveAppWindowsContainers.getOrientation();
2023 if (orientation != SCREEN_ORIENTATION_UNSET) {
2024 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002025 }
Wale Ogunwale51362492016-09-08 17:49:17 -07002026 }
2027
Wale Ogunwale399c8692017-05-08 14:22:42 -07002028 // Top system windows are not requesting an orientation. Start searching from apps.
2029 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07002030 }
2031
Craig Mautner46ac6fa2013-08-01 10:06:34 -07002032 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07002033 // Check if display metrics changed and update base values if needed.
2034 updateBaseDisplayMetricsIfNeeded();
2035
Craig Mautner722285e2012-09-07 13:55:58 -07002036 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07002037 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07002038
Garfield Tan2f145f22018-11-01 15:27:03 -07002039 onDisplayChanged(this);
Craig Mautner722285e2012-09-07 13:55:58 -07002040 }
2041
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02002042 @Override
2043 void onDisplayChanged(DisplayContent dc) {
2044 super.onDisplayChanged(dc);
2045 updateSystemGestureExclusionLimit();
2046 }
2047
2048 void updateSystemGestureExclusionLimit() {
Adrian Roos1c2e9a12019-08-20 18:23:47 +02002049 mSystemGestureExclusionLimit = mWmService.mConstants.mSystemGestureExclusionLimitDp
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02002050 * mDisplayMetrics.densityDpi / DENSITY_DEFAULT;
2051 updateSystemGestureExclusion();
2052 }
2053
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002054 void initializeDisplayBaseInfo() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002055 final DisplayManagerInternal displayManagerInternal = mWmService.mDisplayManagerInternal;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002056 if (displayManagerInternal != null) {
2057 // Bootstrap the default logical display from the display manager.
2058 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
2059 if (newDisplayInfo != null) {
2060 mDisplayInfo.copyFrom(newDisplayInfo);
2061 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002062 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002063
Andrii Kuliancd097992017-03-23 18:31:59 -07002064 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
2065 mDisplayInfo.logicalDensityDpi);
2066 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
2067 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
2068 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002069 mInitialDisplayCutout = mDisplayInfo.displayCutout;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002070 }
2071
Andrii Kuliancd097992017-03-23 18:31:59 -07002072 /**
2073 * If display metrics changed, overrides are not set and it's not just a rotation - update base
2074 * values.
2075 */
2076 private void updateBaseDisplayMetricsIfNeeded() {
2077 // Get real display metrics without overrides from WM.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002078 mWmService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
Andrii Kuliancd097992017-03-23 18:31:59 -07002079 final int orientation = mDisplayInfo.rotation;
2080 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2081 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
2082 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
2083 final int newDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002084 final DisplayCutout newCutout = mDisplayInfo.displayCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07002085
2086 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
2087 || mInitialDisplayHeight != newHeight
Adrian Roos1cf585052018-01-03 18:43:27 +01002088 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi
2089 || !Objects.equals(mInitialDisplayCutout, newCutout);
Andrii Kuliancd097992017-03-23 18:31:59 -07002090
2091 if (displayMetricsChanged) {
2092 // Check if display size or density is forced.
2093 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
2094 || mBaseDisplayHeight != mInitialDisplayHeight;
2095 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
2096
2097 // If there is an override set for base values - use it, otherwise use new values.
2098 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
2099 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
2100 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
2101
2102 // Real display metrics changed, so we should also update initial values.
2103 mInitialDisplayWidth = newWidth;
2104 mInitialDisplayHeight = newHeight;
2105 mInitialDisplayDensity = newDensity;
Adrian Roos1cf585052018-01-03 18:43:27 +01002106 mInitialDisplayCutout = newCutout;
Shivam Agrawal6472e0e2019-07-03 16:27:49 -07002107 reconfigureDisplayLocked();
Andrii Kuliancd097992017-03-23 18:31:59 -07002108 }
2109 }
2110
Bryce Lee27cec322017-03-21 09:41:37 -07002111 /** Sets the maximum width the screen resolution can be */
2112 void setMaxUiWidth(int width) {
2113 if (DEBUG_DISPLAY) {
2114 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
2115 }
2116
2117 mMaxUiWidth = width;
2118
2119 // Update existing metrics.
2120 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
2121 }
2122
2123 /** Update base (override) display metrics. */
2124 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
2125 mBaseDisplayWidth = baseWidth;
2126 mBaseDisplayHeight = baseHeight;
2127 mBaseDisplayDensity = baseDensity;
2128
2129 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
2130 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
2131 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
2132 mBaseDisplayWidth = mMaxUiWidth;
2133
2134 if (DEBUG_DISPLAY) {
2135 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
2136 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
2137 + " on display:" + getDisplayId());
2138 }
2139 }
2140
2141 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08002142
2143 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07002144 }
2145
Riddle Hsuf53da812018-08-15 22:00:27 +08002146 /**
2147 * Forces this display to use the specified density.
2148 *
2149 * @param density The density in DPI to use. If the value equals to initial density, the setting
2150 * will be cleared.
2151 * @param userId The target user to apply. Only meaningful when this is default display. If the
2152 * user id is {@link UserHandle#USER_CURRENT}, it means to apply current settings
2153 * so only need to configure display.
2154 */
2155 void setForcedDensity(int density, int userId) {
Riddle Hsuf53da812018-08-15 22:00:27 +08002156 final boolean updateCurrent = userId == UserHandle.USER_CURRENT;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002157 if (mWmService.mCurrentUserId == userId || updateCurrent) {
Riddle Hsuf53da812018-08-15 22:00:27 +08002158 mBaseDisplayDensity = density;
Shivam Agrawal6472e0e2019-07-03 16:27:49 -07002159 reconfigureDisplayLocked();
Riddle Hsuf53da812018-08-15 22:00:27 +08002160 }
2161 if (updateCurrent) {
2162 // We are applying existing settings so no need to save it again.
2163 return;
2164 }
2165
2166 if (density == mInitialDisplayDensity) {
2167 density = 0;
2168 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002169 mWmService.mDisplayWindowSettings.setForcedDensity(this, density, userId);
Riddle Hsuf53da812018-08-15 22:00:27 +08002170 }
2171
2172 /** @param mode {@link #FORCE_SCALING_MODE_AUTO} or {@link #FORCE_SCALING_MODE_DISABLED}. */
2173 void setForcedScalingMode(@ForceScalingMode int mode) {
2174 if (mode != FORCE_SCALING_MODE_DISABLED) {
2175 mode = FORCE_SCALING_MODE_AUTO;
2176 }
2177
2178 mDisplayScalingDisabled = (mode != FORCE_SCALING_MODE_AUTO);
2179 Slog.i(TAG_WM, "Using display scaling mode: " + (mDisplayScalingDisabled ? "off" : "auto"));
Shivam Agrawal6472e0e2019-07-03 16:27:49 -07002180 reconfigureDisplayLocked();
Riddle Hsuf53da812018-08-15 22:00:27 +08002181
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002182 mWmService.mDisplayWindowSettings.setForcedScalingMode(this, mode);
Riddle Hsuf53da812018-08-15 22:00:27 +08002183 }
2184
2185 /** If the given width and height equal to initial size, the setting will be cleared. */
2186 void setForcedSize(int width, int height) {
2187 final boolean clear = mInitialDisplayWidth == width && mInitialDisplayHeight == height;
2188 if (!clear) {
2189 // Set some sort of reasonable bounds on the size of the display that we will try
2190 // to emulate.
2191 final int minSize = 200;
2192 final int maxScale = 2;
2193 width = Math.min(Math.max(width, minSize), mInitialDisplayWidth * maxScale);
2194 height = Math.min(Math.max(height, minSize), mInitialDisplayHeight * maxScale);
2195 }
2196
2197 Slog.i(TAG_WM, "Using new display size: " + width + "x" + height);
2198 updateBaseDisplayMetrics(width, height, mBaseDisplayDensity);
Shivam Agrawal6472e0e2019-07-03 16:27:49 -07002199 reconfigureDisplayLocked();
Riddle Hsuf53da812018-08-15 22:00:27 +08002200
2201 if (clear) {
2202 width = height = 0;
2203 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002204 mWmService.mDisplayWindowSettings.setForcedSize(this, width, height);
Riddle Hsuf53da812018-08-15 22:00:27 +08002205 }
2206
Wale Ogunwaledb506192017-12-08 10:57:32 -08002207 void getStableRect(Rect out) {
2208 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08002209 }
2210
Louis Changdc077272019-11-12 16:52:56 +08002211 void setStackOnDisplay(ActivityStack stack, int position) {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07002212 if (DEBUG_STACK) Slog.d(TAG_WM, "Set stack=" + stack + " on displayId=" + mDisplayId);
2213 mTaskStackContainers.addChild(stack, position);
Andrii Kulian839def92016-11-02 10:58:58 -07002214 }
2215
Louis Changdc077272019-11-12 16:52:56 +08002216 void moveStackToDisplay(ActivityStack stack, boolean onTop) {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07002217 stack.reparent(mTaskStackContainers, onTop ? POSITION_TOP: POSITION_BOTTOM);
2218 }
Andrii Kulian839def92016-11-02 10:58:58 -07002219
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002220 @Override
2221 protected void addChild(DisplayChildWindowContainer child,
2222 Comparator<DisplayChildWindowContainer> comparator) {
2223 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2224 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002225
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002226 @Override
2227 protected void addChild(DisplayChildWindowContainer child, int index) {
2228 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2229 }
2230
2231 @Override
2232 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002233 // Only allow removal of direct children from this display if the display is in the process
2234 // of been removed.
2235 if (mRemovingDisplay) {
2236 super.removeChild(child);
2237 return;
2238 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002239 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002240 }
2241
Andrii Kuliand2765632016-12-12 22:26:34 -08002242 @Override
2243 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
2244 // Children of the display are statically ordered, so the real intention here is to perform
2245 // the operation on the display and not the static direct children.
2246 getParent().positionChildAt(position, this, includingParents);
2247 }
2248
Louis Changdc077272019-11-12 16:52:56 +08002249 void positionStackAt(int position, ActivityStack child, boolean includingParents) {
Riddle Hsu57831b52018-07-27 00:31:48 +08002250 mTaskStackContainers.positionChildAt(position, child, includingParents);
Winson Chung59a47ded2018-01-25 17:46:06 +00002251 layoutAndAssignWindowLayersIfNeeded();
2252 }
2253
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002254 /**
Winson Chungc5fe7ff2019-02-19 14:49:25 -08002255 * Returns true if the input point is within an app window.
2256 */
2257 boolean pointWithinAppWindow(int x, int y) {
2258 final int[] targetWindowType = {-1};
Wale Ogunwalef5f3f1b2019-11-12 09:47:30 -08002259 final PooledConsumer fn = PooledLambda.obtainConsumer((w, nonArg) -> {
Winson Chungc5fe7ff2019-02-19 14:49:25 -08002260 if (targetWindowType[0] != -1) {
2261 return;
2262 }
2263
2264 if (w.isOnScreen() && w.isVisibleLw() && w.getFrameLw().contains(x, y)) {
2265 targetWindowType[0] = w.mAttrs.type;
2266 return;
2267 }
2268 }, PooledLambda.__(WindowState.class), mTmpRect);
2269 forAllWindows(fn, true /* traverseTopToBottom */);
Wale Ogunwalef5f3f1b2019-11-12 09:47:30 -08002270 fn.recycle();
Winson Chungc5fe7ff2019-02-19 14:49:25 -08002271 return FIRST_APPLICATION_WINDOW <= targetWindowType[0]
2272 && targetWindowType[0] <= LAST_APPLICATION_WINDOW;
2273 }
2274
2275 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08002276 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07002277 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07002278 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07002279 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002280 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale85fb19a2019-12-05 10:41:05 +09002281 return mTmpTaskForResizePointSearchResult.process(mTaskStackContainers, x, y, delta);
Chong Zhang8e89b312015-09-09 15:09:30 -07002282 }
2283
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002284 void updateTouchExcludeRegion() {
2285 final Task focusedTask = (mFocusedApp != null ? mFocusedApp.getTask() : null);
David Stevensee9e2772017-02-09 16:30:27 -08002286 if (focusedTask == null) {
2287 mTouchExcludeRegion.setEmpty();
2288 } else {
2289 mTouchExcludeRegion.set(mBaseDisplayRect);
2290 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale85fb19a2019-12-05 10:41:05 +09002291 mTmpRect.setEmpty();
David Stevensee9e2772017-02-09 16:30:27 -08002292 mTmpRect2.setEmpty();
Wale Ogunwale85fb19a2019-12-05 10:41:05 +09002293
2294 final PooledConsumer c = PooledLambda.obtainConsumer(
2295 DisplayContent::processTaskForTouchExcludeRegion, this,
2296 PooledLambda.__(Task.class), focusedTask, delta);
2297 mTaskStackContainers.forAllTasks(c);
2298 c.recycle();
2299
David Stevensee9e2772017-02-09 16:30:27 -08002300 // If we removed the focused task above, add it back and only leave its
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002301 // outside touch area in the exclusion. TapDetector is not interested in
David Stevensee9e2772017-02-09 16:30:27 -08002302 // any touch inside the focused task itself.
2303 if (!mTmpRect2.isEmpty()) {
2304 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
2305 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08002306 }
lumark90120a82018-08-15 00:33:03 +08002307 if (mInputMethodWindow != null && mInputMethodWindow.isVisibleLw()) {
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002308 // If the input method is visible and the user is typing, we don't want these touch
2309 // events to be intercepted and used to change focus. This would likely cause a
2310 // disappearance of the input method.
lumark90120a82018-08-15 00:33:03 +08002311 mInputMethodWindow.getTouchableRegion(mTmpRegion);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002312 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002313 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002314 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002315 final WindowState win = mTapExcludedWindows.get(i);
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002316 win.getTouchableRegion(mTmpRegion);
2317 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
2318 }
Tiger Huang04dc4cc2019-01-17 18:41:41 +08002319 amendWindowTapExcludeRegion(mTouchExcludeRegion);
Andrii Kulian03c403d2016-11-11 11:14:12 -08002320 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07002321 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002322 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07002323 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002324 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
2325 }
Riddle Hsu2588ab02019-02-25 14:23:56 +08002326 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner6601b7b2013-04-29 10:29:11 -07002327 }
2328
Wale Ogunwale85fb19a2019-12-05 10:41:05 +09002329 private void processTaskForTouchExcludeRegion(Task task, Task focusedTask, int delta) {
2330 final ActivityRecord topVisibleActivity = task.getTopVisibleActivity();
2331
2332 if (topVisibleActivity == null || !topVisibleActivity.hasContentToDisplay()) {
2333 return;
2334 }
2335
2336 // Exclusion region is the region that TapDetector doesn't care about.
2337 // Here we want to remove all non-focused tasks from the exclusion region.
2338 // We also remove the outside touch area for resizing for all freeform
2339 // tasks (including the focused).
2340 // We save the focused task region once we find it, and add it back at the end.
2341 // If the task is home stack and it is resizable in the minimized state, we want to
2342 // exclude the docked stack from touch so we need the entire screen area and not just a
2343 // small portion which the home stack currently is resized to.
2344 if (task.isActivityTypeHome() && task.getStack().isMinimizedDockAndHomeStackResizable()) {
2345 mDisplayContent.getBounds(mTmpRect);
2346 } else {
2347 task.getDimBounds(mTmpRect);
2348 }
2349
2350 if (task == focusedTask) {
2351 // Add the focused task rect back into the exclude region once we are done
2352 // processing stacks.
2353 mTmpRect2.set(mTmpRect);
2354 }
2355
2356 final boolean isFreeformed = task.inFreeformWindowingMode();
2357 if (task != focusedTask || isFreeformed) {
2358 if (isFreeformed) {
2359 // If the task is freeformed, enlarge the area to account for outside
2360 // touch area for resize.
2361 mTmpRect.inset(-delta, -delta);
2362 // Intersect with display content rect. If we have system decor (status bar/
2363 // navigation bar), we want to exclude that from the tap detection.
2364 // Otherwise, if the app is partially placed under some system button (eg.
2365 // Recents, Home), pressing that button would cause a full series of
2366 // unwanted transfer focus/resume/pause, before we could go home.
2367 mTmpRect.intersect(mDisplayFrames.mContent);
2368 }
2369 mTouchExcludeRegion.op(mTmpRect, Region.Op.DIFFERENCE);
2370 }
2371 }
2372
Tiger Huang04dc4cc2019-01-17 18:41:41 +08002373 /**
2374 * Union the region with all the tap exclude region provided by windows on this display.
2375 *
2376 * @param inOutRegion The region to be amended.
2377 */
Wale Ogunwale85fb19a2019-12-05 10:41:05 +09002378 private void amendWindowTapExcludeRegion(Region inOutRegion) {
Tiger Huang04dc4cc2019-01-17 18:41:41 +08002379 for (int i = mTapExcludeProvidingWindows.size() - 1; i >= 0; i--) {
2380 final WindowState win = mTapExcludeProvidingWindows.valueAt(i);
2381 win.amendTapExcludeRegion(inOutRegion);
2382 }
2383 }
2384
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002385 @Override
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07002386 void switchUser(int userId) {
2387 super.switchUser(userId);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002388 mWmService.mWindowsChanged = true;
Winson Chungda20fec2019-04-10 12:19:59 -07002389 mDisplayPolicy.switchUser();
Craig Mautner858d8a62013-04-23 17:08:34 -07002390 }
2391
Wale Ogunwale10124582016-09-15 20:25:50 -07002392 @Override
2393 void removeIfPossible() {
lumark9bca6b42019-10-17 18:35:22 +08002394 if (isAnimating(TRANSITION | PARENTS)) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002395 mDeferredRemoval = true;
2396 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07002397 }
Wale Ogunwale10124582016-09-15 20:25:50 -07002398 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07002399 }
2400
Wale Ogunwale10124582016-09-15 20:25:50 -07002401 @Override
2402 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002403 mRemovingDisplay = true;
2404 try {
Jackal Guoc43a0a62019-04-23 09:15:14 +08002405 if (mParentWindow != null) {
2406 mParentWindow.removeEmbeddedDisplayContent(this);
2407 }
lumark588a3e82018-07-20 18:53:54 +08002408 // Clear all transitions & screen frozen states when removing display.
2409 mOpeningApps.clear();
2410 mClosingApps.clear();
Evan Rosky2289ba12018-11-19 18:28:18 -08002411 mChangingApps.clear();
lumark588a3e82018-07-20 18:53:54 +08002412 mUnknownAppVisibilityController.clear();
2413 mAppTransition.removeAppTransitionTimeoutCallbacks();
2414 handleAnimatingStoppedAndTransition();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002415 mWmService.stopFreezingDisplayLocked();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002416 super.removeImmediately();
2417 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Riddle Hsu2588ab02019-02-25 14:23:56 +08002418 mPointerEventDispatcher.dispose();
Vadim Caenb3715832019-08-13 17:06:38 +02002419 setRotationAnimation(null);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002420 mWmService.mAnimator.removeDisplayLocked(mDisplayId);
Louis Chang207f9112018-07-30 08:58:54 +08002421 mWindowingLayer.release();
2422 mOverlayLayer.release();
Jorim Jaggiae962e62018-12-27 17:23:48 +01002423 mInputMonitor.onDisplayRemoved();
Louis Chang2453d062019-11-19 22:30:48 +08002424 // TODO(display-merge): Remove cast
2425 mWmService.mDisplayNotificationController
2426 .dispatchDisplayRemoved((ActivityDisplay) this);
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002427 } finally {
Riddle Hsu4e611772018-10-31 18:58:28 +08002428 mDisplayReady = false;
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002429 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08002430 }
Robert Carr679ccb02018-08-08 15:32:35 -07002431
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002432 mWmService.mWindowPlacerLocked.requestTraversal();
Craig Mautner95da1082014-02-24 17:54:35 -08002433 }
2434
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002435 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07002436 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002437 boolean checkCompleteDeferredRemoval() {
Riddle Hsu793442c52019-11-05 01:04:55 +08002438 boolean stillDeferringRemoval = false;
2439
2440 for (int i = getChildCount() - 1; i >= 0; --i) {
2441 final DisplayChildWindowContainer child = getChildAt(i);
2442 stillDeferringRemoval |= child.checkCompleteDeferredRemoval();
2443 if (getChildCount() == 0) {
2444 // If this display is pending to be removed because it contains an activity with
2445 // {@link ActivityRecord#mIsExiting} is true, this display may be removed when
2446 // completing the removal of the last activity from
2447 // {@link ActivityRecord#checkCompleteDeferredRemoval}.
2448 return false;
2449 }
2450 }
Wale Ogunwale10124582016-09-15 20:25:50 -07002451
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002452 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002453 removeImmediately();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002454 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08002455 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002456 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08002457 }
2458
Andrii Kulian0214ed92017-05-16 13:44:05 -07002459 /** @return 'true' if removal of this display content is deferred due to active animation. */
2460 boolean isRemovalDeferred() {
2461 return mDeferredRemoval;
2462 }
2463
Wale Ogunwale10124582016-09-15 20:25:50 -07002464 boolean animateForIme(float interpolatedValue, float animationTarget,
2465 float dividerAnimationTarget) {
2466 boolean updated = false;
2467
Wale Ogunwale61911492017-10-11 08:50:50 -07002468 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
Louis Changdc077272019-11-12 16:52:56 +08002469 final ActivityStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002470 if (stack == null || !stack.isAdjustedForIme()) {
2471 continue;
2472 }
2473
2474 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
2475 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2476 updated = true;
2477 } else {
2478 mDividerControllerLocked.mLastAnimationProgress =
2479 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
2480 mDividerControllerLocked.mLastDividerProgress =
2481 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
2482 updated |= stack.updateAdjustForIme(
2483 mDividerControllerLocked.mLastAnimationProgress,
2484 mDividerControllerLocked.mLastDividerProgress,
2485 false /* force */);
2486 }
2487 if (interpolatedValue >= 1f) {
2488 stack.endImeAdjustAnimation();
2489 }
2490 }
2491
2492 return updated;
2493 }
2494
2495 boolean clearImeAdjustAnimation() {
2496 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002497 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
Louis Changdc077272019-11-12 16:52:56 +08002498 final ActivityStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002499 if (stack != null && stack.isAdjustedForIme()) {
2500 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2501 changed = true;
2502 }
2503 }
2504 return changed;
2505 }
2506
2507 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002508 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
Louis Changdc077272019-11-12 16:52:56 +08002509 final ActivityStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002510 if (stack.isVisible() && stack.isAdjustedForIme()) {
2511 stack.beginImeAdjustAnimation();
2512 }
2513 }
2514 }
2515
2516 void adjustForImeIfNeeded() {
lumark90120a82018-08-15 00:33:03 +08002517 final WindowState imeWin = mInputMethodWindow;
Wale Ogunwale10124582016-09-15 20:25:50 -07002518 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2519 && !mDividerControllerLocked.isImeHideRequested();
Louis Changdc077272019-11-12 16:52:56 +08002520 final ActivityStack dockedStack = getSplitScreenPrimaryStack();
Riddle Hsuc6814252019-05-16 17:06:52 +08002521 final boolean dockVisible = dockedStack != null;
Wale Ogunwalea38654f2019-11-17 20:37:15 -08002522 final Task topDockedTask = dockVisible ? dockedStack.getTask((t) -> true): null;
Louis Changdc077272019-11-12 16:52:56 +08002523 final ActivityStack imeTargetStack = mWmService.getImeFocusStackLocked();
Wale Ogunwale10124582016-09-15 20:25:50 -07002524 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2525 imeTargetStack.getDockSide() : DOCKED_INVALID;
2526 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2527 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002528 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002529 final boolean imeHeightChanged = imeVisible &&
2530 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2531
Riddle Hsuc6814252019-05-16 17:06:52 +08002532 // This includes a case where the docked stack is unminimizing and IME is visible for the
2533 // bottom side stack. The condition prevents adjusting the override task bounds for IME to
2534 // the minimized docked stack bounds.
2535 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock()
2536 || (topDockedTask != null && imeOnBottom && !dockedStack.isAdjustedForIme()
2537 && dockedStack.getBounds().height() < topDockedTask.getBounds().height());
2538
Wale Ogunwale10124582016-09-15 20:25:50 -07002539 // The divider could be adjusted for IME position, or be thinner than usual,
2540 // or both. There are three possible cases:
2541 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2542 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2543 // - If IME is not visible, divider is not moved and is normal width.
2544
2545 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002546 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
Louis Changdc077272019-11-12 16:52:56 +08002547 final ActivityStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002548 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002549 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2550 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002551 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2552 } else {
2553 stack.resetAdjustedForIme(false);
2554 }
2555 }
2556 mDividerControllerLocked.setAdjustedForIme(
2557 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2558 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002559 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
Louis Changdc077272019-11-12 16:52:56 +08002560 final ActivityStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002561 stack.resetAdjustedForIme(!dockVisible);
2562 }
2563 mDividerControllerLocked.setAdjustedForIme(
2564 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2565 }
Winson Chung655332c2016-10-31 13:14:28 -07002566 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002567 }
2568
2569 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002570 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
Louis Changdc077272019-11-12 16:52:56 +08002571 final ActivityStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002572 stack.prepareFreezingTaskBounds();
2573 }
2574 }
2575
Wale Ogunwale94744212015-09-21 19:01:47 -07002576 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002577 getBounds(mTmpRect, newRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002578 rotateBounds(mTmpRect, oldRotation, newRotation, bounds);
2579 }
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002580
Evan Rosky39b6f232018-10-30 18:35:41 -07002581 void rotateBounds(Rect parentBounds, int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002582 // Compute a transform matrix to undo the coordinate space transformation,
2583 // and present the window at the same physical position it previously occupied.
2584 final int deltaRotation = deltaRotation(newRotation, oldRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002585 createRotationMatrix(
2586 deltaRotation, parentBounds.width(), parentBounds.height(), mTmpMatrix);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002587
2588 mTmpRectF.set(bounds);
2589 mTmpMatrix.mapRect(mTmpRectF);
2590 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002591 }
2592
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002593 static int deltaRotation(int oldRotation, int newRotation) {
2594 int delta = newRotation - oldRotation;
2595 if (delta < 0) delta += 4;
2596 return delta;
2597 }
2598
Vadim Caenb3715832019-08-13 17:06:38 +02002599 public void setRotationAnimation(ScreenRotationAnimation screenRotationAnimation) {
2600 if (mScreenRotationAnimation != null) {
2601 mScreenRotationAnimation.kill();
2602 }
2603 mScreenRotationAnimation = screenRotationAnimation;
2604 }
2605
2606 public ScreenRotationAnimation getRotationAnimation() {
2607 return mScreenRotationAnimation;
2608 }
2609
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002610 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002611 Matrix outMatrix) {
2612 // For rotations without Z-ordering we don't need the target rectangle's position.
2613 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2614 displayHeight, outMatrix);
2615 }
2616
2617 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2618 float displayWidth, float displayHeight, Matrix outMatrix) {
2619 switch (rotation) {
2620 case ROTATION_0:
2621 outMatrix.reset();
2622 break;
2623 case ROTATION_270:
2624 outMatrix.setRotate(270, 0, 0);
2625 outMatrix.postTranslate(0, displayHeight);
2626 outMatrix.postTranslate(rectTop, 0);
2627 break;
2628 case ROTATION_180:
2629 outMatrix.reset();
2630 break;
2631 case ROTATION_90:
2632 outMatrix.setRotate(90, 0, 0);
2633 outMatrix.postTranslate(displayWidth, 0);
2634 outMatrix.postTranslate(-rectTop, rectLeft);
2635 break;
2636 }
2637 }
2638
Louis Chang2453d062019-11-19 22:30:48 +08002639 // TODO(proto-merge): Remove once protos for ActivityDisplay and DisplayContent are merged.
Jeffrey Huangcb782852019-12-05 11:28:11 -08002640 public void dumpDebugInner(ProtoOutputStream proto, long fieldId,
Nataniel Borges023ecb52019-01-16 14:15:43 -08002641 @WindowTraceLogLevel int logLevel) {
2642 // Critical log level logs only visible elements to mitigate performance overheard
2643 if (logLevel == WindowTraceLogLevel.CRITICAL && !isVisible()) {
2644 return;
2645 }
2646
Steven Timotiusaf03df62017-07-18 16:56:43 -07002647 final long token = proto.start(fieldId);
Jeffrey Huangcb782852019-12-05 11:28:11 -08002648 super.dumpDebug(proto, WINDOW_CONTAINER, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002649 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002650 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
Louis Changdc077272019-11-12 16:52:56 +08002651 final ActivityStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jeffrey Huangcb782852019-12-05 11:28:11 -08002652 stack.dumpDebugInnerStackOnly(proto, STACKS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002653 }
Jeffrey Huangcb782852019-12-05 11:28:11 -08002654 mDividerControllerLocked.dumpDebug(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2655 mPinnedStackControllerLocked.dumpDebug(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002656 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2657 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Jeffrey Huangcb782852019-12-05 11:28:11 -08002658 windowToken.dumpDebug(proto, ABOVE_APP_WINDOWS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002659 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002660 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2661 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Jeffrey Huangcb782852019-12-05 11:28:11 -08002662 windowToken.dumpDebug(proto, BELOW_APP_WINDOWS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002663 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002664 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2665 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Jeffrey Huangcb782852019-12-05 11:28:11 -08002666 windowToken.dumpDebug(proto, IME_WINDOWS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002667 }
2668 proto.write(DPI, mBaseDisplayDensity);
Jeffrey Huangcb782852019-12-05 11:28:11 -08002669 mDisplayInfo.dumpDebug(proto, DISPLAY_INFO);
Riddle Hsuccf09402019-08-13 00:33:06 +08002670 proto.write(ROTATION, getRotation());
Vadim Caenb3715832019-08-13 17:06:38 +02002671 final ScreenRotationAnimation screenRotationAnimation = getRotationAnimation();
Steven Timotiusf2d68892017-08-28 17:00:01 -07002672 if (screenRotationAnimation != null) {
Jeffrey Huangcb782852019-12-05 11:28:11 -08002673 screenRotationAnimation.dumpDebug(proto, SCREEN_ROTATION_ANIMATION);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002674 }
Jeffrey Huangcb782852019-12-05 11:28:11 -08002675 mDisplayFrames.dumpDebug(proto, DISPLAY_FRAMES);
2676 mAppTransition.dumpDebug(proto, APP_TRANSITION);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002677 if (mFocusedApp != null) {
2678 mFocusedApp.writeNameToProto(proto, FOCUSED_APP);
2679 }
Jorim Jaggi1112fed2019-04-15 13:32:14 +02002680 for (int i = mOpeningApps.size() - 1; i >= 0; i--) {
Wale Ogunwaleda8b8272018-11-29 19:37:37 -08002681 mOpeningApps.valueAt(i).writeIdentifierToProto(proto, OPENING_APPS);
Jorim Jaggi1112fed2019-04-15 13:32:14 +02002682 }
2683 for (int i = mClosingApps.size() - 1; i >= 0; i--) {
Wale Ogunwaleda8b8272018-11-29 19:37:37 -08002684 mClosingApps.valueAt(i).writeIdentifierToProto(proto, CLOSING_APPS);
Jorim Jaggi1112fed2019-04-15 13:32:14 +02002685 }
2686 for (int i = mChangingApps.size() - 1; i >= 0; i--) {
Wale Ogunwaleda8b8272018-11-29 19:37:37 -08002687 mChangingApps.valueAt(i).writeIdentifierToProto(proto, CHANGING_APPS);
Jorim Jaggi1112fed2019-04-15 13:32:14 +02002688 }
Steven Timotiusaf03df62017-07-18 16:56:43 -07002689 proto.end(token);
2690 }
2691
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002692 @Override
2693 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2694 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002695 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2696 final String subPrefix = " " + prefix;
2697 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2698 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2699 pw.print("dpi");
2700 if (mInitialDisplayWidth != mBaseDisplayWidth
2701 || mInitialDisplayHeight != mBaseDisplayHeight
2702 || mInitialDisplayDensity != mBaseDisplayDensity) {
2703 pw.print(" base=");
2704 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2705 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2706 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002707 if (mDisplayScalingDisabled) {
2708 pw.println(" noscale");
2709 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002710 pw.print(" cur=");
2711 pw.print(mDisplayInfo.logicalWidth);
2712 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2713 pw.print(" app=");
2714 pw.print(mDisplayInfo.appWidth);
2715 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2716 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2717 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2718 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2719 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002720 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002721 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002722 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002723
Craig Mautnerdc548482014-02-05 13:35:24 -08002724 pw.println();
Adrian Roos5251b1d2018-03-23 18:57:43 +01002725 pw.print(prefix); pw.print("mLayoutSeq="); pw.println(mLayoutSeq);
2726
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002727 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
2728 if (mLastFocus != mCurrentFocus) {
2729 pw.print(" mLastFocus="); pw.println(mLastFocus);
2730 }
2731 if (mLosingFocus.size() > 0) {
2732 pw.println();
2733 pw.println(" Windows losing focus:");
2734 for (int i = mLosingFocus.size() - 1; i >= 0; i--) {
2735 final WindowState w = mLosingFocus.get(i);
2736 pw.print(" Losing #"); pw.print(i); pw.print(' ');
2737 pw.print(w);
2738 if (dumpAll) {
2739 pw.println(":");
2740 w.dump(pw, " ", true);
2741 } else {
2742 pw.println();
2743 }
2744 }
2745 }
2746 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
Tiger Huang7c610aa2018-10-27 00:01:01 +08002747 if (mLastStatusBarVisibility != 0) {
2748 pw.print(" mLastStatusBarVisibility=0x");
2749 pw.println(Integer.toHexString(mLastStatusBarVisibility));
2750 }
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002751
Adrian Roos5251b1d2018-03-23 18:57:43 +01002752 pw.println();
wilsonshihc32538e2018-11-07 17:27:34 +08002753 mWallpaperController.dump(pw, " ");
2754
2755 pw.println();
Adrian Roos4ffc8972019-02-07 20:45:11 +01002756 pw.print("mSystemGestureExclusion=");
2757 if (mSystemGestureExclusionListeners.getRegisteredCallbackCount() > 0) {
2758 pw.println(mSystemGestureExclusion);
2759 } else {
2760 pw.println("<no lstnrs>");
2761 }
2762
2763 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002764 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002765 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
Louis Changdc077272019-11-12 16:52:56 +08002766 final ActivityStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002767 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002768 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002769
Craig Mautnerdc548482014-02-05 13:35:24 -08002770 pw.println();
2771 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002772 pw.println();
2773 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002774 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002775 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002776 pw.print(" Exiting #"); pw.print(i);
2777 pw.print(' '); pw.print(token);
2778 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002779 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002780 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002781 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002782
Vadim Caenb3715832019-08-13 17:06:38 +02002783 final ScreenRotationAnimation rotationAnimation = getRotationAnimation();
2784 if (rotationAnimation != null) {
2785 pw.print(subPrefix);
2786 pw.println(" mScreenRotationAnimation:");
2787 rotationAnimation.printTo(" ", pw);
2788 } else if (dumpAll) {
2789 pw.print(subPrefix);
2790 pw.println(" no ScreenRotationAnimation ");
2791 }
2792
Jorim Jaggi31f71702016-05-04 16:43:04 -07002793 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002794
2795 // Dump stack references
Louis Changdc077272019-11-12 16:52:56 +08002796 final ActivityStack homeStack = getHomeStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002797 if (homeStack != null) {
2798 pw.println(prefix + "homeStack=" + homeStack.getName());
2799 }
Louis Changdc077272019-11-12 16:52:56 +08002800 final ActivityStack pinnedStack = getPinnedStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002801 if (pinnedStack != null) {
2802 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2803 }
Louis Changdc077272019-11-12 16:52:56 +08002804 final ActivityStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002805 if (splitScreenPrimaryStack != null) {
2806 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2807 }
2808
2809 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002810 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002811 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002812 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002813
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002814 pw.println();
2815 mDisplayFrames.dump(prefix, pw);
Arthur Hung95b38a92018-07-20 18:56:12 +08002816 pw.println();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08002817 mDisplayPolicy.dump(prefix, pw);
2818 pw.println();
Riddle Hsuad256a12018-07-18 16:11:30 +08002819 mDisplayRotation.dump(prefix, pw);
2820 pw.println();
Arthur Hung95b38a92018-07-20 18:56:12 +08002821 mInputMonitor.dump(pw, " ");
Jorim Jaggif96c90a2018-09-26 16:55:15 +02002822 pw.println();
2823 mInsetsStateController.dump(prefix, pw);
Craig Mautner59c00972012-07-30 12:10:24 -07002824 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002825
2826 @Override
2827 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002828 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002829 }
2830
2831 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002832 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002833 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002834
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002835 /** Returns true if the stack in the windowing mode is visible. */
2836 boolean isStackVisible(int windowingMode) {
Louis Changdc077272019-11-12 16:52:56 +08002837 final ActivityStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002838 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002839 }
2840
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002841 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002842 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002843 final int x = (int) xf;
2844 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002845 final WindowState touchedWin = getWindow(w -> {
2846 final int flags = w.mAttrs.flags;
2847 if (!w.isVisibleLw()) {
2848 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002849 }
2850 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002851 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002852 }
2853
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002854 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002855 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002856 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002857 }
2858
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002859 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002860
2861 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002862 return mTmpRegion.contains(x, y) || touchFlags == 0;
2863 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002864
2865 return touchedWin;
2866 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002867
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002868 boolean canAddToastWindowForUid(int uid) {
2869 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002870 // Also if the app is focused adding more than one toast at
2871 // a time for better backwards compatibility.
2872 final WindowState focusedWindowForUid = getWindow(w ->
2873 w.mOwnerUid == uid && w.isFocused());
2874 if (focusedWindowForUid != null) {
2875 return true;
2876 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002877 final WindowState win = getWindow(w ->
2878 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2879 && !w.mWindowRemovalAllowed);
2880 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002881 }
2882
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002883 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002884 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2885 return;
2886 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002887
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002888 // Used to communicate the old focus to the callback method.
2889 mTmpWindow = oldFocus;
2890
2891 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002892 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002893
Louis Changa9350fe2019-04-25 17:14:20 +08002894 /**
2895 * Looking for the focused window on this display if the top focused display hasn't been
2896 * found yet (topFocusedDisplayId is INVALID_DISPLAY) or per-display focused was allowed.
2897 *
2898 * @param topFocusedDisplayId Id of the top focused display.
2899 * @return The focused window or null if there isn't any or no need to seek.
2900 */
2901 WindowState findFocusedWindowIfNeeded(int topFocusedDisplayId) {
2902 return (mWmService.mPerDisplayFocusEnabled || topFocusedDisplayId == INVALID_DISPLAY)
2903 ? findFocusedWindow() : null;
Tiger Huang51c5a1d2018-12-11 20:24:51 +08002904 }
2905
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002906 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002907 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002908
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002909 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002910
2911 if (mTmpWindow == null) {
Adrian Roosb125e0b2019-10-02 14:55:14 +02002912 ProtoLog.v(WM_DEBUG_FOCUS_LIGHT, "findFocusedWindow: No focusable windows.");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002913 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002914 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002915 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002916 }
2917
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002918 /**
2919 * Update the focused window and make some adjustments if the focus has changed.
2920 *
2921 * @param mode Indicates the situation we are in. Possible modes are:
2922 * {@link WindowManagerService#UPDATE_FOCUS_NORMAL},
2923 * {@link WindowManagerService#UPDATE_FOCUS_PLACING_SURFACES},
2924 * {@link WindowManagerService#UPDATE_FOCUS_WILL_PLACE_SURFACES},
2925 * {@link WindowManagerService#UPDATE_FOCUS_REMOVING_FOCUS}
2926 * @param updateInputWindows Whether to sync the window information to the input module.
Louis Changa9350fe2019-04-25 17:14:20 +08002927 * @param topFocusedDisplayId Display id of current top focused display.
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002928 * @return {@code true} if the focused window has changed.
2929 */
Louis Changa9350fe2019-04-25 17:14:20 +08002930 boolean updateFocusedWindowLocked(int mode, boolean updateInputWindows,
2931 int topFocusedDisplayId) {
2932 WindowState newFocus = findFocusedWindowIfNeeded(topFocusedDisplayId);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002933 if (mCurrentFocus == newFocus) {
2934 return false;
2935 }
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002936 boolean imWindowChanged = false;
Tiger Huang51c5a1d2018-12-11 20:24:51 +08002937 final WindowState imWindow = mInputMethodWindow;
2938 if (imWindow != null) {
2939 final WindowState prevTarget = mInputMethodTarget;
2940 final WindowState newTarget = computeImeTarget(true /* updateImeTarget*/);
2941 imWindowChanged = prevTarget != newTarget;
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002942
Tiger Huang51c5a1d2018-12-11 20:24:51 +08002943 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS
2944 && mode != UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2945 assignWindowLayers(false /* setLayoutNeeded */);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002946 }
2947 }
2948
2949 if (imWindowChanged) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002950 mWmService.mWindowsChanged = true;
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002951 setLayoutNeeded();
Louis Changa9350fe2019-04-25 17:14:20 +08002952 newFocus = findFocusedWindowIfNeeded(topFocusedDisplayId);
Tiger Huang51c5a1d2018-12-11 20:24:51 +08002953 }
2954 if (mCurrentFocus != newFocus) {
2955 mWmService.mH.obtainMessage(REPORT_FOCUS_CHANGE, this).sendToTarget();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002956 }
2957
Adrian Roosb125e0b2019-10-02 14:55:14 +02002958 ProtoLog.v(WM_DEBUG_FOCUS_LIGHT, "Changing focus from %s to %s displayId=%d Callers=%s",
2959 mCurrentFocus, newFocus, getDisplayId(), Debug.getCallers(4));
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002960 final WindowState oldFocus = mCurrentFocus;
2961 mCurrentFocus = newFocus;
2962 mLosingFocus.remove(newFocus);
2963
2964 if (newFocus != null) {
2965 mWinAddedSinceNullFocus.clear();
2966 mWinRemovedSinceNullFocus.clear();
2967
2968 if (newFocus.canReceiveKeys()) {
2969 // Displaying a window implicitly causes dispatching to be unpaused.
2970 // This is to protect against bugs if someone pauses dispatching but
2971 // forgets to resume.
2972 newFocus.mToken.paused = false;
2973 }
2974 }
2975
Tiger Huang7c610aa2018-10-27 00:01:01 +08002976 int focusChanged = getDisplayPolicy().focusChangedLw(oldFocus, newFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002977
2978 if (imWindowChanged && oldFocus != mInputMethodWindow) {
2979 // Focus of the input method window changed. Perform layout if needed.
2980 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2981 performLayout(true /*initial*/, updateInputWindows);
2982 focusChanged &= ~FINISH_LAYOUT_REDO_LAYOUT;
2983 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2984 // Client will do the layout, but we need to assign layers
2985 // for handleNewWindowLocked() below.
2986 assignWindowLayers(false /* setLayoutNeeded */);
2987 }
2988 }
2989
2990 if ((focusChanged & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2991 // The change in focus caused us to need to do a layout. Okay.
2992 setLayoutNeeded();
2993 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2994 performLayout(true /*initial*/, updateInputWindows);
2995 } else if (mode == UPDATE_FOCUS_REMOVING_FOCUS) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002996 mWmService.mRoot.performSurfacePlacement(false);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002997 }
2998 }
2999
3000 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
3001 // If we defer assigning layers, then the caller is responsible for doing this part.
3002 getInputMonitor().setInputFocusLw(newFocus, updateInputWindows);
3003 }
3004
3005 adjustForImeIfNeeded();
3006
3007 // We may need to schedule some toast windows to be removed. The toasts for an app that
3008 // does not have input focus are removed within a timeout to prevent apps to redress
3009 // other apps' UI.
3010 scheduleToastWindowsTimeoutIfNeededLocked(oldFocus, newFocus);
3011
3012 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
3013 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
3014 }
3015 return true;
3016 }
3017
3018 /**
3019 * Set the new focused app to this display.
3020 *
Garfield Tane8d84ab2019-10-11 09:49:40 -07003021 * @param newFocus the new focused {@link ActivityRecord}.
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003022 * @return true if the focused app is changed.
3023 */
Garfield Tane8d84ab2019-10-11 09:49:40 -07003024 boolean setFocusedApp(ActivityRecord newFocus) {
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003025 if (newFocus != null) {
3026 final DisplayContent appDisplay = newFocus.getDisplayContent();
3027 if (appDisplay != this) {
3028 throw new IllegalStateException(newFocus + " is not on " + getName()
3029 + " but " + ((appDisplay != null) ? appDisplay.getName() : "none"));
3030 }
3031 }
3032 if (mFocusedApp == newFocus) {
3033 return false;
3034 }
3035 mFocusedApp = newFocus;
3036 getInputMonitor().setFocusedAppLw(newFocus);
3037 updateTouchExcludeRegion();
3038 return true;
3039 }
3040
Wale Ogunwalec69694a2016-10-18 13:51:15 -07003041 /** Updates the layer assignment of windows on this display. */
3042 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggi4981f152019-03-26 18:58:45 +01003043 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07003044 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07003045 if (setLayoutNeeded) {
3046 setLayoutNeeded();
3047 }
Robert Carrb1579c82017-09-05 14:54:47 -07003048
Robert Carrf59b8dd2017-10-02 18:58:36 -07003049 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07003050 // the application of this transaction until the animation pass triggers
3051 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
3052 // the hiding and showing of surfaces.
3053 scheduleAnimation();
Jorim Jaggi4981f152019-03-26 18:58:45 +01003054 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07003055 }
3056
Wale Ogunwale1666e312016-12-16 11:27:18 -08003057 // TODO: This should probably be called any time a visual change is made to the hierarchy like
3058 // moving containers or resizing them. Need to investigate the best way to have it automatically
3059 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003060 void layoutAndAssignWindowLayersIfNeeded() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003061 mWmService.mWindowsChanged = true;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003062 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003063
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003064 if (!mWmService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003065 false /*updateInputWindows*/)) {
3066 assignWindowLayers(false /* setLayoutNeeded */);
3067 }
3068
Arthur Hung95b38a92018-07-20 18:56:12 +08003069 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003070 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Arthur Hung95b38a92018-07-20 18:56:12 +08003071 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003072 }
3073
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003074 /** Returns true if a leaked surface was destroyed */
3075 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003076 // Used to indicate that a surface was leaked.
3077 mTmpWindow = null;
3078 forAllWindows(w -> {
3079 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003080 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003081 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003082 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003083 if (!mWmService.mSessions.contains(wsa.mSession)) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003084 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003085 + w + " surface=" + wsa.mSurfaceController
3086 + " token=" + w.mToken
3087 + " pid=" + w.mSession.mPid
3088 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003089 wsa.destroySurface();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003090 mWmService.mForceRemoves.add(w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003091 mTmpWindow = w;
Issei Suzukid4ee1cc2019-11-08 13:20:14 +01003092 } else if (w.mActivityRecord != null && !w.mActivityRecord.isClientVisible()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003093 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003094 + w + " surface=" + wsa.mSurfaceController
Garfield Tane8d84ab2019-10-11 09:49:40 -07003095 + " token=" + w.mActivityRecord);
Adrian Roosb125e0b2019-10-02 14:55:14 +02003096 ProtoLog.i(WM_SHOW_TRANSACTIONS, "SURFACE LEAK DESTROY: %s", w);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003097 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003098 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003099 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003100 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003101
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003102 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003103 }
3104
Riddle Hsuccf09402019-08-13 00:33:06 +08003105 boolean hasAlertWindowSurfaces() {
3106 for (int i = mWmService.mSessions.size() - 1; i >= 0; --i) {
3107 if (mWmService.mSessions.valueAt(i).hasAlertWindowSurfaces(this)) {
3108 return true;
3109 }
3110 }
3111 return false;
3112 }
3113
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003114 /**
lumark90120a82018-08-15 00:33:03 +08003115 * Set input method window for the display.
3116 * @param win Set when window added or Null when destroyed.
3117 */
3118 void setInputMethodWindowLocked(WindowState win) {
3119 mInputMethodWindow = win;
lumark70865a82018-09-17 16:56:26 +08003120 // Update display configuration for IME process.
3121 if (mInputMethodWindow != null) {
3122 final int imePid = mInputMethodWindow.mSession.mPid;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003123 mWmService.mAtmInternal.onImeWindowSetOnDisplay(imePid,
lumark70865a82018-09-17 16:56:26 +08003124 mInputMethodWindow.getDisplayId());
3125 }
lumark90120a82018-08-15 00:33:03 +08003126 computeImeTarget(true /* updateImeTarget */);
Tiger Huang332793b2019-10-29 23:21:27 +08003127 mInsetsStateController.getSourceProvider(ITYPE_IME).setWindow(win,
Jorim Jaggif96c90a2018-09-26 16:55:15 +02003128 null /* frameProvider */);
lumark90120a82018-08-15 00:33:03 +08003129 }
3130
3131 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003132 * Determine and return the window that should be the IME target.
3133 * @param updateImeTarget If true the system IME target will be updated to match what we found.
3134 * @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 +00003135 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003136 WindowState computeImeTarget(boolean updateImeTarget) {
lumark90120a82018-08-15 00:33:03 +08003137 if (mInputMethodWindow == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003138 // There isn't an IME so there shouldn't be a target...That was easy!
3139 if (updateImeTarget) {
3140 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
lumarkff0ab692018-11-05 20:32:30 +08003141 + mInputMethodTarget + " to null since mInputMethodWindow is null");
3142 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003143 }
3144 return null;
3145 }
3146
lumarkff0ab692018-11-05 20:32:30 +08003147 final WindowState curTarget = mInputMethodTarget;
Chavi Weingarten3a748552018-05-14 17:32:42 +00003148 if (!canUpdateImeTarget()) {
3149 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Defer updating IME target");
3150 return curTarget;
3151 }
3152
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003153 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
3154 // same display. Or even when the current IME/target are not on the same screen as the next
3155 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08003156 mUpdateImeTarget = updateImeTarget;
3157 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003158
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003159
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003160 // Yet more tricksyness! If this window is a "starting" window, we do actually want
3161 // to be on top of it, but it is not -really- where input will go. So look down below
3162 // for a real window to target...
3163 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07003164 final ActivityRecord activity = target.mActivityRecord;
3165 if (activity != null) {
3166 final WindowState betterTarget = activity.getImeTargetBelowWindow(target);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003167 if (betterTarget != null) {
3168 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003169 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003170 }
3171 }
3172
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003173 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
lumarkff0ab692018-11-05 20:32:30 +08003174 "Proposed new IME target: " + target + " for display: " + getDisplayId());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003175
chaviw1c13ad32018-06-12 16:44:23 -07003176 // Now, a special case -- if the last target's window is in the process of exiting, but
Tarandeep Singh2d0aca42019-03-28 14:08:58 -07003177 // not removed, keep on the last target to avoid IME flicker.
chaviw1c13ad32018-06-12 16:44:23 -07003178 if (curTarget != null && !curTarget.mRemoved && curTarget.isDisplayedLw()
Tarandeep Singh2d0aca42019-03-28 14:08:58 -07003179 && curTarget.isClosing()) {
3180 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Not changing target till current window is"
3181 + " closing and not removed");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003182 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003183 }
3184
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003185 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
3186 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003187
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003188 if (target == null) {
3189 if (updateImeTarget) {
3190 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
3191 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
3192 + Debug.getCallers(4) : ""));
lumarkff0ab692018-11-05 20:32:30 +08003193 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003194 }
3195
3196 return null;
3197 }
3198
3199 if (updateImeTarget) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07003200 ActivityRecord activity = curTarget == null ? null : curTarget.mActivityRecord;
3201 if (activity != null) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003202
3203 // Now some fun for dealing with window animations that modify the Z order. We need
3204 // to look at all windows below the current target that are in this app, finding the
3205 // highest visible one in layering.
3206 WindowState highestTarget = null;
lumark9bca6b42019-10-17 18:35:22 +08003207 if (activity.isAnimating(TRANSITION)) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07003208 highestTarget = activity.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003209 }
3210
3211 if (highestTarget != null) {
lumark9bca6b42019-10-17 18:35:22 +08003212 if (DEBUG_INPUT_METHOD) {
3213 Slog.v(TAG_WM, mAppTransition + " " + highestTarget + " animating="
3214 + highestTarget.isAnimating(TRANSITION | PARENTS));
3215 }
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003216
lumark588a3e82018-07-20 18:53:54 +08003217 if (mAppTransition.isTransitionSet()) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003218 // If we are currently setting up for an animation, hold everything until we
3219 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003220 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003221 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003222 }
3223 }
3224 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003225
3226 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
3227 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003228 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003229 }
3230
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003231 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003232 }
3233
lumarkff0ab692018-11-05 20:32:30 +08003234 /**
3235 * Calling {@link #computeImeTarget(boolean)} to update the input method target window in
3236 * the candidate app window token if needed.
3237 */
Garfield Tane8d84ab2019-10-11 09:49:40 -07003238 void computeImeTargetIfNeeded(ActivityRecord candidate) {
3239 if (mInputMethodTarget != null && mInputMethodTarget.mActivityRecord == candidate) {
lumarkff0ab692018-11-05 20:32:30 +08003240 computeImeTarget(true /* updateImeTarget */);
3241 }
3242 }
3243
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003244 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
lumarkff0ab692018-11-05 20:32:30 +08003245 if (target == mInputMethodTarget && mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003246 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003247 }
3248
lumarkff0ab692018-11-05 20:32:30 +08003249 mInputMethodTarget = target;
3250 mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003251 assignWindowLayers(false /* setLayoutNeeded */);
Tarandeep Singh215929b2019-01-11 18:24:37 -08003252 mInsetsStateController.onImeTargetChanged(target);
Tarandeep Singha6f35612019-01-11 19:50:46 -08003253 updateImeParent();
3254 }
3255
3256 private void updateImeParent() {
Jackal Guo818c3f12019-03-08 18:00:39 +08003257 // Force attaching IME to the display when magnifying, or it would be magnified with
3258 // target app together.
3259 final boolean shouldAttachToDisplay = (mMagnificationSpec != null);
3260 final SurfaceControl newParent =
3261 shouldAttachToDisplay ? mWindowingLayer : computeImeParent();
Tarandeep Singha6f35612019-01-11 19:50:46 -08003262 if (newParent != null) {
Tiger Huanged6794e2019-05-07 20:07:59 +08003263 getPendingTransaction().reparent(mImeWindowsContainers.mSurfaceControl, newParent);
Tarandeep Singha6f35612019-01-11 19:50:46 -08003264 scheduleAnimation();
3265 }
3266 }
3267
3268 /**
3269 * Computes the window the IME should be attached to.
3270 */
3271 @VisibleForTesting
3272 SurfaceControl computeImeParent() {
3273
3274 // Attach it to app if the target is part of an app and such app is covering the entire
3275 // screen. If it's not covering the entire screen the IME might extend beyond the apps
3276 // bounds.
Garfield Tane8d84ab2019-10-11 09:49:40 -07003277 if (mInputMethodTarget != null && mInputMethodTarget.mActivityRecord != null
Riddle Hsu6d6f67c2019-03-14 16:54:26 +08003278 && mInputMethodTarget.getWindowingMode() == WINDOWING_MODE_FULLSCREEN
3279 // An activity with override bounds should be letterboxed inside its parent bounds,
3280 // so it doesn't fill the screen.
Garfield Tane8d84ab2019-10-11 09:49:40 -07003281 && mInputMethodTarget.mActivityRecord.matchParentBounds()) {
3282 return mInputMethodTarget.mActivityRecord.getSurfaceControl();
Tarandeep Singha6f35612019-01-11 19:50:46 -08003283 }
3284
3285 // Otherwise, we just attach it to the display.
3286 return mWindowingLayer;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003287 }
3288
3289 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
3290 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3291 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
3292 }
3293
3294 // Used to indicate we have reached the first window in the range we are interested in.
3295 mTmpWindow = null;
3296
3297 // TODO: Figure-out a more efficient way to do this.
3298 final WindowState candidate = getWindow(w -> {
3299 if (w == top) {
3300 // Reached the first window in the range we are interested in.
3301 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003302 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003303 if (mTmpWindow == null) {
3304 return false;
3305 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003306
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003307 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3308 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003309 }
3310 // If we reached the bottom of the range of windows we are considering,
3311 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003312 if (w == bottom) {
3313 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003314 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003315 return false;
3316 });
3317
3318 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003319 }
3320
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003321 void setLayoutNeeded() {
3322 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
3323 mLayoutNeeded = true;
3324 }
3325
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003326 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003327 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
3328 mLayoutNeeded = false;
3329 }
3330
3331 boolean isLayoutNeeded() {
3332 return mLayoutNeeded;
3333 }
3334
Wale Ogunwale02319a62016-09-26 15:21:22 -07003335 void dumpTokens(PrintWriter pw, boolean dumpAll) {
3336 if (mTokenMap.isEmpty()) {
3337 return;
3338 }
3339 pw.println(" Display #" + mDisplayId);
3340 final Iterator<WindowToken> it = mTokenMap.values().iterator();
3341 while (it.hasNext()) {
3342 final WindowToken token = it.next();
3343 pw.print(" ");
3344 pw.print(token);
3345 if (dumpAll) {
3346 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003347 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07003348 } else {
3349 pw.println();
3350 }
3351 }
lumark588a3e82018-07-20 18:53:54 +08003352
Evan Rosky2289ba12018-11-19 18:28:18 -08003353 if (!mOpeningApps.isEmpty() || !mClosingApps.isEmpty() || !mChangingApps.isEmpty()) {
lumark588a3e82018-07-20 18:53:54 +08003354 pw.println();
3355 if (mOpeningApps.size() > 0) {
3356 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
3357 }
3358 if (mClosingApps.size() > 0) {
3359 pw.print(" mClosingApps="); pw.println(mClosingApps);
3360 }
Evan Rosky2289ba12018-11-19 18:28:18 -08003361 if (mChangingApps.size() > 0) {
3362 pw.print(" mChangingApps="); pw.println(mChangingApps);
3363 }
lumark588a3e82018-07-20 18:53:54 +08003364 }
3365
3366 mUnknownAppVisibilityController.dump(pw, " ");
Wale Ogunwale02319a62016-09-26 15:21:22 -07003367 }
3368
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003369 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003370 final int[] index = new int[1];
3371 forAllWindows(w -> {
3372 final WindowStateAnimator wAnim = w.mWinAnimator;
3373 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
3374 index[0] = index[0] + 1;
3375 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003376 }
3377
Jorim Jaggife762342016-10-13 14:33:27 +02003378 /**
3379 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
3380 */
Issei Suzuki5609ccb2019-06-13 15:04:08 +02003381 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade,
3382 boolean subtle) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003383 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003384 forAllWindows(w -> {
Garfield Tane8d84ab2019-10-11 09:49:40 -07003385 if (w.mActivityRecord == null && policy.canBeHiddenByKeyguardLw(w)
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02003386 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Issei Suzuki5609ccb2019-06-13 15:04:08 +02003387 w.startAnimation(policy.createHiddenByKeyguardExit(
3388 onWallpaper, goingToShade, subtle));
Jorim Jaggife762342016-10-13 14:33:27 +02003389 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003390 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02003391 }
3392
Riddle Hsub2297ad2019-07-26 23:37:25 -06003393 /** @return {@code true} if there is window to wait before enabling the screen. */
3394 boolean shouldWaitForSystemDecorWindowsOnBoot() {
3395 if (!isDefaultDisplay && !supportsSystemDecorations()) {
3396 // Nothing to wait because the secondary display doesn't support system decorations,
3397 // there is no wallpaper, keyguard (status bar) or application (home) window to show
3398 // during booting.
3399 return false;
3400 }
Wale Ogunwale494009b82016-10-21 09:01:38 -07003401
Riddle Hsub2297ad2019-07-26 23:37:25 -06003402 final SparseBooleanArray drawnWindowTypes = new SparseBooleanArray();
3403 // Presuppose keyguard is drawn because if its window isn't attached, we don't know if it
3404 // wants to be shown or hidden, then it should not delay enabling the screen.
3405 drawnWindowTypes.put(TYPE_STATUS_BAR, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003406
Riddle Hsub2297ad2019-07-26 23:37:25 -06003407 final WindowState visibleNotDrawnWindow = getWindow(w -> {
3408 if (w.mViewVisibility == View.VISIBLE && !w.mObscured && !w.isDrawnLw()) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003409 return true;
3410 }
3411 if (w.isDrawnLw()) {
Riddle Hsub2297ad2019-07-26 23:37:25 -06003412 final int type = w.mAttrs.type;
3413 if (type == TYPE_BOOT_PROGRESS || type == TYPE_BASE_APPLICATION
3414 || type == TYPE_WALLPAPER) {
3415 drawnWindowTypes.put(type, true);
3416 } else if (type == TYPE_STATUS_BAR) {
3417 drawnWindowTypes.put(TYPE_STATUS_BAR, mWmService.mPolicy.isKeyguardDrawnLw());
Wale Ogunwale494009b82016-10-21 09:01:38 -07003418 }
3419 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003420 return false;
3421 });
3422
Riddle Hsub2297ad2019-07-26 23:37:25 -06003423 if (visibleNotDrawnWindow != null) {
3424 // Wait for the visible window to be drawn.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003425 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003426 }
3427
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003428 // if the wallpaper service is disabled on the device, we're never going to have
3429 // wallpaper, don't bother waiting for it
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003430 boolean wallpaperEnabled = mWmService.mContext.getResources().getBoolean(
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003431 com.android.internal.R.bool.config_enableWallpaperService)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003432 && mWmService.mContext.getResources().getBoolean(
Daichi Hironoc1432222018-03-29 13:06:07 +09003433 com.android.internal.R.bool.config_checkWallpaperAtBoot)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003434 && !mWmService.mOnlyCore;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003435
Riddle Hsub2297ad2019-07-26 23:37:25 -06003436 final boolean haveBootMsg = drawnWindowTypes.get(TYPE_BOOT_PROGRESS);
3437 final boolean haveApp = drawnWindowTypes.get(TYPE_BASE_APPLICATION);
3438 final boolean haveWallpaper = drawnWindowTypes.get(TYPE_WALLPAPER);
3439 final boolean haveKeyguard = drawnWindowTypes.get(TYPE_STATUS_BAR);
3440
Adrian Roosb125e0b2019-10-02 14:55:14 +02003441 ProtoLog.i(WM_DEBUG_SCREEN_ON,
Riddle Hsub2297ad2019-07-26 23:37:25 -06003442 "******** booted=%b msg=%b haveBoot=%b haveApp=%b haveWall=%b "
3443 + "wallEnabled=%b haveKeyguard=%b",
3444 mWmService.mSystemBooted, mWmService.mShowingBootMessages, haveBootMsg,
3445 haveApp, haveWallpaper, wallpaperEnabled, haveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003446
3447 // If we are turning on the screen to show the boot message, don't do it until the boot
3448 // message is actually displayed.
Riddle Hsub2297ad2019-07-26 23:37:25 -06003449 if (!mWmService.mSystemBooted && !haveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003450 return true;
3451 }
3452
3453 // If we are turning on the screen after the boot is completed normally, don't do so until
3454 // we have the application and wallpaper.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003455 if (mWmService.mSystemBooted
Riddle Hsub2297ad2019-07-26 23:37:25 -06003456 && ((!haveApp && !haveKeyguard) || (wallpaperEnabled && !haveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003457 return true;
3458 }
3459
3460 return false;
3461 }
3462
Jorim Jaggi8f520872018-08-14 17:00:20 +02003463 void updateWindowsForAnimator() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003464 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003465 }
3466
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003467 boolean isInputMethodClientFocus(int uid, int pid) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003468 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003469 if (imFocus == null) {
3470 return false;
3471 }
3472
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003473 if (DEBUG_INPUT_METHOD) {
3474 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003475 Slog.i(TAG_WM, "Current focus: " + mCurrentFocus + " displayId=" + mDisplayId);
3476 Slog.i(TAG_WM, "Last focus: " + mLastFocus + " displayId=" + mDisplayId);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003477 }
3478
Wale Ogunwale494009b82016-10-21 09:01:38 -07003479 if (DEBUG_INPUT_METHOD) {
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003480 Slog.i(TAG_WM, "IM target uid/pid: " + imFocus.mSession.mUid
3481 + "/" + imFocus.mSession.mPid);
3482 Slog.i(TAG_WM, "Requesting client uid/pid: " + uid + "/" + pid);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003483 }
3484
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003485 return imFocus.mSession.mUid == uid && imFocus.mSession.mPid == pid;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003486 }
3487
3488 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003489 final WindowState win = getWindow(
3490 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
3491 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003492 }
3493
Jorim Jaggi956ca412019-01-07 14:49:14 +01003494 void hideTransientBars() {
3495 // TODO(b/118118435): Remove this after migration
3496 final int transientFlags = View.STATUS_BAR_TRANSIENT | View.NAVIGATION_BAR_TRANSIENT;
3497 statusBarVisibilityChanged(mLastStatusBarVisibility & ~transientFlags);
3498
3499 getInsetsPolicy().hideTransient();
3500 }
3501
Tiger Huang7c610aa2018-10-27 00:01:01 +08003502 void statusBarVisibilityChanged(int visibility) {
3503 mLastStatusBarVisibility = visibility;
3504 visibility = getDisplayPolicy().adjustSystemUiVisibilityLw(visibility);
3505 updateStatusBarVisibilityLocked(visibility);
3506 }
3507
3508 private boolean updateStatusBarVisibilityLocked(int visibility) {
3509 if (mLastDispatchedSystemUiVisibility == visibility) {
3510 return false;
3511 }
3512 final int globalDiff = (visibility ^ mLastDispatchedSystemUiVisibility)
3513 // We are only interested in differences of one of the
3514 // clearable flags...
3515 & View.SYSTEM_UI_CLEARABLE_FLAGS
3516 // ...if it has actually been cleared.
3517 & ~visibility;
3518
3519 mLastDispatchedSystemUiVisibility = visibility;
3520 if (isDefaultDisplay) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003521 mWmService.mInputManager.setSystemUiVisibility(visibility);
Tiger Huang7c610aa2018-10-27 00:01:01 +08003522 }
3523 updateSystemUiVisibility(visibility, globalDiff);
3524 return true;
3525 }
3526
Wale Ogunwale494009b82016-10-21 09:01:38 -07003527 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003528 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003529 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003530 final int curValue = w.mSystemUiVisibility;
3531 final int diff = (curValue ^ visibility) & globalDiff;
3532 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003533 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003534 w.mSeq++;
3535 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003536 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003537 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
3538 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07003539 visibility, newValue, diff);
3540 }
3541 } catch (RemoteException e) {
3542 // so sorry
3543 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003544 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003545 }
3546
Tiger Huang7c610aa2018-10-27 00:01:01 +08003547 void reevaluateStatusBarVisibility() {
3548 int visibility = getDisplayPolicy().adjustSystemUiVisibilityLw(mLastStatusBarVisibility);
3549 if (updateStatusBarVisibilityLocked(visibility)) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003550 mWmService.mWindowPlacerLocked.requestTraversal();
Tiger Huang7c610aa2018-10-27 00:01:01 +08003551 }
3552 }
3553
Wale Ogunwale494009b82016-10-21 09:01:38 -07003554 void onWindowFreezeTimeout() {
3555 Slog.w(TAG_WM, "Window freeze timeout expired.");
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003556 mWmService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003557
3558 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07003559 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003560 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003561 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07003562 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003563 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003564 - mWmService.mDisplayFreezeTime);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003565 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003566 }, true /* traverseTopToBottom */);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003567 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003568 }
3569
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003570 // TODO: Super crazy long method that should be broken down...
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003571 void applySurfaceChangesTransaction(boolean recoveringMemory) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003572 final WindowSurfacePlacer surfacePlacer = mWmService.mWindowPlacerLocked;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003573
3574 mTmpUpdateAllDrawn.clear();
3575
3576 int repeats = 0;
3577 do {
3578 repeats++;
3579 if (repeats > 6) {
3580 Slog.w(TAG, "Animation repeat aborted after too many iterations");
3581 clearLayoutNeeded();
3582 break;
3583 }
3584
3585 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
3586 pendingLayoutChanges);
3587
wilsonshihc32538e2018-11-07 17:27:34 +08003588 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
3589 mWallpaperController.adjustWallpaperWindows();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003590 }
3591
Riddle Hsu654a6f92018-07-13 22:59:36 +08003592 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003593 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
Riddle Hsuccf09402019-08-13 00:33:06 +08003594 if (updateOrientation()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003595 setLayoutNeeded();
Shivam Agrawal6472e0e2019-07-03 16:27:49 -07003596 sendNewConfiguration();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003597 }
3598 }
3599
3600 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
3601 setLayoutNeeded();
3602 }
3603
3604 // FIRST LOOP: Perform a layout, if needed.
3605 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
3606 performLayout(repeats == 1, false /* updateInputWindows */);
3607 } else {
3608 Slog.w(TAG, "Layout repeat skipped after too many iterations");
3609 }
3610
3611 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
3612 pendingLayoutChanges = 0;
3613
Jorim Jaggi4981f152019-03-26 18:58:45 +01003614 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "applyPostLayoutPolicy");
3615 try {
3616 mDisplayPolicy.beginPostLayoutPolicyLw();
3617 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
3618 pendingLayoutChanges |= mDisplayPolicy.finishPostLayoutPolicyLw();
3619 } finally {
3620 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
3621 }
Tiger Huang7c610aa2018-10-27 00:01:01 +08003622 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
3623 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
Jorim Jaggif96c90a2018-09-26 16:55:15 +02003624 mInsetsStateController.onPostLayout();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003625 } while (pendingLayoutChanges != 0);
3626
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003627 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003628
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003629 mTmpRecoveringMemory = recoveringMemory;
Jorim Jaggi4981f152019-03-26 18:58:45 +01003630
3631 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "applyWindowSurfaceChanges");
3632 try {
3633 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
3634 } finally {
3635 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
3636 }
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01003637 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003638
Jorim Jaggif1292892018-09-10 11:58:13 +02003639 mLastHasContent = mTmpApplySurfaceChangesTransactionState.displayHasContent;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003640 mWmService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Jorim Jaggif1292892018-09-10 11:58:13 +02003641 mLastHasContent,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003642 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
3643 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003644 true /* inTraversal, must call performTraversalInTrans... below */);
3645
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003646 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
3647 if (wallpaperVisible != mLastWallpaperVisible) {
3648 mLastWallpaperVisible = wallpaperVisible;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003649 mWmService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003650 }
3651
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003652 while (!mTmpUpdateAllDrawn.isEmpty()) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07003653 final ActivityRecord activity = mTmpUpdateAllDrawn.removeLast();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003654 // See if any windows have been drawn, so they (and others associated with them)
3655 // can now be shown.
Garfield Tane8d84ab2019-10-11 09:49:40 -07003656 activity.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003657 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003658 }
3659
Bryce Leef3c6a472017-11-14 14:53:06 -08003660 private void updateBounds() {
Evan Rosky39b6f232018-10-30 18:35:41 -07003661 calculateBounds(mDisplayInfo, mTmpBounds);
Bryce Leef3c6a472017-11-14 14:53:06 -08003662 setBounds(mTmpBounds);
Tiger Huang04dc4cc2019-01-17 18:41:41 +08003663 if (mPortalWindowHandle != null && mParentSurfaceControl != null) {
3664 mPortalWindowHandle.touchableRegion.getBounds(mTmpRect);
3665 if (!mTmpBounds.equals(mTmpRect)) {
3666 mPortalWindowHandle.touchableRegion.set(mTmpBounds);
Tiger Huanged6794e2019-05-07 20:07:59 +08003667 getPendingTransaction().setInputWindowInfo(
3668 mParentSurfaceControl, mPortalWindowHandle);
Tiger Huang04dc4cc2019-01-17 18:41:41 +08003669 }
3670 }
Bryce Leef3c6a472017-11-14 14:53:06 -08003671 }
3672
3673 // Determines the current display bounds based on the current state
Evan Rosky39b6f232018-10-30 18:35:41 -07003674 private void calculateBounds(DisplayInfo displayInfo, Rect out) {
Bryce Leef3c6a472017-11-14 14:53:06 -08003675 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Evan Rosky39b6f232018-10-30 18:35:41 -07003676 final int rotation = displayInfo.rotation;
3677 boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Bryce Leef3c6a472017-11-14 14:53:06 -08003678 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
3679 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Evan Rosky39b6f232018-10-30 18:35:41 -07003680 int width = displayInfo.logicalWidth;
Bryce Leef3c6a472017-11-14 14:53:06 -08003681 int left = (physWidth - width) / 2;
Evan Rosky39b6f232018-10-30 18:35:41 -07003682 int height = displayInfo.logicalHeight;
Bryce Leef3c6a472017-11-14 14:53:06 -08003683 int top = (physHeight - height) / 2;
3684 out.set(left, top, left + width, top + height);
3685 }
3686
Bryce Leef3c6a472017-11-14 14:53:06 -08003687 private void getBounds(Rect out, int orientation) {
3688 getBounds(out);
3689
3690 // Rotate the Rect if needed.
3691 final int currentRotation = mDisplayInfo.rotation;
3692 final int rotationDelta = deltaRotation(currentRotation, orientation);
3693 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
3694 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
3695 mTmpRectF.set(out);
3696 mTmpMatrix.mapRect(mTmpRectF);
3697 mTmpRectF.round(out);
3698 }
3699 }
3700
Evan Rosky730f6e82018-12-03 17:40:11 -08003701 /** @returns the orientation of the display when it's rotation is ROTATION_0. */
3702 int getNaturalOrientation() {
3703 return mBaseDisplayWidth < mBaseDisplayHeight
3704 ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
3705 }
3706
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003707 void performLayout(boolean initial, boolean updateInputWindows) {
Jorim Jaggi4981f152019-03-26 18:58:45 +01003708 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "performLayout");
3709 try {
3710 performLayoutNoTrace(initial, updateInputWindows);
3711 } finally {
3712 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
3713 }
3714 }
3715
3716 private void performLayoutNoTrace(boolean initial, boolean updateInputWindows) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003717 if (!isLayoutNeeded()) {
3718 return;
3719 }
3720 clearLayoutNeeded();
3721
3722 final int dw = mDisplayInfo.logicalWidth;
3723 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003724 if (DEBUG_LAYOUT) {
3725 Slog.v(TAG, "-------------------------------------");
Jorim Jaggi4981f152019-03-26 18:58:45 +01003726 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw
3727 + " dh=" + dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003728 }
3729
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01003730 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
3731 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Jorim Jaggi4981f152019-03-26 18:58:45 +01003732 // TODO: Not sure if we really need to set the rotation here since we are updating from
3733 // the display info above...
Riddle Hsuccf09402019-08-13 00:33:06 +08003734 mDisplayFrames.mRotation = getRotation();
Tiger Huang7c610aa2018-10-27 00:01:01 +08003735 mDisplayPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003736
Adrian Roos5251b1d2018-03-23 18:57:43 +01003737 int seq = mLayoutSeq + 1;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003738 if (seq < 0) seq = 0;
Adrian Roos5251b1d2018-03-23 18:57:43 +01003739 mLayoutSeq = seq;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003740
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003741 // Used to indicate that we have processed the dream window and all additional windows are
3742 // behind it.
3743 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003744 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003745
3746 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003747 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003748
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003749 // Used to indicate that we have processed the dream window and all additional attached
3750 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003751 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003752 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003753
3754 // Now perform layout of attached windows, which usually depend on the position of the
3755 // window they are attached to. XXX does not deal with windows that are attached to windows
3756 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003757 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003758
3759 // Window frames may have changed. Tell the input dispatcher about it.
Arthur Hung95b38a92018-07-20 18:56:12 +08003760 mInputMonitor.layoutInputConsumers(dw, dh);
3761 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003762 if (updateInputWindows) {
Arthur Hung95b38a92018-07-20 18:56:12 +08003763 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003764 }
3765
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003766 mWmService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003767 }
3768
3769 /**
3770 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
3771 * In portrait mode, it grabs the full screenshot.
3772 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003773 * @param config of the output bitmap
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003774 */
chaviw0315a1a2018-03-05 15:28:35 -08003775 Bitmap screenshotDisplayLocked(Bitmap.Config config) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003776 if (!mWmService.mPolicy.isScreenOn()) {
chaviw0315a1a2018-03-05 15:28:35 -08003777 if (DEBUG_SCREENSHOT) {
3778 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003779 }
chaviw0315a1a2018-03-05 15:28:35 -08003780 return null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003781 }
chaviwfbe47df2017-11-10 16:14:49 -08003782
chaviw0315a1a2018-03-05 15:28:35 -08003783 int dw = mDisplayInfo.logicalWidth;
3784 int dh = mDisplayInfo.logicalHeight;
chaviwfbe47df2017-11-10 16:14:49 -08003785
chaviw0315a1a2018-03-05 15:28:35 -08003786 if (dw <= 0 || dh <= 0) {
3787 return null;
3788 }
chaviwfbe47df2017-11-10 16:14:49 -08003789
chaviw0315a1a2018-03-05 15:28:35 -08003790 final Rect frame = new Rect(0, 0, dw, dh);
chaviwfbe47df2017-11-10 16:14:49 -08003791
chaviw0315a1a2018-03-05 15:28:35 -08003792 // The screenshot API does not apply the current screen rotation.
3793 int rot = mDisplay.getRotation();
chaviwfbe47df2017-11-10 16:14:49 -08003794
chaviw0315a1a2018-03-05 15:28:35 -08003795 if (rot == ROTATION_90 || rot == ROTATION_270) {
3796 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3797 }
chaviwfbe47df2017-11-10 16:14:49 -08003798
chaviw0315a1a2018-03-05 15:28:35 -08003799 // SurfaceFlinger is not aware of orientation, so convert our logical
3800 // crop to SurfaceFlinger's portrait orientation.
3801 convertCropForSurfaceFlinger(frame, rot, dw, dh);
3802
3803 final ScreenRotationAnimation screenRotationAnimation =
Vadim Caenb3715832019-08-13 17:06:38 +02003804 mWmService.mRoot.getDisplayContent(DEFAULT_DISPLAY).getRotationAnimation();
chaviw0315a1a2018-03-05 15:28:35 -08003805 final boolean inRotation = screenRotationAnimation != null &&
3806 screenRotationAnimation.isAnimating();
3807 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
3808
3809 // TODO(b/68392460): We should screenshot Task controls directly
3810 // but it's difficult at the moment as the Task doesn't have the
3811 // correct size set.
chaviw08520a02018-09-10 16:44:56 -07003812 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, inRotation, rot);
chaviw0315a1a2018-03-05 15:28:35 -08003813 if (bitmap == null) {
3814 Slog.w(TAG_WM, "Failed to take screenshot");
3815 return null;
3816 }
3817
3818 // Create a copy of the screenshot that is immutable and backed in ashmem.
3819 // This greatly reduces the overhead of passing the bitmap between processes.
3820 final Bitmap ret = bitmap.createAshmemBitmap(config);
3821 bitmap.recycle();
3822 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003823 }
3824
3825 // TODO: Can this use createRotationMatrix()?
3826 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3827 if (rot == Surface.ROTATION_90) {
3828 final int tmp = crop.top;
3829 crop.top = dw - crop.right;
3830 crop.right = crop.bottom;
3831 crop.bottom = dw - crop.left;
3832 crop.left = tmp;
3833 } else if (rot == Surface.ROTATION_180) {
3834 int tmp = crop.top;
3835 crop.top = dh - crop.bottom;
3836 crop.bottom = dh - tmp;
3837 tmp = crop.right;
3838 crop.right = dw - crop.left;
3839 crop.left = dw - tmp;
3840 } else if (rot == Surface.ROTATION_270) {
3841 final int tmp = crop.top;
3842 crop.top = crop.left;
3843 crop.left = dh - crop.bottom;
3844 crop.bottom = crop.right;
3845 crop.right = dh - tmp;
3846 }
3847 }
3848
Wale Ogunwale1666e312016-12-16 11:27:18 -08003849 void setExitingTokensHasVisible(boolean hasVisible) {
3850 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3851 mExitingTokens.get(i).hasVisible = hasVisible;
3852 }
3853
3854 // Initialize state of exiting applications.
3855 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3856 }
3857
3858 void removeExistingTokensIfPossible() {
3859 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3860 final WindowToken token = mExitingTokens.get(i);
3861 if (!token.hasVisible) {
3862 mExitingTokens.remove(i);
3863 }
3864 }
3865
3866 // Time to remove any exiting applications?
3867 mTaskStackContainers.removeExistingAppTokensIfPossible();
3868 }
3869
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003870 @Override
3871 void onDescendantOverrideConfigurationChanged() {
3872 setLayoutNeeded();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003873 mWmService.requestTraversal();
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003874 }
3875
David Stevens9440dc82017-03-16 19:00:20 -07003876 boolean okToDisplay() {
3877 if (mDisplayId == DEFAULT_DISPLAY) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003878 return !mWmService.mDisplayFrozen
3879 && mWmService.mDisplayEnabled && mWmService.mPolicy.isScreenOn();
David Stevens9440dc82017-03-16 19:00:20 -07003880 }
3881 return mDisplayInfo.state == Display.STATE_ON;
3882 }
3883
3884 boolean okToAnimate() {
3885 return okToDisplay() &&
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003886 (mDisplayId != DEFAULT_DISPLAY || mWmService.mPolicy.okToAnimate());
David Stevens9440dc82017-03-16 19:00:20 -07003887 }
3888
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003889 static final class TaskForResizePointSearchResult {
Wale Ogunwale85fb19a2019-12-05 10:41:05 +09003890 private Task taskForResize;
3891 private int x;
3892 private int y;
3893 private int delta;
3894 private Rect mTmpRect = new Rect();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003895
Wale Ogunwale85fb19a2019-12-05 10:41:05 +09003896 Task process(WindowContainer root, int x, int y, int delta) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003897 taskForResize = null;
Wale Ogunwale85fb19a2019-12-05 10:41:05 +09003898 this.x = x;
3899 this.y = y;
3900 this.delta = delta;
3901 mTmpRect.setEmpty();
3902
3903 final PooledFunction f = PooledLambda.obtainFunction(
3904 TaskForResizePointSearchResult::processTask, this, PooledLambda.__(Task.class));
3905 root.forAllTasks(f);
3906 f.recycle();
3907
3908 return taskForResize;
3909 }
3910
3911 private boolean processTask(Task task) {
3912 if (!task.getStack().getWindowConfiguration().canResizeTask()) {
3913 return true;
3914 }
3915
3916 if (task.getWindowingMode() == WINDOWING_MODE_FULLSCREEN) {
3917 return true;
3918 }
3919
3920 // We need to use the task's dim bounds (which is derived from the visible bounds of
3921 // its apps windows) for any touch-related tests. Can't use the task's original
3922 // bounds because it might be adjusted to fit the content frame. One example is when
3923 // the task is put to top-left quadrant, the actual visible area would not start at
3924 // (0,0) after it's adjusted for the status bar.
3925 task.getDimBounds(mTmpRect);
3926 mTmpRect.inset(-delta, -delta);
3927 if (mTmpRect.contains(x, y)) {
3928 mTmpRect.inset(delta, delta);
3929
3930 if (!mTmpRect.contains(x, y)) {
3931 taskForResize = task;
3932 return true;
3933 }
3934 // User touched inside the task. No need to look further,
3935 // focus transfer will be handled in ACTION_UP.
3936 return true;
3937 }
3938
3939 return false;
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003940 }
3941 }
Robert Carr3b716242016-08-16 16:02:21 -07003942
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003943 private static final class ApplySurfaceChangesTransactionState {
3944 boolean displayHasContent;
3945 boolean obscured;
3946 boolean syswin;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003947 float preferredRefreshRate;
3948 int preferredModeId;
3949
3950 void reset() {
3951 displayHasContent = false;
3952 obscured = false;
3953 syswin = false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003954 preferredRefreshRate = 0;
3955 preferredModeId = 0;
3956 }
3957 }
3958
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003959 /**
3960 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3961 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3962 * homogeneous children type which is currently required by sub-classes of
3963 * {@link WindowContainer} class.
3964 */
3965 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3966
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003967 DisplayChildWindowContainer(WindowManagerService service) {
3968 super(service);
3969 }
3970
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003971 @Override
3972 boolean fillsParent() {
3973 return true;
3974 }
3975
3976 @Override
3977 boolean isVisible() {
3978 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003979 }
3980 }
3981
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003982 /**
3983 * Window container class that contains all containers on this display relating to Apps.
3984 * I.e Activities.
3985 */
Louis Changdc077272019-11-12 16:52:56 +08003986 private final class TaskStackContainers extends DisplayChildWindowContainer<ActivityStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003987 /**
3988 * A control placed at the appropriate level for transitions to occur.
3989 */
chaviw23ee71c2017-12-18 11:29:41 -08003990 SurfaceControl mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02003991 SurfaceControl mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02003992 SurfaceControl mHomeAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003993
Robert Carrf7a7ca82017-12-06 13:17:07 -08003994 /**
3995 * Given that the split-screen divider does not have an AppWindowToken, it
3996 * will have to live inside of a "NonAppWindowContainer", in particular
3997 * {@link DisplayContent#mAboveAppWindowsContainers}. However, in visual Z order
3998 * it will need to be interleaved with some of our children, appearing on top of
3999 * both docked stacks but underneath any assistant stacks.
4000 *
4001 * To solve this problem we have this anchor control, which will always exist so
4002 * we can always assign it the correct value in our {@link #assignChildLayers}.
4003 * Likewise since it always exists, {@link AboveAppWindowContainers} can always
4004 * assign the divider a layer relative to it. This way we prevent linking lifecycle
4005 * events between the two containers.
4006 */
4007 SurfaceControl mSplitScreenDividerAnchor = null;
4008
Wale Ogunwale61911492017-10-11 08:50:50 -07004009 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
4010 // through the list to find them.
Louis Changdc077272019-11-12 16:52:56 +08004011 private ActivityStack mHomeStack = null;
Louis Chang2453d062019-11-19 22:30:48 +08004012 private ActivityStack mRecentsStack = null;
Louis Changdc077272019-11-12 16:52:56 +08004013 private ActivityStack mPinnedStack = null;
4014 private ActivityStack mSplitScreenPrimaryStack = null;
Wale Ogunwale61911492017-10-11 08:50:50 -07004015
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004016 TaskStackContainers(WindowManagerService service) {
4017 super(service);
4018 }
4019
Wale Ogunwale61911492017-10-11 08:50:50 -07004020 /**
4021 * Returns the topmost stack on the display that is compatible with the input windowing mode
4022 * and activity type. Null is no compatible stack on the display.
4023 */
Louis Changdc077272019-11-12 16:52:56 +08004024 ActivityStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07004025 if (activityType == ACTIVITY_TYPE_HOME) {
4026 return mHomeStack;
4027 }
4028 if (windowingMode == WINDOWING_MODE_PINNED) {
4029 return mPinnedStack;
4030 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
4031 return mSplitScreenPrimaryStack;
4032 }
4033 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
Louis Changdc077272019-11-12 16:52:56 +08004034 final ActivityStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004035 if (activityType == ACTIVITY_TYPE_UNDEFINED
4036 && windowingMode == stack.getWindowingMode()) {
4037 // Passing in undefined type means we want to match the topmost stack with the
4038 // windowing mode.
4039 return stack;
4040 }
Wale Ogunwale61911492017-10-11 08:50:50 -07004041 if (stack.isCompatible(windowingMode, activityType)) {
4042 return stack;
4043 }
4044 }
4045 return null;
4046 }
4047
4048 @VisibleForTesting
Louis Changdc077272019-11-12 16:52:56 +08004049 ActivityStack getTopStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07004050 return mTaskStackContainers.getChildCount() > 0
4051 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
4052 }
4053
Louis Chang2453d062019-11-19 22:30:48 +08004054 int getIndexOf(ActivityStack stack) {
4055 return mTaskStackContainers.mChildren.indexOf(stack);
4056 }
4057
Louis Changdc077272019-11-12 16:52:56 +08004058 ActivityStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07004059 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
4060 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
4061 }
4062 return mHomeStack;
4063 }
4064
Louis Chang2453d062019-11-19 22:30:48 +08004065 ActivityStack getRecentsStack() {
4066 return mRecentsStack;
4067 }
4068
Louis Changdc077272019-11-12 16:52:56 +08004069 ActivityStack getPinnedStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07004070 return mPinnedStack;
4071 }
4072
Louis Changdc077272019-11-12 16:52:56 +08004073 ActivityStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07004074 return mSplitScreenPrimaryStack;
4075 }
4076
Winson Chunge2d72172018-01-25 17:46:20 +00004077 ArrayList<Task> getVisibleTasks() {
4078 final ArrayList<Task> visibleTasks = new ArrayList<>();
4079 forAllTasks(task -> {
4080 if (task.isVisible()) {
4081 visibleTasks.add(task);
4082 }
4083 });
4084 return visibleTasks;
4085 }
4086
Louis Changdc077272019-11-12 16:52:56 +08004087 void onStackWindowingModeChanged(ActivityStack stack) {
Wale Ogunwale61911492017-10-11 08:50:50 -07004088 removeStackReferenceIfNeeded(stack);
4089 addStackReferenceIfNeeded(stack);
4090 if (stack == mPinnedStack && getTopStack() != stack) {
4091 // Looks like this stack changed windowing mode to pinned. Move it to the top.
4092 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
4093 }
4094 }
4095
Louis Changdc077272019-11-12 16:52:56 +08004096 private void addStackReferenceIfNeeded(ActivityStack stack) {
Wale Ogunwale61911492017-10-11 08:50:50 -07004097 if (stack.isActivityTypeHome()) {
4098 if (mHomeStack != null) {
Louis Chang7d0037c2018-08-13 12:42:06 +08004099 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
Wale Ogunwale61911492017-10-11 08:50:50 -07004100 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
Louis Chang7d0037c2018-08-13 12:42:06 +08004101
Wale Ogunwale61911492017-10-11 08:50:50 -07004102 }
Louis Chang7d0037c2018-08-13 12:42:06 +08004103 mHomeStack = stack;
Louis Chang2453d062019-11-19 22:30:48 +08004104 } else if (stack.isActivityTypeRecents()) {
4105 if (mRecentsStack != null && mRecentsStack != stack) {
4106 throw new IllegalArgumentException(
4107 "addStackReferenceIfNeeded: recents stack=" + mRecentsStack
4108 + " already exist on display=" + this + " stack=" + stack);
4109 }
4110 mRecentsStack = stack;
Wale Ogunwale61911492017-10-11 08:50:50 -07004111 }
4112 final int windowingMode = stack.getWindowingMode();
4113 if (windowingMode == WINDOWING_MODE_PINNED) {
4114 if (mPinnedStack != null) {
4115 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
4116 + mPinnedStack + " already exist on display=" + this
4117 + " stack=" + stack);
4118 }
4119 mPinnedStack = stack;
4120 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
4121 if (mSplitScreenPrimaryStack != null) {
4122 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
4123 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
4124 + " already exist on display=" + this + " stack=" + stack);
4125 }
4126 mSplitScreenPrimaryStack = stack;
Louis Chang2453d062019-11-19 22:30:48 +08004127 // TODO(display-merge): Remove cast
4128 ((ActivityDisplay) this.mDisplayContent).onSplitScreenModeActivated();
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004129 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07004130 }
4131 }
4132
Louis Chang2453d062019-11-19 22:30:48 +08004133 void removeStackReferenceIfNeeded(ActivityStack stack) {
Wale Ogunwale61911492017-10-11 08:50:50 -07004134 if (stack == mHomeStack) {
4135 mHomeStack = null;
Louis Chang2453d062019-11-19 22:30:48 +08004136 } else if (stack == mRecentsStack) {
4137 mRecentsStack = null;
Wale Ogunwale61911492017-10-11 08:50:50 -07004138 } else if (stack == mPinnedStack) {
4139 mPinnedStack = null;
4140 } else if (stack == mSplitScreenPrimaryStack) {
4141 mSplitScreenPrimaryStack = null;
Louis Chang2453d062019-11-19 22:30:48 +08004142 // TODO(display-merge): Remove cast
4143 ((ActivityDisplay) this.mDisplayContent).onSplitScreenModeDismissed();
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004144 // Re-set the split-screen create mode whenever the split-screen stack is removed.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004145 mWmService.setDockedStackCreateStateLocked(
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004146 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
4147 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07004148 }
Andrii Kulian839def92016-11-02 10:58:58 -07004149 }
4150
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07004151 @Override
Louis Changdc077272019-11-12 16:52:56 +08004152 void addChild(ActivityStack stack, int position) {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07004153 addStackReferenceIfNeeded(stack);
4154 position = findPositionForStack(position, stack, true /* adding */);
4155
4156 super.addChild(stack, position);
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07004157
4158 // The reparenting case is handled in WindowContainer.
4159 if (!stack.mReparenting) {
4160 setLayoutNeeded();
4161 stack.onDisplayChanged(DisplayContent.this);
4162 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004163 }
4164
Wale Ogunwale61911492017-10-11 08:50:50 -07004165 @Override
Louis Changdc077272019-11-12 16:52:56 +08004166 protected void removeChild(ActivityStack stack) {
Wale Ogunwale61911492017-10-11 08:50:50 -07004167 super.removeChild(stack);
Louis Chang2453d062019-11-19 22:30:48 +08004168 // TODO(display-merge): Remove cast
4169 ((ActivityDisplay) this.mDisplayContent).onStackRemoved(stack);
Wale Ogunwale61911492017-10-11 08:50:50 -07004170 removeStackReferenceIfNeeded(stack);
4171 }
Bryce Lee00d586d2017-07-28 20:48:43 -07004172
4173 @Override
4174 boolean isOnTop() {
4175 // Considered always on top
4176 return true;
4177 }
4178
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004179 @Override
Louis Changdc077272019-11-12 16:52:56 +08004180 void positionChildAt(int position, ActivityStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07004181 if (child.getWindowConfiguration().isAlwaysOnTop()
Louis Chang2453d062019-11-19 22:30:48 +08004182 && position != POSITION_TOP && position != mChildren.size()) {
Andrii Kuliand2765632016-12-12 22:26:34 -08004183 // This stack is always-on-top, override the default behavior.
4184 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
4185
4186 // Moving to its current position, as we must call super but we don't want to
4187 // perform any meaningful action.
4188 final int currentPosition = mChildren.indexOf(child);
4189 super.positionChildAt(currentPosition, child, false /* includingParents */);
4190 return;
4191 }
lumarkd0b5c8f2019-09-29 11:30:37 +08004192 // We don't allow untrusted display to top when task stack moves to top,
4193 // until user tapping this display to change display position as top intentionally.
4194 if (isUntrustedVirtualDisplay() && !getParent().isOnTop()) {
4195 includingParents = false;
4196 }
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004197 final int targetPosition = findPositionForStack(position, child, false /* adding */);
4198 super.positionChildAt(targetPosition, child, includingParents);
4199
Riddle Hsu2f6e1742018-08-23 22:44:36 +08004200 if (includingParents) {
4201 // We still want to move the display of this stack container to top because even the
4202 // target position is adjusted to non-top, the intention of the condition is to have
4203 // higher z-order to gain focus (e.g. moving a task of a fullscreen stack to front
4204 // in a non-top display which is using picture-in-picture mode).
4205 final int topChildPosition = getChildCount() - 1;
4206 if (targetPosition < topChildPosition && position >= topChildPosition) {
4207 getParent().positionChildAt(POSITION_TOP, this /* child */,
4208 true /* includingParents */);
4209 }
4210 }
4211
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004212 setLayoutNeeded();
4213 }
4214
4215 /**
4216 * When stack is added or repositioned, find a proper position for it.
4217 * This will make sure that pinned stack always stays on top.
4218 * @param requestedPosition Position requested by caller.
4219 * @param stack Stack to be added or positioned.
4220 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
4221 * @return The proper position for the stack.
4222 */
Louis Changdc077272019-11-12 16:52:56 +08004223 private int findPositionForStack(int requestedPosition, ActivityStack stack,
4224 boolean adding) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09004225 if (stack.inPinnedWindowingMode()) {
Kazuki Takisef85197b2018-06-18 18:18:36 +09004226 return POSITION_TOP;
Kazuki Takise148d00a2018-05-31 15:32:19 +09004227 }
4228
Kazuki Takisef85197b2018-06-18 18:18:36 +09004229 final int topChildPosition = mChildren.size() - 1;
4230 int belowAlwaysOnTopPosition = POSITION_BOTTOM;
4231 for (int i = topChildPosition; i >= 0; --i) {
Louis Chang2453d062019-11-19 22:30:48 +08004232 // Since a stack could be repositioned while being one of the child, return
4233 // current index if that's the same stack we are positioning and it is always on
4234 // top.
4235 final boolean sameStack = getStacks().get(i) == stack;
4236 if ((sameStack && stack.isAlwaysOnTop())
4237 || (!sameStack && !getStacks().get(i).isAlwaysOnTop())) {
Kazuki Takisef85197b2018-06-18 18:18:36 +09004238 belowAlwaysOnTopPosition = i;
4239 break;
4240 }
4241 }
4242
4243 // The max possible position we can insert the stack at.
4244 int maxPosition = POSITION_TOP;
4245 // The min possible position we can insert the stack at.
4246 int minPosition = POSITION_BOTTOM;
4247
4248 if (stack.isAlwaysOnTop()) {
4249 if (hasPinnedStack()) {
4250 // Always-on-top stacks go below the pinned stack.
4251 maxPosition = getStacks().indexOf(mPinnedStack) - 1;
4252 }
4253 // Always-on-top stacks need to be above all other stacks.
4254 minPosition = belowAlwaysOnTopPosition !=
4255 POSITION_BOTTOM ? belowAlwaysOnTopPosition : topChildPosition;
4256 } else {
4257 // Other stacks need to be below the always-on-top stacks.
4258 maxPosition = belowAlwaysOnTopPosition !=
Arthur Hung928d8dc2018-07-18 15:38:05 +08004259 POSITION_BOTTOM ? belowAlwaysOnTopPosition : 0;
Kazuki Takisef85197b2018-06-18 18:18:36 +09004260 }
4261
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07004262 // Cap the requested position to something reasonable for the previous position check
4263 // below.
4264 if (requestedPosition == POSITION_TOP) {
4265 requestedPosition = mChildren.size();
4266 } else if (requestedPosition == POSITION_BOTTOM) {
4267 requestedPosition = 0;
4268 }
4269
Louis Chang2453d062019-11-19 22:30:48 +08004270 int targetPosition = requestedPosition;
4271 targetPosition = Math.min(targetPosition, maxPosition);
4272 targetPosition = Math.max(targetPosition, minPosition);
4273
Kazuki Takisef85197b2018-06-18 18:18:36 +09004274 int prevPosition = getStacks().indexOf(stack);
4275 // The positions we calculated above (maxPosition, minPosition) do not take into
4276 // consideration the following edge cases.
4277 // 1) We need to adjust the position depending on the value "adding".
4278 // 2) When we are moving a stack to another position, we also need to adjust the
4279 // position depending on whether the stack is moving to a higher or lower position.
4280 if ((targetPosition != requestedPosition) &&
4281 (adding || targetPosition < prevPosition)) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09004282 targetPosition++;
4283 }
4284
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004285 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08004286 }
4287
4288 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004289 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
4290 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004291 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004292 if (super.forAllWindows(callback, traverseTopToBottom)) {
4293 return true;
4294 }
4295 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4296 return true;
4297 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004298 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004299 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4300 return true;
4301 }
4302 if (super.forAllWindows(callback, traverseTopToBottom)) {
4303 return true;
4304 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004305 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004306 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004307 }
4308
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004309 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004310 boolean traverseTopToBottom) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07004311 // For legacy reasons we process the TaskStack.mExitingActivities first here before the
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004312 // app tokens.
4313 // TODO: Investigate if we need to continue to do this or if we can just process them
4314 // in-order.
4315 if (traverseTopToBottom) {
4316 for (int i = mChildren.size() - 1; i >= 0; --i) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07004317 final List<ActivityRecord> activities = mChildren.get(i).mExitingActivities;
4318 for (int j = activities.size() - 1; j >= 0; --j) {
4319 if (activities.get(j).forAllWindowsUnchecked(callback,
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004320 traverseTopToBottom)) {
4321 return true;
4322 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004323 }
4324 }
4325 } else {
4326 final int count = mChildren.size();
4327 for (int i = 0; i < count; ++i) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07004328 final List<ActivityRecord> activities = mChildren.get(i).mExitingActivities;
4329 final int appTokensCount = activities.size();
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004330 for (int j = 0; j < appTokensCount; j++) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07004331 if (activities.get(j).forAllWindowsUnchecked(callback,
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004332 traverseTopToBottom)) {
4333 return true;
4334 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004335 }
4336 }
4337 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004338 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004339 }
4340
Wale Ogunwale1666e312016-12-16 11:27:18 -08004341 void setExitingTokensHasVisible(boolean hasVisible) {
4342 for (int i = mChildren.size() - 1; i >= 0; --i) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07004343 final ArrayList<ActivityRecord> activities = mChildren.get(i).mExitingActivities;
4344 for (int j = activities.size() - 1; j >= 0; --j) {
4345 activities.get(j).hasVisible = hasVisible;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004346 }
4347 }
4348 }
4349
4350 void removeExistingAppTokensIfPossible() {
4351 for (int i = mChildren.size() - 1; i >= 0; --i) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07004352 final ArrayList<ActivityRecord> activities = mChildren.get(i).mExitingActivities;
4353 for (int j = activities.size() - 1; j >= 0; --j) {
4354 final ActivityRecord activity = activities.get(j);
4355 if (!activity.hasVisible && !mClosingApps.contains(activity)
4356 && (!activity.mIsExiting || activity.isEmpty())) {
4357 // Make sure there is no animation running on this activity, so any windows
Wale Ogunwale1666e312016-12-16 11:27:18 -08004358 // associated with it will be removed as soon as their animations are
4359 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02004360 cancelAnimation();
Adrian Roosb125e0b2019-10-02 14:55:14 +02004361 ProtoLog.v(WM_DEBUG_ADD_REMOVE,
Garfield Tane8d84ab2019-10-11 09:49:40 -07004362 "performLayout: Activity exiting now removed %s", activity);
4363 activity.removeIfPossible();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004364 }
4365 }
4366 }
4367 }
4368
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004369 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004370 int getOrientation() {
Garfield Tan98eead32019-11-18 13:24:53 -08004371 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004372 // Apps and their containers are not allowed to specify an orientation while the
Garfield Tan98eead32019-11-18 13:24:53 -08004373 // docked stack is visible...except for the home stack if the docked stack is
4374 // minimized and it actually set something and the bounds is different from the
4375 // display.
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004376 if (mHomeStack != null && mHomeStack.isVisible()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004377 && mDividerControllerLocked.isMinimizedDock()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004378 && !(mDividerControllerLocked.isHomeStackResizable()
4379 && mHomeStack.matchParentBounds())) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004380 final int orientation = mHomeStack.getOrientation();
4381 if (orientation != SCREEN_ORIENTATION_UNSET) {
4382 return orientation;
4383 }
4384 }
4385 return SCREEN_ORIENTATION_UNSPECIFIED;
4386 }
4387
4388 final int orientation = super.getOrientation();
4389 if (orientation != SCREEN_ORIENTATION_UNSET
4390 && orientation != SCREEN_ORIENTATION_BEHIND) {
Adrian Roosb125e0b2019-10-02 14:55:14 +02004391 ProtoLog.v(WM_DEBUG_ORIENTATION,
4392 "App is requesting an orientation, return %d for display id=%d",
4393 orientation, mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004394 return orientation;
4395 }
4396
Adrian Roosb125e0b2019-10-02 14:55:14 +02004397 ProtoLog.v(WM_DEBUG_ORIENTATION,
4398 "No app is requesting an orientation, return %d for display id=%d",
4399 getLastOrientation(), mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004400 // The next app has not been requested to be visible, so we keep the current orientation
4401 // to prevent freezing/unfreezing the display too early.
Riddle Hsuccf09402019-08-13 00:33:06 +08004402 return getLastOrientation();
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004403 }
Robert Carrb1579c82017-09-05 14:54:47 -07004404
4405 @Override
4406 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004407 assignStackOrdering(t);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004408
Robert Carr2f8aa392018-01-31 14:46:51 -08004409 for (int i = 0; i < mChildren.size(); i++) {
Louis Changdc077272019-11-12 16:52:56 +08004410 final ActivityStack s = mChildren.get(i);
Robert Carr2f8aa392018-01-31 14:46:51 -08004411 s.assignChildLayers(t);
4412 }
4413 }
4414
4415 void assignStackOrdering(SurfaceControl.Transaction t) {
Issei Suzuki25a9e2b2019-08-14 16:46:26 +02004416 if (getParent() == null) {
4417 return;
4418 }
4419 mTmpAlwaysOnTopStacks.clear();
4420 mTmpHomeStacks.clear();
4421 mTmpNormalStacks.clear();
4422 for (int i = 0; i < mChildren.size(); ++i) {
Louis Changdc077272019-11-12 16:52:56 +08004423 final ActivityStack s = mChildren.get(i);
Issei Suzuki25a9e2b2019-08-14 16:46:26 +02004424 if (s.isAlwaysOnTop()) {
4425 mTmpAlwaysOnTopStacks.add(s);
4426 } else if (s.isActivityTypeHome()) {
4427 mTmpHomeStacks.add(s);
4428 } else {
4429 mTmpNormalStacks.add(s);
4430 }
4431 }
Robert Carrf7a7ca82017-12-06 13:17:07 -08004432
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004433 int layer = 0;
Issei Suzuki25a9e2b2019-08-14 16:46:26 +02004434 // Place home stacks to the bottom.
4435 for (int i = 0; i < mTmpHomeStacks.size(); i++) {
4436 mTmpHomeStacks.get(i).assignLayer(t, layer++);
4437 }
4438 // The home animation layer is between the home stacks and the normal stacks.
4439 final int layerForHomeAnimationLayer = layer++;
4440 int layerForSplitScreenDividerAnchor = layer++;
4441 int layerForAnimationLayer = layer++;
4442 for (int i = 0; i < mTmpNormalStacks.size(); i++) {
Louis Changdc077272019-11-12 16:52:56 +08004443 final ActivityStack s = mTmpNormalStacks.get(i);
Issei Suzuki25a9e2b2019-08-14 16:46:26 +02004444 s.assignLayer(t, layer++);
4445 if (s.inSplitScreenWindowingMode()) {
4446 // The split screen divider anchor is located above the split screen window.
4447 layerForSplitScreenDividerAnchor = layer++;
4448 }
lumark9bca6b42019-10-17 18:35:22 +08004449 if (s.isTaskAnimating() || s.isAppTransitioning()) {
Issei Suzuki25a9e2b2019-08-14 16:46:26 +02004450 // The animation layer is located above the highest animating stack and no
4451 // higher.
4452 layerForAnimationLayer = layer++;
4453 }
4454 }
4455 // The boosted animation layer is between the normal stacks and the always on top
4456 // stacks.
4457 final int layerForBoostedAnimationLayer = layer++;
4458 for (int i = 0; i < mTmpAlwaysOnTopStacks.size(); i++) {
4459 mTmpAlwaysOnTopStacks.get(i).assignLayer(t, layer++);
4460 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004461
Issei Suzuki25a9e2b2019-08-14 16:46:26 +02004462 t.setLayer(mHomeAppAnimationLayer, layerForHomeAnimationLayer);
4463 t.setLayer(mAppAnimationLayer, layerForAnimationLayer);
4464 t.setLayer(mSplitScreenDividerAnchor, layerForSplitScreenDividerAnchor);
4465 t.setLayer(mBoostedAppAnimationLayer, layerForBoostedAnimationLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07004466 }
4467
4468 @Override
Jorim Jaggi391790622018-04-18 15:30:44 +02004469 SurfaceControl getAppAnimationLayer(@AnimationLayer int animationLayer) {
4470 switch (animationLayer) {
4471 case ANIMATION_LAYER_BOOSTED:
4472 return mBoostedAppAnimationLayer;
4473 case ANIMATION_LAYER_HOME:
4474 return mHomeAppAnimationLayer;
4475 case ANIMATION_LAYER_STANDARD:
4476 default:
4477 return mAppAnimationLayer;
4478 }
chaviw23ee71c2017-12-18 11:29:41 -08004479 }
4480
Robert Carrf7a7ca82017-12-06 13:17:07 -08004481 SurfaceControl getSplitScreenDividerAnchor() {
4482 return mSplitScreenDividerAnchor;
4483 }
4484
chaviw23ee71c2017-12-18 11:29:41 -08004485 @Override
Wale Ogunwalec17418e2019-10-13 23:00:40 +02004486 void onParentChanged(ConfigurationContainer newParent, ConfigurationContainer oldParent) {
Robert Carrb1579c82017-09-05 14:54:47 -07004487 if (getParent() != null) {
Wale Ogunwalec17418e2019-10-13 23:00:40 +02004488 super.onParentChanged(newParent, oldParent, () -> {
Issei Suzuki25a9e2b2019-08-14 16:46:26 +02004489 mAppAnimationLayer = makeChildSurface(null)
4490 .setName("animationLayer")
4491 .build();
4492 mBoostedAppAnimationLayer = makeChildSurface(null)
4493 .setName("boostedAnimationLayer")
4494 .build();
4495 mHomeAppAnimationLayer = makeChildSurface(null)
4496 .setName("homeAnimationLayer")
4497 .build();
4498 mSplitScreenDividerAnchor = makeChildSurface(null)
4499 .setName("splitScreenDividerAnchor")
4500 .build();
4501 getPendingTransaction()
4502 .show(mAppAnimationLayer)
4503 .show(mBoostedAppAnimationLayer)
4504 .show(mHomeAppAnimationLayer)
4505 .show(mSplitScreenDividerAnchor);
4506 });
Robert Carrb1579c82017-09-05 14:54:47 -07004507 } else {
Wale Ogunwalec17418e2019-10-13 23:00:40 +02004508 super.onParentChanged(newParent, oldParent);
Vishnu Nair33197392019-08-30 10:29:37 -07004509 mWmService.mTransactionFactory.get()
chaviw9f6171e2019-06-07 16:33:50 -07004510 .remove(mAppAnimationLayer)
4511 .remove(mBoostedAppAnimationLayer)
4512 .remove(mHomeAppAnimationLayer)
4513 .remove(mSplitScreenDividerAnchor)
4514 .apply();
chaviw23ee71c2017-12-18 11:29:41 -08004515 mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02004516 mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02004517 mHomeAppAnimationLayer = null;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004518 mSplitScreenDividerAnchor = null;
Robert Carrb1579c82017-09-05 14:54:47 -07004519 }
4520 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004521 }
4522
Robert Carree4d4b92017-11-22 12:21:46 -08004523 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004524 AboveAppWindowContainers(String name, WindowManagerService service) {
4525 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08004526 }
4527
Robert Carrb9506032018-02-13 13:54:00 -08004528 @Override
Vishnu Nair83537a72018-07-19 21:27:48 -07004529 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
4530 final SurfaceControl.Builder builder = super.makeChildSurface(child);
4531 if (child instanceof WindowToken && ((WindowToken) child).mRoundedCornerOverlay) {
4532 // To draw above the ColorFade layer during the screen off transition, the
4533 // rounded corner overlays need to be at the root of the surface hierarchy.
4534 // TODO: move the ColorLayer into the display overlay layer such that this is not
4535 // necessary anymore.
4536 builder.setParent(null);
4537 }
4538 return builder;
4539 }
4540
4541 @Override
Robert Carrb9506032018-02-13 13:54:00 -08004542 void assignChildLayers(SurfaceControl.Transaction t) {
4543 assignChildLayers(t, null /* imeContainer */);
4544 }
4545
Robert Carree4d4b92017-11-22 12:21:46 -08004546 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
4547 boolean needAssignIme = imeContainer != null
4548 && imeContainer.getSurfaceControl() != null;
4549 for (int j = 0; j < mChildren.size(); ++j) {
4550 final WindowToken wt = mChildren.get(j);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004551
4552 // See {@link mSplitScreenDividerAnchor}
4553 if (wt.windowType == TYPE_DOCK_DIVIDER) {
4554 wt.assignRelativeLayer(t, mTaskStackContainers.getSplitScreenDividerAnchor(), 1);
4555 continue;
4556 }
Vishnu Nair83537a72018-07-19 21:27:48 -07004557 if (wt.mRoundedCornerOverlay) {
4558 wt.assignLayer(t, WindowManagerPolicy.COLOR_FADE_LAYER + 1);
4559 continue;
4560 }
Robert Carree4d4b92017-11-22 12:21:46 -08004561 wt.assignLayer(t, j);
4562 wt.assignChildLayers(t);
4563
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004564 int layer = mWmService.mPolicy.getWindowLayerFromTypeLw(
Robert Carree4d4b92017-11-22 12:21:46 -08004565 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08004566
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004567 if (needAssignIme && layer >= mWmService.mPolicy.getWindowLayerFromTypeLw(
Robert Carrb8e78fa2017-11-28 13:07:58 -08004568 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004569 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08004570 needAssignIme = false;
4571 }
4572 }
4573 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004574 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08004575 }
4576 }
4577 }
4578
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004579 /**
4580 * Window container class that contains all containers on this display that are not related to
4581 * Apps. E.g. status bar.
4582 */
Robert Carree4d4b92017-11-22 12:21:46 -08004583 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07004584 /**
4585 * Compares two child window tokens returns -1 if the first is lesser than the second in
4586 * terms of z-order and 1 otherwise.
4587 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004588 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07004589 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004590 mWmService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004591 token1.mOwnerCanManageAppTokens)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004592 < mWmService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004593 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004594
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004595 private final Predicate<WindowState> mGetOrientingWindow = w -> {
chaviw15ad49f2019-04-24 15:05:39 -07004596 if (!w.isVisibleLw() || !w.mLegacyPolicyVisibilityAfterAnim) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004597 return false;
4598 }
4599 final int req = w.mAttrs.screenOrientation;
4600 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
4601 || req == SCREEN_ORIENTATION_UNSET) {
4602 return false;
4603 }
4604 return true;
4605 };
4606
Wale Ogunwale3a931692016-11-02 16:49:48 -07004607 private final String mName;
chaviwf29223e2018-01-31 13:23:51 -08004608 private final Dimmer mDimmer = new Dimmer(this);
4609 private final Rect mTmpDimBoundsRect = new Rect();
4610
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004611 NonAppWindowContainers(String name, WindowManagerService service) {
4612 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07004613 mName = name;
4614 }
4615
Wale Ogunwaledfbeed72019-11-20 08:57:39 -08004616 @Override
4617 boolean hasActivity() {
4618 // I am a non-app-window-container :P
4619 return false;
4620 }
4621
Wale Ogunwale3a931692016-11-02 16:49:48 -07004622 void addChild(WindowToken token) {
4623 addChild(token, mWindowComparator);
4624 }
4625
4626 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004627 int getOrientation() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004628 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004629 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004630 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004631
4632 if (win != null) {
4633 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004634 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004635 mLastKeyguardForcedOrientation = req;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004636 if (mWmService.mKeyguardGoingAway) {
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07004637 // Keyguard can't affect the orientation if it is going away...
4638 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
4639 return SCREEN_ORIENTATION_UNSET;
4640 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004641 }
Adrian Roosb125e0b2019-10-02 14:55:14 +02004642 ProtoLog.v(WM_DEBUG_ORIENTATION,
4643 "%s forcing orientation to %d for display id=%d", win, req,
4644 mDisplayId);
Andrii Kulian8ee72852017-03-10 10:36:45 -08004645 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004646 }
4647
Andrii Kulian8ee72852017-03-10 10:36:45 -08004648 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004649
Jorim Jaggi1c530592018-04-06 15:11:47 +02004650 // Only allow force setting the orientation when all unknown visibilities have been
4651 // resolved, as otherwise we just may be starting another occluding activity.
4652 final boolean isUnoccluding =
lumark588a3e82018-07-20 18:53:54 +08004653 mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
4654 && mUnknownAppVisibilityController.allResolved();
Jorim Jaggi1c530592018-04-06 15:11:47 +02004655 if (policy.isKeyguardShowingAndNotOccluded() || isUnoccluding) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004656 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004657 }
4658
4659 return SCREEN_ORIENTATION_UNSET;
4660 }
4661
4662 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07004663 String getName() {
4664 return mName;
4665 }
chaviwf29223e2018-01-31 13:23:51 -08004666
4667 @Override
4668 Dimmer getDimmer() {
4669 return mDimmer;
4670 }
4671
4672 @Override
4673 void prepareSurfaces() {
4674 mDimmer.resetDimStates();
4675 super.prepareSurfaces();
4676 getBounds(mTmpDimBoundsRect);
4677
4678 if (mDimmer.updateDims(getPendingTransaction(), mTmpDimBoundsRect)) {
4679 scheduleAnimation();
4680 }
4681 }
Robert Carr3b716242016-08-16 16:02:21 -07004682 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01004683
Robert Carrb1579c82017-09-05 14:54:47 -07004684 SurfaceControl.Builder makeSurface(SurfaceSession s) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004685 return mWmService.makeSurfaceBuilder(s)
Robert Carrb1579c82017-09-05 14:54:47 -07004686 .setParent(mWindowingLayer);
4687 }
4688
4689 @Override
4690 SurfaceSession getSession() {
4691 return mSession;
4692 }
4693
4694 @Override
4695 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07004696 SurfaceSession s = child != null ? child.getSession() : getSession();
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +00004697 final SurfaceControl.Builder b = mWmService.makeSurfaceBuilder(s).setContainerLayer();
Robert Carrf59b8dd2017-10-02 18:58:36 -07004698 if (child == null) {
4699 return b;
4700 }
4701
Robert Carree4d4b92017-11-22 12:21:46 -08004702 return b.setName(child.getName())
4703 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07004704 }
4705
4706 /**
4707 * The makeSurface variants are for use by the window-container
4708 * hierarchy. makeOverlay here is a function for various non windowing
4709 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
4710 * and other potpourii.
4711 */
4712 SurfaceControl.Builder makeOverlay() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004713 return mWmService.makeSurfaceBuilder(mSession)
Robert Carrb1579c82017-09-05 14:54:47 -07004714 .setParent(mOverlayLayer);
4715 }
4716
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004717 /**
4718 * Reparents the given surface to mOverlayLayer.
4719 */
4720 void reparentToOverlay(Transaction transaction, SurfaceControl surface) {
Robert Carr10584fa2019-01-14 15:55:19 -08004721 transaction.reparent(surface, mOverlayLayer);
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004722 }
4723
Robert Carrb1579c82017-09-05 14:54:47 -07004724 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carr24be9ab2018-04-30 17:54:53 -07004725 if (spec.scale != 1.0) {
4726 mMagnificationSpec = spec;
4727 } else {
4728 mMagnificationSpec = null;
4729 }
Jackal Guo818c3f12019-03-08 18:00:39 +08004730 // Re-parent IME's SurfaceControl when MagnificationSpec changed.
4731 updateImeParent();
Robert Carr24be9ab2018-04-30 17:54:53 -07004732
Robert Carrf59b8dd2017-10-02 18:58:36 -07004733 applyMagnificationSpec(getPendingTransaction(), spec);
4734 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07004735 }
4736
Robert Carr24be9ab2018-04-30 17:54:53 -07004737 void reapplyMagnificationSpec() {
4738 if (mMagnificationSpec != null) {
4739 applyMagnificationSpec(getPendingTransaction(), mMagnificationSpec);
4740 }
4741 }
4742
Robert Carrb1579c82017-09-05 14:54:47 -07004743 @Override
Wale Ogunwalec17418e2019-10-13 23:00:40 +02004744 void onParentChanged(ConfigurationContainer newParent, ConfigurationContainer oldParent) {
Robert Carrb1579c82017-09-05 14:54:47 -07004745 // Since we are the top of the SurfaceControl hierarchy here
4746 // we create the root surfaces explicitly rather than chaining
Riddle Hsu3a4bb612019-01-31 00:02:22 +08004747 // up as the default implementation in onParentChanged does. So we
Robert Carrb1579c82017-09-05 14:54:47 -07004748 // explicitly do NOT call super here.
4749 }
4750
4751 @Override
4752 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07004753
4754 // These are layers as children of "mWindowingLayer"
4755 mBelowAppWindowsContainers.assignLayer(t, 0);
4756 mTaskStackContainers.assignLayer(t, 1);
4757 mAboveAppWindowsContainers.assignLayer(t, 2);
4758
lumarkff0ab692018-11-05 20:32:30 +08004759 final WindowState imeTarget = mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08004760 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08004761
Robert Carree4d4b92017-11-22 12:21:46 -08004762 // In the case where we have an IME target that is not in split-screen
4763 // mode IME assignment is easy. We just need the IME to go directly above
4764 // the target. This way children of the target will naturally go above the IME
4765 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08004766 //
Robert Carree4d4b92017-11-22 12:21:46 -08004767 // In the case of split-screen windowing mode, we need to elevate the IME above the
4768 // docked divider while keeping the app itself below the docked divider, so instead
4769 // we use relative layering of the IME targets child windows, and place the
4770 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08004771 //
Robert Carr234b6332018-03-20 13:38:16 -07004772 // In the case the IME target is animating, the animation Z order may be different
4773 // than the WindowContainer Z order, so it's difficult to be sure we have the correct
4774 // IME target. In this case we just layer the IME over all transitions by placing it in the
4775 // above applications layer.
4776 //
Robert Carree4d4b92017-11-22 12:21:46 -08004777 // In the case where we have no IME target we assign it where it's base layer would
4778 // place it in the AboveAppWindowContainers.
Tarandeep Singh69ab1042019-07-30 13:30:03 -07004779 //
4780 // Keep IME window in mAboveAppWindowsContainers as long as app's starting window exists
4781 // so it get's layered above the starting window.
4782 if (imeTarget != null
Garfield Tane8d84ab2019-10-11 09:49:40 -07004783 && !(imeTarget.mActivityRecord != null && imeTarget.mActivityRecord.hasStartingWindow())
lumark9bca6b42019-10-17 18:35:22 +08004784 && (!(imeTarget.inSplitScreenWindowingMode()
4785 || imeTarget.mToken.isAppTransitioning())
Tarandeep Singh69ab1042019-07-30 13:30:03 -07004786 && (imeTarget.getSurfaceControl() != null))) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004787 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07004788 // TODO: We need to use an extra level on the app surface to ensure
4789 // this is always above SurfaceView but always below attached window.
4790 1);
Robert Carree4d4b92017-11-22 12:21:46 -08004791 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07004792 }
4793
4794 // Above we have assigned layers to our children, now we ask them to assign
4795 // layers to their children.
4796 mBelowAppWindowsContainers.assignChildLayers(t);
4797 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08004798 mAboveAppWindowsContainers.assignChildLayers(t,
4799 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07004800 mImeWindowsContainers.assignChildLayers(t);
4801 }
4802
4803 /**
4804 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
4805 * that the IME target is one of the docked applications. We'd like the docked divider to be
4806 * above both of the applications, and we'd like the IME to be above the docked divider.
4807 * However we need child windows of the applications to be above the IME (Text drag handles).
4808 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
4809 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
Tiger Huang7c610aa2018-10-27 00:01:01 +08004810 * with {@link WindowState#assignLayer}
Robert Carrb1579c82017-09-05 14:54:47 -07004811 */
4812 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
Robert Carrbb5de662018-04-26 13:29:51 -07004813 child.assignRelativeLayer(t, mImeWindowsContainers.getSurfaceControl(), 1);
Robert Carrb1579c82017-09-05 14:54:47 -07004814 }
4815
4816 @Override
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004817 void prepareSurfaces() {
Jorim Jaggi4981f152019-03-26 18:58:45 +01004818 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "prepareSurfaces");
4819 try {
Tiger Huanged6794e2019-05-07 20:07:59 +08004820 final Transaction transaction = getPendingTransaction();
Jorim Jaggi4981f152019-03-26 18:58:45 +01004821 super.prepareSurfaces();
Tiger Huanged6794e2019-05-07 20:07:59 +08004822
4823 // TODO: Once we totally eliminate global transaction we will pass transaction in here
4824 // rather than merging to global.
4825 SurfaceControl.mergeToGlobalTransaction(transaction);
Jorim Jaggi4981f152019-03-26 18:58:45 +01004826 } finally {
4827 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
4828 }
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004829 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004830
Jorim Jaggibe418292018-03-26 16:14:12 +02004831 void assignStackOrdering() {
4832 mTaskStackContainers.assignStackOrdering(getPendingTransaction());
Robert Carr2f8aa392018-01-31 14:46:51 -08004833 }
Chavi Weingarten3a748552018-05-14 17:32:42 +00004834
4835 /**
4836 * Increment the deferral count to determine whether to update the IME target.
4837 */
4838 void deferUpdateImeTarget() {
4839 mDeferUpdateImeTargetCount++;
4840 }
4841
4842 /**
4843 * Decrement the deferral count to determine whether to update the IME target. If the count
4844 * reaches 0, a new ime target will get computed.
4845 */
4846 void continueUpdateImeTarget() {
4847 if (mDeferUpdateImeTargetCount == 0) {
4848 return;
4849 }
4850
4851 mDeferUpdateImeTargetCount--;
4852 if (mDeferUpdateImeTargetCount == 0) {
4853 computeImeTarget(true /* updateImeTarget */);
4854 }
4855 }
4856
4857 /**
4858 * @return Whether a new IME target should be computed.
4859 */
4860 private boolean canUpdateImeTarget() {
4861 return mDeferUpdateImeTargetCount == 0;
4862 }
Arthur Hung95b38a92018-07-20 18:56:12 +08004863
4864 InputMonitor getInputMonitor() {
4865 return mInputMonitor;
4866 }
Jorim Jaggif1292892018-09-10 11:58:13 +02004867
4868 /**
4869 * @return Cached value whether we told display manager that we have content.
4870 */
4871 boolean getLastHasContent() {
4872 return mLastHasContent;
4873 }
Arthur Hungbe5ce212018-09-13 18:41:56 +08004874
4875 void registerPointerEventListener(@NonNull PointerEventListener listener) {
Riddle Hsu2588ab02019-02-25 14:23:56 +08004876 mPointerEventDispatcher.registerInputEventListener(listener);
Arthur Hungbe5ce212018-09-13 18:41:56 +08004877 }
4878
4879 void unregisterPointerEventListener(@NonNull PointerEventListener listener) {
Riddle Hsu2588ab02019-02-25 14:23:56 +08004880 mPointerEventDispatcher.unregisterInputEventListener(listener);
Arthur Hungbe5ce212018-09-13 18:41:56 +08004881 }
lumark588a3e82018-07-20 18:53:54 +08004882
4883 void prepareAppTransition(@WindowManager.TransitionType int transit,
Wale Ogunwale3a256e62018-12-06 14:41:18 -08004884 boolean alwaysKeepCurrent) {
4885 prepareAppTransition(transit, alwaysKeepCurrent, 0 /* flags */, false /* forceOverride */);
4886 }
4887
4888 void prepareAppTransition(@WindowManager.TransitionType int transit,
lumark588a3e82018-07-20 18:53:54 +08004889 boolean alwaysKeepCurrent, @WindowManager.TransitionFlags int flags,
4890 boolean forceOverride) {
4891 final boolean prepared = mAppTransition.prepareAppTransitionLocked(
4892 transit, alwaysKeepCurrent, flags, forceOverride);
4893 if (prepared && okToAnimate()) {
4894 mSkipAppTransitionAnimation = false;
4895 }
4896 }
4897
4898 void executeAppTransition() {
4899 if (mAppTransition.isTransitionSet()) {
Adrian Roosb125e0b2019-10-02 14:55:14 +02004900 ProtoLog.w(WM_DEBUG_APP_TRANSITIONS,
4901 "Execute app transition: %s, displayId: %d Callers=%s",
4902 mAppTransition, mDisplayId, Debug.getCallers(5));
lumark588a3e82018-07-20 18:53:54 +08004903 mAppTransition.setReady();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004904 mWmService.mWindowPlacerLocked.requestTraversal();
lumark588a3e82018-07-20 18:53:54 +08004905 }
4906 }
4907
4908 /**
4909 * Update pendingLayoutChanges after app transition has finished.
4910 */
4911 void handleAnimatingStoppedAndTransition() {
4912 int changes = 0;
4913
4914 mAppTransition.setIdle();
4915
4916 for (int i = mNoAnimationNotifyOnTransitionFinished.size() - 1; i >= 0; i--) {
4917 final IBinder token = mNoAnimationNotifyOnTransitionFinished.get(i);
4918 mAppTransition.notifyAppTransitionFinishedLocked(token);
4919 }
4920 mNoAnimationNotifyOnTransitionFinished.clear();
4921
4922 mWallpaperController.hideDeferredWallpapersIfNeeded();
4923
4924 onAppTransitionDone();
4925
4926 changes |= FINISH_LAYOUT_REDO_LAYOUT;
4927 if (DEBUG_WALLPAPER_LIGHT) {
4928 Slog.v(TAG_WM, "Wallpaper layer changed: assigning layers + relayout");
4929 }
4930 computeImeTarget(true /* updateImeTarget */);
wilsonshihc32538e2018-11-07 17:27:34 +08004931 mWallpaperMayChange = true;
lumark588a3e82018-07-20 18:53:54 +08004932 // Since the window list has been rebuilt, focus might have to be recomputed since the
4933 // actual order of windows might have changed again.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004934 mWmService.mFocusMayChange = true;
lumark588a3e82018-07-20 18:53:54 +08004935
4936 pendingLayoutChanges |= changes;
4937 }
Andrii Kulian15cfb422018-11-07 13:38:49 -08004938
Wale Ogunwale3a256e62018-12-06 14:41:18 -08004939 /** Check if pending app transition is for activity / task launch. */
4940 boolean isNextTransitionForward() {
4941 final int transit = mAppTransition.getAppTransition();
4942 return transit == TRANSIT_ACTIVITY_OPEN
4943 || transit == TRANSIT_TASK_OPEN
4944 || transit == TRANSIT_TASK_TO_FRONT;
4945 }
4946
Andrii Kulian15cfb422018-11-07 13:38:49 -08004947 /**
4948 * @see Display#FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS
4949 */
4950 boolean supportsSystemDecorations() {
Arthur Hung879a8ab2019-03-13 19:51:10 +08004951 return (mWmService.mDisplayWindowSettings.shouldShowSystemDecorsLocked(this)
Andrii Kuliandd989612019-02-21 12:13:28 -08004952 || (mDisplay.getFlags() & FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS) != 0
Chilun891aed22019-05-02 18:08:47 +08004953 || (mWmService.mForceDesktopModeOnExternalDisplays && !isUntrustedVirtualDisplay()))
Arthur Hung879a8ab2019-03-13 19:51:10 +08004954 // VR virtual display will be used to run and render 2D app within a VR experience.
4955 && mDisplayId != mWmService.mVr2dDisplayId;
Andrii Kulian15cfb422018-11-07 13:38:49 -08004956 }
chaviwff2e7d82018-11-02 11:11:27 -07004957
Tiger Huang04dc4cc2019-01-17 18:41:41 +08004958 /**
Chilun891aed22019-05-02 18:08:47 +08004959 * @return {@code true} if the display is non-system created virtual display.
4960 */
4961 boolean isUntrustedVirtualDisplay() {
4962 return mDisplay.getType() == Display.TYPE_VIRTUAL
4963 && mDisplay.getOwnerUid() != Process.SYSTEM_UID;
4964 }
4965
4966 /**
chaviwff2e7d82018-11-02 11:11:27 -07004967 * Re-parent the DisplayContent's top surfaces, {@link #mWindowingLayer} and
Tiger Huangd8ec9382019-04-18 14:35:09 -07004968 * {@link #mOverlayLayer} to the specified SurfaceControl.
chaviwff2e7d82018-11-02 11:11:27 -07004969 *
Tiger Huangd8ec9382019-04-18 14:35:09 -07004970 * @param win The window which owns the SurfaceControl. This indicates the z-order of the
4971 * windows of this display against the windows on the parent display.
Tiger Huang04dc4cc2019-01-17 18:41:41 +08004972 * @param sc The new SurfaceControl, where the DisplayContent's surfaces will be re-parented to.
chaviwff2e7d82018-11-02 11:11:27 -07004973 */
Tiger Huangd8ec9382019-04-18 14:35:09 -07004974 void reparentDisplayContent(WindowState win, SurfaceControl sc) {
4975 mParentWindow = win;
Jackal Guoc43a0a62019-04-23 09:15:14 +08004976 mParentWindow.addEmbeddedDisplayContent(this);
Tiger Huang04dc4cc2019-01-17 18:41:41 +08004977 mParentSurfaceControl = sc;
4978 if (mPortalWindowHandle == null) {
4979 mPortalWindowHandle = createPortalWindowHandle(sc.toString());
4980 }
Tiger Huanged6794e2019-05-07 20:07:59 +08004981 getPendingTransaction().setInputWindowInfo(sc, mPortalWindowHandle)
Vadim Caen1812c762019-08-12 11:38:22 +02004982 .reparent(mSurfaceControl, sc);
Tiger Huang04dc4cc2019-01-17 18:41:41 +08004983 }
4984
Tiger Huangd8ec9382019-04-18 14:35:09 -07004985 /**
4986 * Get the window which owns the surface that this DisplayContent is re-parented to.
4987 *
4988 * @return the parent window.
4989 */
4990 WindowState getParentWindow() {
4991 return mParentWindow;
4992 }
4993
4994 /**
4995 * Update the location of this display in the parent window. This enables windows in this
4996 * display to compute the global transformation matrix.
4997 *
4998 * @param win The parent window of this display.
4999 * @param x The x coordinate in the parent window.
5000 * @param y The y coordinate in the parent window.
5001 */
5002 void updateLocation(WindowState win, int x, int y) {
5003 if (mParentWindow != win) {
5004 throw new IllegalArgumentException(
5005 "The given window is not the parent window of this display.");
5006 }
Jackal Guoc43a0a62019-04-23 09:15:14 +08005007 if (!mLocationInParentWindow.equals(x, y)) {
5008 mLocationInParentWindow.set(x, y);
Tiger Huangd8ec9382019-04-18 14:35:09 -07005009 if (mWmService.mAccessibilityController != null) {
Jacky Kaof93252b2019-07-18 15:19:52 +08005010 mWmService.mAccessibilityController.onSomeWindowResizedOrMovedLocked(mDisplayId);
Tiger Huangd8ec9382019-04-18 14:35:09 -07005011 }
Jackal Guoc43a0a62019-04-23 09:15:14 +08005012 notifyLocationInParentDisplayChanged();
Tiger Huangd8ec9382019-04-18 14:35:09 -07005013 }
5014 }
5015
5016 Point getLocationInParentWindow() {
5017 return mLocationInParentWindow;
5018 }
5019
Jackal Guoc43a0a62019-04-23 09:15:14 +08005020 Point getLocationInParentDisplay() {
5021 final Point location = new Point();
5022 if (mParentWindow != null) {
5023 // LocationInParentWindow indicates the offset to (0,0) of window, but what we need is
5024 // the offset to (0,0) of display.
5025 DisplayContent dc = this;
5026 do {
5027 final WindowState displayParent = dc.getParentWindow();
5028 location.x += displayParent.getFrameLw().left
5029 + (dc.getLocationInParentWindow().x * displayParent.mGlobalScale + 0.5f);
5030 location.y += displayParent.getFrameLw().top
5031 + (dc.getLocationInParentWindow().y * displayParent.mGlobalScale + 0.5f);
5032 dc = displayParent.getDisplayContent();
5033 } while (dc != null && dc.getParentWindow() != null);
5034 }
5035 return location;
5036 }
5037
5038 void notifyLocationInParentDisplayChanged() {
5039 forAllWindows(w -> {
5040 w.updateLocationInParentDisplayIfNeeded();
5041 }, false /* traverseTopToBottom */);
5042 }
5043
Tarandeep Singha6f35612019-01-11 19:50:46 -08005044 @VisibleForTesting
5045 SurfaceControl getWindowingLayer() {
5046 return mWindowingLayer;
5047 }
5048
Vadim Caenf8474262019-08-12 17:52:01 +02005049 SurfaceControl getOverlayLayer() {
5050 return mOverlayLayer;
5051 }
5052
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005053 /**
Adrian Roos4ffc8972019-02-07 20:45:11 +01005054 * Updates the display's system gesture exclusion.
5055 *
5056 * @return true, if the exclusion changed.
5057 */
5058 boolean updateSystemGestureExclusion() {
5059 if (mSystemGestureExclusionListeners.getRegisteredCallbackCount() == 0) {
5060 // No one's interested anyways.
5061 return false;
5062 }
5063
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005064 final Region systemGestureExclusion = Region.obtain();
5065 mSystemGestureExclusionWasRestricted = calculateSystemGestureExclusion(
5066 systemGestureExclusion, mSystemGestureExclusionUnrestricted);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005067 try {
5068 if (mSystemGestureExclusion.equals(systemGestureExclusion)) {
5069 return false;
5070 }
5071 mSystemGestureExclusion.set(systemGestureExclusion);
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005072 final Region unrestrictedOrNull = mSystemGestureExclusionWasRestricted
5073 ? mSystemGestureExclusionUnrestricted : null;
Adrian Roos4ffc8972019-02-07 20:45:11 +01005074 for (int i = mSystemGestureExclusionListeners.beginBroadcast() - 1; i >= 0; --i) {
5075 try {
5076 mSystemGestureExclusionListeners.getBroadcastItem(i)
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005077 .onSystemGestureExclusionChanged(mDisplayId, systemGestureExclusion,
5078 unrestrictedOrNull);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005079 } catch (RemoteException e) {
5080 Slog.e(TAG, "Failed to notify SystemGestureExclusionListener", e);
5081 }
5082 }
5083 mSystemGestureExclusionListeners.finishBroadcast();
5084 return true;
5085 } finally {
5086 systemGestureExclusion.recycle();
5087 }
5088 }
5089
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005090 /**
5091 * Calculates the system gesture exclusion.
5092 *
5093 * @param outExclusion will be set to the gesture exclusion region
5094 * @param outExclusionUnrestricted will be set to the gesture exclusion region without
5095 * any restrictions applied.
5096 * @return whether any restrictions were applied, i.e. outExclusion and outExclusionUnrestricted
5097 * differ.
5098 */
Adrian Roos4ffc8972019-02-07 20:45:11 +01005099 @VisibleForTesting
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005100 boolean calculateSystemGestureExclusion(Region outExclusion, @Nullable
5101 Region outExclusionUnrestricted) {
5102 outExclusion.setEmpty();
5103 if (outExclusionUnrestricted != null) {
5104 outExclusionUnrestricted.setEmpty();
5105 }
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005106 final Region unhandled = Region.obtain();
5107 unhandled.set(0, 0, mDisplayFrames.mDisplayWidth, mDisplayFrames.mDisplayHeight);
5108
Tiger Huang332793b2019-10-29 23:21:27 +08005109 final Rect leftEdge = mInsetsStateController.getSourceProvider(ITYPE_LEFT_GESTURES)
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005110 .getSource().getFrame();
Tiger Huang332793b2019-10-29 23:21:27 +08005111 final Rect rightEdge = mInsetsStateController.getSourceProvider(ITYPE_RIGHT_GESTURES)
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005112 .getSource().getFrame();
5113
Adrian Roos4ffc8972019-02-07 20:45:11 +01005114 final Region touchableRegion = Region.obtain();
5115 final Region local = Region.obtain();
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005116 final int[] remainingLeftRight =
5117 {mSystemGestureExclusionLimit, mSystemGestureExclusionLimit};
Adrian Roos4ffc8972019-02-07 20:45:11 +01005118
Adrian Roosb1063792019-06-28 12:10:51 +02005119 // Traverse all windows top down to assemble the gesture exclusion rects.
Adrian Roos4ffc8972019-02-07 20:45:11 +01005120 // For each window, we only take the rects that fall within its touchable region.
5121 forAllWindows(w -> {
5122 if (w.cantReceiveTouchInput() || !w.isVisible()
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005123 || (w.getAttrs().flags & FLAG_NOT_TOUCHABLE) != 0
5124 || unhandled.isEmpty()) {
Adrian Roos4ffc8972019-02-07 20:45:11 +01005125 return;
5126 }
Adrian Roos4ffc8972019-02-07 20:45:11 +01005127
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005128 // Get the touchable region of the window, and intersect with where the screen is still
5129 // touchable, i.e. touchable regions on top are not covering it yet.
Adrian Roosb1063792019-06-28 12:10:51 +02005130 w.getEffectiveTouchableRegion(touchableRegion);
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005131 touchableRegion.op(unhandled, Op.INTERSECT);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005132
Adrian Roos019a52b2019-07-02 16:47:44 +02005133 if (w.isImplicitlyExcludingAllSystemGestures()) {
5134 local.set(touchableRegion);
5135 } else {
5136 rectListToRegion(w.getSystemGestureExclusion(), local);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005137
Adrian Roos019a52b2019-07-02 16:47:44 +02005138 // Transform to display coordinates
5139 local.scale(w.mGlobalScale);
5140 final Rect frame = w.getWindowFrames().mFrame;
5141 local.translate(frame.left, frame.top);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005142
Adrian Roos019a52b2019-07-02 16:47:44 +02005143 // A window can only exclude system gestures where it is actually touchable
5144 local.op(touchableRegion, Op.INTERSECT);
5145 }
Adrian Roos4ffc8972019-02-07 20:45:11 +01005146
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005147 // Apply restriction if necessary.
5148 if (needsGestureExclusionRestrictions(w, mLastDispatchedSystemUiVisibility)) {
5149
5150 // Processes the region along the left edge.
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005151 remainingLeftRight[0] = addToGlobalAndConsumeLimit(local, outExclusion, leftEdge,
5152 remainingLeftRight[0], w, EXCLUSION_LEFT);
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005153
5154 // Processes the region along the right edge.
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005155 remainingLeftRight[1] = addToGlobalAndConsumeLimit(local, outExclusion, rightEdge,
5156 remainingLeftRight[1], w, EXCLUSION_RIGHT);
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005157
5158 // Adds the middle (unrestricted area)
5159 final Region middle = Region.obtain(local);
5160 middle.op(leftEdge, Op.DIFFERENCE);
5161 middle.op(rightEdge, Op.DIFFERENCE);
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005162 outExclusion.op(middle, Op.UNION);
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005163 middle.recycle();
5164 } else {
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005165 boolean loggable = needsGestureExclusionRestrictions(w, 0 /* lastSysUiVis */);
5166 if (loggable) {
5167 addToGlobalAndConsumeLimit(local, outExclusion, leftEdge,
5168 Integer.MAX_VALUE, w, EXCLUSION_LEFT);
5169 addToGlobalAndConsumeLimit(local, outExclusion, rightEdge,
5170 Integer.MAX_VALUE, w, EXCLUSION_RIGHT);
5171 }
5172 outExclusion.op(local, Op.UNION);
5173 }
5174 if (outExclusionUnrestricted != null) {
5175 outExclusionUnrestricted.op(local, Op.UNION);
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005176 }
5177 unhandled.op(touchableRegion, Op.DIFFERENCE);
5178 }, true /* topToBottom */);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005179 local.recycle();
5180 touchableRegion.recycle();
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005181 unhandled.recycle();
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005182 return remainingLeftRight[0] < mSystemGestureExclusionLimit
5183 || remainingLeftRight[1] < mSystemGestureExclusionLimit;
Adrian Roos4ffc8972019-02-07 20:45:11 +01005184 }
5185
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005186 /**
5187 * @return Whether gesture exclusion area should be restricted from the window depending on the
5188 * current SystemUI visibility flags.
5189 */
5190 private static boolean needsGestureExclusionRestrictions(WindowState win, int sysUiVisibility) {
5191 final int type = win.mAttrs.type;
5192 final int stickyHideNavFlags =
5193 SYSTEM_UI_FLAG_HIDE_NAVIGATION | SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
5194 final boolean stickyHideNav =
5195 (sysUiVisibility & stickyHideNavFlags) == stickyHideNavFlags;
5196 return !stickyHideNav && type != TYPE_INPUT_METHOD && type != TYPE_STATUS_BAR
5197 && win.getActivityType() != ACTIVITY_TYPE_HOME;
5198 }
5199
5200 /**
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005201 * @return Whether gesture exclusion area should be logged for the given window
5202 */
5203 static boolean logsGestureExclusionRestrictions(WindowState win) {
Adrian Roos1c2e9a12019-08-20 18:23:47 +02005204 if (win.mWmService.mConstants.mSystemGestureExclusionLogDebounceTimeoutMillis <= 0) {
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005205 return false;
5206 }
5207 final WindowManager.LayoutParams attrs = win.getAttrs();
5208 final int type = attrs.type;
5209 return type != TYPE_WALLPAPER
5210 && type != TYPE_APPLICATION_STARTING
5211 && type != TYPE_NAVIGATION_BAR
5212 && (attrs.flags & FLAG_NOT_TOUCHABLE) == 0
5213 && needsGestureExclusionRestrictions(win, 0 /* sysUiVisibility */)
5214 && win.getDisplayContent().mDisplayPolicy.hasSideGestures();
5215 }
5216
5217 /**
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005218 * Adds a local gesture exclusion area to the global area while applying a limit per edge.
5219 *
5220 * @param local The gesture exclusion area to add.
5221 * @param global The destination.
5222 * @param edge Only processes the part in that region.
5223 * @param limit How much limit in pixels we have.
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005224 * @param win The WindowState that is being processed
5225 * @param side The side that is being processed, either {@link WindowState#EXCLUSION_LEFT} or
5226 * {@link WindowState#EXCLUSION_RIGHT}
5227 * @return How much of the limit is remaining.
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005228 */
5229 private static int addToGlobalAndConsumeLimit(Region local, Region global, Rect edge,
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005230 int limit, WindowState win, int side) {
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005231 final Region r = Region.obtain(local);
5232 r.op(edge, Op.INTERSECT);
5233
5234 final int[] remaining = {limit};
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005235 final int[] requestedExclusion = {0};
Adrian Roos2351d5f2019-07-03 15:35:07 +02005236 forEachRectReverse(r, rect -> {
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005237 if (remaining[0] <= 0) {
5238 return;
5239 }
5240 final int height = rect.height();
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005241 requestedExclusion[0] += height;
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005242 if (height > remaining[0]) {
Adrian Roos2351d5f2019-07-03 15:35:07 +02005243 rect.top = rect.bottom - remaining[0];
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005244 }
5245 remaining[0] -= height;
5246 global.op(rect, Op.UNION);
5247 });
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005248
5249 final int grantedExclusion = limit - remaining[0];
5250 win.setLastExclusionHeights(side, requestedExclusion[0], grantedExclusion);
5251
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005252 r.recycle();
5253 return remaining[0];
5254 }
5255
Adrian Roos4ffc8972019-02-07 20:45:11 +01005256 void registerSystemGestureExclusionListener(ISystemGestureExclusionListener listener) {
5257 mSystemGestureExclusionListeners.register(listener);
5258 final boolean changed;
5259 if (mSystemGestureExclusionListeners.getRegisteredCallbackCount() == 1) {
5260 changed = updateSystemGestureExclusion();
5261 } else {
5262 changed = false;
5263 }
5264
5265 if (!changed) {
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005266 final Region unrestrictedOrNull = mSystemGestureExclusionWasRestricted
5267 ? mSystemGestureExclusionUnrestricted : null;
Adrian Roos4ffc8972019-02-07 20:45:11 +01005268 // If updateSystemGestureExclusion changed the exclusion, it will already have
5269 // notified the listener. Otherwise, we'll do it here.
5270 try {
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005271 listener.onSystemGestureExclusionChanged(mDisplayId, mSystemGestureExclusion,
5272 unrestrictedOrNull);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005273 } catch (RemoteException e) {
5274 Slog.e(TAG, "Failed to notify SystemGestureExclusionListener during register", e);
5275 }
5276 }
5277 }
5278
5279 void unregisterSystemGestureExclusionListener(ISystemGestureExclusionListener listener) {
5280 mSystemGestureExclusionListeners.unregister(listener);
5281 }
5282
5283 /**
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005284 * Create a portal window handle for input. This window transports any touch to the display
5285 * indicated by {@link InputWindowHandle#portalToDisplayId} if the touch hits this window.
5286 *
5287 * @param name The name of the portal window handle.
5288 * @return the new portal window handle.
5289 */
5290 private InputWindowHandle createPortalWindowHandle(String name) {
5291 // Let surface flinger to set the display ID of this input window handle because we don't
5292 // know which display the parent surface control is on.
5293 final InputWindowHandle portalWindowHandle = new InputWindowHandle(
Vishnu Nair18782162019-10-08 14:57:16 -07005294 null /* inputApplicationHandle */, INVALID_DISPLAY);
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005295 portalWindowHandle.name = name;
5296 portalWindowHandle.token = new Binder();
5297 portalWindowHandle.layoutParamsFlags =
5298 FLAG_SPLIT_TOUCH | FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL;
5299 getBounds(mTmpBounds);
5300 portalWindowHandle.touchableRegion.set(mTmpBounds);
5301 portalWindowHandle.scaleFactor = 1f;
5302 portalWindowHandle.ownerPid = Process.myPid();
5303 portalWindowHandle.ownerUid = Process.myUid();
5304 portalWindowHandle.portalToDisplayId = mDisplayId;
5305 return portalWindowHandle;
chaviwff2e7d82018-11-02 11:11:27 -07005306 }
Issei Suzukia5dbf522019-02-01 17:58:15 +01005307
5308 /**
5309 * @see IWindowManager#setForwardedInsets
5310 */
5311 public void setForwardedInsets(Insets insets) {
5312 if (insets == null) {
5313 insets = Insets.NONE;
5314 }
5315 if (mDisplayPolicy.getForwardedInsets().equals(insets)) {
5316 return;
5317 }
5318 mDisplayPolicy.setForwardedInsets(insets);
5319 setLayoutNeeded();
5320 mWmService.mWindowPlacerLocked.requestTraversal();
5321 }
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -05005322
5323 protected MetricsLogger getMetricsLogger() {
5324 if (mMetricsLogger == null) {
5325 mMetricsLogger = new MetricsLogger();
5326 }
5327 return mMetricsLogger;
5328 }
Craig Mautner59c00972012-07-30 12:10:24 -07005329}