blob: 283b92c8e47827d125698e7c4ed07b4da1fd3796 [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;
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700141import android.annotation.CallSuper;
Riddle Hsuf53da812018-08-15 22:00:27 +0800142import android.annotation.IntDef;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700143import android.annotation.NonNull;
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200144import android.annotation.Nullable;
Riddle Hsuccf09402019-08-13 00:33:06 +0800145import android.content.pm.ActivityInfo.ScreenOrientation;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700146import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700147import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700148import android.graphics.Bitmap;
Issei Suzukia5dbf522019-02-01 17:58:15 +0100149import android.graphics.Insets;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700150import android.graphics.Matrix;
Tiger Huangd8ec9382019-04-18 14:35:09 -0700151import android.graphics.Point;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800152import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700153import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700154import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100155import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700156import android.hardware.display.DisplayManagerInternal;
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -0500157import android.metrics.LogMaker;
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800158import android.os.Binder;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700159import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700160import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700161import android.os.IBinder;
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800162import android.os.Process;
Adrian Roos4ffc8972019-02-07 20:45:11 +0100163import android.os.RemoteCallbackList;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700164import android.os.RemoteException;
165import android.os.SystemClock;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100166import android.os.Trace;
Riddle Hsuf53da812018-08-15 22:00:27 +0800167import android.os.UserHandle;
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800168import android.util.ArraySet;
Chong Zhang8e89b312015-09-09 15:09:30 -0700169import android.util.DisplayMetrics;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700170import android.util.Slog;
Riddle Hsub2297ad2019-07-26 23:37:25 -0600171import android.util.SparseBooleanArray;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700172import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700173import android.view.Display;
Adrian Roos1cf585052018-01-03 18:43:27 +0100174import android.view.DisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700175import android.view.DisplayInfo;
Riddle Hsua4d6fa22018-08-11 00:50:39 +0800176import android.view.Gravity;
Adrian Roos4ffc8972019-02-07 20:45:11 +0100177import android.view.ISystemGestureExclusionListener;
Arthur Hungbe5ce212018-09-13 18:41:56 +0800178import android.view.InputChannel;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700179import android.view.InputDevice;
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800180import android.view.InputWindowHandle;
Tiger Huang332793b2019-10-29 23:21:27 +0800181import android.view.InsetsState.InternalInsetsType;
Robert Carrb1579c82017-09-05 14:54:47 -0700182import android.view.MagnificationSpec;
Evan Rosky966759f2019-01-15 10:33:58 -0800183import android.view.RemoteAnimationDefinition;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700184import android.view.Surface;
185import android.view.SurfaceControl;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100186import android.view.SurfaceControl.Transaction;
Robert Carrb1579c82017-09-05 14:54:47 -0700187import android.view.SurfaceSession;
Tiger Huang7c610aa2018-10-27 00:01:01 +0800188import android.view.View;
lumark588a3e82018-07-20 18:53:54 +0800189import android.view.WindowManager;
Evan Rosky39b6f232018-10-30 18:35:41 -0700190import android.view.WindowManagerPolicyConstants.PointerEventListener;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700191
Bryce Lee48f4b572017-04-10 10:54:15 -0700192import com.android.internal.annotations.VisibleForTesting;
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -0500193import com.android.internal.logging.MetricsLogger;
194import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800195import com.android.internal.util.ToBooleanFunction;
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200196import com.android.internal.util.function.TriConsumer;
Winson Chungc5fe7ff2019-02-19 14:49:25 -0800197import com.android.internal.util.function.pooled.PooledConsumer;
198import 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>
Riddle Hsu6b76cd32019-10-08 00:37:19 +0800224 implements WindowManagerPolicy.DisplayContentInfo, ConfigurationContainerListener {
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
Craig Mautner59c00972012-07-30 12:10:24 -0700239 /** Unique identifier of this stack. */
240 private final int mDisplayId;
241
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800242 // TODO: Remove once unification is complete.
Riddle Hsud1549d22019-10-07 17:00:47 +0800243 ActivityDisplay mActivityDisplay;
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800244
Wale Ogunwale3a931692016-11-02 16:49:48 -0700245 /** The containers below are the only child containers the display can have. */
246 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800247 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers(mWmService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700248 // Contains all non-app window containers that should be displayed above the app containers
249 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800250 private final AboveAppWindowContainers mAboveAppWindowsContainers =
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800251 new AboveAppWindowContainers("mAboveAppWindowsContainers", mWmService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700252 // Contains all non-app window containers that should be displayed below the app containers
253 // (e.g. Wallpaper).
254 private final NonAppWindowContainers mBelowAppWindowsContainers =
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800255 new NonAppWindowContainers("mBelowAppWindowsContainers", mWmService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700256 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
257 // 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 -0800258 // window containers together and move them in-sync if/when needed. We use a subclass of
259 // WindowContainer which is omitted from screen magnification, as the IME is never magnified.
Eugene Susla25a24952019-04-08 13:57:44 -0700260 private final NonAppWindowContainers mImeWindowsContainers =
261 new NonAppWindowContainers("mImeWindowsContainers", mWmService);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700262
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000263 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800264 private WindowState mTmpWindow2;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800265 private boolean mTmpRecoveringMemory;
266 private boolean mUpdateImeTarget;
267 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700268 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700269
lumark588a3e82018-07-20 18:53:54 +0800270 final AppTransition mAppTransition;
271 final AppTransitionController mAppTransitionController;
272 boolean mSkipAppTransitionAnimation = false;
273
Garfield Tane8d84ab2019-10-11 09:49:40 -0700274 final ArraySet<ActivityRecord> mOpeningApps = new ArraySet<>();
275 final ArraySet<ActivityRecord> mClosingApps = new ArraySet<>();
276 final ArraySet<ActivityRecord> mChangingApps = new ArraySet<>();
lumark588a3e82018-07-20 18:53:54 +0800277 final UnknownAppVisibilityController mUnknownAppVisibilityController;
278 BoundsAnimationController mBoundsAnimationController;
279
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -0500280 private MetricsLogger mMetricsLogger;
281
lumark588a3e82018-07-20 18:53:54 +0800282 /**
283 * List of clients without a transtiton animation that we notify once we are done
284 * transitioning since they won't be notified through the app window animator.
285 */
286 final List<IBinder> mNoAnimationNotifyOnTransitionFinished = new ArrayList<>();
287
Wale Ogunwale02319a62016-09-26 15:21:22 -0700288 // Mapping from a token IBinder to a WindowToken object on this display.
289 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
290
Andrii Kuliancd097992017-03-23 18:31:59 -0700291 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700292 int mInitialDisplayWidth = 0;
293 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700294 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700295
Adrian Roos1cf585052018-01-03 18:43:27 +0100296 DisplayCutout mInitialDisplayCutout;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100297 private final RotationCache<DisplayCutout, WmDisplayCutout> mDisplayCutoutCache
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100298 = new RotationCache<>(this::calculateDisplayCutoutForRotationUncached);
Adrian Roos1cf585052018-01-03 18:43:27 +0100299
Andrii Kuliancd097992017-03-23 18:31:59 -0700300 /**
301 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
302 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
303 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
304 */
Craig Mautner59c00972012-07-30 12:10:24 -0700305 int mBaseDisplayWidth = 0;
306 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700307 /**
308 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
309 * but can be set from Settings or via shell command "adb shell wm density".
310 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
311 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700312 int mBaseDisplayDensity = 0;
Riddle Hsuf53da812018-08-15 22:00:27 +0800313
314 /**
315 * Whether to disable display scaling. This can be set via shell command "adb shell wm scaling".
316 * @see WindowManagerService#setForcedDisplayScalingMode(int, int)
317 */
Jeff Brownd46747a2015-04-15 19:02:36 -0700318 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700319 private final DisplayInfo mDisplayInfo = new DisplayInfo();
320 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700321 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800322 private final DisplayPolicy mDisplayPolicy;
Riddle Hsuccf09402019-08-13 00:33:06 +0800323 private final DisplayRotation mDisplayRotation;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000324 DisplayFrames mDisplayFrames;
325
Adrian Roos4ffc8972019-02-07 20:45:11 +0100326 private final RemoteCallbackList<ISystemGestureExclusionListener>
327 mSystemGestureExclusionListeners = new RemoteCallbackList<>();
328 private final Region mSystemGestureExclusion = new Region();
Adrian Roos5f2c9a12019-07-03 18:31:46 +0200329 private boolean mSystemGestureExclusionWasRestricted = false;
330 private final Region mSystemGestureExclusionUnrestricted = new Region();
Adrian Roosbf3bc1b2019-06-18 16:13:53 +0200331 private int mSystemGestureExclusionLimit;
Adrian Roos4ffc8972019-02-07 20:45:11 +0100332
Andrii Kulian06d07d62017-03-14 11:11:47 -0700333 /**
334 * For default display it contains real metrics, empty for others.
chaviw619da692019-06-10 15:39:40 -0700335 * @see WindowManagerService#createWatermark()
Andrii Kulian06d07d62017-03-14 11:11:47 -0700336 */
337 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800338
339 /** @see #computeCompatSmallestWidth(boolean, int, int, int, DisplayCutout) */
Andrii Kulian06d07d62017-03-14 11:11:47 -0700340 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700341
Andrii Kulian06d07d62017-03-14 11:11:47 -0700342 /**
343 * Compat metrics computed based on {@link #mDisplayMetrics}.
344 * @see #updateDisplayAndOrientation(int)
345 */
346 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
347
348 /** The desired scaling factor for compatible apps. */
349 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700350
Andrii Kulian8ee72852017-03-10 10:36:45 -0800351 /**
Andrii Kulian8ee72852017-03-10 10:36:45 -0800352 * Orientation forced by some window. If there is no visible window that specifies orientation
353 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
354 *
355 * @see NonAppWindowContainers#getOrientation()
356 */
357 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700358
Andrii Kulian8ee72852017-03-10 10:36:45 -0800359 /**
360 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
361 * occluded.
362 *
363 * @see NonAppWindowContainers#getOrientation()
364 */
365 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
366
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700367 /**
Tiger Huang86e6d072019-05-02 20:23:47 +0800368 * The maximum aspect ratio (longerSide/shorterSide) that is treated as close-to-square. The
369 * orientation requests from apps would be ignored if the display is close-to-square.
370 */
371 @VisibleForTesting
372 final float mCloseToSquareMaxAspectRatio;
373
374 /**
375 * If this is true, we would not rotate the display for apps. The rotation would be either the
376 * sensor rotation or the user rotation, controlled by
377 * {@link WindowManagerPolicy.UserRotationMode}.
378 */
379 private boolean mIgnoreRotationForApps;
380
381 /**
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700382 * Keep track of wallpaper visibility to notify changes.
383 */
384 private boolean mLastWallpaperVisible = false;
385
Andrii Kulian06d07d62017-03-14 11:11:47 -0700386 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700387
Craig Mautner39834192012-09-02 07:47:24 -0700388 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700389 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700390 int pendingLayoutChanges;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800391
Vishnu Nairba183352018-11-21 11:16:49 -0800392 /**
393 * Used to gate application window layout until we have sent the complete configuration.
394 * TODO: There are still scenarios where we may be out of sync with the client. Ideally
395 * we want to replace this flag with a mechanism that will confirm the configuration
396 * applied by the client is the one expected by the system server.
397 */
398 boolean mWaitingForConfig;
399
Andrii Kulian839def92016-11-02 10:58:58 -0700400 // TODO(multi-display): remove some of the usages.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800401 @VisibleForTesting
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800402 boolean isDefaultDisplay;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800403
Andrii Kulianf0379de2018-03-14 16:24:07 -0700404 /**
405 * Flag indicating whether WindowManager should override info for this display in
406 * DisplayManager.
407 */
408 boolean mShouldOverrideDisplayConfiguration = true;
Craig Mautner39834192012-09-02 07:47:24 -0700409
Craig Mautnerdc548482014-02-05 13:35:24 -0800410 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700411 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800412
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700413 /** Detect user tapping outside of current focused task bounds .*/
Riddle Hsu2588ab02019-02-25 14:23:56 +0800414 @VisibleForTesting
415 final TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700416
Craig Mautner6601b7b2013-04-29 10:29:11 -0700417 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700418 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700419
Craig Mautner6601b7b2013-04-29 10:29:11 -0700420 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800421 private final Rect mTmpRect = new Rect();
422 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700423 private final RectF mTmpRectF = new RectF();
424 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800425 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700426
Bryce Leef3c6a472017-11-14 14:53:06 -0800427 /** Used for handing back size of display */
428 private final Rect mTmpBounds = new Rect();
429
Garfield Tan90b04282018-12-11 14:04:42 -0800430 private final Configuration mTmpConfiguration = new Configuration();
431
Craig Mautner95da1082014-02-24 17:54:35 -0800432 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700433 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800434
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700435 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700436 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700437
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800438 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800439 /** A collection of windows that provide tap exclude regions inside of them. */
440 final ArraySet<WindowState> mTapExcludeProvidingWindows = new ArraySet<>();
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800441
Garfield Tane8d84ab2019-10-11 09:49:40 -0700442 private final LinkedList<ActivityRecord> mTmpUpdateAllDrawn = new LinkedList();
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700443
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700444 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
445 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000446 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
447 new ApplySurfaceChangesTransactionState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700448
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700449 // True if this display is in the process of being removed. Used to determine if the removal of
450 // the display's direct children should be allowed.
451 private boolean mRemovingDisplay = false;
452
Bryce Leed1871262017-06-12 14:12:29 -0700453 // {@code false} if this display is in the processing of being created.
454 private boolean mDisplayReady = false;
455
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800456 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700457
wilsonshihc32538e2018-11-07 17:27:34 +0800458 boolean mWallpaperMayChange = false;
459
Robert Carrb1579c82017-09-05 14:54:47 -0700460 private final SurfaceSession mSession = new SurfaceSession();
461
462 /**
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800463 * Window that is currently interacting with the user. This window is responsible for receiving
464 * key events and pointer events from the user.
465 */
466 WindowState mCurrentFocus = null;
467
468 /**
469 * The last focused window that we've notified the client that the focus is changed.
470 */
471 WindowState mLastFocus = null;
472
473 /**
474 * Windows that have lost input focus and are waiting for the new focus window to be displayed
475 * before they are told about this.
476 */
477 ArrayList<WindowState> mLosingFocus = new ArrayList<>();
478
479 /**
480 * The foreground app of this display. Windows below this app cannot be the focused window. If
481 * the user taps on the area outside of the task of the focused app, we will notify AM about the
482 * new task the user wants to interact with.
483 */
Garfield Tane8d84ab2019-10-11 09:49:40 -0700484 ActivityRecord mFocusedApp = null;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800485
486 /** Windows added since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
487 final ArrayList<WindowState> mWinAddedSinceNullFocus = new ArrayList<>();
488
489 /** Windows removed since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
490 final ArrayList<WindowState> mWinRemovedSinceNullFocus = new ArrayList<>();
491
Vadim Caenb3715832019-08-13 17:06:38 +0200492 private ScreenRotationAnimation mScreenRotationAnimation;
493
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800494 /**
Robert Carrb1579c82017-09-05 14:54:47 -0700495 * We organize all top-level Surfaces in to the following layers.
496 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800497 * and omitted from Screen-Magnification, for example the strict mode flash or
498 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700499 * {@link #mWindowingLayer} contains everything else.
500 */
501 private SurfaceControl mOverlayLayer;
502
503 /**
504 * See {@link #mOverlayLayer}
505 */
506 private SurfaceControl mWindowingLayer;
507
508 /**
Adrian Roos5251b1d2018-03-23 18:57:43 +0100509 * Sequence number for the current layout pass.
510 */
511 int mLayoutSeq = 0;
512
Chavi Weingarten3a748552018-05-14 17:32:42 +0000513 /**
514 * Specifies the count to determine whether to defer updating the IME target until ready.
515 */
516 private int mDeferUpdateImeTargetCount;
517
Robert Carr24be9ab2018-04-30 17:54:53 -0700518 private MagnificationSpec mMagnificationSpec;
519
Arthur Hung95b38a92018-07-20 18:56:12 +0800520 private InputMonitor mInputMonitor;
521
Jorim Jaggif1292892018-09-10 11:58:13 +0200522 /** Caches the value whether told display manager that we have content. */
523 private boolean mLastHasContent;
524
Issei Suzuki43190bd2018-08-20 17:28:41 +0200525 private DisplayRotationUtil mRotationUtil = new DisplayRotationUtil();
526
lumark90120a82018-08-15 00:33:03 +0800527 /**
528 * The input method window for this display.
529 */
530 WindowState mInputMethodWindow;
531
lumarkff0ab692018-11-05 20:32:30 +0800532 /**
533 * This just indicates the window the input method is on top of, not
534 * necessarily the window its input is going to.
535 */
536 WindowState mInputMethodTarget;
537
538 /** If true hold off on modifying the animation layer of mInputMethodTarget */
539 boolean mInputMethodTargetWaitingAnim;
540
Arthur Hungbe5ce212018-09-13 18:41:56 +0800541 private final PointerEventDispatcher mPointerEventDispatcher;
542
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200543 private final InsetsStateController mInsetsStateController;
Jorim Jaggi28620472019-01-02 23:21:49 +0100544 private final InsetsPolicy mInsetsPolicy;
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200545
Tiger Huangd8ec9382019-04-18 14:35:09 -0700546 /** @see #getParentWindow() */
547 private WindowState mParentWindow;
548
549 private Point mLocationInParentWindow = new Point();
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800550 private SurfaceControl mParentSurfaceControl;
551 private InputWindowHandle mPortalWindowHandle;
552
Tiger Huang7c610aa2018-10-27 00:01:01 +0800553 // Last systemUiVisibility we received from status bar.
554 private int mLastStatusBarVisibility = 0;
555 // Last systemUiVisibility we dispatched to windows.
556 private int mLastDispatchedSystemUiVisibility = 0;
557
Louis Changdc077272019-11-12 16:52:56 +0800558 private final ArrayList<ActivityStack> mTmpAlwaysOnTopStacks = new ArrayList<>();
559 private final ArrayList<ActivityStack> mTmpNormalStacks = new ArrayList<>();
560 private final ArrayList<ActivityStack> mTmpHomeStacks = new ArrayList<>();
Issei Suzuki25a9e2b2019-08-14 16:46:26 +0200561
Tiger Huang43b8fc22019-04-26 11:49:29 +0800562 /** Corner radius that windows should have in order to match the display. */
563 private final float mWindowCornerRadius;
564
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800565 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
566 WindowStateAnimator winAnimator = w.mWinAnimator;
Garfield Tane8d84ab2019-10-11 09:49:40 -0700567 final ActivityRecord activity = w.mActivityRecord;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800568 if (winAnimator.mDrawState == READY_TO_SHOW) {
Garfield Tane8d84ab2019-10-11 09:49:40 -0700569 if (activity == null || activity.canShowWindows()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800570 if (w.performShowLocked()) {
571 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
572 if (DEBUG_LAYOUT_REPEATS) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800573 mWmService.mWindowPlacerLocked.debugLayoutRepeats(
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800574 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
575 }
576 }
577 }
578 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800579 };
580
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800581 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
582 final int lostFocusUid = mTmpWindow.mOwnerUid;
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800583 final Handler handler = mWmService.mH;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800584 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
585 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
586 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
587 w.mAttrs.hideTimeoutMilliseconds);
588 }
589 }
590 };
591
592 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
Garfield Tane8d84ab2019-10-11 09:49:40 -0700593 final ActivityRecord focusedApp = mFocusedApp;
Adrian Roosb125e0b2019-10-02 14:55:14 +0200594 ProtoLog.v(WM_DEBUG_FOCUS, "Looking for focus: %s, flags=%d, canReceive=%b",
595 w, w.mAttrs.flags, w.canReceiveKeys());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800596
597 if (!w.canReceiveKeys()) {
598 return false;
599 }
600
Garfield Tane8d84ab2019-10-11 09:49:40 -0700601 final ActivityRecord activity = w.mActivityRecord;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800602
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800603 if (focusedApp == null) {
Adrian Roosb125e0b2019-10-02 14:55:14 +0200604 ProtoLog.v(WM_DEBUG_FOCUS_LIGHT,
605 "findFocusedWindow: focusedApp=null using new focus @ %s", w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800606 mTmpWindow = w;
607 return true;
608 }
609
610 if (!focusedApp.windowsAreFocusable()) {
611 // Current focused app windows aren't focusable...
Adrian Roosb125e0b2019-10-02 14:55:14 +0200612 ProtoLog.v(WM_DEBUG_FOCUS_LIGHT, "findFocusedWindow: focusedApp windows not"
613 + " focusable using new focus @ %s", w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800614 mTmpWindow = w;
615 return true;
616 }
617
618 // Descend through all of the app tokens and find the first that either matches
Garfield Tane8d84ab2019-10-11 09:49:40 -0700619 // win.mActivityRecord (return win) or mFocusedApp (return null).
620 if (activity != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
621 if (focusedApp.compareTo(activity) > 0) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800622 // App stack below focused app stack. No focus for you!!!
Adrian Roosb125e0b2019-10-02 14:55:14 +0200623 ProtoLog.v(WM_DEBUG_FOCUS_LIGHT,
624 "findFocusedWindow: Reached focused app=%s", focusedApp);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800625 mTmpWindow = null;
626 return true;
627 }
628 }
629
Adrian Roosb125e0b2019-10-02 14:55:14 +0200630 ProtoLog.v(WM_DEBUG_FOCUS_LIGHT, "findFocusedWindow: Found new focus @ %s", w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800631 mTmpWindow = w;
632 return true;
633 };
634
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800635 private final Consumer<WindowState> mPerformLayout = w -> {
636 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
637 // wasting time and funky changes while a window is animating away.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800638 final boolean gone = (mTmpWindow != null && mWmService.mPolicy.canBeHiddenByKeyguardLw(w))
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800639 || w.isGoneForLayoutLw();
640
641 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
642 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
643 + " mLayoutAttached=" + w.mLayoutAttached
Jorim Jaggi381cd722019-03-27 17:33:02 +0100644 + " config reported=" + w.isLastConfigReportedToClient());
Garfield Tane8d84ab2019-10-11 09:49:40 -0700645 final ActivityRecord activity = w.mActivityRecord;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800646 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Issei Suzukif2f6c912019-11-08 11:24:18 +0100647 + " mRelayoutCalled=" + w.mRelayoutCalled + " visible=" + w.mToken.isVisible()
Issei Suzuki1669ea42019-11-06 14:20:59 +0100648 + " visibleRequested=" + (activity != null && activity.mVisibleRequested)
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800649 + " parentHidden=" + w.isParentWindowHidden());
650 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Issei Suzukif2f6c912019-11-08 11:24:18 +0100651 + " mRelayoutCalled=" + w.mRelayoutCalled + " visible=" + w.mToken.isVisible()
Issei Suzuki1669ea42019-11-06 14:20:59 +0100652 + " visibleRequested=" + (activity != null && activity.mVisibleRequested)
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800653 + " parentHidden=" + w.isParentWindowHidden());
654 }
655
656 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
657 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
658 // since that means "perform layout as normal, just don't display").
Jorim Jaggi91d382a2019-03-27 17:00:48 +0100659 if ((!gone || !w.mHaveFrame || w.mLayoutNeeded) && !w.mLayoutAttached) {
660 if (mTmpInitial) {
661 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800662 }
Jorim Jaggi91d382a2019-03-27 17:00:48 +0100663 if (w.mAttrs.type == TYPE_DREAM) {
664 // Don't layout windows behind a dream, so that if it does stuff like hide
665 // the status bar we won't get a bad transition when it goes away.
666 mTmpWindow = w;
667 }
668 w.mLayoutNeeded = false;
669 w.prelayout();
670 final boolean firstLayout = !w.isLaidOut();
671 getDisplayPolicy().layoutWindowLw(w, null, mDisplayFrames);
672 w.mLayoutSeq = mLayoutSeq;
673
674 // If this is the first layout, we need to initialize the last inset values as
675 // otherwise we'd immediately cause an unnecessary resize.
676 if (firstLayout) {
677 w.updateLastInsetValues();
678 }
679
Garfield Tane8d84ab2019-10-11 09:49:40 -0700680 if (w.mActivityRecord != null) {
681 w.mActivityRecord.layoutLetterbox(w);
Jorim Jaggi91d382a2019-03-27 17:00:48 +0100682 }
683
684 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
685 + " mContainingFrame=" + w.getContainingFrame()
686 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800687 }
688 };
689
690 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
691 if (w.mLayoutAttached) {
692 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
693 + " mViewVisibility=" + w.mViewVisibility
694 + " mRelayoutCalled=" + w.mRelayoutCalled);
695 // If this view is GONE, then skip it -- keep the current frame, and let the caller
696 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
697 // windows, since that means "perform layout as normal, just don't display").
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800698 if (mTmpWindow != null && mWmService.mPolicy.canBeHiddenByKeyguardLw(w)) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800699 return;
700 }
701 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
702 || w.mLayoutNeeded) {
703 if (mTmpInitial) {
704 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700705 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800706 }
707 w.mLayoutNeeded = false;
708 w.prelayout();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800709 getDisplayPolicy().layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100710 w.mLayoutSeq = mLayoutSeq;
chaviw492139a2018-07-16 16:07:35 -0700711 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700712 + " mContainingFrame=" + w.getContainingFrame()
713 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800714 }
715 } else if (w.mAttrs.type == TYPE_DREAM) {
716 // Don't layout windows behind a dream, so that if it does stuff like hide the
717 // status bar we won't get a bad transition when it goes away.
718 mTmpWindow = mTmpWindow2;
719 }
720 };
721
722 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
723 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
724 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
725 return w.canBeImeTarget();
726 };
727
728 private final Consumer<WindowState> mApplyPostLayoutPolicy =
Tiger Huang7c610aa2018-10-27 00:01:01 +0800729 w -> getDisplayPolicy().applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
lumarkff0ab692018-11-05 20:32:30 +0800730 mInputMethodTarget);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800731
732 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800733 final WindowSurfacePlacer surfacePlacer = mWmService.mWindowPlacerLocked;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800734 final boolean obscuredChanged = w.mObscured !=
735 mTmpApplySurfaceChangesTransactionState.obscured;
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800736 final RootWindowContainer root = mWmService.mRoot;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800737
738 // Update effect.
739 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
740 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
741 final boolean isDisplayed = w.isDisplayedLw();
742
743 if (isDisplayed && w.isObscuringDisplay()) {
744 // This window completely covers everything behind it, so we want to leave all
745 // of them as undimmed (for performance reasons).
746 root.mObscuringWindow = w;
747 mTmpApplySurfaceChangesTransactionState.obscured = true;
748 }
749
Arthur Hungfbc8f412019-08-01 19:57:54 +0800750 final boolean displayHasContent = root.handleNotObscuredLocked(w,
751 mTmpApplySurfaceChangesTransactionState.obscured,
752 mTmpApplySurfaceChangesTransactionState.syswin);
753
754 if (!mTmpApplySurfaceChangesTransactionState.displayHasContent
755 && !getDisplayPolicy().isWindowExcludedFromContent(w)) {
756 mTmpApplySurfaceChangesTransactionState.displayHasContent |= displayHasContent;
757 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800758
759 if (w.mHasSurface && isDisplayed) {
760 final int type = w.mAttrs.type;
761 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
762 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
763 mTmpApplySurfaceChangesTransactionState.syswin = true;
764 }
765 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
766 && w.mAttrs.preferredRefreshRate != 0) {
767 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
768 = w.mAttrs.preferredRefreshRate;
769 }
Ady Abrahamf3e05312019-05-13 18:04:59 -0700770 final int preferredModeId = getDisplayPolicy().getRefreshRatePolicy()
771 .getPreferredModeId(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800772 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
Ady Abrahamf3e05312019-05-13 18:04:59 -0700773 && preferredModeId != 0) {
774 mTmpApplySurfaceChangesTransactionState.preferredModeId = preferredModeId;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800775 }
776 }
777 }
778
wilsonshihc32538e2018-11-07 17:27:34 +0800779 if (obscuredChanged && w.isVisibleLw() && mWallpaperController.isWallpaperTarget(w)) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800780 // This is the wallpaper target and its obscured state changed... make sure the
781 // current wallpaper's visibility has been updated accordingly.
782 mWallpaperController.updateWallpaperVisibility();
783 }
784
chaviw161ea3e2018-01-31 12:01:12 -0800785 w.handleWindowMovedIfNeeded();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800786
787 final WindowStateAnimator winAnimator = w.mWinAnimator;
788
789 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
chaviw1454b392018-08-06 09:54:04 -0700790 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800791
792 // Moved from updateWindowsAndWallpaperLocked().
793 if (w.mHasSurface) {
794 // Take care of the window being ready to display.
795 final boolean committed = winAnimator.commitFinishDrawingLocked();
796 if (isDefaultDisplay && committed) {
797 if (w.mAttrs.type == TYPE_DREAM) {
798 // HACK: When a dream is shown, it may at that point hide the lock screen.
799 // So we need to redo the layout to let the phone window manager make this
800 // happen.
801 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
802 if (DEBUG_LAYOUT_REPEATS) {
803 surfacePlacer.debugLayoutRepeats(
804 "dream and commitFinishDrawingLocked true",
805 pendingLayoutChanges);
806 }
807 }
808 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
809 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
810 "First draw done in potential wallpaper target " + w);
wilsonshihc32538e2018-11-07 17:27:34 +0800811 mWallpaperMayChange = true;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800812 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
813 if (DEBUG_LAYOUT_REPEATS) {
814 surfacePlacer.debugLayoutRepeats(
815 "wallpaper and commitFinishDrawingLocked true",
816 pendingLayoutChanges);
817 }
818 }
819 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800820 }
821
Garfield Tane8d84ab2019-10-11 09:49:40 -0700822 final ActivityRecord activity = w.mActivityRecord;
823 if (activity != null) {
824 activity.updateLetterboxSurface(w);
825 final boolean updateAllDrawn = activity.updateDrawnWindowStates(w);
826 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(activity)) {
827 mTmpUpdateAllDrawn.add(activity);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800828 }
829 }
830
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800831 if (!mLosingFocus.isEmpty() && w.isFocused() && w.isDisplayedLw()) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800832 mWmService.mH.obtainMessage(REPORT_LOSING_FOCUS, this).sendToTarget();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800833 }
834
835 w.updateResizingWindowIfNeeded();
836 };
837
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800838 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800839 * Create new {@link DisplayContent} instance, add itself to the root window container and
840 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700841 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800842 * @param service You know.
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800843 * @param activityDisplay The ActivityDisplay for the display container.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700844 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700845 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800846 ActivityDisplay activityDisplay) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100847 super(service);
Riddle Hsud1549d22019-10-07 17:00:47 +0800848 mActivityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -0800849 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
850 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
851 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
852 + " new=" + display);
853 }
854
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
Andrii Kulian06d07d62017-03-14 11:11:47 -07001128 /**
Wale Ogunwale3a256e62018-12-06 14:41:18 -08001129 * The display content may have configuration set from {@link #DisplayWindowSettings}. This
1130 * callback let the owner of container know there is existing configuration to prevent the
1131 * values from being replaced by the initializing {@link #ActivityDisplay}.
1132 */
1133 void initializeDisplayOverrideConfiguration() {
Riddle Hsu6b76cd32019-10-08 00:37:19 +08001134 if (mActivityDisplay == null) {
1135 return;
Wale Ogunwale3a256e62018-12-06 14:41:18 -08001136 }
Riddle Hsu6b76cd32019-10-08 00:37:19 +08001137 mActivityDisplay.onRequestedOverrideConfigurationChanged(
1138 getResolvedOverrideConfiguration());
1139 mActivityDisplay.registerConfigurationChangeListener(this);
Wale Ogunwale3a256e62018-12-06 14:41:18 -08001140 }
1141
Shivam Agrawal6472e0e2019-07-03 16:27:49 -07001142 void reconfigureDisplayLocked() {
1143 if (!isReady()) {
1144 return;
1145 }
1146 configureDisplayPolicy();
1147 setLayoutNeeded();
1148
Riddle Hsuccf09402019-08-13 00:33:06 +08001149 boolean configChanged = updateOrientation();
Shivam Agrawal6472e0e2019-07-03 16:27:49 -07001150 final Configuration currentDisplayConfig = getConfiguration();
1151 mTmpConfiguration.setTo(currentDisplayConfig);
1152 computeScreenConfiguration(mTmpConfiguration);
1153 configChanged |= currentDisplayConfig.diff(mTmpConfiguration) != 0;
1154
1155 if (configChanged) {
1156 mWaitingForConfig = true;
1157 mWmService.startFreezingDisplayLocked(0 /* exitAnim */, 0 /* enterAnim */, this);
1158 sendNewConfiguration();
1159 }
1160
1161 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Riddle Hsu4e611772018-10-31 18:58:28 +08001162 }
1163
Shivam Agrawal1d3db652019-07-01 15:26:11 -07001164 void sendNewConfiguration() {
Riddle Hsud1549d22019-10-07 17:00:47 +08001165 if (!isReady() || mActivityDisplay == null) {
Shivam Agrawal6472e0e2019-07-03 16:27:49 -07001166 return;
1167 }
Evan Rosky69cace42019-09-20 16:28:13 -07001168 if (mDisplayRotation.isWaitingForRemoteRotation()) {
1169 return;
1170 }
Riddle Hsud1549d22019-10-07 17:00:47 +08001171 final boolean configUpdated = mActivityDisplay.updateDisplayOverrideConfigurationLocked();
Shivam Agrawal6472e0e2019-07-03 16:27:49 -07001172 if (configUpdated) {
1173 return;
1174 }
1175 // Something changed (E.g. device rotation), but no configuration update is needed.
1176 // E.g. changing device rotation by 180 degrees. Go ahead and perform surface placement to
1177 // unfreeze the display since we froze it when the rotation was updated in
1178 // DisplayContent#updateRotationUnchecked.
1179 if (mWaitingForConfig) {
1180 mWaitingForConfig = false;
1181 mWmService.mLastFinishedFreezeSource = "config-unchanged";
1182 setLayoutNeeded();
1183 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Shivam Agrawal1d3db652019-07-01 15:26:11 -07001184 }
1185 }
1186
Garfield Tan90b04282018-12-11 14:04:42 -08001187 @Override
1188 boolean onDescendantOrientationChanged(IBinder freezeDisplayToken,
1189 ConfigurationContainer requestingContainer) {
Riddle Hsuccf09402019-08-13 00:33:06 +08001190 final Configuration config = updateOrientation(
1191 getRequestedOverrideConfiguration(), freezeDisplayToken, false /* forceUpdate */);
Garfield Tan49dae102019-02-04 09:51:59 -08001192 // If display rotation class tells us that it doesn't consider app requested orientation,
1193 // this display won't rotate just because of an app changes its requested orientation. Thus
1194 // it indicates that this display chooses not to handle this request.
1195 final boolean handled = getDisplayRotation().respectAppRequestedOrientation();
Garfield Tan90b04282018-12-11 14:04:42 -08001196 if (config == null) {
1197 return handled;
1198 }
1199
1200 if (handled && requestingContainer instanceof ActivityRecord) {
1201 final ActivityRecord activityRecord = (ActivityRecord) requestingContainer;
Riddle Hsud1549d22019-10-07 17:00:47 +08001202 final boolean kept = mActivityDisplay.updateDisplayOverrideConfigurationLocked(
Shivam Agrawal1d3db652019-07-01 15:26:11 -07001203 config, activityRecord, false /* deferResume */, null /* result */);
Garfield Tan90b04282018-12-11 14:04:42 -08001204 activityRecord.frozenBeforeDestroy = true;
1205 if (!kept) {
1206 mWmService.mAtmService.mRootActivityContainer.resumeFocusedStacksTopActivities();
1207 }
1208 } else {
1209 // We have a new configuration to push so we need to update ATMS for now.
1210 // TODO: Clean up display configuration push between ATMS and WMS after unification.
Riddle Hsud1549d22019-10-07 17:00:47 +08001211 mActivityDisplay.updateDisplayOverrideConfigurationLocked(
Shivam Agrawal1d3db652019-07-01 15:26:11 -07001212 config, null /* starting */, false /* deferResume */, null);
Garfield Tan90b04282018-12-11 14:04:42 -08001213 }
1214 return handled;
1215 }
1216
Garfield Tan49dae102019-02-04 09:51:59 -08001217 @Override
1218 boolean handlesOrientationChangeFromDescendant() {
1219 return getDisplayRotation().respectAppRequestedOrientation();
1220 }
1221
Riddle Hsu4e611772018-10-31 18:58:28 +08001222 /**
1223 * Determine the new desired orientation of this display.
1224 *
Riddle Hsuccf09402019-08-13 00:33:06 +08001225 * @see #getOrientation()
1226 * @return {@code true} if the orientation is changed and the caller should call
1227 * {@link #sendNewConfiguration} if the method returns {@code true}.
Riddle Hsu4e611772018-10-31 18:58:28 +08001228 */
Riddle Hsuccf09402019-08-13 00:33:06 +08001229 boolean updateOrientation() {
1230 return mDisplayRotation.updateOrientation(getOrientation(), false /* forceUpdate */);
Riddle Hsu4e611772018-10-31 18:58:28 +08001231 }
1232
Garfield Tan90b04282018-12-11 14:04:42 -08001233 /**
Riddle Hsuccf09402019-08-13 00:33:06 +08001234 * Update orientation of the display, returning a non-null new Configuration if it has
Garfield Tan90b04282018-12-11 14:04:42 -08001235 * changed from the current orientation. If a non-null configuration is returned, someone must
1236 * call {@link WindowManagerService#setNewDisplayOverrideConfiguration(Configuration,
1237 * DisplayContent)} to tell the window manager it can unfreeze the screen. This will typically
Riddle Hsuccf09402019-08-13 00:33:06 +08001238 * be done by calling {@link #sendNewConfiguration}.
1239 *
1240 * @param currentConfig The current requested override configuration (it is usually set from
1241 * the last {@link #sendNewConfiguration}) of the display. It is used to
1242 * check if the configuration container has the latest state.
1243 * @param freezeDisplayToken Freeze the app window token if the orientation is changed.
1244 * @param forceUpdate See {@link DisplayRotation#updateRotationUnchecked(boolean)}
Garfield Tan90b04282018-12-11 14:04:42 -08001245 */
Riddle Hsuccf09402019-08-13 00:33:06 +08001246 Configuration updateOrientation(Configuration currentConfig, IBinder freezeDisplayToken,
1247 boolean forceUpdate) {
Garfield Tan90b04282018-12-11 14:04:42 -08001248 if (!mDisplayReady) {
1249 return null;
1250 }
1251
1252 Configuration config = null;
Riddle Hsuccf09402019-08-13 00:33:06 +08001253 if (mDisplayRotation.updateOrientation(getOrientation(), forceUpdate)) {
Garfield Tan90b04282018-12-11 14:04:42 -08001254 // If we changed the orientation but mOrientationChangeComplete is already true,
1255 // we used seamless rotation, and we don't need to freeze the screen.
1256 if (freezeDisplayToken != null && !mWmService.mRoot.mOrientationChangeComplete) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07001257 final ActivityRecord activity = getActivityRecord(freezeDisplayToken);
1258 if (activity != null) {
1259 activity.startFreezingScreen();
Garfield Tan90b04282018-12-11 14:04:42 -08001260 }
1261 }
1262 config = new Configuration();
1263 computeScreenConfiguration(config);
1264 } else if (currentConfig != null) {
1265 // No obvious action we need to take, but if our current state mismatches the
1266 // activity manager's, update it, disregarding font scale, which should remain set
1267 // to the value of the previous configuration.
1268 // Here we're calling Configuration#unset() instead of setToDefaults() because we
1269 // need to keep override configs clear of non-empty values (e.g. fontSize).
1270 mTmpConfiguration.unset();
1271 mTmpConfiguration.updateFrom(currentConfig);
1272 computeScreenConfiguration(mTmpConfiguration);
1273 if (currentConfig.diff(mTmpConfiguration) != 0) {
1274 mWaitingForConfig = true;
1275 setLayoutNeeded();
Riddle Hsuccf09402019-08-13 00:33:06 +08001276 mDisplayRotation.prepareNormalRotationAnimation();
Garfield Tan90b04282018-12-11 14:04:42 -08001277 config = new Configuration(mTmpConfiguration);
1278 }
1279 }
1280
1281 return config;
1282 }
1283
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001284 /**
Andrii Kulian06d07d62017-03-14 11:11:47 -07001285 * Update rotation of the display.
1286 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001287 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
Riddle Hsuccf09402019-08-13 00:33:06 +08001288 * {@link #sendNewConfiguration} TO UNFREEZE THE SCREEN.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001289 */
Robert Carrae606b42018-02-15 15:36:23 -08001290 boolean updateRotationUnchecked() {
Riddle Hsuccf09402019-08-13 00:33:06 +08001291 return mDisplayRotation.updateRotationUnchecked(false /* forceUpdate */);
Evan Roskye747c3e2018-10-30 20:06:41 -07001292 }
1293
1294 /**
1295 * Applies the rotation transaction. This must be called after {@link #updateRotationUnchecked}
1296 * (if it returned {@code true}) to actually finish the rotation.
1297 *
1298 * @param oldRotation the rotation we are coming from.
1299 * @param rotation the rotation to apply.
1300 */
1301 void applyRotationLocked(final int oldRotation, final int rotation) {
Riddle Hsuccf09402019-08-13 00:33:06 +08001302 mDisplayRotation.applyCurrentRotation(rotation);
1303 final boolean rotateSeamlessly = mDisplayRotation.isRotatingSeamlessly();
Riddle Hsuccf09402019-08-13 00:33:06 +08001304 final Transaction transaction = getPendingTransaction();
Vadim Caenb3715832019-08-13 17:06:38 +02001305 ScreenRotationAnimation screenRotationAnimation = rotateSeamlessly
1306 ? null : getRotationAnimation();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001307 // We need to update our screen size information to match the new rotation. If the rotation
1308 // has actually changed then this method will return true and, according to the comment at
1309 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1310 // By updating the Display info here it will be available to
1311 // #computeScreenConfiguration() later.
Tiger Huanga817b1f2019-05-09 20:04:17 +08001312 updateDisplayAndOrientation(getConfiguration().uiMode, null /* outConfig */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001313
Robert Carrae606b42018-02-15 15:36:23 -08001314 // NOTE: We disable the rotation in the emulator because
1315 // it doesn't support hardware OpenGL emulation yet.
Vadim Caenba4fd6c2019-08-05 16:45:46 +02001316 if (screenRotationAnimation != null && screenRotationAnimation.hasScreenshot()) {
Riddle Hsuccf09402019-08-13 00:33:06 +08001317 screenRotationAnimation.setRotation(transaction, rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001318 }
1319
Vishnu Nair83537a72018-07-19 21:27:48 -07001320 forAllWindows(w -> {
Riddle Hsuccf09402019-08-13 00:33:06 +08001321 w.seamlesslyRotateIfAllowed(transaction, oldRotation, rotation, rotateSeamlessly);
Vishnu Nair83537a72018-07-19 21:27:48 -07001322 }, true /* traverseTopToBottom */);
1323
Riddle Hsuccf09402019-08-13 00:33:06 +08001324 mWmService.mDisplayManagerInternal.performTraversal(transaction);
Robert Carrae606b42018-02-15 15:36:23 -08001325 scheduleAnimation();
1326
Andrii Kulian06d07d62017-03-14 11:11:47 -07001327 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001328 if (w.mHasSurface && !rotateSeamlessly) {
Adrian Roosb125e0b2019-10-02 14:55:14 +02001329 ProtoLog.v(WM_DEBUG_ORIENTATION, "Set mOrientationChanging of %s", w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001330 w.setOrientationChanging(true);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001331 mWmService.mRoot.mOrientationChangeComplete = false;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001332 w.mLastFreezeDuration = 0;
1333 }
1334 w.mReportOrientationChanged = true;
1335 }, true /* traverseTopToBottom */);
1336
1337 if (rotateSeamlessly) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001338 mWmService.mH.sendNewMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
Riddle Hsu654a6f92018-07-13 22:59:36 +08001339 this, SEAMLESS_ROTATION_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001340 }
1341
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001342 for (int i = mWmService.mRotationWatchers.size() - 1; i >= 0; i--) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001343 final WindowManagerService.RotationWatcher rotationWatcher
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001344 = mWmService.mRotationWatchers.get(i);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001345 if (rotationWatcher.mDisplayId == mDisplayId) {
1346 try {
1347 rotationWatcher.mWatcher.onRotationChanged(rotation);
1348 } catch (RemoteException e) {
1349 // Ignore
1350 }
1351 }
1352 }
1353
Andrii Kulian06d07d62017-03-14 11:11:47 -07001354 // Announce rotation only if we will not animate as we already have the
1355 // windows in final state. Otherwise, we make this call at the rotation end.
Rhed Jao02655dc2018-10-30 20:44:52 +08001356 if (screenRotationAnimation == null && mWmService.mAccessibilityController != null) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001357 mWmService.mAccessibilityController.onRotationChangedLocked(this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001358 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001359 }
1360
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001361 void configureDisplayPolicy() {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001362 final int width = mBaseDisplayWidth;
1363 final int height = mBaseDisplayHeight;
1364 final int shortSize;
1365 final int longSize;
1366 if (width > height) {
1367 shortSize = height;
1368 longSize = width;
1369 } else {
1370 shortSize = width;
1371 longSize = height;
1372 }
1373
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02001374 final int shortSizeDp = shortSize * DENSITY_DEFAULT / mBaseDisplayDensity;
1375 final int longSizeDp = longSize * DENSITY_DEFAULT / mBaseDisplayDensity;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001376
Winson Chung4723b4e2019-03-25 16:49:36 -07001377 mDisplayPolicy.updateConfigurationAndScreenSizeDependentBehaviors();
Tiger Huang3d2b8982019-01-29 22:56:48 +08001378 mDisplayRotation.configure(width, height, shortSizeDp, longSizeDp);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001379
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001380 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
1381 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Tiger Huang86e6d072019-05-02 20:23:47 +08001382
1383 // Not much of use to rotate the display for apps since it's close to square.
1384 mIgnoreRotationForApps = isNonDecorDisplayCloseToSquare(Surface.ROTATION_0, width, height);
1385 }
1386
1387 private boolean isNonDecorDisplayCloseToSquare(int rotation, int width, int height) {
1388 final DisplayCutout displayCutout =
1389 calculateDisplayCutoutForRotation(rotation).getDisplayCutout();
1390 final int uiMode = mWmService.mPolicy.getUiMode();
1391 final int w = mDisplayPolicy.getNonDecorDisplayWidth(
1392 width, height, rotation, uiMode, displayCutout);
1393 final int h = mDisplayPolicy.getNonDecorDisplayHeight(
1394 width, height, rotation, uiMode, displayCutout);
1395 final float aspectRatio = Math.max(w, h) / (float) Math.min(w, h);
1396 return aspectRatio <= mCloseToSquareMaxAspectRatio;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001397 }
1398
Andrii Kulian06d07d62017-03-14 11:11:47 -07001399 /**
1400 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1401 * changed.
1402 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1403 */
Tiger Huanga817b1f2019-05-09 20:04:17 +08001404 private DisplayInfo updateDisplayAndOrientation(int uiMode, Configuration outConfig) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001405 // Use the effective "visual" dimensions based on current rotation
Riddle Hsuccf09402019-08-13 00:33:06 +08001406 final int rotation = getRotation();
1407 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Garfield Tan4bb83472019-01-16 14:37:04 -08001408 final int dw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1409 final int dh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001410
1411 // Update application display metrics.
Riddle Hsuccf09402019-08-13 00:33:06 +08001412 final WmDisplayCutout wmDisplayCutout = calculateDisplayCutoutForRotation(rotation);
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001413 final DisplayCutout displayCutout = wmDisplayCutout.getDisplayCutout();
1414
Riddle Hsuccf09402019-08-13 00:33:06 +08001415 final int appWidth = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001416 displayCutout);
Riddle Hsuccf09402019-08-13 00:33:06 +08001417 final int appHeight = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001418 displayCutout);
Riddle Hsuccf09402019-08-13 00:33:06 +08001419 mDisplayInfo.rotation = rotation;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001420 mDisplayInfo.logicalWidth = dw;
1421 mDisplayInfo.logicalHeight = dh;
1422 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1423 mDisplayInfo.appWidth = appWidth;
1424 mDisplayInfo.appHeight = appHeight;
1425 if (isDefaultDisplay) {
1426 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1427 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1428 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001429 mDisplayInfo.displayCutout = displayCutout.isEmpty() ? null : displayCutout;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001430 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1431 if (mDisplayScalingDisabled) {
1432 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1433 } else {
1434 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1435 }
1436
Tiger Huanga817b1f2019-05-09 20:04:17 +08001437 computeSizeRangesAndScreenLayout(mDisplayInfo, rotated, uiMode, dw, dh,
1438 mDisplayMetrics.density, outConfig);
1439
Andrii Kulianf0379de2018-03-14 16:24:07 -07001440 // We usually set the override info in DisplayManager so that we get consistent display
1441 // metrics values when displays are changing and don't send out new values until WM is aware
1442 // of them. However, we don't do this for displays that serve as containers for ActivityView
1443 // because we don't want letter-/pillar-boxing during resize.
1444 final DisplayInfo overrideDisplayInfo = mShouldOverrideDisplayConfiguration
1445 ? mDisplayInfo : null;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001446 mWmService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
Andrii Kulianf0379de2018-03-14 16:24:07 -07001447 overrideDisplayInfo);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001448
1449 mBaseDisplayRect.set(0, 0, dw, dh);
1450
1451 if (isDefaultDisplay) {
1452 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1453 mCompatDisplayMetrics);
1454 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001455
Andrii Kulian06d07d62017-03-14 11:11:47 -07001456 return mDisplayInfo;
1457 }
1458
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001459 WmDisplayCutout calculateDisplayCutoutForRotation(int rotation) {
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001460 return mDisplayCutoutCache.getOrCompute(mInitialDisplayCutout, rotation);
1461 }
1462
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001463 private WmDisplayCutout calculateDisplayCutoutForRotationUncached(
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001464 DisplayCutout cutout, int rotation) {
Adrian Roos24264212018-02-19 16:26:15 +01001465 if (cutout == null || cutout == DisplayCutout.NO_CUTOUT) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001466 return WmDisplayCutout.NO_CUTOUT;
Adrian Roos1cf585052018-01-03 18:43:27 +01001467 }
Adrian Roos11c25582018-02-19 18:06:36 +01001468 if (rotation == ROTATION_0) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001469 return WmDisplayCutout.computeSafeInsets(
1470 cutout, mInitialDisplayWidth, mInitialDisplayHeight);
Adrian Roos24264212018-02-19 16:26:15 +01001471 }
Adrian Roosbed538e2018-02-21 17:50:07 +01001472 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Issei Suzuki43190bd2018-08-20 17:28:41 +02001473 final Rect[] newBounds = mRotationUtil.getRotatedBounds(
1474 WmDisplayCutout.computeSafeInsets(
Jorim Jaggi60640512018-06-29 01:14:31 +02001475 cutout, mInitialDisplayWidth, mInitialDisplayHeight)
Issei Suzuki43190bd2018-08-20 17:28:41 +02001476 .getDisplayCutout().getBoundingRectsAll(),
1477 rotation, mInitialDisplayWidth, mInitialDisplayHeight);
1478 return WmDisplayCutout.computeSafeInsets(DisplayCutout.fromBounds(newBounds),
Adrian Roos24264212018-02-19 16:26:15 +01001479 rotated ? mInitialDisplayHeight : mInitialDisplayWidth,
1480 rotated ? mInitialDisplayWidth : mInitialDisplayHeight);
Adrian Roos1cf585052018-01-03 18:43:27 +01001481 }
1482
Andrii Kulian06d07d62017-03-14 11:11:47 -07001483 /**
1484 * Compute display configuration based on display properties and policy settings.
1485 * Do not call if mDisplayReady == false.
1486 */
1487 void computeScreenConfiguration(Configuration config) {
Tiger Huanga817b1f2019-05-09 20:04:17 +08001488 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode, config);
Evan Roskye747c3e2018-10-30 20:06:41 -07001489 calculateBounds(displayInfo, mTmpBounds);
1490 config.windowConfiguration.setBounds(mTmpBounds);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001491
1492 final int dw = displayInfo.logicalWidth;
1493 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001494 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
Garfield Tane0846042018-07-26 13:42:04 -07001495 config.windowConfiguration.setWindowingMode(getWindowingMode());
Yunfan Chen7daa6ac2018-11-29 18:16:44 -08001496 config.windowConfiguration.setDisplayWindowingMode(getWindowingMode());
Evan Roskye747c3e2018-10-30 20:06:41 -07001497 config.windowConfiguration.setRotation(displayInfo.rotation);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001498
Adrian Roos11c25582018-02-19 18:06:36 +01001499 final float density = mDisplayMetrics.density;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001500 config.screenWidthDp =
Tiger Huang7c610aa2018-10-27 00:01:01 +08001501 (int)(mDisplayPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1502 config.uiMode, displayInfo.displayCutout) / density);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001503 config.screenHeightDp =
Tiger Huang7c610aa2018-10-27 00:01:01 +08001504 (int)(mDisplayPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1505 config.uiMode, displayInfo.displayCutout) / density);
Bryce Lee7566d762017-03-30 09:34:15 -07001506
Tiger Huang7c610aa2018-10-27 00:01:01 +08001507 mDisplayPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh,
Adrian Roos11c25582018-02-19 18:06:36 +01001508 displayInfo.displayCutout, mTmpRect);
Bryce Lee7566d762017-03-30 09:34:15 -07001509 final int leftInset = mTmpRect.left;
1510 final int topInset = mTmpRect.top;
1511 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001512 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1513 leftInset + displayInfo.appWidth /* right */,
1514 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001515 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1516 || displayInfo.rotation == Surface.ROTATION_270);
1517
Andrii Kulian06d07d62017-03-14 11:11:47 -07001518 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1519 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1520 ? Configuration.SCREENLAYOUT_ROUND_YES
1521 : Configuration.SCREENLAYOUT_ROUND_NO);
1522
1523 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1524 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1525 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001526 dh, displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001527 config.densityDpi = displayInfo.logicalDensityDpi;
1528
1529 config.colorMode =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001530 ((displayInfo.isHdr() && mWmService.hasHdrSupport())
Andrii Kulian06d07d62017-03-14 11:11:47 -07001531 ? Configuration.COLOR_MODE_HDR_YES
1532 : Configuration.COLOR_MODE_HDR_NO)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001533 | (displayInfo.isWideColorGamut() && mWmService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001534 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1535 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1536
1537 // Update the configuration based on available input devices, lid switch,
1538 // and platform configuration.
1539 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1540 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1541 config.navigation = Configuration.NAVIGATION_NONAV;
1542
1543 int keyboardPresence = 0;
1544 int navigationPresence = 0;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001545 final InputDevice[] devices = mWmService.mInputManager.getInputDevices();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001546 final int len = devices != null ? devices.length : 0;
1547 for (int i = 0; i < len; i++) {
1548 InputDevice device = devices[i];
Arthur Hung82bbfc32018-11-29 20:24:51 +08001549 // Ignore virtual input device.
1550 if (device.isVirtual()) {
1551 continue;
1552 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001553
Arthur Hung82bbfc32018-11-29 20:24:51 +08001554 // Check if input device can dispatch events to current display.
1555 // If display type is virtual, will follow the default display.
1556 if (!mWmService.mInputManager.canDispatchToDisplay(device.getId(),
1557 displayInfo.type == Display.TYPE_VIRTUAL ? DEFAULT_DISPLAY : mDisplayId)) {
1558 continue;
1559 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001560
Arthur Hung82bbfc32018-11-29 20:24:51 +08001561 final int sources = device.getSources();
1562 final int presenceFlag = device.isExternal()
1563 ? WindowManagerPolicy.PRESENCE_EXTERNAL : WindowManagerPolicy.PRESENCE_INTERNAL;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001564
Arthur Hung82bbfc32018-11-29 20:24:51 +08001565 if (mWmService.mIsTouchDevice) {
1566 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) == InputDevice.SOURCE_TOUCHSCREEN) {
1567 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001568 }
Arthur Hung82bbfc32018-11-29 20:24:51 +08001569 } else {
1570 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1571 }
1572
1573 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1574 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1575 navigationPresence |= presenceFlag;
1576 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1577 && config.navigation == Configuration.NAVIGATION_NONAV) {
1578 config.navigation = Configuration.NAVIGATION_DPAD;
1579 navigationPresence |= presenceFlag;
1580 }
1581
1582 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1583 config.keyboard = Configuration.KEYBOARD_QWERTY;
1584 keyboardPresence |= presenceFlag;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001585 }
1586 }
1587
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001588 if (config.navigation == Configuration.NAVIGATION_NONAV && mWmService.mHasPermanentDpad) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001589 config.navigation = Configuration.NAVIGATION_DPAD;
1590 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1591 }
1592
1593 // Determine whether a hard keyboard is available and enabled.
1594 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1595 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001596 if (hardKeyboardAvailable != mWmService.mHardKeyboardAvailable) {
1597 mWmService.mHardKeyboardAvailable = hardKeyboardAvailable;
1598 mWmService.mH.removeMessages(REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1599 mWmService.mH.sendEmptyMessage(REPORT_HARD_KEYBOARD_STATUS_CHANGE);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001600 }
1601
Winson Chung4723b4e2019-03-25 16:49:36 -07001602 mDisplayPolicy.updateConfigurationAndScreenSizeDependentBehaviors();
Tiger Huang7c610aa2018-10-27 00:01:01 +08001603
Andrii Kulian06d07d62017-03-14 11:11:47 -07001604 // Let the policy update hidden states.
1605 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1606 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1607 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001608 mWmService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001609 }
1610
1611 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001612 DisplayCutout displayCutout) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001613 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1614 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1615 final int unrotDw, unrotDh;
1616 if (rotated) {
1617 unrotDw = dh;
1618 unrotDh = dw;
1619 } else {
1620 unrotDw = dw;
1621 unrotDh = dh;
1622 }
1623 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001624 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001625 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001626 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001627 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001628 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001629 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001630 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001631 return sw;
1632 }
1633
1634 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001635 DisplayMetrics dm, int dw, int dh, DisplayCutout displayCutout) {
1636 dm.noncompatWidthPixels = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1637 displayCutout);
1638 dm.noncompatHeightPixels = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode,
1639 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001640 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1641 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1642 if (curSize == 0 || size < curSize) {
1643 curSize = size;
1644 }
1645 return curSize;
1646 }
1647
Tiger Huang7c610aa2018-10-27 00:01:01 +08001648 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, boolean rotated,
1649 int uiMode, int dw, int dh, float density, Configuration outConfig) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001650
1651 // We need to determine the smallest width that will occur under normal
1652 // operation. To this, start with the base screen size and compute the
1653 // width under the different possible rotations. We need to un-rotate
1654 // the current screen dimensions before doing this.
1655 int unrotDw, unrotDh;
1656 if (rotated) {
1657 unrotDw = dh;
1658 unrotDh = dw;
1659 } else {
1660 unrotDw = dw;
1661 unrotDh = dh;
1662 }
1663 displayInfo.smallestNominalAppWidth = 1<<30;
1664 displayInfo.smallestNominalAppHeight = 1<<30;
1665 displayInfo.largestNominalAppWidth = 0;
1666 displayInfo.largestNominalAppHeight = 0;
Tiger Huang7c610aa2018-10-27 00:01:01 +08001667 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_0, uiMode, unrotDw, unrotDh);
1668 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_90, uiMode, unrotDh, unrotDw);
1669 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_180, uiMode, unrotDw, unrotDh);
1670 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_270, uiMode, unrotDh, unrotDw);
Tiger Huanga817b1f2019-05-09 20:04:17 +08001671
1672 if (outConfig == null) {
1673 return;
1674 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001675 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1676 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001677 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001678 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001679 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001680 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001681 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001682 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001683 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001684 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1685 outConfig.screenLayout = sl;
1686 }
1687
1688 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001689 int uiMode, DisplayCutout displayCutout) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001690 // Get the app screen size at this rotation.
Tiger Huang7c610aa2018-10-27 00:01:01 +08001691 int w = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayCutout);
1692 int h = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001693
1694 // Compute the screen layout size class for this rotation.
1695 int longSize = w;
1696 int shortSize = h;
1697 if (longSize < shortSize) {
1698 int tmp = longSize;
1699 longSize = shortSize;
1700 shortSize = tmp;
1701 }
1702 longSize = (int)(longSize/density);
1703 shortSize = (int)(shortSize/density);
1704 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1705 }
1706
Tiger Huang7c610aa2018-10-27 00:01:01 +08001707 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int rotation,
Andrii Kulian06d07d62017-03-14 11:11:47 -07001708 int uiMode, int dw, int dh) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001709 final DisplayCutout displayCutout = calculateDisplayCutoutForRotation(
1710 rotation).getDisplayCutout();
Tiger Huang7c610aa2018-10-27 00:01:01 +08001711 final int width = mDisplayPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1712 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001713 if (width < displayInfo.smallestNominalAppWidth) {
1714 displayInfo.smallestNominalAppWidth = width;
1715 }
1716 if (width > displayInfo.largestNominalAppWidth) {
1717 displayInfo.largestNominalAppWidth = width;
1718 }
Tiger Huang7c610aa2018-10-27 00:01:01 +08001719 final int height = mDisplayPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1720 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001721 if (height < displayInfo.smallestNominalAppHeight) {
1722 displayInfo.smallestNominalAppHeight = height;
1723 }
1724 if (height > displayInfo.largestNominalAppHeight) {
1725 displayInfo.largestNominalAppHeight = height;
1726 }
1727 }
1728
Riddle Hsua4d6fa22018-08-11 00:50:39 +08001729 /**
1730 * Apps that use the compact menu panel (as controlled by the panelMenuIsCompact
1731 * theme attribute) on devices that feature a physical options menu key attempt to position
1732 * their menu panel window along the edge of the screen nearest the physical menu key.
1733 * This lowers the travel distance between invoking the menu panel and selecting
1734 * a menu option.
1735 *
1736 * This method helps control where that menu is placed. Its current implementation makes
1737 * assumptions about the menu key and its relationship to the screen based on whether
1738 * the device's natural orientation is portrait (width < height) or landscape.
1739 *
1740 * The menu key is assumed to be located along the bottom edge of natural-portrait
1741 * devices and along the right edge of natural-landscape devices. If these assumptions
1742 * do not hold for the target device, this method should be changed to reflect that.
1743 *
1744 * @return A {@link Gravity} value for placing the options menu window.
1745 */
1746 int getPreferredOptionsPanelGravity() {
1747 final int rotation = getRotation();
1748 if (mInitialDisplayWidth < mInitialDisplayHeight) {
1749 // On devices with a natural orientation of portrait.
1750 switch (rotation) {
1751 default:
1752 case Surface.ROTATION_0:
1753 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1754 case Surface.ROTATION_90:
1755 return Gravity.RIGHT | Gravity.BOTTOM;
1756 case Surface.ROTATION_180:
1757 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1758 case Surface.ROTATION_270:
1759 return Gravity.START | Gravity.BOTTOM;
1760 }
1761 }
1762
1763 // On devices with a natural orientation of landscape.
1764 switch (rotation) {
1765 default:
1766 case Surface.ROTATION_0:
1767 return Gravity.RIGHT | Gravity.BOTTOM;
1768 case Surface.ROTATION_90:
1769 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1770 case Surface.ROTATION_180:
1771 return Gravity.START | Gravity.BOTTOM;
1772 case Surface.ROTATION_270:
1773 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1774 }
1775 }
1776
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001777 DockedStackDividerController getDockedDividerController() {
1778 return mDividerControllerLocked;
1779 }
1780
Winson Chung655332c2016-10-31 13:14:28 -07001781 PinnedStackController getPinnedStackController() {
1782 return mPinnedStackControllerLocked;
1783 }
1784
Jeff Browna506a6e2013-06-04 00:02:38 -07001785 /**
1786 * Returns true if the specified UID has access to this display.
1787 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001788 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001789 return mDisplay.hasAccess(uid);
1790 }
1791
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001792 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001793 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001794 }
1795
Louis Changdc077272019-11-12 16:52:56 +08001796 ActivityStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001797 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001798 }
1799
Wale Ogunwale61911492017-10-11 08:50:50 -07001800 /**
1801 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1802 */
Louis Changdc077272019-11-12 16:52:56 +08001803 ActivityStack getSplitScreenPrimaryStack() {
1804 ActivityStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001805 return (stack != null && stack.isVisible()) ? stack : null;
1806 }
1807
Robert Carr9785cf32018-04-25 15:06:07 -07001808 boolean hasSplitScreenPrimaryStack() {
1809 return getSplitScreenPrimaryStack() != null;
1810 }
1811
Wale Ogunwale61911492017-10-11 08:50:50 -07001812 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001813 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001814 * not visible.
1815 */
Louis Changdc077272019-11-12 16:52:56 +08001816 ActivityStack getSplitScreenPrimaryStackIgnoringVisibility() {
Matthew Ng64e77cf2017-10-31 14:01:31 -07001817 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001818 }
1819
Louis Changdc077272019-11-12 16:52:56 +08001820 ActivityStack getPinnedStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001821 return mTaskStackContainers.getPinnedStack();
1822 }
1823
Riddle Hsuccf09402019-08-13 00:33:06 +08001824 boolean hasPinnedStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001825 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001826 }
1827
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001828 /**
1829 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1830 * Null is no compatible stack on the display.
1831 */
Louis Changdc077272019-11-12 16:52:56 +08001832 ActivityStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001833 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1834 }
1835
1836 /**
1837 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1838 * activity type. Null is no compatible stack on the display.
1839 */
Louis Changdc077272019-11-12 16:52:56 +08001840 ActivityStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001841 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001842 }
1843
Bryce Lee48f4b572017-04-10 10:54:15 -07001844 @VisibleForTesting
Louis Changdc077272019-11-12 16:52:56 +08001845 WindowList<ActivityStack> getStacks() {
Kazuki Takise148d00a2018-05-31 15:32:19 +09001846 return mTaskStackContainers.mChildren;
1847 }
1848
1849 @VisibleForTesting
Louis Changdc077272019-11-12 16:52:56 +08001850 ActivityStack getTopStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001851 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001852 }
1853
Winson Chunge2d72172018-01-25 17:46:20 +00001854 ArrayList<Task> getVisibleTasks() {
1855 return mTaskStackContainers.getVisibleTasks();
1856 }
1857
Louis Changdc077272019-11-12 16:52:56 +08001858 void onStackWindowingModeChanged(ActivityStack stack) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001859 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001860 }
1861
Andrii Kulian441e4492016-09-29 15:25:00 -07001862 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001863 public void onConfigurationChanged(Configuration newParentConfig) {
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -05001864 final int lastOrientation = getConfiguration().orientation;
Andrii Kulian441e4492016-09-29 15:25:00 -07001865 super.onConfigurationChanged(newParentConfig);
Tiger Huang7c610aa2018-10-27 00:01:01 +08001866 if (mDisplayPolicy != null) {
1867 mDisplayPolicy.onConfigurationChanged();
1868 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001869
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -05001870 if (lastOrientation != getConfiguration().orientation) {
1871 getMetricsLogger().write(
1872 new LogMaker(MetricsEvent.ACTION_PHONE_ORIENTATION_CHANGED)
1873 .setSubtype(getConfiguration().orientation)
1874 .addTaggedData(MetricsEvent.FIELD_DISPLAY_ID, getDisplayId()));
1875 }
1876
Evan Roskye747c3e2018-10-30 20:06:41 -07001877 // If there was no pinned stack, we still need to notify the controller of the display info
1878 // update as a result of the config change.
1879 if (mPinnedStackControllerLocked != null && !hasPinnedStack()) {
1880 mPinnedStackControllerLocked.onDisplayInfoChanged(getDisplayInfo());
1881 }
Evan Roskye747c3e2018-10-30 20:06:41 -07001882 }
1883
1884 /**
1885 * Updates the resources used by docked/pinned controllers. This needs to be called at the
1886 * beginning of a configuration update cascade since the metrics from these resources are used
1887 * for bounds calculations. Since ActivityDisplay initiates the configuration update, this
1888 * should be called from there instead of DisplayContent's onConfigurationChanged.
1889 */
1890 void preOnConfigurationChanged() {
Bryce Leef3c6a472017-11-14 14:53:06 -08001891 final DockedStackDividerController dividerController = getDockedDividerController();
1892
1893 if (dividerController != null) {
1894 getDockedDividerController().onConfigurationChanged();
1895 }
1896
1897 final PinnedStackController pinnedStackController = getPinnedStackController();
1898
1899 if (pinnedStackController != null) {
1900 getPinnedStackController().onConfigurationChanged();
1901 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001902 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001903
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001904 @Override
1905 boolean fillsParent() {
1906 return true;
1907 }
1908
1909 @Override
1910 boolean isVisible() {
1911 return true;
1912 }
1913
1914 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001915 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001916 super.onAppTransitionDone();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001917 mWmService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001918 }
1919
Yunfan Chen7daa6ac2018-11-29 18:16:44 -08001920 @Override
1921 public void setWindowingMode(int windowingMode) {
1922 super.setWindowingMode(windowingMode);
1923 super.setDisplayWindowingMode(windowingMode);
1924 }
1925
1926 @Override
1927 void setDisplayWindowingMode(int windowingMode) {
1928 setWindowingMode(windowingMode);
1929 }
1930
Robert Carr9785cf32018-04-25 15:06:07 -07001931 /**
1932 * In split-screen mode we process the IME containers above the docked divider
1933 * rather than directly above their target.
1934 */
1935 private boolean skipTraverseChild(WindowContainer child) {
lumarkff0ab692018-11-05 20:32:30 +08001936 if (child == mImeWindowsContainers && mInputMethodTarget != null
Robert Carr9785cf32018-04-25 15:06:07 -07001937 && !hasSplitScreenPrimaryStack()) {
1938 return true;
1939 }
1940 return false;
1941 }
1942
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001943 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001944 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1945 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1946 // target, or here in order if there isn't an IME target.
1947 if (traverseTopToBottom) {
1948 for (int i = mChildren.size() - 1; i >= 0; --i) {
1949 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001950 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001951 continue;
1952 }
Robert Carr9785cf32018-04-25 15:06:07 -07001953
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001954 if (child.forAllWindows(callback, traverseTopToBottom)) {
1955 return true;
1956 }
1957 }
1958 } else {
1959 final int count = mChildren.size();
1960 for (int i = 0; i < count; i++) {
1961 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001962 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001963 continue;
1964 }
Robert Carr9785cf32018-04-25 15:06:07 -07001965
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001966 if (child.forAllWindows(callback, traverseTopToBottom)) {
1967 return true;
1968 }
1969 }
1970 }
1971 return false;
1972 }
1973
1974 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1975 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1976 }
1977
Riddle Hsuccf09402019-08-13 00:33:06 +08001978 /**
1979 * In the general case, the orientation is computed from the above app windows first. If none of
1980 * the above app windows specify orientation, the orientation is computed from the child window
1981 * container, e.g. {@link AppWindowToken#getOrientation(int)}.
1982 */
Wale Ogunwale399c8692017-05-08 14:22:42 -07001983 @Override
1984 int getOrientation() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001985 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001986
Tiger Huang86e6d072019-05-02 20:23:47 +08001987 if (mIgnoreRotationForApps) {
1988 return SCREEN_ORIENTATION_USER;
1989 }
1990
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001991 if (mWmService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001992 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Adrian Roosb125e0b2019-10-02 14:55:14 +02001993 ProtoLog.v(WM_DEBUG_ORIENTATION,
1994 "Display id=%d is frozen, return %d", mDisplayId,
1995 mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001996 // If the display is frozen, some activities may be in the middle of restarting, and
1997 // thus have removed their old window. If the window has the flag to hide the lock
1998 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1999 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08002000 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002001 } else if (policy.isKeyguardLocked()) {
2002 // Use the last orientation the while the display is frozen with the keyguard
2003 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
2004 // window. We don't want to check the show when locked window directly though as
2005 // things aren't stable while the display is frozen, for example the window could be
2006 // momentarily unavailable due to activity relaunch.
Adrian Roosb125e0b2019-10-02 14:55:14 +02002007 ProtoLog.v(WM_DEBUG_ORIENTATION,
2008 "Display id=%d is frozen while keyguard locked, return %d",
2009 mDisplayId, getLastOrientation());
Riddle Hsuccf09402019-08-13 00:33:06 +08002010 return getLastOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07002011 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002012 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002013 final int orientation = mAboveAppWindowsContainers.getOrientation();
2014 if (orientation != SCREEN_ORIENTATION_UNSET) {
2015 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002016 }
Wale Ogunwale51362492016-09-08 17:49:17 -07002017 }
2018
Wale Ogunwale399c8692017-05-08 14:22:42 -07002019 // Top system windows are not requesting an orientation. Start searching from apps.
2020 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07002021 }
2022
Craig Mautner46ac6fa2013-08-01 10:06:34 -07002023 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07002024 // Check if display metrics changed and update base values if needed.
2025 updateBaseDisplayMetricsIfNeeded();
2026
Craig Mautner722285e2012-09-07 13:55:58 -07002027 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07002028 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07002029
Garfield Tan2f145f22018-11-01 15:27:03 -07002030 onDisplayChanged(this);
Craig Mautner722285e2012-09-07 13:55:58 -07002031 }
2032
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02002033 @Override
2034 void onDisplayChanged(DisplayContent dc) {
2035 super.onDisplayChanged(dc);
2036 updateSystemGestureExclusionLimit();
2037 }
2038
2039 void updateSystemGestureExclusionLimit() {
Adrian Roos1c2e9a12019-08-20 18:23:47 +02002040 mSystemGestureExclusionLimit = mWmService.mConstants.mSystemGestureExclusionLimitDp
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02002041 * mDisplayMetrics.densityDpi / DENSITY_DEFAULT;
2042 updateSystemGestureExclusion();
2043 }
2044
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002045 void initializeDisplayBaseInfo() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002046 final DisplayManagerInternal displayManagerInternal = mWmService.mDisplayManagerInternal;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002047 if (displayManagerInternal != null) {
2048 // Bootstrap the default logical display from the display manager.
2049 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
2050 if (newDisplayInfo != null) {
2051 mDisplayInfo.copyFrom(newDisplayInfo);
2052 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002053 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002054
Andrii Kuliancd097992017-03-23 18:31:59 -07002055 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
2056 mDisplayInfo.logicalDensityDpi);
2057 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
2058 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
2059 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002060 mInitialDisplayCutout = mDisplayInfo.displayCutout;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002061 }
2062
Andrii Kuliancd097992017-03-23 18:31:59 -07002063 /**
2064 * If display metrics changed, overrides are not set and it's not just a rotation - update base
2065 * values.
2066 */
2067 private void updateBaseDisplayMetricsIfNeeded() {
2068 // Get real display metrics without overrides from WM.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002069 mWmService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
Andrii Kuliancd097992017-03-23 18:31:59 -07002070 final int orientation = mDisplayInfo.rotation;
2071 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2072 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
2073 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
2074 final int newDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002075 final DisplayCutout newCutout = mDisplayInfo.displayCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07002076
2077 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
2078 || mInitialDisplayHeight != newHeight
Adrian Roos1cf585052018-01-03 18:43:27 +01002079 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi
2080 || !Objects.equals(mInitialDisplayCutout, newCutout);
Andrii Kuliancd097992017-03-23 18:31:59 -07002081
2082 if (displayMetricsChanged) {
2083 // Check if display size or density is forced.
2084 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
2085 || mBaseDisplayHeight != mInitialDisplayHeight;
2086 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
2087
2088 // If there is an override set for base values - use it, otherwise use new values.
2089 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
2090 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
2091 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
2092
2093 // Real display metrics changed, so we should also update initial values.
2094 mInitialDisplayWidth = newWidth;
2095 mInitialDisplayHeight = newHeight;
2096 mInitialDisplayDensity = newDensity;
Adrian Roos1cf585052018-01-03 18:43:27 +01002097 mInitialDisplayCutout = newCutout;
Shivam Agrawal6472e0e2019-07-03 16:27:49 -07002098 reconfigureDisplayLocked();
Andrii Kuliancd097992017-03-23 18:31:59 -07002099 }
2100 }
2101
Bryce Lee27cec322017-03-21 09:41:37 -07002102 /** Sets the maximum width the screen resolution can be */
2103 void setMaxUiWidth(int width) {
2104 if (DEBUG_DISPLAY) {
2105 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
2106 }
2107
2108 mMaxUiWidth = width;
2109
2110 // Update existing metrics.
2111 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
2112 }
2113
2114 /** Update base (override) display metrics. */
2115 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
2116 mBaseDisplayWidth = baseWidth;
2117 mBaseDisplayHeight = baseHeight;
2118 mBaseDisplayDensity = baseDensity;
2119
2120 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
2121 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
2122 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
2123 mBaseDisplayWidth = mMaxUiWidth;
2124
2125 if (DEBUG_DISPLAY) {
2126 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
2127 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
2128 + " on display:" + getDisplayId());
2129 }
2130 }
2131
2132 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08002133
2134 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07002135 }
2136
Riddle Hsuf53da812018-08-15 22:00:27 +08002137 /**
2138 * Forces this display to use the specified density.
2139 *
2140 * @param density The density in DPI to use. If the value equals to initial density, the setting
2141 * will be cleared.
2142 * @param userId The target user to apply. Only meaningful when this is default display. If the
2143 * user id is {@link UserHandle#USER_CURRENT}, it means to apply current settings
2144 * so only need to configure display.
2145 */
2146 void setForcedDensity(int density, int userId) {
Riddle Hsuf53da812018-08-15 22:00:27 +08002147 final boolean updateCurrent = userId == UserHandle.USER_CURRENT;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002148 if (mWmService.mCurrentUserId == userId || updateCurrent) {
Riddle Hsuf53da812018-08-15 22:00:27 +08002149 mBaseDisplayDensity = density;
Shivam Agrawal6472e0e2019-07-03 16:27:49 -07002150 reconfigureDisplayLocked();
Riddle Hsuf53da812018-08-15 22:00:27 +08002151 }
2152 if (updateCurrent) {
2153 // We are applying existing settings so no need to save it again.
2154 return;
2155 }
2156
2157 if (density == mInitialDisplayDensity) {
2158 density = 0;
2159 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002160 mWmService.mDisplayWindowSettings.setForcedDensity(this, density, userId);
Riddle Hsuf53da812018-08-15 22:00:27 +08002161 }
2162
2163 /** @param mode {@link #FORCE_SCALING_MODE_AUTO} or {@link #FORCE_SCALING_MODE_DISABLED}. */
2164 void setForcedScalingMode(@ForceScalingMode int mode) {
2165 if (mode != FORCE_SCALING_MODE_DISABLED) {
2166 mode = FORCE_SCALING_MODE_AUTO;
2167 }
2168
2169 mDisplayScalingDisabled = (mode != FORCE_SCALING_MODE_AUTO);
2170 Slog.i(TAG_WM, "Using display scaling mode: " + (mDisplayScalingDisabled ? "off" : "auto"));
Shivam Agrawal6472e0e2019-07-03 16:27:49 -07002171 reconfigureDisplayLocked();
Riddle Hsuf53da812018-08-15 22:00:27 +08002172
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002173 mWmService.mDisplayWindowSettings.setForcedScalingMode(this, mode);
Riddle Hsuf53da812018-08-15 22:00:27 +08002174 }
2175
2176 /** If the given width and height equal to initial size, the setting will be cleared. */
2177 void setForcedSize(int width, int height) {
2178 final boolean clear = mInitialDisplayWidth == width && mInitialDisplayHeight == height;
2179 if (!clear) {
2180 // Set some sort of reasonable bounds on the size of the display that we will try
2181 // to emulate.
2182 final int minSize = 200;
2183 final int maxScale = 2;
2184 width = Math.min(Math.max(width, minSize), mInitialDisplayWidth * maxScale);
2185 height = Math.min(Math.max(height, minSize), mInitialDisplayHeight * maxScale);
2186 }
2187
2188 Slog.i(TAG_WM, "Using new display size: " + width + "x" + height);
2189 updateBaseDisplayMetrics(width, height, mBaseDisplayDensity);
Shivam Agrawal6472e0e2019-07-03 16:27:49 -07002190 reconfigureDisplayLocked();
Riddle Hsuf53da812018-08-15 22:00:27 +08002191
2192 if (clear) {
2193 width = height = 0;
2194 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002195 mWmService.mDisplayWindowSettings.setForcedSize(this, width, height);
Riddle Hsuf53da812018-08-15 22:00:27 +08002196 }
2197
Wale Ogunwaledb506192017-12-08 10:57:32 -08002198 void getStableRect(Rect out) {
2199 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08002200 }
2201
Louis Changdc077272019-11-12 16:52:56 +08002202 void setStackOnDisplay(ActivityStack stack, int position) {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07002203 if (DEBUG_STACK) Slog.d(TAG_WM, "Set stack=" + stack + " on displayId=" + mDisplayId);
2204 mTaskStackContainers.addChild(stack, position);
Andrii Kulian839def92016-11-02 10:58:58 -07002205 }
2206
Louis Changdc077272019-11-12 16:52:56 +08002207 void moveStackToDisplay(ActivityStack stack, boolean onTop) {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07002208 stack.reparent(mTaskStackContainers, onTop ? POSITION_TOP: POSITION_BOTTOM);
2209 }
Andrii Kulian839def92016-11-02 10:58:58 -07002210
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07002211 // TODO(display-unify): No longer needed then.
Louis Changdc077272019-11-12 16:52:56 +08002212 void removeStackFromDisplay(ActivityStack stack) {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07002213 mTaskStackContainers.removeChild(stack);
Craig Mautnerc00204b2013-03-05 15:02:14 -08002214 }
2215
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002216 @Override
2217 protected void addChild(DisplayChildWindowContainer child,
2218 Comparator<DisplayChildWindowContainer> comparator) {
2219 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2220 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002221
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002222 @Override
2223 protected void addChild(DisplayChildWindowContainer child, int index) {
2224 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2225 }
2226
2227 @Override
2228 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002229 // Only allow removal of direct children from this display if the display is in the process
2230 // of been removed.
2231 if (mRemovingDisplay) {
2232 super.removeChild(child);
2233 return;
2234 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002235 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002236 }
2237
Andrii Kuliand2765632016-12-12 22:26:34 -08002238 @Override
2239 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
2240 // Children of the display are statically ordered, so the real intention here is to perform
2241 // the operation on the display and not the static direct children.
2242 getParent().positionChildAt(position, this, includingParents);
2243 }
2244
Louis Changdc077272019-11-12 16:52:56 +08002245 void positionStackAt(int position, ActivityStack child, boolean includingParents) {
Riddle Hsu57831b52018-07-27 00:31:48 +08002246 mTaskStackContainers.positionChildAt(position, child, includingParents);
Winson Chung59a47ded2018-01-25 17:46:06 +00002247 layoutAndAssignWindowLayersIfNeeded();
2248 }
2249
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002250 /**
Winson Chungc5fe7ff2019-02-19 14:49:25 -08002251 * Returns true if the input point is within an app window.
2252 */
2253 boolean pointWithinAppWindow(int x, int y) {
2254 final int[] targetWindowType = {-1};
Wale Ogunwalef5f3f1b2019-11-12 09:47:30 -08002255 final PooledConsumer fn = PooledLambda.obtainConsumer((w, nonArg) -> {
Winson Chungc5fe7ff2019-02-19 14:49:25 -08002256 if (targetWindowType[0] != -1) {
2257 return;
2258 }
2259
2260 if (w.isOnScreen() && w.isVisibleLw() && w.getFrameLw().contains(x, y)) {
2261 targetWindowType[0] = w.mAttrs.type;
2262 return;
2263 }
2264 }, PooledLambda.__(WindowState.class), mTmpRect);
2265 forAllWindows(fn, true /* traverseTopToBottom */);
Wale Ogunwalef5f3f1b2019-11-12 09:47:30 -08002266 fn.recycle();
Winson Chungc5fe7ff2019-02-19 14:49:25 -08002267 return FIRST_APPLICATION_WINDOW <= targetWindowType[0]
2268 && targetWindowType[0] <= LAST_APPLICATION_WINDOW;
2269 }
2270
2271 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08002272 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07002273 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07002274 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07002275 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002276 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002277 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07002278 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
Louis Changdc077272019-11-12 16:52:56 +08002279 final ActivityStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002280 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002281 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002282 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002283
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002284 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
2285 if (mTmpTaskForResizePointSearchResult.searchDone) {
2286 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07002287 }
2288 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002289 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002290 }
2291
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002292 void updateTouchExcludeRegion() {
2293 final Task focusedTask = (mFocusedApp != null ? mFocusedApp.getTask() : null);
David Stevensee9e2772017-02-09 16:30:27 -08002294 if (focusedTask == null) {
2295 mTouchExcludeRegion.setEmpty();
2296 } else {
2297 mTouchExcludeRegion.set(mBaseDisplayRect);
2298 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
2299 mTmpRect2.setEmpty();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002300 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0;
2301 --stackNdx) {
Louis Changdc077272019-11-12 16:52:56 +08002302 final ActivityStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002303 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
2304 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08002305 }
2306 // If we removed the focused task above, add it back and only leave its
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002307 // outside touch area in the exclusion. TapDetector is not interested in
David Stevensee9e2772017-02-09 16:30:27 -08002308 // any touch inside the focused task itself.
2309 if (!mTmpRect2.isEmpty()) {
2310 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
2311 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08002312 }
lumark90120a82018-08-15 00:33:03 +08002313 if (mInputMethodWindow != null && mInputMethodWindow.isVisibleLw()) {
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002314 // If the input method is visible and the user is typing, we don't want these touch
2315 // events to be intercepted and used to change focus. This would likely cause a
2316 // disappearance of the input method.
lumark90120a82018-08-15 00:33:03 +08002317 mInputMethodWindow.getTouchableRegion(mTmpRegion);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002318 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002319 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002320 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002321 final WindowState win = mTapExcludedWindows.get(i);
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002322 win.getTouchableRegion(mTmpRegion);
2323 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
2324 }
Tiger Huang04dc4cc2019-01-17 18:41:41 +08002325 amendWindowTapExcludeRegion(mTouchExcludeRegion);
Andrii Kulian03c403d2016-11-11 11:14:12 -08002326 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07002327 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002328 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07002329 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002330 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
2331 }
Riddle Hsu2588ab02019-02-25 14:23:56 +08002332 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner6601b7b2013-04-29 10:29:11 -07002333 }
2334
Tiger Huang04dc4cc2019-01-17 18:41:41 +08002335 /**
2336 * Union the region with all the tap exclude region provided by windows on this display.
2337 *
2338 * @param inOutRegion The region to be amended.
2339 */
2340 void amendWindowTapExcludeRegion(Region inOutRegion) {
2341 for (int i = mTapExcludeProvidingWindows.size() - 1; i >= 0; i--) {
2342 final WindowState win = mTapExcludeProvidingWindows.valueAt(i);
2343 win.amendTapExcludeRegion(inOutRegion);
2344 }
2345 }
2346
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002347 @Override
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07002348 void switchUser(int userId) {
2349 super.switchUser(userId);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002350 mWmService.mWindowsChanged = true;
Winson Chungda20fec2019-04-10 12:19:59 -07002351 mDisplayPolicy.switchUser();
Craig Mautner858d8a62013-04-23 17:08:34 -07002352 }
2353
Wale Ogunwale10124582016-09-15 20:25:50 -07002354 @Override
2355 void removeIfPossible() {
lumark9bca6b42019-10-17 18:35:22 +08002356 if (isAnimating(TRANSITION | PARENTS)) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002357 mDeferredRemoval = true;
2358 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07002359 }
Wale Ogunwale10124582016-09-15 20:25:50 -07002360 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07002361 }
2362
Wale Ogunwale10124582016-09-15 20:25:50 -07002363 @Override
2364 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002365 mRemovingDisplay = true;
2366 try {
Riddle Hsu6b76cd32019-10-08 00:37:19 +08002367 if (mActivityDisplay != null) {
2368 mActivityDisplay.unregisterConfigurationChangeListener(this);
2369 }
Jackal Guoc43a0a62019-04-23 09:15:14 +08002370 if (mParentWindow != null) {
2371 mParentWindow.removeEmbeddedDisplayContent(this);
2372 }
lumark588a3e82018-07-20 18:53:54 +08002373 // Clear all transitions & screen frozen states when removing display.
2374 mOpeningApps.clear();
2375 mClosingApps.clear();
Evan Rosky2289ba12018-11-19 18:28:18 -08002376 mChangingApps.clear();
lumark588a3e82018-07-20 18:53:54 +08002377 mUnknownAppVisibilityController.clear();
2378 mAppTransition.removeAppTransitionTimeoutCallbacks();
2379 handleAnimatingStoppedAndTransition();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002380 mWmService.stopFreezingDisplayLocked();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002381 super.removeImmediately();
2382 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Riddle Hsu2588ab02019-02-25 14:23:56 +08002383 mPointerEventDispatcher.dispose();
Vadim Caenb3715832019-08-13 17:06:38 +02002384 setRotationAnimation(null);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002385 mWmService.mAnimator.removeDisplayLocked(mDisplayId);
Louis Chang207f9112018-07-30 08:58:54 +08002386 mWindowingLayer.release();
2387 mOverlayLayer.release();
Jorim Jaggiae962e62018-12-27 17:23:48 +01002388 mInputMonitor.onDisplayRemoved();
Evan Rosky69cace42019-09-20 16:28:13 -07002389 mWmService.mDisplayNotificationController.dispatchDisplayRemoved(mActivityDisplay);
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002390 } finally {
Riddle Hsu4e611772018-10-31 18:58:28 +08002391 mDisplayReady = false;
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002392 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08002393 }
Robert Carr679ccb02018-08-08 15:32:35 -07002394
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002395 mWmService.mWindowPlacerLocked.requestTraversal();
Craig Mautner95da1082014-02-24 17:54:35 -08002396 }
2397
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002398 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07002399 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002400 boolean checkCompleteDeferredRemoval() {
Riddle Hsu793442c52019-11-05 01:04:55 +08002401 boolean stillDeferringRemoval = false;
2402
2403 for (int i = getChildCount() - 1; i >= 0; --i) {
2404 final DisplayChildWindowContainer child = getChildAt(i);
2405 stillDeferringRemoval |= child.checkCompleteDeferredRemoval();
2406 if (getChildCount() == 0) {
2407 // If this display is pending to be removed because it contains an activity with
2408 // {@link ActivityRecord#mIsExiting} is true, this display may be removed when
2409 // completing the removal of the last activity from
2410 // {@link ActivityRecord#checkCompleteDeferredRemoval}.
2411 return false;
2412 }
2413 }
Wale Ogunwale10124582016-09-15 20:25:50 -07002414
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002415 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002416 removeImmediately();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002417 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08002418 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002419 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08002420 }
2421
Andrii Kulian0214ed92017-05-16 13:44:05 -07002422 /** @return 'true' if removal of this display content is deferred due to active animation. */
2423 boolean isRemovalDeferred() {
2424 return mDeferredRemoval;
2425 }
2426
Wale Ogunwale10124582016-09-15 20:25:50 -07002427 boolean animateForIme(float interpolatedValue, float animationTarget,
2428 float dividerAnimationTarget) {
2429 boolean updated = false;
2430
Wale Ogunwale61911492017-10-11 08:50:50 -07002431 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
Louis Changdc077272019-11-12 16:52:56 +08002432 final ActivityStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002433 if (stack == null || !stack.isAdjustedForIme()) {
2434 continue;
2435 }
2436
2437 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
2438 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2439 updated = true;
2440 } else {
2441 mDividerControllerLocked.mLastAnimationProgress =
2442 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
2443 mDividerControllerLocked.mLastDividerProgress =
2444 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
2445 updated |= stack.updateAdjustForIme(
2446 mDividerControllerLocked.mLastAnimationProgress,
2447 mDividerControllerLocked.mLastDividerProgress,
2448 false /* force */);
2449 }
2450 if (interpolatedValue >= 1f) {
2451 stack.endImeAdjustAnimation();
2452 }
2453 }
2454
2455 return updated;
2456 }
2457
2458 boolean clearImeAdjustAnimation() {
2459 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002460 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
Louis Changdc077272019-11-12 16:52:56 +08002461 final ActivityStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002462 if (stack != null && stack.isAdjustedForIme()) {
2463 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2464 changed = true;
2465 }
2466 }
2467 return changed;
2468 }
2469
2470 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002471 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
Louis Changdc077272019-11-12 16:52:56 +08002472 final ActivityStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002473 if (stack.isVisible() && stack.isAdjustedForIme()) {
2474 stack.beginImeAdjustAnimation();
2475 }
2476 }
2477 }
2478
2479 void adjustForImeIfNeeded() {
lumark90120a82018-08-15 00:33:03 +08002480 final WindowState imeWin = mInputMethodWindow;
Wale Ogunwale10124582016-09-15 20:25:50 -07002481 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2482 && !mDividerControllerLocked.isImeHideRequested();
Louis Changdc077272019-11-12 16:52:56 +08002483 final ActivityStack dockedStack = getSplitScreenPrimaryStack();
Riddle Hsuc6814252019-05-16 17:06:52 +08002484 final boolean dockVisible = dockedStack != null;
2485 final Task topDockedTask = dockVisible ? dockedStack.getTopChild() : null;
Louis Changdc077272019-11-12 16:52:56 +08002486 final ActivityStack imeTargetStack = mWmService.getImeFocusStackLocked();
Wale Ogunwale10124582016-09-15 20:25:50 -07002487 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2488 imeTargetStack.getDockSide() : DOCKED_INVALID;
2489 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2490 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002491 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002492 final boolean imeHeightChanged = imeVisible &&
2493 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2494
Riddle Hsuc6814252019-05-16 17:06:52 +08002495 // This includes a case where the docked stack is unminimizing and IME is visible for the
2496 // bottom side stack. The condition prevents adjusting the override task bounds for IME to
2497 // the minimized docked stack bounds.
2498 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock()
2499 || (topDockedTask != null && imeOnBottom && !dockedStack.isAdjustedForIme()
2500 && dockedStack.getBounds().height() < topDockedTask.getBounds().height());
2501
Wale Ogunwale10124582016-09-15 20:25:50 -07002502 // The divider could be adjusted for IME position, or be thinner than usual,
2503 // or both. There are three possible cases:
2504 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2505 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2506 // - If IME is not visible, divider is not moved and is normal width.
2507
2508 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002509 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
Louis Changdc077272019-11-12 16:52:56 +08002510 final ActivityStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002511 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002512 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2513 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002514 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2515 } else {
2516 stack.resetAdjustedForIme(false);
2517 }
2518 }
2519 mDividerControllerLocked.setAdjustedForIme(
2520 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2521 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002522 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
Louis Changdc077272019-11-12 16:52:56 +08002523 final ActivityStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002524 stack.resetAdjustedForIme(!dockVisible);
2525 }
2526 mDividerControllerLocked.setAdjustedForIme(
2527 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2528 }
Winson Chung655332c2016-10-31 13:14:28 -07002529 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002530 }
2531
2532 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002533 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
Louis Changdc077272019-11-12 16:52:56 +08002534 final ActivityStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002535 stack.prepareFreezingTaskBounds();
2536 }
2537 }
2538
Wale Ogunwale94744212015-09-21 19:01:47 -07002539 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002540 getBounds(mTmpRect, newRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002541 rotateBounds(mTmpRect, oldRotation, newRotation, bounds);
2542 }
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002543
Evan Rosky39b6f232018-10-30 18:35:41 -07002544 void rotateBounds(Rect parentBounds, int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002545 // Compute a transform matrix to undo the coordinate space transformation,
2546 // and present the window at the same physical position it previously occupied.
2547 final int deltaRotation = deltaRotation(newRotation, oldRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002548 createRotationMatrix(
2549 deltaRotation, parentBounds.width(), parentBounds.height(), mTmpMatrix);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002550
2551 mTmpRectF.set(bounds);
2552 mTmpMatrix.mapRect(mTmpRectF);
2553 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002554 }
2555
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002556 static int deltaRotation(int oldRotation, int newRotation) {
2557 int delta = newRotation - oldRotation;
2558 if (delta < 0) delta += 4;
2559 return delta;
2560 }
2561
Vadim Caenb3715832019-08-13 17:06:38 +02002562 public void setRotationAnimation(ScreenRotationAnimation screenRotationAnimation) {
2563 if (mScreenRotationAnimation != null) {
2564 mScreenRotationAnimation.kill();
2565 }
2566 mScreenRotationAnimation = screenRotationAnimation;
2567 }
2568
2569 public ScreenRotationAnimation getRotationAnimation() {
2570 return mScreenRotationAnimation;
2571 }
2572
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002573 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002574 Matrix outMatrix) {
2575 // For rotations without Z-ordering we don't need the target rectangle's position.
2576 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2577 displayHeight, outMatrix);
2578 }
2579
2580 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2581 float displayWidth, float displayHeight, Matrix outMatrix) {
2582 switch (rotation) {
2583 case ROTATION_0:
2584 outMatrix.reset();
2585 break;
2586 case ROTATION_270:
2587 outMatrix.setRotate(270, 0, 0);
2588 outMatrix.postTranslate(0, displayHeight);
2589 outMatrix.postTranslate(rectTop, 0);
2590 break;
2591 case ROTATION_180:
2592 outMatrix.reset();
2593 break;
2594 case ROTATION_90:
2595 outMatrix.setRotate(90, 0, 0);
2596 outMatrix.postTranslate(displayWidth, 0);
2597 outMatrix.postTranslate(-rectTop, rectLeft);
2598 break;
2599 }
2600 }
2601
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002602 @CallSuper
2603 @Override
Nataniel Borges023ecb52019-01-16 14:15:43 -08002604 public void writeToProto(ProtoOutputStream proto, long fieldId,
2605 @WindowTraceLogLevel int logLevel) {
2606 // Critical log level logs only visible elements to mitigate performance overheard
2607 if (logLevel == WindowTraceLogLevel.CRITICAL && !isVisible()) {
2608 return;
2609 }
2610
Steven Timotiusaf03df62017-07-18 16:56:43 -07002611 final long token = proto.start(fieldId);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002612 super.writeToProto(proto, WINDOW_CONTAINER, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002613 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002614 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
Louis Changdc077272019-11-12 16:52:56 +08002615 final ActivityStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07002616 stack.writeToProtoInnerStackOnly(proto, STACKS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002617 }
2618 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2619 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002620 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2621 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002622 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002623 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002624 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2625 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002626 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002627 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002628 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2629 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002630 windowToken.writeToProto(proto, IME_WINDOWS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002631 }
2632 proto.write(DPI, mBaseDisplayDensity);
2633 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Riddle Hsuccf09402019-08-13 00:33:06 +08002634 proto.write(ROTATION, getRotation());
Vadim Caenb3715832019-08-13 17:06:38 +02002635 final ScreenRotationAnimation screenRotationAnimation = getRotationAnimation();
Steven Timotiusf2d68892017-08-28 17:00:01 -07002636 if (screenRotationAnimation != null) {
2637 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2638 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002639 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
lumark588a3e82018-07-20 18:53:54 +08002640 mAppTransition.writeToProto(proto, APP_TRANSITION);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002641 if (mFocusedApp != null) {
2642 mFocusedApp.writeNameToProto(proto, FOCUSED_APP);
2643 }
Jorim Jaggi1112fed2019-04-15 13:32:14 +02002644 for (int i = mOpeningApps.size() - 1; i >= 0; i--) {
Wale Ogunwaleda8b8272018-11-29 19:37:37 -08002645 mOpeningApps.valueAt(i).writeIdentifierToProto(proto, OPENING_APPS);
Jorim Jaggi1112fed2019-04-15 13:32:14 +02002646 }
2647 for (int i = mClosingApps.size() - 1; i >= 0; i--) {
Wale Ogunwaleda8b8272018-11-29 19:37:37 -08002648 mClosingApps.valueAt(i).writeIdentifierToProto(proto, CLOSING_APPS);
Jorim Jaggi1112fed2019-04-15 13:32:14 +02002649 }
2650 for (int i = mChangingApps.size() - 1; i >= 0; i--) {
Wale Ogunwaleda8b8272018-11-29 19:37:37 -08002651 mChangingApps.valueAt(i).writeIdentifierToProto(proto, CHANGING_APPS);
Jorim Jaggi1112fed2019-04-15 13:32:14 +02002652 }
Steven Timotiusaf03df62017-07-18 16:56:43 -07002653 proto.end(token);
2654 }
2655
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002656 @Override
2657 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2658 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002659 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2660 final String subPrefix = " " + prefix;
2661 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2662 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2663 pw.print("dpi");
2664 if (mInitialDisplayWidth != mBaseDisplayWidth
2665 || mInitialDisplayHeight != mBaseDisplayHeight
2666 || mInitialDisplayDensity != mBaseDisplayDensity) {
2667 pw.print(" base=");
2668 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2669 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2670 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002671 if (mDisplayScalingDisabled) {
2672 pw.println(" noscale");
2673 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002674 pw.print(" cur=");
2675 pw.print(mDisplayInfo.logicalWidth);
2676 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2677 pw.print(" app=");
2678 pw.print(mDisplayInfo.appWidth);
2679 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2680 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2681 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2682 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2683 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002684 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002685 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002686 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002687
Craig Mautnerdc548482014-02-05 13:35:24 -08002688 pw.println();
Adrian Roos5251b1d2018-03-23 18:57:43 +01002689 pw.print(prefix); pw.print("mLayoutSeq="); pw.println(mLayoutSeq);
2690
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002691 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
2692 if (mLastFocus != mCurrentFocus) {
2693 pw.print(" mLastFocus="); pw.println(mLastFocus);
2694 }
2695 if (mLosingFocus.size() > 0) {
2696 pw.println();
2697 pw.println(" Windows losing focus:");
2698 for (int i = mLosingFocus.size() - 1; i >= 0; i--) {
2699 final WindowState w = mLosingFocus.get(i);
2700 pw.print(" Losing #"); pw.print(i); pw.print(' ');
2701 pw.print(w);
2702 if (dumpAll) {
2703 pw.println(":");
2704 w.dump(pw, " ", true);
2705 } else {
2706 pw.println();
2707 }
2708 }
2709 }
2710 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
Tiger Huang7c610aa2018-10-27 00:01:01 +08002711 if (mLastStatusBarVisibility != 0) {
2712 pw.print(" mLastStatusBarVisibility=0x");
2713 pw.println(Integer.toHexString(mLastStatusBarVisibility));
2714 }
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002715
Adrian Roos5251b1d2018-03-23 18:57:43 +01002716 pw.println();
wilsonshihc32538e2018-11-07 17:27:34 +08002717 mWallpaperController.dump(pw, " ");
2718
2719 pw.println();
Adrian Roos4ffc8972019-02-07 20:45:11 +01002720 pw.print("mSystemGestureExclusion=");
2721 if (mSystemGestureExclusionListeners.getRegisteredCallbackCount() > 0) {
2722 pw.println(mSystemGestureExclusion);
2723 } else {
2724 pw.println("<no lstnrs>");
2725 }
2726
2727 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002728 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002729 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
Louis Changdc077272019-11-12 16:52:56 +08002730 final ActivityStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002731 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002732 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002733
Craig Mautnerdc548482014-02-05 13:35:24 -08002734 pw.println();
2735 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002736 pw.println();
2737 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002738 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002739 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002740 pw.print(" Exiting #"); pw.print(i);
2741 pw.print(' '); pw.print(token);
2742 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002743 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002744 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002745 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002746
Vadim Caenb3715832019-08-13 17:06:38 +02002747 final ScreenRotationAnimation rotationAnimation = getRotationAnimation();
2748 if (rotationAnimation != null) {
2749 pw.print(subPrefix);
2750 pw.println(" mScreenRotationAnimation:");
2751 rotationAnimation.printTo(" ", pw);
2752 } else if (dumpAll) {
2753 pw.print(subPrefix);
2754 pw.println(" no ScreenRotationAnimation ");
2755 }
2756
Jorim Jaggi31f71702016-05-04 16:43:04 -07002757 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002758
2759 // Dump stack references
Louis Changdc077272019-11-12 16:52:56 +08002760 final ActivityStack homeStack = getHomeStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002761 if (homeStack != null) {
2762 pw.println(prefix + "homeStack=" + homeStack.getName());
2763 }
Louis Changdc077272019-11-12 16:52:56 +08002764 final ActivityStack pinnedStack = getPinnedStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002765 if (pinnedStack != null) {
2766 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2767 }
Louis Changdc077272019-11-12 16:52:56 +08002768 final ActivityStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002769 if (splitScreenPrimaryStack != null) {
2770 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2771 }
2772
2773 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002774 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002775 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002776 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002777
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002778 pw.println();
2779 mDisplayFrames.dump(prefix, pw);
Arthur Hung95b38a92018-07-20 18:56:12 +08002780 pw.println();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08002781 mDisplayPolicy.dump(prefix, pw);
2782 pw.println();
Riddle Hsuad256a12018-07-18 16:11:30 +08002783 mDisplayRotation.dump(prefix, pw);
2784 pw.println();
Arthur Hung95b38a92018-07-20 18:56:12 +08002785 mInputMonitor.dump(pw, " ");
Jorim Jaggif96c90a2018-09-26 16:55:15 +02002786 pw.println();
2787 mInsetsStateController.dump(prefix, pw);
Craig Mautner59c00972012-07-30 12:10:24 -07002788 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002789
2790 @Override
2791 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002792 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002793 }
2794
2795 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002796 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002797 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002798
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002799 /** Returns true if the stack in the windowing mode is visible. */
2800 boolean isStackVisible(int windowingMode) {
Louis Changdc077272019-11-12 16:52:56 +08002801 final ActivityStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002802 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002803 }
2804
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002805 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002806 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002807 final int x = (int) xf;
2808 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002809 final WindowState touchedWin = getWindow(w -> {
2810 final int flags = w.mAttrs.flags;
2811 if (!w.isVisibleLw()) {
2812 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002813 }
2814 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002815 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002816 }
2817
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002818 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002819 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002820 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002821 }
2822
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002823 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002824
2825 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002826 return mTmpRegion.contains(x, y) || touchFlags == 0;
2827 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002828
2829 return touchedWin;
2830 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002831
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002832 boolean canAddToastWindowForUid(int uid) {
2833 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002834 // Also if the app is focused adding more than one toast at
2835 // a time for better backwards compatibility.
2836 final WindowState focusedWindowForUid = getWindow(w ->
2837 w.mOwnerUid == uid && w.isFocused());
2838 if (focusedWindowForUid != null) {
2839 return true;
2840 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002841 final WindowState win = getWindow(w ->
2842 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2843 && !w.mWindowRemovalAllowed);
2844 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002845 }
2846
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002847 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002848 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2849 return;
2850 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002851
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002852 // Used to communicate the old focus to the callback method.
2853 mTmpWindow = oldFocus;
2854
2855 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002856 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002857
Louis Changa9350fe2019-04-25 17:14:20 +08002858 /**
2859 * Looking for the focused window on this display if the top focused display hasn't been
2860 * found yet (topFocusedDisplayId is INVALID_DISPLAY) or per-display focused was allowed.
2861 *
2862 * @param topFocusedDisplayId Id of the top focused display.
2863 * @return The focused window or null if there isn't any or no need to seek.
2864 */
2865 WindowState findFocusedWindowIfNeeded(int topFocusedDisplayId) {
2866 return (mWmService.mPerDisplayFocusEnabled || topFocusedDisplayId == INVALID_DISPLAY)
2867 ? findFocusedWindow() : null;
Tiger Huang51c5a1d2018-12-11 20:24:51 +08002868 }
2869
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002870 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002871 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002872
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002873 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002874
2875 if (mTmpWindow == null) {
Adrian Roosb125e0b2019-10-02 14:55:14 +02002876 ProtoLog.v(WM_DEBUG_FOCUS_LIGHT, "findFocusedWindow: No focusable windows.");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002877 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002878 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002879 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002880 }
2881
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002882 /**
2883 * Update the focused window and make some adjustments if the focus has changed.
2884 *
2885 * @param mode Indicates the situation we are in. Possible modes are:
2886 * {@link WindowManagerService#UPDATE_FOCUS_NORMAL},
2887 * {@link WindowManagerService#UPDATE_FOCUS_PLACING_SURFACES},
2888 * {@link WindowManagerService#UPDATE_FOCUS_WILL_PLACE_SURFACES},
2889 * {@link WindowManagerService#UPDATE_FOCUS_REMOVING_FOCUS}
2890 * @param updateInputWindows Whether to sync the window information to the input module.
Louis Changa9350fe2019-04-25 17:14:20 +08002891 * @param topFocusedDisplayId Display id of current top focused display.
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002892 * @return {@code true} if the focused window has changed.
2893 */
Louis Changa9350fe2019-04-25 17:14:20 +08002894 boolean updateFocusedWindowLocked(int mode, boolean updateInputWindows,
2895 int topFocusedDisplayId) {
2896 WindowState newFocus = findFocusedWindowIfNeeded(topFocusedDisplayId);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002897 if (mCurrentFocus == newFocus) {
2898 return false;
2899 }
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002900 boolean imWindowChanged = false;
Tiger Huang51c5a1d2018-12-11 20:24:51 +08002901 final WindowState imWindow = mInputMethodWindow;
2902 if (imWindow != null) {
2903 final WindowState prevTarget = mInputMethodTarget;
2904 final WindowState newTarget = computeImeTarget(true /* updateImeTarget*/);
2905 imWindowChanged = prevTarget != newTarget;
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002906
Tiger Huang51c5a1d2018-12-11 20:24:51 +08002907 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS
2908 && mode != UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2909 assignWindowLayers(false /* setLayoutNeeded */);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002910 }
2911 }
2912
2913 if (imWindowChanged) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002914 mWmService.mWindowsChanged = true;
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002915 setLayoutNeeded();
Louis Changa9350fe2019-04-25 17:14:20 +08002916 newFocus = findFocusedWindowIfNeeded(topFocusedDisplayId);
Tiger Huang51c5a1d2018-12-11 20:24:51 +08002917 }
2918 if (mCurrentFocus != newFocus) {
2919 mWmService.mH.obtainMessage(REPORT_FOCUS_CHANGE, this).sendToTarget();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002920 }
2921
Adrian Roosb125e0b2019-10-02 14:55:14 +02002922 ProtoLog.v(WM_DEBUG_FOCUS_LIGHT, "Changing focus from %s to %s displayId=%d Callers=%s",
2923 mCurrentFocus, newFocus, getDisplayId(), Debug.getCallers(4));
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002924 final WindowState oldFocus = mCurrentFocus;
2925 mCurrentFocus = newFocus;
2926 mLosingFocus.remove(newFocus);
2927
2928 if (newFocus != null) {
2929 mWinAddedSinceNullFocus.clear();
2930 mWinRemovedSinceNullFocus.clear();
2931
2932 if (newFocus.canReceiveKeys()) {
2933 // Displaying a window implicitly causes dispatching to be unpaused.
2934 // This is to protect against bugs if someone pauses dispatching but
2935 // forgets to resume.
2936 newFocus.mToken.paused = false;
2937 }
2938 }
2939
Tiger Huang7c610aa2018-10-27 00:01:01 +08002940 int focusChanged = getDisplayPolicy().focusChangedLw(oldFocus, newFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002941
2942 if (imWindowChanged && oldFocus != mInputMethodWindow) {
2943 // Focus of the input method window changed. Perform layout if needed.
2944 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2945 performLayout(true /*initial*/, updateInputWindows);
2946 focusChanged &= ~FINISH_LAYOUT_REDO_LAYOUT;
2947 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2948 // Client will do the layout, but we need to assign layers
2949 // for handleNewWindowLocked() below.
2950 assignWindowLayers(false /* setLayoutNeeded */);
2951 }
2952 }
2953
2954 if ((focusChanged & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2955 // The change in focus caused us to need to do a layout. Okay.
2956 setLayoutNeeded();
2957 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2958 performLayout(true /*initial*/, updateInputWindows);
2959 } else if (mode == UPDATE_FOCUS_REMOVING_FOCUS) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002960 mWmService.mRoot.performSurfacePlacement(false);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002961 }
2962 }
2963
2964 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
2965 // If we defer assigning layers, then the caller is responsible for doing this part.
2966 getInputMonitor().setInputFocusLw(newFocus, updateInputWindows);
2967 }
2968
2969 adjustForImeIfNeeded();
2970
2971 // We may need to schedule some toast windows to be removed. The toasts for an app that
2972 // does not have input focus are removed within a timeout to prevent apps to redress
2973 // other apps' UI.
2974 scheduleToastWindowsTimeoutIfNeededLocked(oldFocus, newFocus);
2975
2976 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2977 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
2978 }
2979 return true;
2980 }
2981
2982 /**
2983 * Set the new focused app to this display.
2984 *
Garfield Tane8d84ab2019-10-11 09:49:40 -07002985 * @param newFocus the new focused {@link ActivityRecord}.
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002986 * @return true if the focused app is changed.
2987 */
Garfield Tane8d84ab2019-10-11 09:49:40 -07002988 boolean setFocusedApp(ActivityRecord newFocus) {
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002989 if (newFocus != null) {
2990 final DisplayContent appDisplay = newFocus.getDisplayContent();
2991 if (appDisplay != this) {
2992 throw new IllegalStateException(newFocus + " is not on " + getName()
2993 + " but " + ((appDisplay != null) ? appDisplay.getName() : "none"));
2994 }
2995 }
2996 if (mFocusedApp == newFocus) {
2997 return false;
2998 }
2999 mFocusedApp = newFocus;
3000 getInputMonitor().setFocusedAppLw(newFocus);
3001 updateTouchExcludeRegion();
3002 return true;
3003 }
3004
Wale Ogunwalec69694a2016-10-18 13:51:15 -07003005 /** Updates the layer assignment of windows on this display. */
3006 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggi4981f152019-03-26 18:58:45 +01003007 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07003008 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07003009 if (setLayoutNeeded) {
3010 setLayoutNeeded();
3011 }
Robert Carrb1579c82017-09-05 14:54:47 -07003012
Robert Carrf59b8dd2017-10-02 18:58:36 -07003013 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07003014 // the application of this transaction until the animation pass triggers
3015 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
3016 // the hiding and showing of surfaces.
3017 scheduleAnimation();
Jorim Jaggi4981f152019-03-26 18:58:45 +01003018 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07003019 }
3020
Wale Ogunwale1666e312016-12-16 11:27:18 -08003021 // TODO: This should probably be called any time a visual change is made to the hierarchy like
3022 // moving containers or resizing them. Need to investigate the best way to have it automatically
3023 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003024 void layoutAndAssignWindowLayersIfNeeded() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003025 mWmService.mWindowsChanged = true;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003026 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003027
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003028 if (!mWmService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003029 false /*updateInputWindows*/)) {
3030 assignWindowLayers(false /* setLayoutNeeded */);
3031 }
3032
Arthur Hung95b38a92018-07-20 18:56:12 +08003033 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003034 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Arthur Hung95b38a92018-07-20 18:56:12 +08003035 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003036 }
3037
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003038 /** Returns true if a leaked surface was destroyed */
3039 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003040 // Used to indicate that a surface was leaked.
3041 mTmpWindow = null;
3042 forAllWindows(w -> {
3043 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003044 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003045 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003046 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003047 if (!mWmService.mSessions.contains(wsa.mSession)) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003048 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003049 + w + " surface=" + wsa.mSurfaceController
3050 + " token=" + w.mToken
3051 + " pid=" + w.mSession.mPid
3052 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003053 wsa.destroySurface();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003054 mWmService.mForceRemoves.add(w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003055 mTmpWindow = w;
Issei Suzukid4ee1cc2019-11-08 13:20:14 +01003056 } else if (w.mActivityRecord != null && !w.mActivityRecord.isClientVisible()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003057 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003058 + w + " surface=" + wsa.mSurfaceController
Garfield Tane8d84ab2019-10-11 09:49:40 -07003059 + " token=" + w.mActivityRecord);
Adrian Roosb125e0b2019-10-02 14:55:14 +02003060 ProtoLog.i(WM_SHOW_TRANSACTIONS, "SURFACE LEAK DESTROY: %s", w);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003061 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003062 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003063 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003064 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003065
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003066 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003067 }
3068
Riddle Hsuccf09402019-08-13 00:33:06 +08003069 boolean hasAlertWindowSurfaces() {
3070 for (int i = mWmService.mSessions.size() - 1; i >= 0; --i) {
3071 if (mWmService.mSessions.valueAt(i).hasAlertWindowSurfaces(this)) {
3072 return true;
3073 }
3074 }
3075 return false;
3076 }
3077
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003078 /**
lumark90120a82018-08-15 00:33:03 +08003079 * Set input method window for the display.
3080 * @param win Set when window added or Null when destroyed.
3081 */
3082 void setInputMethodWindowLocked(WindowState win) {
3083 mInputMethodWindow = win;
lumark70865a82018-09-17 16:56:26 +08003084 // Update display configuration for IME process.
3085 if (mInputMethodWindow != null) {
3086 final int imePid = mInputMethodWindow.mSession.mPid;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003087 mWmService.mAtmInternal.onImeWindowSetOnDisplay(imePid,
lumark70865a82018-09-17 16:56:26 +08003088 mInputMethodWindow.getDisplayId());
3089 }
lumark90120a82018-08-15 00:33:03 +08003090 computeImeTarget(true /* updateImeTarget */);
Tiger Huang332793b2019-10-29 23:21:27 +08003091 mInsetsStateController.getSourceProvider(ITYPE_IME).setWindow(win,
Jorim Jaggif96c90a2018-09-26 16:55:15 +02003092 null /* frameProvider */);
lumark90120a82018-08-15 00:33:03 +08003093 }
3094
3095 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003096 * Determine and return the window that should be the IME target.
3097 * @param updateImeTarget If true the system IME target will be updated to match what we found.
3098 * @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 +00003099 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003100 WindowState computeImeTarget(boolean updateImeTarget) {
lumark90120a82018-08-15 00:33:03 +08003101 if (mInputMethodWindow == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003102 // There isn't an IME so there shouldn't be a target...That was easy!
3103 if (updateImeTarget) {
3104 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
lumarkff0ab692018-11-05 20:32:30 +08003105 + mInputMethodTarget + " to null since mInputMethodWindow is null");
3106 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003107 }
3108 return null;
3109 }
3110
lumarkff0ab692018-11-05 20:32:30 +08003111 final WindowState curTarget = mInputMethodTarget;
Chavi Weingarten3a748552018-05-14 17:32:42 +00003112 if (!canUpdateImeTarget()) {
3113 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Defer updating IME target");
3114 return curTarget;
3115 }
3116
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003117 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
3118 // same display. Or even when the current IME/target are not on the same screen as the next
3119 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08003120 mUpdateImeTarget = updateImeTarget;
3121 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003122
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003123
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003124 // Yet more tricksyness! If this window is a "starting" window, we do actually want
3125 // to be on top of it, but it is not -really- where input will go. So look down below
3126 // for a real window to target...
3127 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07003128 final ActivityRecord activity = target.mActivityRecord;
3129 if (activity != null) {
3130 final WindowState betterTarget = activity.getImeTargetBelowWindow(target);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003131 if (betterTarget != null) {
3132 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003133 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003134 }
3135 }
3136
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003137 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
lumarkff0ab692018-11-05 20:32:30 +08003138 "Proposed new IME target: " + target + " for display: " + getDisplayId());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003139
chaviw1c13ad32018-06-12 16:44:23 -07003140 // Now, a special case -- if the last target's window is in the process of exiting, but
Tarandeep Singh2d0aca42019-03-28 14:08:58 -07003141 // not removed, keep on the last target to avoid IME flicker.
chaviw1c13ad32018-06-12 16:44:23 -07003142 if (curTarget != null && !curTarget.mRemoved && curTarget.isDisplayedLw()
Tarandeep Singh2d0aca42019-03-28 14:08:58 -07003143 && curTarget.isClosing()) {
3144 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Not changing target till current window is"
3145 + " closing and not removed");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003146 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003147 }
3148
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003149 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
3150 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003151
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003152 if (target == null) {
3153 if (updateImeTarget) {
3154 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
3155 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
3156 + Debug.getCallers(4) : ""));
lumarkff0ab692018-11-05 20:32:30 +08003157 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003158 }
3159
3160 return null;
3161 }
3162
3163 if (updateImeTarget) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07003164 ActivityRecord activity = curTarget == null ? null : curTarget.mActivityRecord;
3165 if (activity != null) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003166
3167 // Now some fun for dealing with window animations that modify the Z order. We need
3168 // to look at all windows below the current target that are in this app, finding the
3169 // highest visible one in layering.
3170 WindowState highestTarget = null;
lumark9bca6b42019-10-17 18:35:22 +08003171 if (activity.isAnimating(TRANSITION)) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07003172 highestTarget = activity.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003173 }
3174
3175 if (highestTarget != null) {
lumark9bca6b42019-10-17 18:35:22 +08003176 if (DEBUG_INPUT_METHOD) {
3177 Slog.v(TAG_WM, mAppTransition + " " + highestTarget + " animating="
3178 + highestTarget.isAnimating(TRANSITION | PARENTS));
3179 }
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003180
lumark588a3e82018-07-20 18:53:54 +08003181 if (mAppTransition.isTransitionSet()) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003182 // If we are currently setting up for an animation, hold everything until we
3183 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003184 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003185 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003186 }
3187 }
3188 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003189
3190 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
3191 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003192 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003193 }
3194
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003195 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003196 }
3197
lumarkff0ab692018-11-05 20:32:30 +08003198 /**
3199 * Calling {@link #computeImeTarget(boolean)} to update the input method target window in
3200 * the candidate app window token if needed.
3201 */
Garfield Tane8d84ab2019-10-11 09:49:40 -07003202 void computeImeTargetIfNeeded(ActivityRecord candidate) {
3203 if (mInputMethodTarget != null && mInputMethodTarget.mActivityRecord == candidate) {
lumarkff0ab692018-11-05 20:32:30 +08003204 computeImeTarget(true /* updateImeTarget */);
3205 }
3206 }
3207
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003208 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
lumarkff0ab692018-11-05 20:32:30 +08003209 if (target == mInputMethodTarget && mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003210 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003211 }
3212
lumarkff0ab692018-11-05 20:32:30 +08003213 mInputMethodTarget = target;
3214 mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003215 assignWindowLayers(false /* setLayoutNeeded */);
Tarandeep Singh215929b2019-01-11 18:24:37 -08003216 mInsetsStateController.onImeTargetChanged(target);
Tarandeep Singha6f35612019-01-11 19:50:46 -08003217 updateImeParent();
3218 }
3219
3220 private void updateImeParent() {
Jackal Guo818c3f12019-03-08 18:00:39 +08003221 // Force attaching IME to the display when magnifying, or it would be magnified with
3222 // target app together.
3223 final boolean shouldAttachToDisplay = (mMagnificationSpec != null);
3224 final SurfaceControl newParent =
3225 shouldAttachToDisplay ? mWindowingLayer : computeImeParent();
Tarandeep Singha6f35612019-01-11 19:50:46 -08003226 if (newParent != null) {
Tiger Huanged6794e2019-05-07 20:07:59 +08003227 getPendingTransaction().reparent(mImeWindowsContainers.mSurfaceControl, newParent);
Tarandeep Singha6f35612019-01-11 19:50:46 -08003228 scheduleAnimation();
3229 }
3230 }
3231
3232 /**
3233 * Computes the window the IME should be attached to.
3234 */
3235 @VisibleForTesting
3236 SurfaceControl computeImeParent() {
3237
3238 // Attach it to app if the target is part of an app and such app is covering the entire
3239 // screen. If it's not covering the entire screen the IME might extend beyond the apps
3240 // bounds.
Garfield Tane8d84ab2019-10-11 09:49:40 -07003241 if (mInputMethodTarget != null && mInputMethodTarget.mActivityRecord != null
Riddle Hsu6d6f67c2019-03-14 16:54:26 +08003242 && mInputMethodTarget.getWindowingMode() == WINDOWING_MODE_FULLSCREEN
3243 // An activity with override bounds should be letterboxed inside its parent bounds,
3244 // so it doesn't fill the screen.
Garfield Tane8d84ab2019-10-11 09:49:40 -07003245 && mInputMethodTarget.mActivityRecord.matchParentBounds()) {
3246 return mInputMethodTarget.mActivityRecord.getSurfaceControl();
Tarandeep Singha6f35612019-01-11 19:50:46 -08003247 }
3248
3249 // Otherwise, we just attach it to the display.
3250 return mWindowingLayer;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003251 }
3252
3253 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
3254 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3255 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
3256 }
3257
3258 // Used to indicate we have reached the first window in the range we are interested in.
3259 mTmpWindow = null;
3260
3261 // TODO: Figure-out a more efficient way to do this.
3262 final WindowState candidate = getWindow(w -> {
3263 if (w == top) {
3264 // Reached the first window in the range we are interested in.
3265 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003266 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003267 if (mTmpWindow == null) {
3268 return false;
3269 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003270
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003271 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3272 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003273 }
3274 // If we reached the bottom of the range of windows we are considering,
3275 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003276 if (w == bottom) {
3277 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003278 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003279 return false;
3280 });
3281
3282 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003283 }
3284
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003285 void setLayoutNeeded() {
3286 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
3287 mLayoutNeeded = true;
3288 }
3289
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003290 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003291 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
3292 mLayoutNeeded = false;
3293 }
3294
3295 boolean isLayoutNeeded() {
3296 return mLayoutNeeded;
3297 }
3298
Wale Ogunwale02319a62016-09-26 15:21:22 -07003299 void dumpTokens(PrintWriter pw, boolean dumpAll) {
3300 if (mTokenMap.isEmpty()) {
3301 return;
3302 }
3303 pw.println(" Display #" + mDisplayId);
3304 final Iterator<WindowToken> it = mTokenMap.values().iterator();
3305 while (it.hasNext()) {
3306 final WindowToken token = it.next();
3307 pw.print(" ");
3308 pw.print(token);
3309 if (dumpAll) {
3310 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003311 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07003312 } else {
3313 pw.println();
3314 }
3315 }
lumark588a3e82018-07-20 18:53:54 +08003316
Evan Rosky2289ba12018-11-19 18:28:18 -08003317 if (!mOpeningApps.isEmpty() || !mClosingApps.isEmpty() || !mChangingApps.isEmpty()) {
lumark588a3e82018-07-20 18:53:54 +08003318 pw.println();
3319 if (mOpeningApps.size() > 0) {
3320 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
3321 }
3322 if (mClosingApps.size() > 0) {
3323 pw.print(" mClosingApps="); pw.println(mClosingApps);
3324 }
Evan Rosky2289ba12018-11-19 18:28:18 -08003325 if (mChangingApps.size() > 0) {
3326 pw.print(" mChangingApps="); pw.println(mChangingApps);
3327 }
lumark588a3e82018-07-20 18:53:54 +08003328 }
3329
3330 mUnknownAppVisibilityController.dump(pw, " ");
Wale Ogunwale02319a62016-09-26 15:21:22 -07003331 }
3332
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003333 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003334 final int[] index = new int[1];
3335 forAllWindows(w -> {
3336 final WindowStateAnimator wAnim = w.mWinAnimator;
3337 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
3338 index[0] = index[0] + 1;
3339 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003340 }
3341
Jorim Jaggife762342016-10-13 14:33:27 +02003342 /**
3343 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
3344 */
Issei Suzuki5609ccb2019-06-13 15:04:08 +02003345 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade,
3346 boolean subtle) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003347 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003348 forAllWindows(w -> {
Garfield Tane8d84ab2019-10-11 09:49:40 -07003349 if (w.mActivityRecord == null && policy.canBeHiddenByKeyguardLw(w)
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02003350 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Issei Suzuki5609ccb2019-06-13 15:04:08 +02003351 w.startAnimation(policy.createHiddenByKeyguardExit(
3352 onWallpaper, goingToShade, subtle));
Jorim Jaggife762342016-10-13 14:33:27 +02003353 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003354 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02003355 }
3356
Riddle Hsub2297ad2019-07-26 23:37:25 -06003357 /** @return {@code true} if there is window to wait before enabling the screen. */
3358 boolean shouldWaitForSystemDecorWindowsOnBoot() {
3359 if (!isDefaultDisplay && !supportsSystemDecorations()) {
3360 // Nothing to wait because the secondary display doesn't support system decorations,
3361 // there is no wallpaper, keyguard (status bar) or application (home) window to show
3362 // during booting.
3363 return false;
3364 }
Wale Ogunwale494009b82016-10-21 09:01:38 -07003365
Riddle Hsub2297ad2019-07-26 23:37:25 -06003366 final SparseBooleanArray drawnWindowTypes = new SparseBooleanArray();
3367 // Presuppose keyguard is drawn because if its window isn't attached, we don't know if it
3368 // wants to be shown or hidden, then it should not delay enabling the screen.
3369 drawnWindowTypes.put(TYPE_STATUS_BAR, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003370
Riddle Hsub2297ad2019-07-26 23:37:25 -06003371 final WindowState visibleNotDrawnWindow = getWindow(w -> {
3372 if (w.mViewVisibility == View.VISIBLE && !w.mObscured && !w.isDrawnLw()) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003373 return true;
3374 }
3375 if (w.isDrawnLw()) {
Riddle Hsub2297ad2019-07-26 23:37:25 -06003376 final int type = w.mAttrs.type;
3377 if (type == TYPE_BOOT_PROGRESS || type == TYPE_BASE_APPLICATION
3378 || type == TYPE_WALLPAPER) {
3379 drawnWindowTypes.put(type, true);
3380 } else if (type == TYPE_STATUS_BAR) {
3381 drawnWindowTypes.put(TYPE_STATUS_BAR, mWmService.mPolicy.isKeyguardDrawnLw());
Wale Ogunwale494009b82016-10-21 09:01:38 -07003382 }
3383 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003384 return false;
3385 });
3386
Riddle Hsub2297ad2019-07-26 23:37:25 -06003387 if (visibleNotDrawnWindow != null) {
3388 // Wait for the visible window to be drawn.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003389 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003390 }
3391
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003392 // if the wallpaper service is disabled on the device, we're never going to have
3393 // wallpaper, don't bother waiting for it
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003394 boolean wallpaperEnabled = mWmService.mContext.getResources().getBoolean(
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003395 com.android.internal.R.bool.config_enableWallpaperService)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003396 && mWmService.mContext.getResources().getBoolean(
Daichi Hironoc1432222018-03-29 13:06:07 +09003397 com.android.internal.R.bool.config_checkWallpaperAtBoot)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003398 && !mWmService.mOnlyCore;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003399
Riddle Hsub2297ad2019-07-26 23:37:25 -06003400 final boolean haveBootMsg = drawnWindowTypes.get(TYPE_BOOT_PROGRESS);
3401 final boolean haveApp = drawnWindowTypes.get(TYPE_BASE_APPLICATION);
3402 final boolean haveWallpaper = drawnWindowTypes.get(TYPE_WALLPAPER);
3403 final boolean haveKeyguard = drawnWindowTypes.get(TYPE_STATUS_BAR);
3404
Adrian Roosb125e0b2019-10-02 14:55:14 +02003405 ProtoLog.i(WM_DEBUG_SCREEN_ON,
Riddle Hsub2297ad2019-07-26 23:37:25 -06003406 "******** booted=%b msg=%b haveBoot=%b haveApp=%b haveWall=%b "
3407 + "wallEnabled=%b haveKeyguard=%b",
3408 mWmService.mSystemBooted, mWmService.mShowingBootMessages, haveBootMsg,
3409 haveApp, haveWallpaper, wallpaperEnabled, haveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003410
3411 // If we are turning on the screen to show the boot message, don't do it until the boot
3412 // message is actually displayed.
Riddle Hsub2297ad2019-07-26 23:37:25 -06003413 if (!mWmService.mSystemBooted && !haveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003414 return true;
3415 }
3416
3417 // If we are turning on the screen after the boot is completed normally, don't do so until
3418 // we have the application and wallpaper.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003419 if (mWmService.mSystemBooted
Riddle Hsub2297ad2019-07-26 23:37:25 -06003420 && ((!haveApp && !haveKeyguard) || (wallpaperEnabled && !haveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003421 return true;
3422 }
3423
3424 return false;
3425 }
3426
Jorim Jaggi8f520872018-08-14 17:00:20 +02003427 void updateWindowsForAnimator() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003428 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003429 }
3430
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003431 boolean isInputMethodClientFocus(int uid, int pid) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003432 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003433 if (imFocus == null) {
3434 return false;
3435 }
3436
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003437 if (DEBUG_INPUT_METHOD) {
3438 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003439 Slog.i(TAG_WM, "Current focus: " + mCurrentFocus + " displayId=" + mDisplayId);
3440 Slog.i(TAG_WM, "Last focus: " + mLastFocus + " displayId=" + mDisplayId);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003441 }
3442
Wale Ogunwale494009b82016-10-21 09:01:38 -07003443 if (DEBUG_INPUT_METHOD) {
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003444 Slog.i(TAG_WM, "IM target uid/pid: " + imFocus.mSession.mUid
3445 + "/" + imFocus.mSession.mPid);
3446 Slog.i(TAG_WM, "Requesting client uid/pid: " + uid + "/" + pid);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003447 }
3448
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003449 return imFocus.mSession.mUid == uid && imFocus.mSession.mPid == pid;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003450 }
3451
3452 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003453 final WindowState win = getWindow(
3454 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
3455 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003456 }
3457
Jorim Jaggi956ca412019-01-07 14:49:14 +01003458 void hideTransientBars() {
3459 // TODO(b/118118435): Remove this after migration
3460 final int transientFlags = View.STATUS_BAR_TRANSIENT | View.NAVIGATION_BAR_TRANSIENT;
3461 statusBarVisibilityChanged(mLastStatusBarVisibility & ~transientFlags);
3462
3463 getInsetsPolicy().hideTransient();
3464 }
3465
Tiger Huang7c610aa2018-10-27 00:01:01 +08003466 void statusBarVisibilityChanged(int visibility) {
3467 mLastStatusBarVisibility = visibility;
3468 visibility = getDisplayPolicy().adjustSystemUiVisibilityLw(visibility);
3469 updateStatusBarVisibilityLocked(visibility);
3470 }
3471
3472 private boolean updateStatusBarVisibilityLocked(int visibility) {
3473 if (mLastDispatchedSystemUiVisibility == visibility) {
3474 return false;
3475 }
3476 final int globalDiff = (visibility ^ mLastDispatchedSystemUiVisibility)
3477 // We are only interested in differences of one of the
3478 // clearable flags...
3479 & View.SYSTEM_UI_CLEARABLE_FLAGS
3480 // ...if it has actually been cleared.
3481 & ~visibility;
3482
3483 mLastDispatchedSystemUiVisibility = visibility;
3484 if (isDefaultDisplay) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003485 mWmService.mInputManager.setSystemUiVisibility(visibility);
Tiger Huang7c610aa2018-10-27 00:01:01 +08003486 }
3487 updateSystemUiVisibility(visibility, globalDiff);
3488 return true;
3489 }
3490
Wale Ogunwale494009b82016-10-21 09:01:38 -07003491 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003492 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003493 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003494 final int curValue = w.mSystemUiVisibility;
3495 final int diff = (curValue ^ visibility) & globalDiff;
3496 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003497 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003498 w.mSeq++;
3499 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003500 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003501 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
3502 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07003503 visibility, newValue, diff);
3504 }
3505 } catch (RemoteException e) {
3506 // so sorry
3507 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003508 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003509 }
3510
Tiger Huang7c610aa2018-10-27 00:01:01 +08003511 void reevaluateStatusBarVisibility() {
3512 int visibility = getDisplayPolicy().adjustSystemUiVisibilityLw(mLastStatusBarVisibility);
3513 if (updateStatusBarVisibilityLocked(visibility)) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003514 mWmService.mWindowPlacerLocked.requestTraversal();
Tiger Huang7c610aa2018-10-27 00:01:01 +08003515 }
3516 }
3517
Wale Ogunwale494009b82016-10-21 09:01:38 -07003518 void onWindowFreezeTimeout() {
3519 Slog.w(TAG_WM, "Window freeze timeout expired.");
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003520 mWmService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003521
3522 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07003523 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003524 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003525 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07003526 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003527 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003528 - mWmService.mDisplayFreezeTime);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003529 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003530 }, true /* traverseTopToBottom */);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003531 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003532 }
3533
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003534 // TODO: Super crazy long method that should be broken down...
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003535 void applySurfaceChangesTransaction(boolean recoveringMemory) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003536 final WindowSurfacePlacer surfacePlacer = mWmService.mWindowPlacerLocked;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003537
3538 mTmpUpdateAllDrawn.clear();
3539
3540 int repeats = 0;
3541 do {
3542 repeats++;
3543 if (repeats > 6) {
3544 Slog.w(TAG, "Animation repeat aborted after too many iterations");
3545 clearLayoutNeeded();
3546 break;
3547 }
3548
3549 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
3550 pendingLayoutChanges);
3551
wilsonshihc32538e2018-11-07 17:27:34 +08003552 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
3553 mWallpaperController.adjustWallpaperWindows();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003554 }
3555
Riddle Hsu654a6f92018-07-13 22:59:36 +08003556 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003557 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
Riddle Hsuccf09402019-08-13 00:33:06 +08003558 if (updateOrientation()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003559 setLayoutNeeded();
Shivam Agrawal6472e0e2019-07-03 16:27:49 -07003560 sendNewConfiguration();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003561 }
3562 }
3563
3564 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
3565 setLayoutNeeded();
3566 }
3567
3568 // FIRST LOOP: Perform a layout, if needed.
3569 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
3570 performLayout(repeats == 1, false /* updateInputWindows */);
3571 } else {
3572 Slog.w(TAG, "Layout repeat skipped after too many iterations");
3573 }
3574
3575 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
3576 pendingLayoutChanges = 0;
3577
Jorim Jaggi4981f152019-03-26 18:58:45 +01003578 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "applyPostLayoutPolicy");
3579 try {
3580 mDisplayPolicy.beginPostLayoutPolicyLw();
3581 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
3582 pendingLayoutChanges |= mDisplayPolicy.finishPostLayoutPolicyLw();
3583 } finally {
3584 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
3585 }
Tiger Huang7c610aa2018-10-27 00:01:01 +08003586 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
3587 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
Jorim Jaggif96c90a2018-09-26 16:55:15 +02003588 mInsetsStateController.onPostLayout();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003589 } while (pendingLayoutChanges != 0);
3590
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003591 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003592
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003593 mTmpRecoveringMemory = recoveringMemory;
Jorim Jaggi4981f152019-03-26 18:58:45 +01003594
3595 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "applyWindowSurfaceChanges");
3596 try {
3597 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
3598 } finally {
3599 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
3600 }
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01003601 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003602
Jorim Jaggif1292892018-09-10 11:58:13 +02003603 mLastHasContent = mTmpApplySurfaceChangesTransactionState.displayHasContent;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003604 mWmService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Jorim Jaggif1292892018-09-10 11:58:13 +02003605 mLastHasContent,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003606 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
3607 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003608 true /* inTraversal, must call performTraversalInTrans... below */);
3609
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003610 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
3611 if (wallpaperVisible != mLastWallpaperVisible) {
3612 mLastWallpaperVisible = wallpaperVisible;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003613 mWmService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003614 }
3615
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003616 while (!mTmpUpdateAllDrawn.isEmpty()) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07003617 final ActivityRecord activity = mTmpUpdateAllDrawn.removeLast();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003618 // See if any windows have been drawn, so they (and others associated with them)
3619 // can now be shown.
Garfield Tane8d84ab2019-10-11 09:49:40 -07003620 activity.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003621 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003622 }
3623
Bryce Leef3c6a472017-11-14 14:53:06 -08003624 private void updateBounds() {
Evan Rosky39b6f232018-10-30 18:35:41 -07003625 calculateBounds(mDisplayInfo, mTmpBounds);
Bryce Leef3c6a472017-11-14 14:53:06 -08003626 setBounds(mTmpBounds);
Tiger Huang04dc4cc2019-01-17 18:41:41 +08003627 if (mPortalWindowHandle != null && mParentSurfaceControl != null) {
3628 mPortalWindowHandle.touchableRegion.getBounds(mTmpRect);
3629 if (!mTmpBounds.equals(mTmpRect)) {
3630 mPortalWindowHandle.touchableRegion.set(mTmpBounds);
Tiger Huanged6794e2019-05-07 20:07:59 +08003631 getPendingTransaction().setInputWindowInfo(
3632 mParentSurfaceControl, mPortalWindowHandle);
Tiger Huang04dc4cc2019-01-17 18:41:41 +08003633 }
3634 }
Bryce Leef3c6a472017-11-14 14:53:06 -08003635 }
3636
3637 // Determines the current display bounds based on the current state
Evan Rosky39b6f232018-10-30 18:35:41 -07003638 private void calculateBounds(DisplayInfo displayInfo, Rect out) {
Bryce Leef3c6a472017-11-14 14:53:06 -08003639 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Evan Rosky39b6f232018-10-30 18:35:41 -07003640 final int rotation = displayInfo.rotation;
3641 boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Bryce Leef3c6a472017-11-14 14:53:06 -08003642 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
3643 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Evan Rosky39b6f232018-10-30 18:35:41 -07003644 int width = displayInfo.logicalWidth;
Bryce Leef3c6a472017-11-14 14:53:06 -08003645 int left = (physWidth - width) / 2;
Evan Rosky39b6f232018-10-30 18:35:41 -07003646 int height = displayInfo.logicalHeight;
Bryce Leef3c6a472017-11-14 14:53:06 -08003647 int top = (physHeight - height) / 2;
3648 out.set(left, top, left + width, top + height);
3649 }
3650
Bryce Leef3c6a472017-11-14 14:53:06 -08003651 private void getBounds(Rect out, int orientation) {
3652 getBounds(out);
3653
3654 // Rotate the Rect if needed.
3655 final int currentRotation = mDisplayInfo.rotation;
3656 final int rotationDelta = deltaRotation(currentRotation, orientation);
3657 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
3658 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
3659 mTmpRectF.set(out);
3660 mTmpMatrix.mapRect(mTmpRectF);
3661 mTmpRectF.round(out);
3662 }
3663 }
3664
Evan Rosky730f6e82018-12-03 17:40:11 -08003665 /** @returns the orientation of the display when it's rotation is ROTATION_0. */
3666 int getNaturalOrientation() {
3667 return mBaseDisplayWidth < mBaseDisplayHeight
3668 ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
3669 }
3670
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003671 void performLayout(boolean initial, boolean updateInputWindows) {
Jorim Jaggi4981f152019-03-26 18:58:45 +01003672 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "performLayout");
3673 try {
3674 performLayoutNoTrace(initial, updateInputWindows);
3675 } finally {
3676 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
3677 }
3678 }
3679
3680 private void performLayoutNoTrace(boolean initial, boolean updateInputWindows) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003681 if (!isLayoutNeeded()) {
3682 return;
3683 }
3684 clearLayoutNeeded();
3685
3686 final int dw = mDisplayInfo.logicalWidth;
3687 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003688 if (DEBUG_LAYOUT) {
3689 Slog.v(TAG, "-------------------------------------");
Jorim Jaggi4981f152019-03-26 18:58:45 +01003690 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw
3691 + " dh=" + dh);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003692 }
3693
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01003694 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
3695 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Jorim Jaggi4981f152019-03-26 18:58:45 +01003696 // TODO: Not sure if we really need to set the rotation here since we are updating from
3697 // the display info above...
Riddle Hsuccf09402019-08-13 00:33:06 +08003698 mDisplayFrames.mRotation = getRotation();
Tiger Huang7c610aa2018-10-27 00:01:01 +08003699 mDisplayPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003700
Adrian Roos5251b1d2018-03-23 18:57:43 +01003701 int seq = mLayoutSeq + 1;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003702 if (seq < 0) seq = 0;
Adrian Roos5251b1d2018-03-23 18:57:43 +01003703 mLayoutSeq = seq;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003704
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003705 // Used to indicate that we have processed the dream window and all additional windows are
3706 // behind it.
3707 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003708 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003709
3710 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003711 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003712
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003713 // Used to indicate that we have processed the dream window and all additional attached
3714 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003715 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003716 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003717
3718 // Now perform layout of attached windows, which usually depend on the position of the
3719 // window they are attached to. XXX does not deal with windows that are attached to windows
3720 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003721 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003722
3723 // Window frames may have changed. Tell the input dispatcher about it.
Arthur Hung95b38a92018-07-20 18:56:12 +08003724 mInputMonitor.layoutInputConsumers(dw, dh);
3725 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003726 if (updateInputWindows) {
Arthur Hung95b38a92018-07-20 18:56:12 +08003727 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003728 }
3729
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003730 mWmService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003731 }
3732
3733 /**
3734 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
3735 * In portrait mode, it grabs the full screenshot.
3736 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003737 * @param config of the output bitmap
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003738 */
chaviw0315a1a2018-03-05 15:28:35 -08003739 Bitmap screenshotDisplayLocked(Bitmap.Config config) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003740 if (!mWmService.mPolicy.isScreenOn()) {
chaviw0315a1a2018-03-05 15:28:35 -08003741 if (DEBUG_SCREENSHOT) {
3742 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003743 }
chaviw0315a1a2018-03-05 15:28:35 -08003744 return null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003745 }
chaviwfbe47df2017-11-10 16:14:49 -08003746
chaviw0315a1a2018-03-05 15:28:35 -08003747 int dw = mDisplayInfo.logicalWidth;
3748 int dh = mDisplayInfo.logicalHeight;
chaviwfbe47df2017-11-10 16:14:49 -08003749
chaviw0315a1a2018-03-05 15:28:35 -08003750 if (dw <= 0 || dh <= 0) {
3751 return null;
3752 }
chaviwfbe47df2017-11-10 16:14:49 -08003753
chaviw0315a1a2018-03-05 15:28:35 -08003754 final Rect frame = new Rect(0, 0, dw, dh);
chaviwfbe47df2017-11-10 16:14:49 -08003755
chaviw0315a1a2018-03-05 15:28:35 -08003756 // The screenshot API does not apply the current screen rotation.
3757 int rot = mDisplay.getRotation();
chaviwfbe47df2017-11-10 16:14:49 -08003758
chaviw0315a1a2018-03-05 15:28:35 -08003759 if (rot == ROTATION_90 || rot == ROTATION_270) {
3760 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3761 }
chaviwfbe47df2017-11-10 16:14:49 -08003762
chaviw0315a1a2018-03-05 15:28:35 -08003763 // SurfaceFlinger is not aware of orientation, so convert our logical
3764 // crop to SurfaceFlinger's portrait orientation.
3765 convertCropForSurfaceFlinger(frame, rot, dw, dh);
3766
3767 final ScreenRotationAnimation screenRotationAnimation =
Vadim Caenb3715832019-08-13 17:06:38 +02003768 mWmService.mRoot.getDisplayContent(DEFAULT_DISPLAY).getRotationAnimation();
chaviw0315a1a2018-03-05 15:28:35 -08003769 final boolean inRotation = screenRotationAnimation != null &&
3770 screenRotationAnimation.isAnimating();
3771 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
3772
3773 // TODO(b/68392460): We should screenshot Task controls directly
3774 // but it's difficult at the moment as the Task doesn't have the
3775 // correct size set.
chaviw08520a02018-09-10 16:44:56 -07003776 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, inRotation, rot);
chaviw0315a1a2018-03-05 15:28:35 -08003777 if (bitmap == null) {
3778 Slog.w(TAG_WM, "Failed to take screenshot");
3779 return null;
3780 }
3781
3782 // Create a copy of the screenshot that is immutable and backed in ashmem.
3783 // This greatly reduces the overhead of passing the bitmap between processes.
3784 final Bitmap ret = bitmap.createAshmemBitmap(config);
3785 bitmap.recycle();
3786 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003787 }
3788
3789 // TODO: Can this use createRotationMatrix()?
3790 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3791 if (rot == Surface.ROTATION_90) {
3792 final int tmp = crop.top;
3793 crop.top = dw - crop.right;
3794 crop.right = crop.bottom;
3795 crop.bottom = dw - crop.left;
3796 crop.left = tmp;
3797 } else if (rot == Surface.ROTATION_180) {
3798 int tmp = crop.top;
3799 crop.top = dh - crop.bottom;
3800 crop.bottom = dh - tmp;
3801 tmp = crop.right;
3802 crop.right = dw - crop.left;
3803 crop.left = dw - tmp;
3804 } else if (rot == Surface.ROTATION_270) {
3805 final int tmp = crop.top;
3806 crop.top = crop.left;
3807 crop.left = dh - crop.bottom;
3808 crop.bottom = crop.right;
3809 crop.right = dh - tmp;
3810 }
3811 }
3812
Wale Ogunwale1666e312016-12-16 11:27:18 -08003813 void setExitingTokensHasVisible(boolean hasVisible) {
3814 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3815 mExitingTokens.get(i).hasVisible = hasVisible;
3816 }
3817
3818 // Initialize state of exiting applications.
3819 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3820 }
3821
3822 void removeExistingTokensIfPossible() {
3823 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3824 final WindowToken token = mExitingTokens.get(i);
3825 if (!token.hasVisible) {
3826 mExitingTokens.remove(i);
3827 }
3828 }
3829
3830 // Time to remove any exiting applications?
3831 mTaskStackContainers.removeExistingAppTokensIfPossible();
3832 }
3833
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003834 @Override
3835 void onDescendantOverrideConfigurationChanged() {
3836 setLayoutNeeded();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003837 mWmService.requestTraversal();
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003838 }
3839
David Stevens9440dc82017-03-16 19:00:20 -07003840 boolean okToDisplay() {
3841 if (mDisplayId == DEFAULT_DISPLAY) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003842 return !mWmService.mDisplayFrozen
3843 && mWmService.mDisplayEnabled && mWmService.mPolicy.isScreenOn();
David Stevens9440dc82017-03-16 19:00:20 -07003844 }
3845 return mDisplayInfo.state == Display.STATE_ON;
3846 }
3847
3848 boolean okToAnimate() {
3849 return okToDisplay() &&
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003850 (mDisplayId != DEFAULT_DISPLAY || mWmService.mPolicy.okToAnimate());
David Stevens9440dc82017-03-16 19:00:20 -07003851 }
3852
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003853 static final class TaskForResizePointSearchResult {
3854 boolean searchDone;
3855 Task taskForResize;
3856
3857 void reset() {
3858 searchDone = false;
3859 taskForResize = null;
3860 }
3861 }
Robert Carr3b716242016-08-16 16:02:21 -07003862
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003863 private static final class ApplySurfaceChangesTransactionState {
3864 boolean displayHasContent;
3865 boolean obscured;
3866 boolean syswin;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003867 float preferredRefreshRate;
3868 int preferredModeId;
3869
3870 void reset() {
3871 displayHasContent = false;
3872 obscured = false;
3873 syswin = false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003874 preferredRefreshRate = 0;
3875 preferredModeId = 0;
3876 }
3877 }
3878
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003879 /**
3880 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3881 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3882 * homogeneous children type which is currently required by sub-classes of
3883 * {@link WindowContainer} class.
3884 */
3885 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3886
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003887 DisplayChildWindowContainer(WindowManagerService service) {
3888 super(service);
3889 }
3890
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003891 @Override
3892 boolean fillsParent() {
3893 return true;
3894 }
3895
3896 @Override
3897 boolean isVisible() {
3898 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003899 }
3900 }
3901
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003902 /**
3903 * Window container class that contains all containers on this display relating to Apps.
3904 * I.e Activities.
3905 */
Louis Changdc077272019-11-12 16:52:56 +08003906 private final class TaskStackContainers extends DisplayChildWindowContainer<ActivityStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003907 /**
3908 * A control placed at the appropriate level for transitions to occur.
3909 */
chaviw23ee71c2017-12-18 11:29:41 -08003910 SurfaceControl mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02003911 SurfaceControl mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02003912 SurfaceControl mHomeAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003913
Robert Carrf7a7ca82017-12-06 13:17:07 -08003914 /**
3915 * Given that the split-screen divider does not have an AppWindowToken, it
3916 * will have to live inside of a "NonAppWindowContainer", in particular
3917 * {@link DisplayContent#mAboveAppWindowsContainers}. However, in visual Z order
3918 * it will need to be interleaved with some of our children, appearing on top of
3919 * both docked stacks but underneath any assistant stacks.
3920 *
3921 * To solve this problem we have this anchor control, which will always exist so
3922 * we can always assign it the correct value in our {@link #assignChildLayers}.
3923 * Likewise since it always exists, {@link AboveAppWindowContainers} can always
3924 * assign the divider a layer relative to it. This way we prevent linking lifecycle
3925 * events between the two containers.
3926 */
3927 SurfaceControl mSplitScreenDividerAnchor = null;
3928
Wale Ogunwale61911492017-10-11 08:50:50 -07003929 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3930 // through the list to find them.
Louis Changdc077272019-11-12 16:52:56 +08003931 private ActivityStack mHomeStack = null;
3932 private ActivityStack mPinnedStack = null;
3933 private ActivityStack mSplitScreenPrimaryStack = null;
Wale Ogunwale61911492017-10-11 08:50:50 -07003934
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003935 TaskStackContainers(WindowManagerService service) {
3936 super(service);
3937 }
3938
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07003939 @Override
3940 public void onConfigurationChanged(Configuration newParentConfig) {
3941 // TODO(display-unify): Remove after unification.
3942 onConfigurationChanged(newParentConfig, mActivityDisplay == null /*forwardToChildren*/);
3943 }
3944
Wale Ogunwale61911492017-10-11 08:50:50 -07003945 /**
3946 * Returns the topmost stack on the display that is compatible with the input windowing mode
3947 * and activity type. Null is no compatible stack on the display.
3948 */
Louis Changdc077272019-11-12 16:52:56 +08003949 ActivityStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003950 if (activityType == ACTIVITY_TYPE_HOME) {
3951 return mHomeStack;
3952 }
3953 if (windowingMode == WINDOWING_MODE_PINNED) {
3954 return mPinnedStack;
3955 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3956 return mSplitScreenPrimaryStack;
3957 }
3958 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
Louis Changdc077272019-11-12 16:52:56 +08003959 final ActivityStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003960 if (activityType == ACTIVITY_TYPE_UNDEFINED
3961 && windowingMode == stack.getWindowingMode()) {
3962 // Passing in undefined type means we want to match the topmost stack with the
3963 // windowing mode.
3964 return stack;
3965 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003966 if (stack.isCompatible(windowingMode, activityType)) {
3967 return stack;
3968 }
3969 }
3970 return null;
3971 }
3972
3973 @VisibleForTesting
Louis Changdc077272019-11-12 16:52:56 +08003974 ActivityStack getTopStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003975 return mTaskStackContainers.getChildCount() > 0
3976 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3977 }
3978
Louis Changdc077272019-11-12 16:52:56 +08003979 ActivityStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003980 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3981 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3982 }
3983 return mHomeStack;
3984 }
3985
Louis Changdc077272019-11-12 16:52:56 +08003986 ActivityStack getPinnedStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003987 return mPinnedStack;
3988 }
3989
Louis Changdc077272019-11-12 16:52:56 +08003990 ActivityStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003991 return mSplitScreenPrimaryStack;
3992 }
3993
Winson Chunge2d72172018-01-25 17:46:20 +00003994 ArrayList<Task> getVisibleTasks() {
3995 final ArrayList<Task> visibleTasks = new ArrayList<>();
3996 forAllTasks(task -> {
3997 if (task.isVisible()) {
3998 visibleTasks.add(task);
3999 }
4000 });
4001 return visibleTasks;
4002 }
4003
Louis Changdc077272019-11-12 16:52:56 +08004004 void onStackWindowingModeChanged(ActivityStack stack) {
Wale Ogunwale61911492017-10-11 08:50:50 -07004005 removeStackReferenceIfNeeded(stack);
4006 addStackReferenceIfNeeded(stack);
4007 if (stack == mPinnedStack && getTopStack() != stack) {
4008 // Looks like this stack changed windowing mode to pinned. Move it to the top.
4009 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
4010 }
4011 }
4012
Louis Changdc077272019-11-12 16:52:56 +08004013 private void addStackReferenceIfNeeded(ActivityStack stack) {
Wale Ogunwale61911492017-10-11 08:50:50 -07004014 if (stack.isActivityTypeHome()) {
4015 if (mHomeStack != null) {
Louis Chang7d0037c2018-08-13 12:42:06 +08004016 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
Wale Ogunwale61911492017-10-11 08:50:50 -07004017 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
Louis Chang7d0037c2018-08-13 12:42:06 +08004018
Wale Ogunwale61911492017-10-11 08:50:50 -07004019 }
Louis Chang7d0037c2018-08-13 12:42:06 +08004020 mHomeStack = stack;
Wale Ogunwale61911492017-10-11 08:50:50 -07004021 }
4022 final int windowingMode = stack.getWindowingMode();
4023 if (windowingMode == WINDOWING_MODE_PINNED) {
4024 if (mPinnedStack != null) {
4025 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
4026 + mPinnedStack + " already exist on display=" + this
4027 + " stack=" + stack);
4028 }
4029 mPinnedStack = stack;
4030 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
4031 if (mSplitScreenPrimaryStack != null) {
4032 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
4033 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
4034 + " already exist on display=" + this + " stack=" + stack);
4035 }
4036 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004037 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07004038 }
4039 }
4040
Louis Changdc077272019-11-12 16:52:56 +08004041 private void removeStackReferenceIfNeeded(ActivityStack stack) {
Wale Ogunwale61911492017-10-11 08:50:50 -07004042 if (stack == mHomeStack) {
4043 mHomeStack = null;
4044 } else if (stack == mPinnedStack) {
4045 mPinnedStack = null;
4046 } else if (stack == mSplitScreenPrimaryStack) {
4047 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004048 // Re-set the split-screen create mode whenever the split-screen stack is removed.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004049 mWmService.setDockedStackCreateStateLocked(
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004050 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
4051 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07004052 }
Andrii Kulian839def92016-11-02 10:58:58 -07004053 }
4054
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07004055 @Override
Louis Changdc077272019-11-12 16:52:56 +08004056 void addChild(ActivityStack stack, int position) {
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07004057 addStackReferenceIfNeeded(stack);
4058 position = findPositionForStack(position, stack, true /* adding */);
4059
4060 super.addChild(stack, position);
4061 if (mActivityDisplay != null) {
Louis Changdc077272019-11-12 16:52:56 +08004062 mActivityDisplay.addChild(stack, position, true /*fromDc*/);
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07004063 }
4064
4065 // The reparenting case is handled in WindowContainer.
4066 if (!stack.mReparenting) {
4067 setLayoutNeeded();
4068 stack.onDisplayChanged(DisplayContent.this);
4069 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004070 }
4071
Wale Ogunwale61911492017-10-11 08:50:50 -07004072 @Override
Louis Changdc077272019-11-12 16:52:56 +08004073 protected void removeChild(ActivityStack stack) {
Wale Ogunwale61911492017-10-11 08:50:50 -07004074 super.removeChild(stack);
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07004075 if (mActivityDisplay != null) {
Louis Changdc077272019-11-12 16:52:56 +08004076 mActivityDisplay.onChildRemoved(stack);
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07004077 }
Wale Ogunwale61911492017-10-11 08:50:50 -07004078 removeStackReferenceIfNeeded(stack);
4079 }
Bryce Lee00d586d2017-07-28 20:48:43 -07004080
4081 @Override
4082 boolean isOnTop() {
4083 // Considered always on top
4084 return true;
4085 }
4086
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004087 @Override
Louis Changdc077272019-11-12 16:52:56 +08004088 void positionChildAt(int position, ActivityStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07004089 if (child.getWindowConfiguration().isAlwaysOnTop()
4090 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08004091 // This stack is always-on-top, override the default behavior.
4092 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
4093
4094 // Moving to its current position, as we must call super but we don't want to
4095 // perform any meaningful action.
4096 final int currentPosition = mChildren.indexOf(child);
4097 super.positionChildAt(currentPosition, child, false /* includingParents */);
4098 return;
4099 }
4100
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004101 final int targetPosition = findPositionForStack(position, child, false /* adding */);
4102 super.positionChildAt(targetPosition, child, includingParents);
4103
Riddle Hsu2f6e1742018-08-23 22:44:36 +08004104 if (includingParents) {
4105 // We still want to move the display of this stack container to top because even the
4106 // target position is adjusted to non-top, the intention of the condition is to have
4107 // higher z-order to gain focus (e.g. moving a task of a fullscreen stack to front
4108 // in a non-top display which is using picture-in-picture mode).
4109 final int topChildPosition = getChildCount() - 1;
4110 if (targetPosition < topChildPosition && position >= topChildPosition) {
4111 getParent().positionChildAt(POSITION_TOP, this /* child */,
4112 true /* includingParents */);
4113 }
4114 }
4115
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004116 setLayoutNeeded();
4117 }
4118
4119 /**
4120 * When stack is added or repositioned, find a proper position for it.
4121 * This will make sure that pinned stack always stays on top.
4122 * @param requestedPosition Position requested by caller.
4123 * @param stack Stack to be added or positioned.
4124 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
4125 * @return The proper position for the stack.
4126 */
Louis Changdc077272019-11-12 16:52:56 +08004127 private int findPositionForStack(int requestedPosition, ActivityStack stack,
4128 boolean adding) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09004129 if (stack.inPinnedWindowingMode()) {
Kazuki Takisef85197b2018-06-18 18:18:36 +09004130 return POSITION_TOP;
Kazuki Takise148d00a2018-05-31 15:32:19 +09004131 }
4132
Kazuki Takisef85197b2018-06-18 18:18:36 +09004133 final int topChildPosition = mChildren.size() - 1;
4134 int belowAlwaysOnTopPosition = POSITION_BOTTOM;
4135 for (int i = topChildPosition; i >= 0; --i) {
4136 if (getStacks().get(i) != stack && !getStacks().get(i).isAlwaysOnTop()) {
4137 belowAlwaysOnTopPosition = i;
4138 break;
4139 }
4140 }
4141
4142 // The max possible position we can insert the stack at.
4143 int maxPosition = POSITION_TOP;
4144 // The min possible position we can insert the stack at.
4145 int minPosition = POSITION_BOTTOM;
4146
4147 if (stack.isAlwaysOnTop()) {
4148 if (hasPinnedStack()) {
4149 // Always-on-top stacks go below the pinned stack.
4150 maxPosition = getStacks().indexOf(mPinnedStack) - 1;
4151 }
4152 // Always-on-top stacks need to be above all other stacks.
4153 minPosition = belowAlwaysOnTopPosition !=
4154 POSITION_BOTTOM ? belowAlwaysOnTopPosition : topChildPosition;
4155 } else {
4156 // Other stacks need to be below the always-on-top stacks.
4157 maxPosition = belowAlwaysOnTopPosition !=
Arthur Hung928d8dc2018-07-18 15:38:05 +08004158 POSITION_BOTTOM ? belowAlwaysOnTopPosition : 0;
Kazuki Takisef85197b2018-06-18 18:18:36 +09004159 }
4160
4161 int targetPosition = requestedPosition;
4162 targetPosition = Math.min(targetPosition, maxPosition);
4163 targetPosition = Math.max(targetPosition, minPosition);
4164
Wale Ogunwalebebd8cd2019-10-28 15:53:31 -07004165 // Cap the requested position to something reasonable for the previous position check
4166 // below.
4167 if (requestedPosition == POSITION_TOP) {
4168 requestedPosition = mChildren.size();
4169 } else if (requestedPosition == POSITION_BOTTOM) {
4170 requestedPosition = 0;
4171 }
4172
Kazuki Takisef85197b2018-06-18 18:18:36 +09004173 int prevPosition = getStacks().indexOf(stack);
4174 // The positions we calculated above (maxPosition, minPosition) do not take into
4175 // consideration the following edge cases.
4176 // 1) We need to adjust the position depending on the value "adding".
4177 // 2) When we are moving a stack to another position, we also need to adjust the
4178 // position depending on whether the stack is moving to a higher or lower position.
4179 if ((targetPosition != requestedPosition) &&
4180 (adding || targetPosition < prevPosition)) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09004181 targetPosition++;
4182 }
4183
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004184 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08004185 }
4186
4187 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004188 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
4189 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004190 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004191 if (super.forAllWindows(callback, traverseTopToBottom)) {
4192 return true;
4193 }
4194 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4195 return true;
4196 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004197 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004198 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4199 return true;
4200 }
4201 if (super.forAllWindows(callback, traverseTopToBottom)) {
4202 return true;
4203 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004204 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004205 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004206 }
4207
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004208 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004209 boolean traverseTopToBottom) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07004210 // For legacy reasons we process the TaskStack.mExitingActivities first here before the
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004211 // app tokens.
4212 // TODO: Investigate if we need to continue to do this or if we can just process them
4213 // in-order.
4214 if (traverseTopToBottom) {
4215 for (int i = mChildren.size() - 1; i >= 0; --i) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07004216 final List<ActivityRecord> activities = mChildren.get(i).mExitingActivities;
4217 for (int j = activities.size() - 1; j >= 0; --j) {
4218 if (activities.get(j).forAllWindowsUnchecked(callback,
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004219 traverseTopToBottom)) {
4220 return true;
4221 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004222 }
4223 }
4224 } else {
4225 final int count = mChildren.size();
4226 for (int i = 0; i < count; ++i) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07004227 final List<ActivityRecord> activities = mChildren.get(i).mExitingActivities;
4228 final int appTokensCount = activities.size();
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004229 for (int j = 0; j < appTokensCount; j++) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07004230 if (activities.get(j).forAllWindowsUnchecked(callback,
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004231 traverseTopToBottom)) {
4232 return true;
4233 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004234 }
4235 }
4236 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004237 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004238 }
4239
Wale Ogunwale1666e312016-12-16 11:27:18 -08004240 void setExitingTokensHasVisible(boolean hasVisible) {
4241 for (int i = mChildren.size() - 1; i >= 0; --i) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07004242 final ArrayList<ActivityRecord> activities = mChildren.get(i).mExitingActivities;
4243 for (int j = activities.size() - 1; j >= 0; --j) {
4244 activities.get(j).hasVisible = hasVisible;
Wale Ogunwale1666e312016-12-16 11:27:18 -08004245 }
4246 }
4247 }
4248
4249 void removeExistingAppTokensIfPossible() {
4250 for (int i = mChildren.size() - 1; i >= 0; --i) {
Garfield Tane8d84ab2019-10-11 09:49:40 -07004251 final ArrayList<ActivityRecord> activities = mChildren.get(i).mExitingActivities;
4252 for (int j = activities.size() - 1; j >= 0; --j) {
4253 final ActivityRecord activity = activities.get(j);
4254 if (!activity.hasVisible && !mClosingApps.contains(activity)
4255 && (!activity.mIsExiting || activity.isEmpty())) {
4256 // Make sure there is no animation running on this activity, so any windows
Wale Ogunwale1666e312016-12-16 11:27:18 -08004257 // associated with it will be removed as soon as their animations are
4258 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02004259 cancelAnimation();
Adrian Roosb125e0b2019-10-02 14:55:14 +02004260 ProtoLog.v(WM_DEBUG_ADD_REMOVE,
Garfield Tane8d84ab2019-10-11 09:49:40 -07004261 "performLayout: Activity exiting now removed %s", activity);
4262 activity.removeIfPossible();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004263 }
4264 }
4265 }
4266 }
4267
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004268 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004269 int getOrientation() {
Garfield Tan98eead32019-11-18 13:24:53 -08004270 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004271 // Apps and their containers are not allowed to specify an orientation while the
Garfield Tan98eead32019-11-18 13:24:53 -08004272 // docked stack is visible...except for the home stack if the docked stack is
4273 // minimized and it actually set something and the bounds is different from the
4274 // display.
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004275 if (mHomeStack != null && mHomeStack.isVisible()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004276 && mDividerControllerLocked.isMinimizedDock()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004277 && !(mDividerControllerLocked.isHomeStackResizable()
4278 && mHomeStack.matchParentBounds())) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004279 final int orientation = mHomeStack.getOrientation();
4280 if (orientation != SCREEN_ORIENTATION_UNSET) {
4281 return orientation;
4282 }
4283 }
4284 return SCREEN_ORIENTATION_UNSPECIFIED;
4285 }
4286
4287 final int orientation = super.getOrientation();
4288 if (orientation != SCREEN_ORIENTATION_UNSET
4289 && orientation != SCREEN_ORIENTATION_BEHIND) {
Adrian Roosb125e0b2019-10-02 14:55:14 +02004290 ProtoLog.v(WM_DEBUG_ORIENTATION,
4291 "App is requesting an orientation, return %d for display id=%d",
4292 orientation, mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004293 return orientation;
4294 }
4295
Adrian Roosb125e0b2019-10-02 14:55:14 +02004296 ProtoLog.v(WM_DEBUG_ORIENTATION,
4297 "No app is requesting an orientation, return %d for display id=%d",
4298 getLastOrientation(), mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004299 // The next app has not been requested to be visible, so we keep the current orientation
4300 // to prevent freezing/unfreezing the display too early.
Riddle Hsuccf09402019-08-13 00:33:06 +08004301 return getLastOrientation();
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004302 }
Robert Carrb1579c82017-09-05 14:54:47 -07004303
4304 @Override
4305 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004306 assignStackOrdering(t);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004307
Robert Carr2f8aa392018-01-31 14:46:51 -08004308 for (int i = 0; i < mChildren.size(); i++) {
Louis Changdc077272019-11-12 16:52:56 +08004309 final ActivityStack s = mChildren.get(i);
Robert Carr2f8aa392018-01-31 14:46:51 -08004310 s.assignChildLayers(t);
4311 }
4312 }
4313
4314 void assignStackOrdering(SurfaceControl.Transaction t) {
Issei Suzuki25a9e2b2019-08-14 16:46:26 +02004315 if (getParent() == null) {
4316 return;
4317 }
4318 mTmpAlwaysOnTopStacks.clear();
4319 mTmpHomeStacks.clear();
4320 mTmpNormalStacks.clear();
4321 for (int i = 0; i < mChildren.size(); ++i) {
Louis Changdc077272019-11-12 16:52:56 +08004322 final ActivityStack s = mChildren.get(i);
Issei Suzuki25a9e2b2019-08-14 16:46:26 +02004323 if (s.isAlwaysOnTop()) {
4324 mTmpAlwaysOnTopStacks.add(s);
4325 } else if (s.isActivityTypeHome()) {
4326 mTmpHomeStacks.add(s);
4327 } else {
4328 mTmpNormalStacks.add(s);
4329 }
4330 }
Robert Carrf7a7ca82017-12-06 13:17:07 -08004331
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004332 int layer = 0;
Issei Suzuki25a9e2b2019-08-14 16:46:26 +02004333 // Place home stacks to the bottom.
4334 for (int i = 0; i < mTmpHomeStacks.size(); i++) {
4335 mTmpHomeStacks.get(i).assignLayer(t, layer++);
4336 }
4337 // The home animation layer is between the home stacks and the normal stacks.
4338 final int layerForHomeAnimationLayer = layer++;
4339 int layerForSplitScreenDividerAnchor = layer++;
4340 int layerForAnimationLayer = layer++;
4341 for (int i = 0; i < mTmpNormalStacks.size(); i++) {
Louis Changdc077272019-11-12 16:52:56 +08004342 final ActivityStack s = mTmpNormalStacks.get(i);
Issei Suzuki25a9e2b2019-08-14 16:46:26 +02004343 s.assignLayer(t, layer++);
4344 if (s.inSplitScreenWindowingMode()) {
4345 // The split screen divider anchor is located above the split screen window.
4346 layerForSplitScreenDividerAnchor = layer++;
4347 }
lumark9bca6b42019-10-17 18:35:22 +08004348 if (s.isTaskAnimating() || s.isAppTransitioning()) {
Issei Suzuki25a9e2b2019-08-14 16:46:26 +02004349 // The animation layer is located above the highest animating stack and no
4350 // higher.
4351 layerForAnimationLayer = layer++;
4352 }
4353 }
4354 // The boosted animation layer is between the normal stacks and the always on top
4355 // stacks.
4356 final int layerForBoostedAnimationLayer = layer++;
4357 for (int i = 0; i < mTmpAlwaysOnTopStacks.size(); i++) {
4358 mTmpAlwaysOnTopStacks.get(i).assignLayer(t, layer++);
4359 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004360
Issei Suzuki25a9e2b2019-08-14 16:46:26 +02004361 t.setLayer(mHomeAppAnimationLayer, layerForHomeAnimationLayer);
4362 t.setLayer(mAppAnimationLayer, layerForAnimationLayer);
4363 t.setLayer(mSplitScreenDividerAnchor, layerForSplitScreenDividerAnchor);
4364 t.setLayer(mBoostedAppAnimationLayer, layerForBoostedAnimationLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07004365 }
4366
4367 @Override
Jorim Jaggi391790622018-04-18 15:30:44 +02004368 SurfaceControl getAppAnimationLayer(@AnimationLayer int animationLayer) {
4369 switch (animationLayer) {
4370 case ANIMATION_LAYER_BOOSTED:
4371 return mBoostedAppAnimationLayer;
4372 case ANIMATION_LAYER_HOME:
4373 return mHomeAppAnimationLayer;
4374 case ANIMATION_LAYER_STANDARD:
4375 default:
4376 return mAppAnimationLayer;
4377 }
chaviw23ee71c2017-12-18 11:29:41 -08004378 }
4379
Robert Carrf7a7ca82017-12-06 13:17:07 -08004380 SurfaceControl getSplitScreenDividerAnchor() {
4381 return mSplitScreenDividerAnchor;
4382 }
4383
chaviw23ee71c2017-12-18 11:29:41 -08004384 @Override
Wale Ogunwalec17418e2019-10-13 23:00:40 +02004385 void onParentChanged(ConfigurationContainer newParent, ConfigurationContainer oldParent) {
Robert Carrb1579c82017-09-05 14:54:47 -07004386 if (getParent() != null) {
Wale Ogunwalec17418e2019-10-13 23:00:40 +02004387 super.onParentChanged(newParent, oldParent, () -> {
Issei Suzuki25a9e2b2019-08-14 16:46:26 +02004388 mAppAnimationLayer = makeChildSurface(null)
4389 .setName("animationLayer")
4390 .build();
4391 mBoostedAppAnimationLayer = makeChildSurface(null)
4392 .setName("boostedAnimationLayer")
4393 .build();
4394 mHomeAppAnimationLayer = makeChildSurface(null)
4395 .setName("homeAnimationLayer")
4396 .build();
4397 mSplitScreenDividerAnchor = makeChildSurface(null)
4398 .setName("splitScreenDividerAnchor")
4399 .build();
4400 getPendingTransaction()
4401 .show(mAppAnimationLayer)
4402 .show(mBoostedAppAnimationLayer)
4403 .show(mHomeAppAnimationLayer)
4404 .show(mSplitScreenDividerAnchor);
4405 });
Robert Carrb1579c82017-09-05 14:54:47 -07004406 } else {
Wale Ogunwalec17418e2019-10-13 23:00:40 +02004407 super.onParentChanged(newParent, oldParent);
Vishnu Nair33197392019-08-30 10:29:37 -07004408 mWmService.mTransactionFactory.get()
chaviw9f6171e2019-06-07 16:33:50 -07004409 .remove(mAppAnimationLayer)
4410 .remove(mBoostedAppAnimationLayer)
4411 .remove(mHomeAppAnimationLayer)
4412 .remove(mSplitScreenDividerAnchor)
4413 .apply();
chaviw23ee71c2017-12-18 11:29:41 -08004414 mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02004415 mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02004416 mHomeAppAnimationLayer = null;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004417 mSplitScreenDividerAnchor = null;
Robert Carrb1579c82017-09-05 14:54:47 -07004418 }
4419 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004420 }
4421
Robert Carree4d4b92017-11-22 12:21:46 -08004422 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004423 AboveAppWindowContainers(String name, WindowManagerService service) {
4424 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08004425 }
4426
Robert Carrb9506032018-02-13 13:54:00 -08004427 @Override
Vishnu Nair83537a72018-07-19 21:27:48 -07004428 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
4429 final SurfaceControl.Builder builder = super.makeChildSurface(child);
4430 if (child instanceof WindowToken && ((WindowToken) child).mRoundedCornerOverlay) {
4431 // To draw above the ColorFade layer during the screen off transition, the
4432 // rounded corner overlays need to be at the root of the surface hierarchy.
4433 // TODO: move the ColorLayer into the display overlay layer such that this is not
4434 // necessary anymore.
4435 builder.setParent(null);
4436 }
4437 return builder;
4438 }
4439
4440 @Override
Robert Carrb9506032018-02-13 13:54:00 -08004441 void assignChildLayers(SurfaceControl.Transaction t) {
4442 assignChildLayers(t, null /* imeContainer */);
4443 }
4444
Robert Carree4d4b92017-11-22 12:21:46 -08004445 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
4446 boolean needAssignIme = imeContainer != null
4447 && imeContainer.getSurfaceControl() != null;
4448 for (int j = 0; j < mChildren.size(); ++j) {
4449 final WindowToken wt = mChildren.get(j);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004450
4451 // See {@link mSplitScreenDividerAnchor}
4452 if (wt.windowType == TYPE_DOCK_DIVIDER) {
4453 wt.assignRelativeLayer(t, mTaskStackContainers.getSplitScreenDividerAnchor(), 1);
4454 continue;
4455 }
Vishnu Nair83537a72018-07-19 21:27:48 -07004456 if (wt.mRoundedCornerOverlay) {
4457 wt.assignLayer(t, WindowManagerPolicy.COLOR_FADE_LAYER + 1);
4458 continue;
4459 }
Robert Carree4d4b92017-11-22 12:21:46 -08004460 wt.assignLayer(t, j);
4461 wt.assignChildLayers(t);
4462
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004463 int layer = mWmService.mPolicy.getWindowLayerFromTypeLw(
Robert Carree4d4b92017-11-22 12:21:46 -08004464 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08004465
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004466 if (needAssignIme && layer >= mWmService.mPolicy.getWindowLayerFromTypeLw(
Robert Carrb8e78fa2017-11-28 13:07:58 -08004467 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004468 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08004469 needAssignIme = false;
4470 }
4471 }
4472 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004473 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08004474 }
4475 }
4476 }
4477
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004478 /**
4479 * Window container class that contains all containers on this display that are not related to
4480 * Apps. E.g. status bar.
4481 */
Robert Carree4d4b92017-11-22 12:21:46 -08004482 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07004483 /**
4484 * Compares two child window tokens returns -1 if the first is lesser than the second in
4485 * terms of z-order and 1 otherwise.
4486 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004487 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07004488 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004489 mWmService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004490 token1.mOwnerCanManageAppTokens)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004491 < mWmService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004492 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004493
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004494 private final Predicate<WindowState> mGetOrientingWindow = w -> {
chaviw15ad49f2019-04-24 15:05:39 -07004495 if (!w.isVisibleLw() || !w.mLegacyPolicyVisibilityAfterAnim) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004496 return false;
4497 }
4498 final int req = w.mAttrs.screenOrientation;
4499 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
4500 || req == SCREEN_ORIENTATION_UNSET) {
4501 return false;
4502 }
4503 return true;
4504 };
4505
Wale Ogunwale3a931692016-11-02 16:49:48 -07004506 private final String mName;
chaviwf29223e2018-01-31 13:23:51 -08004507 private final Dimmer mDimmer = new Dimmer(this);
4508 private final Rect mTmpDimBoundsRect = new Rect();
4509
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004510 NonAppWindowContainers(String name, WindowManagerService service) {
4511 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07004512 mName = name;
4513 }
4514
Wale Ogunwaledfbeed72019-11-20 08:57:39 -08004515 @Override
4516 boolean hasActivity() {
4517 // I am a non-app-window-container :P
4518 return false;
4519 }
4520
Wale Ogunwale3a931692016-11-02 16:49:48 -07004521 void addChild(WindowToken token) {
4522 addChild(token, mWindowComparator);
4523 }
4524
4525 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004526 int getOrientation() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004527 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004528 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004529 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004530
4531 if (win != null) {
4532 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004533 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004534 mLastKeyguardForcedOrientation = req;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004535 if (mWmService.mKeyguardGoingAway) {
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07004536 // Keyguard can't affect the orientation if it is going away...
4537 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
4538 return SCREEN_ORIENTATION_UNSET;
4539 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004540 }
Adrian Roosb125e0b2019-10-02 14:55:14 +02004541 ProtoLog.v(WM_DEBUG_ORIENTATION,
4542 "%s forcing orientation to %d for display id=%d", win, req,
4543 mDisplayId);
Andrii Kulian8ee72852017-03-10 10:36:45 -08004544 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004545 }
4546
Andrii Kulian8ee72852017-03-10 10:36:45 -08004547 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004548
Jorim Jaggi1c530592018-04-06 15:11:47 +02004549 // Only allow force setting the orientation when all unknown visibilities have been
4550 // resolved, as otherwise we just may be starting another occluding activity.
4551 final boolean isUnoccluding =
lumark588a3e82018-07-20 18:53:54 +08004552 mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
4553 && mUnknownAppVisibilityController.allResolved();
Jorim Jaggi1c530592018-04-06 15:11:47 +02004554 if (policy.isKeyguardShowingAndNotOccluded() || isUnoccluding) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004555 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004556 }
4557
4558 return SCREEN_ORIENTATION_UNSET;
4559 }
4560
4561 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07004562 String getName() {
4563 return mName;
4564 }
chaviwf29223e2018-01-31 13:23:51 -08004565
4566 @Override
4567 Dimmer getDimmer() {
4568 return mDimmer;
4569 }
4570
4571 @Override
4572 void prepareSurfaces() {
4573 mDimmer.resetDimStates();
4574 super.prepareSurfaces();
4575 getBounds(mTmpDimBoundsRect);
4576
4577 if (mDimmer.updateDims(getPendingTransaction(), mTmpDimBoundsRect)) {
4578 scheduleAnimation();
4579 }
4580 }
Robert Carr3b716242016-08-16 16:02:21 -07004581 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01004582
Robert Carrb1579c82017-09-05 14:54:47 -07004583 SurfaceControl.Builder makeSurface(SurfaceSession s) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004584 return mWmService.makeSurfaceBuilder(s)
Robert Carrb1579c82017-09-05 14:54:47 -07004585 .setParent(mWindowingLayer);
4586 }
4587
4588 @Override
4589 SurfaceSession getSession() {
4590 return mSession;
4591 }
4592
4593 @Override
4594 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07004595 SurfaceSession s = child != null ? child.getSession() : getSession();
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +00004596 final SurfaceControl.Builder b = mWmService.makeSurfaceBuilder(s).setContainerLayer();
Robert Carrf59b8dd2017-10-02 18:58:36 -07004597 if (child == null) {
4598 return b;
4599 }
4600
Robert Carree4d4b92017-11-22 12:21:46 -08004601 return b.setName(child.getName())
4602 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07004603 }
4604
4605 /**
4606 * The makeSurface variants are for use by the window-container
4607 * hierarchy. makeOverlay here is a function for various non windowing
4608 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
4609 * and other potpourii.
4610 */
4611 SurfaceControl.Builder makeOverlay() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004612 return mWmService.makeSurfaceBuilder(mSession)
Robert Carrb1579c82017-09-05 14:54:47 -07004613 .setParent(mOverlayLayer);
4614 }
4615
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004616 /**
4617 * Reparents the given surface to mOverlayLayer.
4618 */
4619 void reparentToOverlay(Transaction transaction, SurfaceControl surface) {
Robert Carr10584fa2019-01-14 15:55:19 -08004620 transaction.reparent(surface, mOverlayLayer);
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004621 }
4622
Robert Carrb1579c82017-09-05 14:54:47 -07004623 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carr24be9ab2018-04-30 17:54:53 -07004624 if (spec.scale != 1.0) {
4625 mMagnificationSpec = spec;
4626 } else {
4627 mMagnificationSpec = null;
4628 }
Jackal Guo818c3f12019-03-08 18:00:39 +08004629 // Re-parent IME's SurfaceControl when MagnificationSpec changed.
4630 updateImeParent();
Robert Carr24be9ab2018-04-30 17:54:53 -07004631
Robert Carrf59b8dd2017-10-02 18:58:36 -07004632 applyMagnificationSpec(getPendingTransaction(), spec);
4633 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07004634 }
4635
Robert Carr24be9ab2018-04-30 17:54:53 -07004636 void reapplyMagnificationSpec() {
4637 if (mMagnificationSpec != null) {
4638 applyMagnificationSpec(getPendingTransaction(), mMagnificationSpec);
4639 }
4640 }
4641
Robert Carrb1579c82017-09-05 14:54:47 -07004642 @Override
Wale Ogunwalec17418e2019-10-13 23:00:40 +02004643 void onParentChanged(ConfigurationContainer newParent, ConfigurationContainer oldParent) {
Robert Carrb1579c82017-09-05 14:54:47 -07004644 // Since we are the top of the SurfaceControl hierarchy here
4645 // we create the root surfaces explicitly rather than chaining
Riddle Hsu3a4bb612019-01-31 00:02:22 +08004646 // up as the default implementation in onParentChanged does. So we
Robert Carrb1579c82017-09-05 14:54:47 -07004647 // explicitly do NOT call super here.
4648 }
4649
4650 @Override
4651 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07004652
4653 // These are layers as children of "mWindowingLayer"
4654 mBelowAppWindowsContainers.assignLayer(t, 0);
4655 mTaskStackContainers.assignLayer(t, 1);
4656 mAboveAppWindowsContainers.assignLayer(t, 2);
4657
lumarkff0ab692018-11-05 20:32:30 +08004658 final WindowState imeTarget = mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08004659 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08004660
Robert Carree4d4b92017-11-22 12:21:46 -08004661 // In the case where we have an IME target that is not in split-screen
4662 // mode IME assignment is easy. We just need the IME to go directly above
4663 // the target. This way children of the target will naturally go above the IME
4664 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08004665 //
Robert Carree4d4b92017-11-22 12:21:46 -08004666 // In the case of split-screen windowing mode, we need to elevate the IME above the
4667 // docked divider while keeping the app itself below the docked divider, so instead
4668 // we use relative layering of the IME targets child windows, and place the
4669 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08004670 //
Robert Carr234b6332018-03-20 13:38:16 -07004671 // In the case the IME target is animating, the animation Z order may be different
4672 // than the WindowContainer Z order, so it's difficult to be sure we have the correct
4673 // IME target. In this case we just layer the IME over all transitions by placing it in the
4674 // above applications layer.
4675 //
Robert Carree4d4b92017-11-22 12:21:46 -08004676 // In the case where we have no IME target we assign it where it's base layer would
4677 // place it in the AboveAppWindowContainers.
Tarandeep Singh69ab1042019-07-30 13:30:03 -07004678 //
4679 // Keep IME window in mAboveAppWindowsContainers as long as app's starting window exists
4680 // so it get's layered above the starting window.
4681 if (imeTarget != null
Garfield Tane8d84ab2019-10-11 09:49:40 -07004682 && !(imeTarget.mActivityRecord != null && imeTarget.mActivityRecord.hasStartingWindow())
lumark9bca6b42019-10-17 18:35:22 +08004683 && (!(imeTarget.inSplitScreenWindowingMode()
4684 || imeTarget.mToken.isAppTransitioning())
Tarandeep Singh69ab1042019-07-30 13:30:03 -07004685 && (imeTarget.getSurfaceControl() != null))) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004686 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07004687 // TODO: We need to use an extra level on the app surface to ensure
4688 // this is always above SurfaceView but always below attached window.
4689 1);
Robert Carree4d4b92017-11-22 12:21:46 -08004690 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07004691 }
4692
4693 // Above we have assigned layers to our children, now we ask them to assign
4694 // layers to their children.
4695 mBelowAppWindowsContainers.assignChildLayers(t);
4696 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08004697 mAboveAppWindowsContainers.assignChildLayers(t,
4698 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07004699 mImeWindowsContainers.assignChildLayers(t);
4700 }
4701
4702 /**
4703 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
4704 * that the IME target is one of the docked applications. We'd like the docked divider to be
4705 * above both of the applications, and we'd like the IME to be above the docked divider.
4706 * However we need child windows of the applications to be above the IME (Text drag handles).
4707 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
4708 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
Tiger Huang7c610aa2018-10-27 00:01:01 +08004709 * with {@link WindowState#assignLayer}
Robert Carrb1579c82017-09-05 14:54:47 -07004710 */
4711 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
Robert Carrbb5de662018-04-26 13:29:51 -07004712 child.assignRelativeLayer(t, mImeWindowsContainers.getSurfaceControl(), 1);
Robert Carrb1579c82017-09-05 14:54:47 -07004713 }
4714
4715 @Override
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004716 void prepareSurfaces() {
Jorim Jaggi4981f152019-03-26 18:58:45 +01004717 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "prepareSurfaces");
4718 try {
Tiger Huanged6794e2019-05-07 20:07:59 +08004719 final Transaction transaction = getPendingTransaction();
Jorim Jaggi4981f152019-03-26 18:58:45 +01004720 super.prepareSurfaces();
Tiger Huanged6794e2019-05-07 20:07:59 +08004721
4722 // TODO: Once we totally eliminate global transaction we will pass transaction in here
4723 // rather than merging to global.
4724 SurfaceControl.mergeToGlobalTransaction(transaction);
Jorim Jaggi4981f152019-03-26 18:58:45 +01004725 } finally {
4726 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
4727 }
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004728 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004729
Jorim Jaggibe418292018-03-26 16:14:12 +02004730 void assignStackOrdering() {
4731 mTaskStackContainers.assignStackOrdering(getPendingTransaction());
Robert Carr2f8aa392018-01-31 14:46:51 -08004732 }
Chavi Weingarten3a748552018-05-14 17:32:42 +00004733
4734 /**
4735 * Increment the deferral count to determine whether to update the IME target.
4736 */
4737 void deferUpdateImeTarget() {
4738 mDeferUpdateImeTargetCount++;
4739 }
4740
4741 /**
4742 * Decrement the deferral count to determine whether to update the IME target. If the count
4743 * reaches 0, a new ime target will get computed.
4744 */
4745 void continueUpdateImeTarget() {
4746 if (mDeferUpdateImeTargetCount == 0) {
4747 return;
4748 }
4749
4750 mDeferUpdateImeTargetCount--;
4751 if (mDeferUpdateImeTargetCount == 0) {
4752 computeImeTarget(true /* updateImeTarget */);
4753 }
4754 }
4755
4756 /**
4757 * @return Whether a new IME target should be computed.
4758 */
4759 private boolean canUpdateImeTarget() {
4760 return mDeferUpdateImeTargetCount == 0;
4761 }
Arthur Hung95b38a92018-07-20 18:56:12 +08004762
4763 InputMonitor getInputMonitor() {
4764 return mInputMonitor;
4765 }
Jorim Jaggif1292892018-09-10 11:58:13 +02004766
4767 /**
4768 * @return Cached value whether we told display manager that we have content.
4769 */
4770 boolean getLastHasContent() {
4771 return mLastHasContent;
4772 }
Arthur Hungbe5ce212018-09-13 18:41:56 +08004773
4774 void registerPointerEventListener(@NonNull PointerEventListener listener) {
Riddle Hsu2588ab02019-02-25 14:23:56 +08004775 mPointerEventDispatcher.registerInputEventListener(listener);
Arthur Hungbe5ce212018-09-13 18:41:56 +08004776 }
4777
4778 void unregisterPointerEventListener(@NonNull PointerEventListener listener) {
Riddle Hsu2588ab02019-02-25 14:23:56 +08004779 mPointerEventDispatcher.unregisterInputEventListener(listener);
Arthur Hungbe5ce212018-09-13 18:41:56 +08004780 }
lumark588a3e82018-07-20 18:53:54 +08004781
4782 void prepareAppTransition(@WindowManager.TransitionType int transit,
Wale Ogunwale3a256e62018-12-06 14:41:18 -08004783 boolean alwaysKeepCurrent) {
4784 prepareAppTransition(transit, alwaysKeepCurrent, 0 /* flags */, false /* forceOverride */);
4785 }
4786
4787 void prepareAppTransition(@WindowManager.TransitionType int transit,
lumark588a3e82018-07-20 18:53:54 +08004788 boolean alwaysKeepCurrent, @WindowManager.TransitionFlags int flags,
4789 boolean forceOverride) {
4790 final boolean prepared = mAppTransition.prepareAppTransitionLocked(
4791 transit, alwaysKeepCurrent, flags, forceOverride);
4792 if (prepared && okToAnimate()) {
4793 mSkipAppTransitionAnimation = false;
4794 }
4795 }
4796
4797 void executeAppTransition() {
4798 if (mAppTransition.isTransitionSet()) {
Adrian Roosb125e0b2019-10-02 14:55:14 +02004799 ProtoLog.w(WM_DEBUG_APP_TRANSITIONS,
4800 "Execute app transition: %s, displayId: %d Callers=%s",
4801 mAppTransition, mDisplayId, Debug.getCallers(5));
lumark588a3e82018-07-20 18:53:54 +08004802 mAppTransition.setReady();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004803 mWmService.mWindowPlacerLocked.requestTraversal();
lumark588a3e82018-07-20 18:53:54 +08004804 }
4805 }
4806
4807 /**
4808 * Update pendingLayoutChanges after app transition has finished.
4809 */
4810 void handleAnimatingStoppedAndTransition() {
4811 int changes = 0;
4812
4813 mAppTransition.setIdle();
4814
4815 for (int i = mNoAnimationNotifyOnTransitionFinished.size() - 1; i >= 0; i--) {
4816 final IBinder token = mNoAnimationNotifyOnTransitionFinished.get(i);
4817 mAppTransition.notifyAppTransitionFinishedLocked(token);
4818 }
4819 mNoAnimationNotifyOnTransitionFinished.clear();
4820
4821 mWallpaperController.hideDeferredWallpapersIfNeeded();
4822
4823 onAppTransitionDone();
4824
4825 changes |= FINISH_LAYOUT_REDO_LAYOUT;
4826 if (DEBUG_WALLPAPER_LIGHT) {
4827 Slog.v(TAG_WM, "Wallpaper layer changed: assigning layers + relayout");
4828 }
4829 computeImeTarget(true /* updateImeTarget */);
wilsonshihc32538e2018-11-07 17:27:34 +08004830 mWallpaperMayChange = true;
lumark588a3e82018-07-20 18:53:54 +08004831 // Since the window list has been rebuilt, focus might have to be recomputed since the
4832 // actual order of windows might have changed again.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004833 mWmService.mFocusMayChange = true;
lumark588a3e82018-07-20 18:53:54 +08004834
4835 pendingLayoutChanges |= changes;
4836 }
Andrii Kulian15cfb422018-11-07 13:38:49 -08004837
Wale Ogunwale3a256e62018-12-06 14:41:18 -08004838 /** Check if pending app transition is for activity / task launch. */
4839 boolean isNextTransitionForward() {
4840 final int transit = mAppTransition.getAppTransition();
4841 return transit == TRANSIT_ACTIVITY_OPEN
4842 || transit == TRANSIT_TASK_OPEN
4843 || transit == TRANSIT_TASK_TO_FRONT;
4844 }
4845
Andrii Kulian15cfb422018-11-07 13:38:49 -08004846 /**
4847 * @see Display#FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS
4848 */
4849 boolean supportsSystemDecorations() {
Arthur Hung879a8ab2019-03-13 19:51:10 +08004850 return (mWmService.mDisplayWindowSettings.shouldShowSystemDecorsLocked(this)
Andrii Kuliandd989612019-02-21 12:13:28 -08004851 || (mDisplay.getFlags() & FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS) != 0
Chilun891aed22019-05-02 18:08:47 +08004852 || (mWmService.mForceDesktopModeOnExternalDisplays && !isUntrustedVirtualDisplay()))
Arthur Hung879a8ab2019-03-13 19:51:10 +08004853 // VR virtual display will be used to run and render 2D app within a VR experience.
4854 && mDisplayId != mWmService.mVr2dDisplayId;
Andrii Kulian15cfb422018-11-07 13:38:49 -08004855 }
chaviwff2e7d82018-11-02 11:11:27 -07004856
Tiger Huang04dc4cc2019-01-17 18:41:41 +08004857 /**
Chilun891aed22019-05-02 18:08:47 +08004858 * @return {@code true} if the display is non-system created virtual display.
4859 */
4860 boolean isUntrustedVirtualDisplay() {
4861 return mDisplay.getType() == Display.TYPE_VIRTUAL
4862 && mDisplay.getOwnerUid() != Process.SYSTEM_UID;
4863 }
4864
4865 /**
chaviwff2e7d82018-11-02 11:11:27 -07004866 * Re-parent the DisplayContent's top surfaces, {@link #mWindowingLayer} and
Tiger Huangd8ec9382019-04-18 14:35:09 -07004867 * {@link #mOverlayLayer} to the specified SurfaceControl.
chaviwff2e7d82018-11-02 11:11:27 -07004868 *
Tiger Huangd8ec9382019-04-18 14:35:09 -07004869 * @param win The window which owns the SurfaceControl. This indicates the z-order of the
4870 * windows of this display against the windows on the parent display.
Tiger Huang04dc4cc2019-01-17 18:41:41 +08004871 * @param sc The new SurfaceControl, where the DisplayContent's surfaces will be re-parented to.
chaviwff2e7d82018-11-02 11:11:27 -07004872 */
Tiger Huangd8ec9382019-04-18 14:35:09 -07004873 void reparentDisplayContent(WindowState win, SurfaceControl sc) {
4874 mParentWindow = win;
Jackal Guoc43a0a62019-04-23 09:15:14 +08004875 mParentWindow.addEmbeddedDisplayContent(this);
Tiger Huang04dc4cc2019-01-17 18:41:41 +08004876 mParentSurfaceControl = sc;
4877 if (mPortalWindowHandle == null) {
4878 mPortalWindowHandle = createPortalWindowHandle(sc.toString());
4879 }
Tiger Huanged6794e2019-05-07 20:07:59 +08004880 getPendingTransaction().setInputWindowInfo(sc, mPortalWindowHandle)
Vadim Caen1812c762019-08-12 11:38:22 +02004881 .reparent(mSurfaceControl, sc);
Tiger Huang04dc4cc2019-01-17 18:41:41 +08004882 }
4883
Tiger Huangd8ec9382019-04-18 14:35:09 -07004884 /**
4885 * Get the window which owns the surface that this DisplayContent is re-parented to.
4886 *
4887 * @return the parent window.
4888 */
4889 WindowState getParentWindow() {
4890 return mParentWindow;
4891 }
4892
4893 /**
4894 * Update the location of this display in the parent window. This enables windows in this
4895 * display to compute the global transformation matrix.
4896 *
4897 * @param win The parent window of this display.
4898 * @param x The x coordinate in the parent window.
4899 * @param y The y coordinate in the parent window.
4900 */
4901 void updateLocation(WindowState win, int x, int y) {
4902 if (mParentWindow != win) {
4903 throw new IllegalArgumentException(
4904 "The given window is not the parent window of this display.");
4905 }
Jackal Guoc43a0a62019-04-23 09:15:14 +08004906 if (!mLocationInParentWindow.equals(x, y)) {
4907 mLocationInParentWindow.set(x, y);
Tiger Huangd8ec9382019-04-18 14:35:09 -07004908 if (mWmService.mAccessibilityController != null) {
Jacky Kaof93252b2019-07-18 15:19:52 +08004909 mWmService.mAccessibilityController.onSomeWindowResizedOrMovedLocked(mDisplayId);
Tiger Huangd8ec9382019-04-18 14:35:09 -07004910 }
Jackal Guoc43a0a62019-04-23 09:15:14 +08004911 notifyLocationInParentDisplayChanged();
Tiger Huangd8ec9382019-04-18 14:35:09 -07004912 }
4913 }
4914
4915 Point getLocationInParentWindow() {
4916 return mLocationInParentWindow;
4917 }
4918
Jackal Guoc43a0a62019-04-23 09:15:14 +08004919 Point getLocationInParentDisplay() {
4920 final Point location = new Point();
4921 if (mParentWindow != null) {
4922 // LocationInParentWindow indicates the offset to (0,0) of window, but what we need is
4923 // the offset to (0,0) of display.
4924 DisplayContent dc = this;
4925 do {
4926 final WindowState displayParent = dc.getParentWindow();
4927 location.x += displayParent.getFrameLw().left
4928 + (dc.getLocationInParentWindow().x * displayParent.mGlobalScale + 0.5f);
4929 location.y += displayParent.getFrameLw().top
4930 + (dc.getLocationInParentWindow().y * displayParent.mGlobalScale + 0.5f);
4931 dc = displayParent.getDisplayContent();
4932 } while (dc != null && dc.getParentWindow() != null);
4933 }
4934 return location;
4935 }
4936
4937 void notifyLocationInParentDisplayChanged() {
4938 forAllWindows(w -> {
4939 w.updateLocationInParentDisplayIfNeeded();
4940 }, false /* traverseTopToBottom */);
4941 }
4942
Tarandeep Singha6f35612019-01-11 19:50:46 -08004943 @VisibleForTesting
4944 SurfaceControl getWindowingLayer() {
4945 return mWindowingLayer;
4946 }
4947
Vadim Caenf8474262019-08-12 17:52:01 +02004948 SurfaceControl getOverlayLayer() {
4949 return mOverlayLayer;
4950 }
4951
Tiger Huang04dc4cc2019-01-17 18:41:41 +08004952 /**
Adrian Roos4ffc8972019-02-07 20:45:11 +01004953 * Updates the display's system gesture exclusion.
4954 *
4955 * @return true, if the exclusion changed.
4956 */
4957 boolean updateSystemGestureExclusion() {
4958 if (mSystemGestureExclusionListeners.getRegisteredCallbackCount() == 0) {
4959 // No one's interested anyways.
4960 return false;
4961 }
4962
Adrian Roos5f2c9a12019-07-03 18:31:46 +02004963 final Region systemGestureExclusion = Region.obtain();
4964 mSystemGestureExclusionWasRestricted = calculateSystemGestureExclusion(
4965 systemGestureExclusion, mSystemGestureExclusionUnrestricted);
Adrian Roos4ffc8972019-02-07 20:45:11 +01004966 try {
4967 if (mSystemGestureExclusion.equals(systemGestureExclusion)) {
4968 return false;
4969 }
4970 mSystemGestureExclusion.set(systemGestureExclusion);
Adrian Roos5f2c9a12019-07-03 18:31:46 +02004971 final Region unrestrictedOrNull = mSystemGestureExclusionWasRestricted
4972 ? mSystemGestureExclusionUnrestricted : null;
Adrian Roos4ffc8972019-02-07 20:45:11 +01004973 for (int i = mSystemGestureExclusionListeners.beginBroadcast() - 1; i >= 0; --i) {
4974 try {
4975 mSystemGestureExclusionListeners.getBroadcastItem(i)
Adrian Roos5f2c9a12019-07-03 18:31:46 +02004976 .onSystemGestureExclusionChanged(mDisplayId, systemGestureExclusion,
4977 unrestrictedOrNull);
Adrian Roos4ffc8972019-02-07 20:45:11 +01004978 } catch (RemoteException e) {
4979 Slog.e(TAG, "Failed to notify SystemGestureExclusionListener", e);
4980 }
4981 }
4982 mSystemGestureExclusionListeners.finishBroadcast();
4983 return true;
4984 } finally {
4985 systemGestureExclusion.recycle();
4986 }
4987 }
4988
Adrian Roos5f2c9a12019-07-03 18:31:46 +02004989 /**
4990 * Calculates the system gesture exclusion.
4991 *
4992 * @param outExclusion will be set to the gesture exclusion region
4993 * @param outExclusionUnrestricted will be set to the gesture exclusion region without
4994 * any restrictions applied.
4995 * @return whether any restrictions were applied, i.e. outExclusion and outExclusionUnrestricted
4996 * differ.
4997 */
Adrian Roos4ffc8972019-02-07 20:45:11 +01004998 @VisibleForTesting
Adrian Roos5f2c9a12019-07-03 18:31:46 +02004999 boolean calculateSystemGestureExclusion(Region outExclusion, @Nullable
5000 Region outExclusionUnrestricted) {
5001 outExclusion.setEmpty();
5002 if (outExclusionUnrestricted != null) {
5003 outExclusionUnrestricted.setEmpty();
5004 }
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005005 final Region unhandled = Region.obtain();
5006 unhandled.set(0, 0, mDisplayFrames.mDisplayWidth, mDisplayFrames.mDisplayHeight);
5007
Tiger Huang332793b2019-10-29 23:21:27 +08005008 final Rect leftEdge = mInsetsStateController.getSourceProvider(ITYPE_LEFT_GESTURES)
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005009 .getSource().getFrame();
Tiger Huang332793b2019-10-29 23:21:27 +08005010 final Rect rightEdge = mInsetsStateController.getSourceProvider(ITYPE_RIGHT_GESTURES)
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005011 .getSource().getFrame();
5012
Adrian Roos4ffc8972019-02-07 20:45:11 +01005013 final Region touchableRegion = Region.obtain();
5014 final Region local = Region.obtain();
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005015 final int[] remainingLeftRight =
5016 {mSystemGestureExclusionLimit, mSystemGestureExclusionLimit};
Adrian Roos4ffc8972019-02-07 20:45:11 +01005017
Adrian Roosb1063792019-06-28 12:10:51 +02005018 // Traverse all windows top down to assemble the gesture exclusion rects.
Adrian Roos4ffc8972019-02-07 20:45:11 +01005019 // For each window, we only take the rects that fall within its touchable region.
5020 forAllWindows(w -> {
5021 if (w.cantReceiveTouchInput() || !w.isVisible()
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005022 || (w.getAttrs().flags & FLAG_NOT_TOUCHABLE) != 0
5023 || unhandled.isEmpty()) {
Adrian Roos4ffc8972019-02-07 20:45:11 +01005024 return;
5025 }
Adrian Roos4ffc8972019-02-07 20:45:11 +01005026
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005027 // Get the touchable region of the window, and intersect with where the screen is still
5028 // touchable, i.e. touchable regions on top are not covering it yet.
Adrian Roosb1063792019-06-28 12:10:51 +02005029 w.getEffectiveTouchableRegion(touchableRegion);
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005030 touchableRegion.op(unhandled, Op.INTERSECT);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005031
Adrian Roos019a52b2019-07-02 16:47:44 +02005032 if (w.isImplicitlyExcludingAllSystemGestures()) {
5033 local.set(touchableRegion);
5034 } else {
5035 rectListToRegion(w.getSystemGestureExclusion(), local);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005036
Adrian Roos019a52b2019-07-02 16:47:44 +02005037 // Transform to display coordinates
5038 local.scale(w.mGlobalScale);
5039 final Rect frame = w.getWindowFrames().mFrame;
5040 local.translate(frame.left, frame.top);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005041
Adrian Roos019a52b2019-07-02 16:47:44 +02005042 // A window can only exclude system gestures where it is actually touchable
5043 local.op(touchableRegion, Op.INTERSECT);
5044 }
Adrian Roos4ffc8972019-02-07 20:45:11 +01005045
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005046 // Apply restriction if necessary.
5047 if (needsGestureExclusionRestrictions(w, mLastDispatchedSystemUiVisibility)) {
5048
5049 // Processes the region along the left edge.
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005050 remainingLeftRight[0] = addToGlobalAndConsumeLimit(local, outExclusion, leftEdge,
5051 remainingLeftRight[0], w, EXCLUSION_LEFT);
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005052
5053 // Processes the region along the right edge.
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005054 remainingLeftRight[1] = addToGlobalAndConsumeLimit(local, outExclusion, rightEdge,
5055 remainingLeftRight[1], w, EXCLUSION_RIGHT);
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005056
5057 // Adds the middle (unrestricted area)
5058 final Region middle = Region.obtain(local);
5059 middle.op(leftEdge, Op.DIFFERENCE);
5060 middle.op(rightEdge, Op.DIFFERENCE);
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005061 outExclusion.op(middle, Op.UNION);
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005062 middle.recycle();
5063 } else {
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005064 boolean loggable = needsGestureExclusionRestrictions(w, 0 /* lastSysUiVis */);
5065 if (loggable) {
5066 addToGlobalAndConsumeLimit(local, outExclusion, leftEdge,
5067 Integer.MAX_VALUE, w, EXCLUSION_LEFT);
5068 addToGlobalAndConsumeLimit(local, outExclusion, rightEdge,
5069 Integer.MAX_VALUE, w, EXCLUSION_RIGHT);
5070 }
5071 outExclusion.op(local, Op.UNION);
5072 }
5073 if (outExclusionUnrestricted != null) {
5074 outExclusionUnrestricted.op(local, Op.UNION);
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005075 }
5076 unhandled.op(touchableRegion, Op.DIFFERENCE);
5077 }, true /* topToBottom */);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005078 local.recycle();
5079 touchableRegion.recycle();
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005080 unhandled.recycle();
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005081 return remainingLeftRight[0] < mSystemGestureExclusionLimit
5082 || remainingLeftRight[1] < mSystemGestureExclusionLimit;
Adrian Roos4ffc8972019-02-07 20:45:11 +01005083 }
5084
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005085 /**
5086 * @return Whether gesture exclusion area should be restricted from the window depending on the
5087 * current SystemUI visibility flags.
5088 */
5089 private static boolean needsGestureExclusionRestrictions(WindowState win, int sysUiVisibility) {
5090 final int type = win.mAttrs.type;
5091 final int stickyHideNavFlags =
5092 SYSTEM_UI_FLAG_HIDE_NAVIGATION | SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
5093 final boolean stickyHideNav =
5094 (sysUiVisibility & stickyHideNavFlags) == stickyHideNavFlags;
5095 return !stickyHideNav && type != TYPE_INPUT_METHOD && type != TYPE_STATUS_BAR
5096 && win.getActivityType() != ACTIVITY_TYPE_HOME;
5097 }
5098
5099 /**
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005100 * @return Whether gesture exclusion area should be logged for the given window
5101 */
5102 static boolean logsGestureExclusionRestrictions(WindowState win) {
Adrian Roos1c2e9a12019-08-20 18:23:47 +02005103 if (win.mWmService.mConstants.mSystemGestureExclusionLogDebounceTimeoutMillis <= 0) {
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005104 return false;
5105 }
5106 final WindowManager.LayoutParams attrs = win.getAttrs();
5107 final int type = attrs.type;
5108 return type != TYPE_WALLPAPER
5109 && type != TYPE_APPLICATION_STARTING
5110 && type != TYPE_NAVIGATION_BAR
5111 && (attrs.flags & FLAG_NOT_TOUCHABLE) == 0
5112 && needsGestureExclusionRestrictions(win, 0 /* sysUiVisibility */)
5113 && win.getDisplayContent().mDisplayPolicy.hasSideGestures();
5114 }
5115
5116 /**
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005117 * Adds a local gesture exclusion area to the global area while applying a limit per edge.
5118 *
5119 * @param local The gesture exclusion area to add.
5120 * @param global The destination.
5121 * @param edge Only processes the part in that region.
5122 * @param limit How much limit in pixels we have.
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005123 * @param win The WindowState that is being processed
5124 * @param side The side that is being processed, either {@link WindowState#EXCLUSION_LEFT} or
5125 * {@link WindowState#EXCLUSION_RIGHT}
5126 * @return How much of the limit is remaining.
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005127 */
5128 private static int addToGlobalAndConsumeLimit(Region local, Region global, Rect edge,
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005129 int limit, WindowState win, int side) {
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005130 final Region r = Region.obtain(local);
5131 r.op(edge, Op.INTERSECT);
5132
5133 final int[] remaining = {limit};
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005134 final int[] requestedExclusion = {0};
Adrian Roos2351d5f2019-07-03 15:35:07 +02005135 forEachRectReverse(r, rect -> {
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005136 if (remaining[0] <= 0) {
5137 return;
5138 }
5139 final int height = rect.height();
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005140 requestedExclusion[0] += height;
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005141 if (height > remaining[0]) {
Adrian Roos2351d5f2019-07-03 15:35:07 +02005142 rect.top = rect.bottom - remaining[0];
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005143 }
5144 remaining[0] -= height;
5145 global.op(rect, Op.UNION);
5146 });
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005147
5148 final int grantedExclusion = limit - remaining[0];
5149 win.setLastExclusionHeights(side, requestedExclusion[0], grantedExclusion);
5150
Adrian Roosbf3bc1b2019-06-18 16:13:53 +02005151 r.recycle();
5152 return remaining[0];
5153 }
5154
Adrian Roos4ffc8972019-02-07 20:45:11 +01005155 void registerSystemGestureExclusionListener(ISystemGestureExclusionListener listener) {
5156 mSystemGestureExclusionListeners.register(listener);
5157 final boolean changed;
5158 if (mSystemGestureExclusionListeners.getRegisteredCallbackCount() == 1) {
5159 changed = updateSystemGestureExclusion();
5160 } else {
5161 changed = false;
5162 }
5163
5164 if (!changed) {
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005165 final Region unrestrictedOrNull = mSystemGestureExclusionWasRestricted
5166 ? mSystemGestureExclusionUnrestricted : null;
Adrian Roos4ffc8972019-02-07 20:45:11 +01005167 // If updateSystemGestureExclusion changed the exclusion, it will already have
5168 // notified the listener. Otherwise, we'll do it here.
5169 try {
Adrian Roos5f2c9a12019-07-03 18:31:46 +02005170 listener.onSystemGestureExclusionChanged(mDisplayId, mSystemGestureExclusion,
5171 unrestrictedOrNull);
Adrian Roos4ffc8972019-02-07 20:45:11 +01005172 } catch (RemoteException e) {
5173 Slog.e(TAG, "Failed to notify SystemGestureExclusionListener during register", e);
5174 }
5175 }
5176 }
5177
5178 void unregisterSystemGestureExclusionListener(ISystemGestureExclusionListener listener) {
5179 mSystemGestureExclusionListeners.unregister(listener);
5180 }
5181
5182 /**
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005183 * Create a portal window handle for input. This window transports any touch to the display
5184 * indicated by {@link InputWindowHandle#portalToDisplayId} if the touch hits this window.
5185 *
5186 * @param name The name of the portal window handle.
5187 * @return the new portal window handle.
5188 */
5189 private InputWindowHandle createPortalWindowHandle(String name) {
5190 // Let surface flinger to set the display ID of this input window handle because we don't
5191 // know which display the parent surface control is on.
5192 final InputWindowHandle portalWindowHandle = new InputWindowHandle(
Vishnu Nair18782162019-10-08 14:57:16 -07005193 null /* inputApplicationHandle */, INVALID_DISPLAY);
Tiger Huang04dc4cc2019-01-17 18:41:41 +08005194 portalWindowHandle.name = name;
5195 portalWindowHandle.token = new Binder();
5196 portalWindowHandle.layoutParamsFlags =
5197 FLAG_SPLIT_TOUCH | FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL;
5198 getBounds(mTmpBounds);
5199 portalWindowHandle.touchableRegion.set(mTmpBounds);
5200 portalWindowHandle.scaleFactor = 1f;
5201 portalWindowHandle.ownerPid = Process.myPid();
5202 portalWindowHandle.ownerUid = Process.myUid();
5203 portalWindowHandle.portalToDisplayId = mDisplayId;
5204 return portalWindowHandle;
chaviwff2e7d82018-11-02 11:11:27 -07005205 }
Issei Suzukia5dbf522019-02-01 17:58:15 +01005206
5207 /**
5208 * @see IWindowManager#setForwardedInsets
5209 */
5210 public void setForwardedInsets(Insets insets) {
5211 if (insets == null) {
5212 insets = Insets.NONE;
5213 }
5214 if (mDisplayPolicy.getForwardedInsets().equals(insets)) {
5215 return;
5216 }
5217 mDisplayPolicy.setForwardedInsets(insets);
5218 setLayoutNeeded();
5219 mWmService.mWindowPlacerLocked.requestTraversal();
5220 }
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -05005221
5222 protected MetricsLogger getMetricsLogger() {
5223 if (mMetricsLogger == null) {
5224 mMetricsLogger = new MetricsLogger();
5225 }
5226 return mMetricsLogger;
5227 }
Craig Mautner59c00972012-07-30 12:10:24 -07005228}