blob: db590308460b89a3000cf9c52d164c1ae196e721 [file] [log] [blame]
Craig Mautner59c00972012-07-30 12:10:24 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wm;
18
Wale Ogunwale65ebd952018-04-25 15:41:44 -070019import static android.app.ActivityTaskManager.SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT;
Wale Ogunwale61911492017-10-11 08:50:50 -070020import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070021import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070022import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
Tarandeep Singh215929b2019-01-11 18:24:37 -080023import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070024import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
25import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwalea77e1462016-09-28 10:09:46 -070026import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070027import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
28import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale687b4272017-07-27 02:56:23 -070029import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
30import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070031import static android.view.Display.DEFAULT_DISPLAY;
32import static android.view.Display.FLAG_PRIVATE;
Andrii Kuliandd989612019-02-21 12:13:28 -080033import static android.view.Display.FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS;
Tiger Huang04dc4cc2019-01-17 18:41:41 +080034import static android.view.Display.INVALID_DISPLAY;
chaviwff2e7d82018-11-02 11:11:27 -070035import static android.view.InsetsState.TYPE_IME;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070036import static android.view.Surface.ROTATION_0;
37import static android.view.Surface.ROTATION_180;
38import static android.view.Surface.ROTATION_270;
39import static android.view.Surface.ROTATION_90;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070040import static android.view.View.GONE;
Wale Ogunwale10124582016-09-15 20:25:50 -070041import static android.view.WindowManager.DOCKED_BOTTOM;
42import static android.view.WindowManager.DOCKED_INVALID;
43import static android.view.WindowManager.DOCKED_TOP;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080044import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
45import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
46import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale494009b82016-10-21 09:01:38 -070047import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070048import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Tiger Huang04dc4cc2019-01-17 18:41:41 +080049import static android.view.WindowManager.LayoutParams.FLAG_SPLIT_TOUCH;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070050import static android.view.WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE;
51import static android.view.WindowManager.LayoutParams.NEEDS_MENU_UNSET;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070052import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale494009b82016-10-21 09:01:38 -070053import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070054import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale494009b82016-10-21 09:01:38 -070055import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
Robert Carrf7a7ca82017-12-06 13:17:07 -080056import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
Wale Ogunwale494009b82016-10-21 09:01:38 -070057import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070058import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070059import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
60import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Wale Ogunwale494009b82016-10-21 09:01:38 -070061import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070062import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
63import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070064import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070065import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Wale Ogunwale3a256e62018-12-06 14:41:18 -080066import static android.view.WindowManager.TRANSIT_ACTIVITY_OPEN;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080067import static android.view.WindowManager.TRANSIT_KEYGUARD_UNOCCLUDE;
Wale Ogunwale3a256e62018-12-06 14:41:18 -080068import static android.view.WindowManager.TRANSIT_TASK_OPEN;
69import static android.view.WindowManager.TRANSIT_TASK_TO_FRONT;
Tiger Huang51c5a1d2018-12-11 20:24:51 +080070
Adrian Roose99bc052017-11-20 17:55:31 +010071import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
72import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
73import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
74import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080075import static com.android.server.wm.DisplayContentProto.ABOVE_APP_WINDOWS;
lumark588a3e82018-07-20 18:53:54 +080076import static com.android.server.wm.DisplayContentProto.APP_TRANSITION;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080077import static com.android.server.wm.DisplayContentProto.BELOW_APP_WINDOWS;
78import static com.android.server.wm.DisplayContentProto.DISPLAY_FRAMES;
79import static com.android.server.wm.DisplayContentProto.DISPLAY_INFO;
80import static com.android.server.wm.DisplayContentProto.DOCKED_STACK_DIVIDER_CONTROLLER;
81import static com.android.server.wm.DisplayContentProto.DPI;
Tiger Huang1e5b10a2018-07-30 20:19:51 +080082import static com.android.server.wm.DisplayContentProto.FOCUSED_APP;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080083import static com.android.server.wm.DisplayContentProto.ID;
84import static com.android.server.wm.DisplayContentProto.IME_WINDOWS;
85import static com.android.server.wm.DisplayContentProto.PINNED_STACK_CONTROLLER;
86import static com.android.server.wm.DisplayContentProto.ROTATION;
87import static com.android.server.wm.DisplayContentProto.SCREEN_ROTATION_ANIMATION;
88import static com.android.server.wm.DisplayContentProto.STACKS;
89import static com.android.server.wm.DisplayContentProto.WINDOW_CONTAINER;
Wale Ogunwale1666e312016-12-16 11:27:18 -080090import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
lumark588a3e82018-07-20 18:53:54 +080091import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_APP_TRANSITIONS;
Wale Ogunwale494009b82016-10-21 09:01:38 -070092import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070093import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070094import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
95import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070096import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070097import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070098import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020099import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700100import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700101import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -0700102import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -0800103import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700104import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -0700105import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700106import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700107import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800108import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700109import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Tiger Huang51c5a1d2018-12-11 20:24:51 +0800110import static com.android.server.wm.WindowManagerService.H.REPORT_FOCUS_CHANGE;
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800111import static com.android.server.wm.WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800112import static com.android.server.wm.WindowManagerService.H.REPORT_LOSING_FOCUS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700113import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
114import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700115import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700116import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700117import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
118import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800119import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_PLACING_SURFACES;
120import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_REMOVING_FOCUS;
121import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_ASSIGN_LAYERS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700122import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700123import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700124import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700125import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700126import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700127import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700128import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700129import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700130import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700131
lumark588a3e82018-07-20 18:53:54 +0800132import android.animation.AnimationHandler;
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700133import android.annotation.CallSuper;
Riddle Hsuf53da812018-08-15 22:00:27 +0800134import android.annotation.IntDef;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700135import android.annotation.NonNull;
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200136import android.annotation.Nullable;
Dean Harding3e5a1522017-10-06 09:19:01 -0700137import android.content.pm.PackageManager;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700138import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700139import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700140import android.graphics.Bitmap;
Issei Suzukia5dbf522019-02-01 17:58:15 +0100141import android.graphics.Insets;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700142import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800143import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700144import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700145import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100146import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700147import android.hardware.display.DisplayManagerInternal;
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -0500148import android.metrics.LogMaker;
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800149import android.os.Binder;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700150import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700151import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700152import android.os.IBinder;
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800153import android.os.Process;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700154import android.os.RemoteException;
155import android.os.SystemClock;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100156import android.os.Trace;
Riddle Hsuf53da812018-08-15 22:00:27 +0800157import android.os.UserHandle;
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800158import android.util.ArraySet;
Chong Zhang8e89b312015-09-09 15:09:30 -0700159import android.util.DisplayMetrics;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700160import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700161import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700162import android.view.Display;
Adrian Roos1cf585052018-01-03 18:43:27 +0100163import android.view.DisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700164import android.view.DisplayInfo;
Riddle Hsua4d6fa22018-08-11 00:50:39 +0800165import android.view.Gravity;
Arthur Hungbe5ce212018-09-13 18:41:56 +0800166import android.view.InputChannel;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700167import android.view.InputDevice;
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800168import android.view.InputWindowHandle;
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200169import android.view.InsetsState.InternalInsetType;
Robert Carrb1579c82017-09-05 14:54:47 -0700170import android.view.MagnificationSpec;
Evan Rosky966759f2019-01-15 10:33:58 -0800171import android.view.RemoteAnimationDefinition;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700172import android.view.Surface;
173import android.view.SurfaceControl;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100174import android.view.SurfaceControl.Transaction;
Robert Carrb1579c82017-09-05 14:54:47 -0700175import android.view.SurfaceSession;
Tiger Huang7c610aa2018-10-27 00:01:01 +0800176import android.view.View;
lumark588a3e82018-07-20 18:53:54 +0800177import android.view.WindowManager;
Evan Rosky39b6f232018-10-30 18:35:41 -0700178import android.view.WindowManagerPolicyConstants.PointerEventListener;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700179
Bryce Lee48f4b572017-04-10 10:54:15 -0700180import com.android.internal.annotations.VisibleForTesting;
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -0500181import com.android.internal.logging.MetricsLogger;
182import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800183import com.android.internal.util.ToBooleanFunction;
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200184import com.android.internal.util.function.TriConsumer;
Winson Chung6ba767a2018-12-13 18:20:11 -0800185import com.android.server.AnimationThread;
Adrian Roose99bc052017-11-20 17:55:31 +0100186import com.android.server.policy.WindowManagerPolicy;
Issei Suzuki43190bd2018-08-20 17:28:41 +0200187import com.android.server.wm.utils.DisplayRotationUtil;
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100188import com.android.server.wm.utils.RotationCache;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100189import com.android.server.wm.utils.WmDisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700190
191import java.io.PrintWriter;
Riddle Hsuf53da812018-08-15 22:00:27 +0800192import java.lang.annotation.Retention;
193import java.lang.annotation.RetentionPolicy;
Craig Mautner59c00972012-07-30 12:10:24 -0700194import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700195import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700196import java.util.HashMap;
197import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700198import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700199import java.util.List;
Adrian Roos1cf585052018-01-03 18:43:27 +0100200import java.util.Objects;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800201import java.util.function.Consumer;
202import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700203
Craig Mautner59c00972012-07-30 12:10:24 -0700204/**
205 * Utility class for keeping track of the WindowStates and other pertinent contents of a
206 * particular Display.
Craig Mautner59c00972012-07-30 12:10:24 -0700207 */
Riddle Hsuad256a12018-07-18 16:11:30 +0800208class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer>
209 implements WindowManagerPolicy.DisplayContentInfo {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700210 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700211
Riddle Hsuf53da812018-08-15 22:00:27 +0800212 /** The default scaling mode that scales content automatically. */
213 static final int FORCE_SCALING_MODE_AUTO = 0;
214 /** For {@link #setForcedScalingMode} to apply flag {@link Display#FLAG_SCALING_DISABLED}. */
215 static final int FORCE_SCALING_MODE_DISABLED = 1;
216
217 @IntDef(prefix = { "FORCE_SCALING_MODE_" }, value = {
218 FORCE_SCALING_MODE_AUTO,
219 FORCE_SCALING_MODE_DISABLED
220 })
221 @Retention(RetentionPolicy.SOURCE)
222 @interface ForceScalingMode {}
223
Craig Mautner59c00972012-07-30 12:10:24 -0700224 /** Unique identifier of this stack. */
225 private final int mDisplayId;
226
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800227 // TODO: Remove once unification is complete.
228 ActivityDisplay mAcitvityDisplay;
229
Wale Ogunwale3a931692016-11-02 16:49:48 -0700230 /** The containers below are the only child containers the display can have. */
231 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800232 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers(mWmService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700233 // Contains all non-app window containers that should be displayed above the app containers
234 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800235 private final AboveAppWindowContainers mAboveAppWindowsContainers =
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800236 new AboveAppWindowContainers("mAboveAppWindowsContainers", mWmService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700237 // Contains all non-app window containers that should be displayed below the app containers
238 // (e.g. Wallpaper).
239 private final NonAppWindowContainers mBelowAppWindowsContainers =
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800240 new NonAppWindowContainers("mBelowAppWindowsContainers", mWmService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700241 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
242 // 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 -0800243 // window containers together and move them in-sync if/when needed. We use a subclass of
244 // WindowContainer which is omitted from screen magnification, as the IME is never magnified.
245 private final NonMagnifiableWindowContainers mImeWindowsContainers =
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800246 new NonMagnifiableWindowContainers("mImeWindowsContainers", mWmService);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700247
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000248 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800249 private WindowState mTmpWindow2;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800250 private boolean mTmpRecoveringMemory;
251 private boolean mUpdateImeTarget;
252 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700253 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700254
lumark588a3e82018-07-20 18:53:54 +0800255 final AppTransition mAppTransition;
256 final AppTransitionController mAppTransitionController;
257 boolean mSkipAppTransitionAnimation = false;
258
259 final ArraySet<AppWindowToken> mOpeningApps = new ArraySet<>();
260 final ArraySet<AppWindowToken> mClosingApps = new ArraySet<>();
Evan Rosky2289ba12018-11-19 18:28:18 -0800261 final ArraySet<AppWindowToken> mChangingApps = new ArraySet<>();
lumark588a3e82018-07-20 18:53:54 +0800262 final UnknownAppVisibilityController mUnknownAppVisibilityController;
263 BoundsAnimationController mBoundsAnimationController;
264
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -0500265 private MetricsLogger mMetricsLogger;
266
lumark588a3e82018-07-20 18:53:54 +0800267 /**
268 * List of clients without a transtiton animation that we notify once we are done
269 * transitioning since they won't be notified through the app window animator.
270 */
271 final List<IBinder> mNoAnimationNotifyOnTransitionFinished = new ArrayList<>();
272
Wale Ogunwale02319a62016-09-26 15:21:22 -0700273 // Mapping from a token IBinder to a WindowToken object on this display.
274 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
275
Andrii Kuliancd097992017-03-23 18:31:59 -0700276 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700277 int mInitialDisplayWidth = 0;
278 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700279 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700280
Adrian Roos1cf585052018-01-03 18:43:27 +0100281 DisplayCutout mInitialDisplayCutout;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100282 private final RotationCache<DisplayCutout, WmDisplayCutout> mDisplayCutoutCache
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100283 = new RotationCache<>(this::calculateDisplayCutoutForRotationUncached);
Adrian Roos1cf585052018-01-03 18:43:27 +0100284
Andrii Kuliancd097992017-03-23 18:31:59 -0700285 /**
286 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
287 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
288 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
289 */
Craig Mautner59c00972012-07-30 12:10:24 -0700290 int mBaseDisplayWidth = 0;
291 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700292 /**
293 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
294 * but can be set from Settings or via shell command "adb shell wm density".
295 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
296 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700297 int mBaseDisplayDensity = 0;
Riddle Hsuf53da812018-08-15 22:00:27 +0800298
299 /**
300 * Whether to disable display scaling. This can be set via shell command "adb shell wm scaling".
301 * @see WindowManagerService#setForcedDisplayScalingMode(int, int)
302 */
Jeff Brownd46747a2015-04-15 19:02:36 -0700303 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700304 private final DisplayInfo mDisplayInfo = new DisplayInfo();
305 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700306 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800307 private final DisplayPolicy mDisplayPolicy;
308 private DisplayRotation mDisplayRotation;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000309 DisplayFrames mDisplayFrames;
310
Andrii Kulian06d07d62017-03-14 11:11:47 -0700311 /**
312 * For default display it contains real metrics, empty for others.
313 * @see WindowManagerService#createWatermarkInTransaction()
314 */
315 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800316
317 /** @see #computeCompatSmallestWidth(boolean, int, int, int, DisplayCutout) */
Andrii Kulian06d07d62017-03-14 11:11:47 -0700318 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700319
Andrii Kulian06d07d62017-03-14 11:11:47 -0700320 /**
321 * Compat metrics computed based on {@link #mDisplayMetrics}.
322 * @see #updateDisplayAndOrientation(int)
323 */
324 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
325
326 /** The desired scaling factor for compatible apps. */
327 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700328
Andrii Kulian8ee72852017-03-10 10:36:45 -0800329 /**
330 * Current rotation of the display.
331 * Constants as per {@link android.view.Surface.Rotation}.
332 *
Robert Carrae606b42018-02-15 15:36:23 -0800333 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800334 */
335 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700336
Andrii Kulian8ee72852017-03-10 10:36:45 -0800337 /**
338 * Last applied orientation of the display.
339 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
340 *
Riddle Hsu4e611772018-10-31 18:58:28 +0800341 * @see #updateOrientationFromAppTokens()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800342 */
343 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700344
Andrii Kulian8ee72852017-03-10 10:36:45 -0800345 /**
Andrii Kulian8ee72852017-03-10 10:36:45 -0800346 * Orientation forced by some window. If there is no visible window that specifies orientation
347 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
348 *
349 * @see NonAppWindowContainers#getOrientation()
350 */
351 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700352
Andrii Kulian8ee72852017-03-10 10:36:45 -0800353 /**
354 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
355 * occluded.
356 *
357 * @see NonAppWindowContainers#getOrientation()
358 */
359 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
360
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700361 /**
362 * Keep track of wallpaper visibility to notify changes.
363 */
364 private boolean mLastWallpaperVisible = false;
365
Andrii Kulian06d07d62017-03-14 11:11:47 -0700366 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700367
Craig Mautner39834192012-09-02 07:47:24 -0700368 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700369 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700370 int pendingLayoutChanges;
Riddle Hsu654a6f92018-07-13 22:59:36 +0800371 int mDeferredRotationPauseCount;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800372
Vishnu Nairba183352018-11-21 11:16:49 -0800373 /**
374 * Used to gate application window layout until we have sent the complete configuration.
375 * TODO: There are still scenarios where we may be out of sync with the client. Ideally
376 * we want to replace this flag with a mechanism that will confirm the configuration
377 * applied by the client is the one expected by the system server.
378 */
379 boolean mWaitingForConfig;
380
Andrii Kulian839def92016-11-02 10:58:58 -0700381 // TODO(multi-display): remove some of the usages.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800382 @VisibleForTesting
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800383 boolean isDefaultDisplay;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800384
Andrii Kulianf0379de2018-03-14 16:24:07 -0700385 /**
386 * Flag indicating whether WindowManager should override info for this display in
387 * DisplayManager.
388 */
389 boolean mShouldOverrideDisplayConfiguration = true;
Craig Mautner39834192012-09-02 07:47:24 -0700390
Craig Mautnerdc548482014-02-05 13:35:24 -0800391 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700392 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800393
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700394 /** Detect user tapping outside of current focused task bounds .*/
Riddle Hsu2588ab02019-02-25 14:23:56 +0800395 @VisibleForTesting
396 final TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700397
Craig Mautner6601b7b2013-04-29 10:29:11 -0700398 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700399 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700400
Craig Mautner6601b7b2013-04-29 10:29:11 -0700401 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800402 private final Rect mTmpRect = new Rect();
403 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700404 private final RectF mTmpRectF = new RectF();
405 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800406 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700407
Bryce Leef3c6a472017-11-14 14:53:06 -0800408 /** Used for handing back size of display */
409 private final Rect mTmpBounds = new Rect();
410
Garfield Tan90b04282018-12-11 14:04:42 -0800411 private final Configuration mTmpConfiguration = new Configuration();
412
Craig Mautner95da1082014-02-24 17:54:35 -0800413 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700414 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800415
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700416 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700417 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700418
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800419 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800420 /** A collection of windows that provide tap exclude regions inside of them. */
421 final ArraySet<WindowState> mTapExcludeProvidingWindows = new ArraySet<>();
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800422
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000423 private boolean mHaveBootMsg = false;
424 private boolean mHaveApp = false;
425 private boolean mHaveWallpaper = false;
426 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700427
428 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
429
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700430 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
431 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000432 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
433 new ApplySurfaceChangesTransactionState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700434
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700435 // True if this display is in the process of being removed. Used to determine if the removal of
436 // the display's direct children should be allowed.
437 private boolean mRemovingDisplay = false;
438
Bryce Leed1871262017-06-12 14:12:29 -0700439 // {@code false} if this display is in the processing of being created.
440 private boolean mDisplayReady = false;
441
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800442 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700443
wilsonshihc32538e2018-11-07 17:27:34 +0800444 boolean mWallpaperMayChange = false;
445
Robert Carrb1579c82017-09-05 14:54:47 -0700446 private final SurfaceSession mSession = new SurfaceSession();
447
448 /**
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800449 * Window that is currently interacting with the user. This window is responsible for receiving
450 * key events and pointer events from the user.
451 */
452 WindowState mCurrentFocus = null;
453
454 /**
455 * The last focused window that we've notified the client that the focus is changed.
456 */
457 WindowState mLastFocus = null;
458
459 /**
460 * Windows that have lost input focus and are waiting for the new focus window to be displayed
461 * before they are told about this.
462 */
463 ArrayList<WindowState> mLosingFocus = new ArrayList<>();
464
465 /**
466 * The foreground app of this display. Windows below this app cannot be the focused window. If
467 * the user taps on the area outside of the task of the focused app, we will notify AM about the
468 * new task the user wants to interact with.
469 */
470 AppWindowToken mFocusedApp = null;
471
472 /** Windows added since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
473 final ArrayList<WindowState> mWinAddedSinceNullFocus = new ArrayList<>();
474
475 /** Windows removed since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
476 final ArrayList<WindowState> mWinRemovedSinceNullFocus = new ArrayList<>();
477
478 /**
Robert Carrb1579c82017-09-05 14:54:47 -0700479 * We organize all top-level Surfaces in to the following layers.
480 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800481 * and omitted from Screen-Magnification, for example the strict mode flash or
482 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700483 * {@link #mWindowingLayer} contains everything else.
484 */
485 private SurfaceControl mOverlayLayer;
486
487 /**
488 * See {@link #mOverlayLayer}
489 */
490 private SurfaceControl mWindowingLayer;
491
492 /**
Adrian Roos5251b1d2018-03-23 18:57:43 +0100493 * Sequence number for the current layout pass.
494 */
495 int mLayoutSeq = 0;
496
Chavi Weingarten3a748552018-05-14 17:32:42 +0000497 /**
498 * Specifies the count to determine whether to defer updating the IME target until ready.
499 */
500 private int mDeferUpdateImeTargetCount;
501
Jorim Jaggi9af095b2017-12-12 17:18:57 +0100502 /** Temporary float array to retrieve 3x3 matrix values. */
503 private final float[] mTmpFloats = new float[9];
504
Robert Carr24be9ab2018-04-30 17:54:53 -0700505 private MagnificationSpec mMagnificationSpec;
506
Arthur Hung95b38a92018-07-20 18:56:12 +0800507 private InputMonitor mInputMonitor;
508
Jorim Jaggif1292892018-09-10 11:58:13 +0200509 /** Caches the value whether told display manager that we have content. */
510 private boolean mLastHasContent;
511
Issei Suzuki43190bd2018-08-20 17:28:41 +0200512 private DisplayRotationUtil mRotationUtil = new DisplayRotationUtil();
513
lumark90120a82018-08-15 00:33:03 +0800514 /**
515 * The input method window for this display.
516 */
517 WindowState mInputMethodWindow;
518
lumarkff0ab692018-11-05 20:32:30 +0800519 /**
520 * This just indicates the window the input method is on top of, not
521 * necessarily the window its input is going to.
522 */
523 WindowState mInputMethodTarget;
524
525 /** If true hold off on modifying the animation layer of mInputMethodTarget */
526 boolean mInputMethodTargetWaitingAnim;
527
Arthur Hungbe5ce212018-09-13 18:41:56 +0800528 private final PointerEventDispatcher mPointerEventDispatcher;
529
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200530 private final InsetsStateController mInsetsStateController;
531
Tiger Huang04dc4cc2019-01-17 18:41:41 +0800532 private SurfaceControl mParentSurfaceControl;
533 private InputWindowHandle mPortalWindowHandle;
534
Tiger Huang7c610aa2018-10-27 00:01:01 +0800535 // Last systemUiVisibility we received from status bar.
536 private int mLastStatusBarVisibility = 0;
537 // Last systemUiVisibility we dispatched to windows.
538 private int mLastDispatchedSystemUiVisibility = 0;
539
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800540 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
541 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800542 final AppWindowToken atoken = w.mAppToken;
543 if (winAnimator.mDrawState == READY_TO_SHOW) {
Jorim Jaggiaf0d6d22018-06-08 15:25:35 +0200544 if (atoken == null || atoken.canShowWindows()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800545 if (w.performShowLocked()) {
546 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
547 if (DEBUG_LAYOUT_REPEATS) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800548 mWmService.mWindowPlacerLocked.debugLayoutRepeats(
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800549 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
550 }
551 }
552 }
553 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800554 };
555
556 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
557 final WindowStateAnimator winAnimator = w.mWinAnimator;
558 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
559 return;
560 }
561
Jorim Jaggi8f520872018-08-14 17:00:20 +0200562 // If this window is animating, ensure the animation background is set.
563 final AnimationAdapter anim = w.mAppToken != null
564 ? w.mAppToken.getAnimation()
565 : w.getAnimation();
566 if (anim != null) {
567 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800568 if (color != 0) {
569 final TaskStack stack = w.getStack();
570 if (stack != null) {
571 stack.setAnimationBackground(winAnimator, color);
572 }
573 }
574 }
575 };
576
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800577 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
578 final int lostFocusUid = mTmpWindow.mOwnerUid;
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800579 final Handler handler = mWmService.mH;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800580 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
581 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
582 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
583 w.mAttrs.hideTimeoutMilliseconds);
584 }
585 }
586 };
587
588 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800589 final AppWindowToken focusedApp = mFocusedApp;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800590 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
591 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
592
593 if (!w.canReceiveKeys()) {
594 return false;
595 }
596
597 final AppWindowToken wtoken = w.mAppToken;
598
599 // If this window's application has been removed, just skip it.
600 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
601 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
602 + (wtoken.removed ? "removed" : "sendingToBottom"));
603 return false;
604 }
605
606 if (focusedApp == null) {
607 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
608 + " using new focus @ " + w);
609 mTmpWindow = w;
610 return true;
611 }
612
613 if (!focusedApp.windowsAreFocusable()) {
614 // Current focused app windows aren't focusable...
615 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
616 + " focusable using new focus @ " + w);
617 mTmpWindow = w;
618 return true;
619 }
620
621 // Descend through all of the app tokens and find the first that either matches
622 // win.mAppToken (return win) or mFocusedApp (return null).
623 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
624 if (focusedApp.compareTo(wtoken) > 0) {
625 // App stack below focused app stack. No focus for you!!!
626 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
627 "findFocusedWindow: Reached focused app=" + focusedApp);
628 mTmpWindow = null;
629 return true;
630 }
631 }
632
633 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
634 mTmpWindow = w;
635 return true;
636 };
637
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800638 private final Consumer<WindowState> mPerformLayout = w -> {
639 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
640 // wasting time and funky changes while a window is animating away.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800641 final boolean gone = (mTmpWindow != null && mWmService.mPolicy.canBeHiddenByKeyguardLw(w))
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800642 || w.isGoneForLayoutLw();
643
644 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
645 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
646 + " mLayoutAttached=" + w.mLayoutAttached
647 + " screen changed=" + w.isConfigChanged());
648 final AppWindowToken atoken = w.mAppToken;
649 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200650 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800651 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
652 + " parentHidden=" + w.isParentWindowHidden());
653 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200654 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800655 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
656 + " parentHidden=" + w.isParentWindowHidden());
657 }
658
659 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
660 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
661 // since that means "perform layout as normal, just don't display").
662 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
663 || ((w.isConfigChanged() || w.setReportResizeHints())
664 && !w.isGoneForLayoutLw() &&
665 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
666 (w.mHasSurface && w.mAppToken != null &&
667 w.mAppToken.layoutConfigChanges)))) {
668 if (!w.mLayoutAttached) {
669 if (mTmpInitial) {
670 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700671 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800672 }
673 if (w.mAttrs.type == TYPE_DREAM) {
674 // Don't layout windows behind a dream, so that if it does stuff like hide
675 // the status bar we won't get a bad transition when it goes away.
676 mTmpWindow = w;
677 }
678 w.mLayoutNeeded = false;
679 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200680 final boolean firstLayout = !w.isLaidOut();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800681 getDisplayPolicy().layoutWindowLw(w, null, mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100682 w.mLayoutSeq = mLayoutSeq;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800683
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200684 // If this is the first layout, we need to initialize the last inset values as
685 // otherwise we'd immediately cause an unnecessary resize.
686 if (firstLayout) {
687 w.updateLastInsetValues();
688 }
689
Adrian Roos23df3a32018-03-15 15:41:13 +0100690 if (w.mAppToken != null) {
691 w.mAppToken.layoutLetterbox(w);
692 }
693
chaviw492139a2018-07-16 16:07:35 -0700694 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700695 + " mContainingFrame=" + w.getContainingFrame()
696 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800697 }
698 }
699 };
700
701 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
702 if (w.mLayoutAttached) {
703 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
704 + " mViewVisibility=" + w.mViewVisibility
705 + " mRelayoutCalled=" + w.mRelayoutCalled);
706 // If this view is GONE, then skip it -- keep the current frame, and let the caller
707 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
708 // windows, since that means "perform layout as normal, just don't display").
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800709 if (mTmpWindow != null && mWmService.mPolicy.canBeHiddenByKeyguardLw(w)) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800710 return;
711 }
712 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
713 || w.mLayoutNeeded) {
714 if (mTmpInitial) {
715 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700716 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800717 }
718 w.mLayoutNeeded = false;
719 w.prelayout();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800720 getDisplayPolicy().layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100721 w.mLayoutSeq = mLayoutSeq;
chaviw492139a2018-07-16 16:07:35 -0700722 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700723 + " mContainingFrame=" + w.getContainingFrame()
724 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800725 }
726 } else if (w.mAttrs.type == TYPE_DREAM) {
727 // Don't layout windows behind a dream, so that if it does stuff like hide the
728 // status bar we won't get a bad transition when it goes away.
729 mTmpWindow = mTmpWindow2;
730 }
731 };
732
733 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
734 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
735 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
736 return w.canBeImeTarget();
737 };
738
739 private final Consumer<WindowState> mApplyPostLayoutPolicy =
Tiger Huang7c610aa2018-10-27 00:01:01 +0800740 w -> getDisplayPolicy().applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
lumarkff0ab692018-11-05 20:32:30 +0800741 mInputMethodTarget);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800742
743 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800744 final WindowSurfacePlacer surfacePlacer = mWmService.mWindowPlacerLocked;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800745 final boolean obscuredChanged = w.mObscured !=
746 mTmpApplySurfaceChangesTransactionState.obscured;
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800747 final RootWindowContainer root = mWmService.mRoot;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800748
749 // Update effect.
750 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
751 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
752 final boolean isDisplayed = w.isDisplayedLw();
753
754 if (isDisplayed && w.isObscuringDisplay()) {
755 // This window completely covers everything behind it, so we want to leave all
756 // of them as undimmed (for performance reasons).
757 root.mObscuringWindow = w;
758 mTmpApplySurfaceChangesTransactionState.obscured = true;
759 }
760
761 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
762 root.handleNotObscuredLocked(w,
763 mTmpApplySurfaceChangesTransactionState.obscured,
764 mTmpApplySurfaceChangesTransactionState.syswin);
765
766 if (w.mHasSurface && isDisplayed) {
767 final int type = w.mAttrs.type;
768 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
769 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
770 mTmpApplySurfaceChangesTransactionState.syswin = true;
771 }
772 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
773 && w.mAttrs.preferredRefreshRate != 0) {
774 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
775 = w.mAttrs.preferredRefreshRate;
776 }
777 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
778 && w.mAttrs.preferredDisplayModeId != 0) {
779 mTmpApplySurfaceChangesTransactionState.preferredModeId
780 = w.mAttrs.preferredDisplayModeId;
781 }
782 }
783 }
784
wilsonshihc32538e2018-11-07 17:27:34 +0800785 if (obscuredChanged && w.isVisibleLw() && mWallpaperController.isWallpaperTarget(w)) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800786 // This is the wallpaper target and its obscured state changed... make sure the
787 // current wallpaper's visibility has been updated accordingly.
788 mWallpaperController.updateWallpaperVisibility();
789 }
790
chaviw161ea3e2018-01-31 12:01:12 -0800791 w.handleWindowMovedIfNeeded();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800792
793 final WindowStateAnimator winAnimator = w.mWinAnimator;
794
795 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
chaviw1454b392018-08-06 09:54:04 -0700796 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800797
798 // Moved from updateWindowsAndWallpaperLocked().
799 if (w.mHasSurface) {
800 // Take care of the window being ready to display.
801 final boolean committed = winAnimator.commitFinishDrawingLocked();
802 if (isDefaultDisplay && committed) {
803 if (w.mAttrs.type == TYPE_DREAM) {
804 // HACK: When a dream is shown, it may at that point hide the lock screen.
805 // So we need to redo the layout to let the phone window manager make this
806 // happen.
807 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
808 if (DEBUG_LAYOUT_REPEATS) {
809 surfacePlacer.debugLayoutRepeats(
810 "dream and commitFinishDrawingLocked true",
811 pendingLayoutChanges);
812 }
813 }
814 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
815 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
816 "First draw done in potential wallpaper target " + w);
wilsonshihc32538e2018-11-07 17:27:34 +0800817 mWallpaperMayChange = true;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800818 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
819 if (DEBUG_LAYOUT_REPEATS) {
820 surfacePlacer.debugLayoutRepeats(
821 "wallpaper and commitFinishDrawingLocked true",
822 pendingLayoutChanges);
823 }
824 }
825 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800826 }
827
828 final AppWindowToken atoken = w.mAppToken;
829 if (atoken != null) {
Adrian Roos23df3a32018-03-15 15:41:13 +0100830 atoken.updateLetterboxSurface(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800831 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
832 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
833 mTmpUpdateAllDrawn.add(atoken);
834 }
835 }
836
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800837 if (!mLosingFocus.isEmpty() && w.isFocused() && w.isDisplayedLw()) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800838 mWmService.mH.obtainMessage(REPORT_LOSING_FOCUS, this).sendToTarget();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800839 }
840
841 w.updateResizingWindowIfNeeded();
842 };
843
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800844 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800845 * Create new {@link DisplayContent} instance, add itself to the root window container and
846 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700847 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800848 * @param service You know.
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800849 * @param activityDisplay The ActivityDisplay for the display container.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700850 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700851 DisplayContent(Display display, WindowManagerService service,
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800852 ActivityDisplay activityDisplay) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100853 super(service);
Wale Ogunwale3a256e62018-12-06 14:41:18 -0800854 mAcitvityDisplay = activityDisplay;
Wale Ogunwale1666e312016-12-16 11:27:18 -0800855 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
856 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
857 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
858 + " new=" + display);
859 }
860
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700861 mDisplay = display;
862 mDisplayId = display.getDisplayId();
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800863 mWallpaperController = new WallpaperController(mWmService, this);
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700864 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700865 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700866 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100867 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo,
868 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700869 initializeDisplayBaseInfo();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700870
lumark588a3e82018-07-20 18:53:54 +0800871 mAppTransition = new AppTransition(service.mContext, service, this);
872 mAppTransition.registerListenerLocked(service.mActivityManagerAppTransitionNotifier);
873 mAppTransitionController = new AppTransitionController(service, this);
874 mUnknownAppVisibilityController = new UnknownAppVisibilityController(service, this);
875
876 AnimationHandler animationHandler = new AnimationHandler();
877 mBoundsAnimationController = new BoundsAnimationController(service.mContext,
Winson Chung6ba767a2018-12-13 18:20:11 -0800878 mAppTransition, AnimationThread.getHandler(), animationHandler);
lumark588a3e82018-07-20 18:53:54 +0800879
Riddle Hsu2588ab02019-02-25 14:23:56 +0800880 final InputChannel inputChannel = mWmService.mInputManager.monitorInput(
881 "PointerEventDispatcher" + mDisplayId, mDisplayId);
882 mPointerEventDispatcher = new PointerEventDispatcher(inputChannel);
883
884 // Tap Listeners are supported for:
885 // 1. All physical displays (multi-display).
886 // 2. VirtualDisplays on VR, AA (and everything else).
887 mTapDetector = new TaskTapPointerEventListener(mWmService, this);
888 registerPointerEventListener(mTapDetector);
889 registerPointerEventListener(mWmService.mMousePositionTracker);
890
Tiger Huang7c610aa2018-10-27 00:01:01 +0800891 mDisplayPolicy = new DisplayPolicy(service, this);
892 mDisplayRotation = new DisplayRotation(service, this);
893 if (isDefaultDisplay) {
894 // The policy may be invoked right after here, so it requires the necessary default
895 // fields of this display content.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800896 mWmService.mPolicy.setDefaultDisplay(this);
Tiger Huang7c610aa2018-10-27 00:01:01 +0800897 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800898 if (mWmService.mDisplayReady) {
Tiger Huang7c610aa2018-10-27 00:01:01 +0800899 mDisplayPolicy.onConfigurationChanged();
900 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800901 if (mWmService.mSystemReady) {
Tiger Huang7c610aa2018-10-27 00:01:01 +0800902 mDisplayPolicy.systemReady();
903 }
904 mDividerControllerLocked = new DockedStackDividerController(service, this);
905 mPinnedStackControllerLocked = new PinnedStackController(service, this);
906
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +0000907 final SurfaceControl.Builder b = mWmService.makeSurfaceBuilder(mSession)
908 .setOpaque(true)
909 .setContainerLayer();
Robert Carrb1579c82017-09-05 14:54:47 -0700910 mWindowingLayer = b.setName("Display Root").build();
911 mOverlayLayer = b.setName("Display Overlays").build();
912
Robert Carrf59b8dd2017-10-02 18:58:36 -0700913 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700914 .setLayerStack(mWindowingLayer, mDisplayId)
915 .show(mWindowingLayer)
916 .setLayer(mOverlayLayer, 1)
917 .setLayerStack(mOverlayLayer, mDisplayId)
918 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700919 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700920
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700921 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700922 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700923 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700924 super.addChild(mAboveAppWindowsContainers, null);
925 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800926
927 // Add itself as a child to the root container.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800928 mWmService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700929
930 // TODO(b/62541591): evaluate whether this is the best spot to declare the
931 // {@link DisplayContent} ready for use.
932 mDisplayReady = true;
Arthur Hung95b38a92018-07-20 18:56:12 +0800933
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800934 mWmService.mAnimator.addDisplayLocked(mDisplayId);
Arthur Hung39134b22018-08-14 11:58:28 +0800935 mInputMonitor = new InputMonitor(service, mDisplayId);
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200936 mInsetsStateController = new InsetsStateController(this);
Bryce Leed1871262017-06-12 14:12:29 -0700937 }
938
939 boolean isReady() {
940 // The display is ready when the system and the individual display are both ready.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800941 return mWmService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700942 }
943
944 int getDisplayId() {
945 return mDisplayId;
946 }
947
Wale Ogunwale02319a62016-09-26 15:21:22 -0700948 WindowToken getWindowToken(IBinder binder) {
949 return mTokenMap.get(binder);
950 }
951
952 AppWindowToken getAppWindowToken(IBinder binder) {
953 final WindowToken token = getWindowToken(binder);
954 if (token == null) {
955 return null;
956 }
957 return token.asAppWindowToken();
958 }
959
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700960 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800961 final DisplayContent dc = mWmService.mRoot.getWindowTokenDisplay(token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700962 if (dc != null) {
963 // We currently don't support adding a window token to the display if the display
964 // already has the binder mapped to another token. If there is a use case for supporting
965 // this moving forward we will either need to merge the WindowTokens some how or have
966 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700967 throw new IllegalArgumentException("Can't map token=" + token + " to display="
968 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700969 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700970 if (binder == null) {
971 throw new IllegalArgumentException("Can't map token=" + token + " to display="
972 + getName() + " binder is null");
973 }
974 if (token == null) {
975 throw new IllegalArgumentException("Can't map null token to display="
976 + getName() + " binder=" + binder);
977 }
978
Wale Ogunwale02319a62016-09-26 15:21:22 -0700979 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700980
981 if (token.asAppWindowToken() == null) {
982 // Add non-app token to container hierarchy on the display. App tokens are added through
983 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700984 switch (token.windowType) {
985 case TYPE_WALLPAPER:
986 mBelowAppWindowsContainers.addChild(token);
987 break;
988 case TYPE_INPUT_METHOD:
989 case TYPE_INPUT_METHOD_DIALOG:
990 mImeWindowsContainers.addChild(token);
991 break;
992 default:
993 mAboveAppWindowsContainers.addChild(token);
994 break;
995 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700996 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700997 }
998
999 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001000 final WindowToken token = mTokenMap.remove(binder);
1001 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -08001002 token.setExiting();
1003 }
1004 return token;
1005 }
1006
1007 /** Changes the display the input window token is housed on to this one. */
1008 void reParentWindowToken(WindowToken token) {
1009 final DisplayContent prevDc = token.getDisplayContent();
1010 if (prevDc == this) {
1011 return;
1012 }
Riddle Hsu85bd04b2018-11-17 00:34:36 +08001013 if (prevDc != null) {
1014 if (prevDc.mTokenMap.remove(token.token) != null && token.asAppWindowToken() == null) {
1015 // Removed the token from the map, but made sure it's not an app token before
1016 // removing from parent.
1017 token.getParent().removeChild(token);
1018 }
1019 if (prevDc.mLastFocus == mCurrentFocus) {
1020 // The window has become the focus of this display, so it should not be notified
1021 // that it lost focus from the previous display.
1022 prevDc.mLastFocus = null;
1023 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001024 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -08001025
1026 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -07001027 }
1028
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001029 void removeAppToken(IBinder binder) {
1030 final WindowToken token = removeWindowToken(binder);
1031 if (token == null) {
1032 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
1033 return;
1034 }
1035
1036 final AppWindowToken appToken = token.asAppWindowToken();
1037
1038 if (appToken == null) {
1039 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
1040 return;
1041 }
1042
1043 appToken.onRemovedFromDisplay();
1044 }
1045
Riddle Hsuad256a12018-07-18 16:11:30 +08001046 @Override
1047 public Display getDisplay() {
Craig Mautnerb47bbc32012-08-22 17:41:48 -07001048 return mDisplay;
1049 }
1050
Craig Mautner59c00972012-07-30 12:10:24 -07001051 DisplayInfo getDisplayInfo() {
1052 return mDisplayInfo;
1053 }
1054
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001055 DisplayMetrics getDisplayMetrics() {
1056 return mDisplayMetrics;
1057 }
1058
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001059 DisplayPolicy getDisplayPolicy() {
1060 return mDisplayPolicy;
1061 }
1062
Riddle Hsuad256a12018-07-18 16:11:30 +08001063 @Override
1064 public DisplayRotation getDisplayRotation() {
1065 return mDisplayRotation;
1066 }
1067
Jorim Jaggif96c90a2018-09-26 16:55:15 +02001068 /**
1069 * Marks a window as providing insets for the rest of the windows in the system.
1070 *
1071 * @param type The type of inset this window provides.
1072 * @param win The window.
1073 * @param frameProvider Function to compute the frame, or {@code null} if the just the frame of
1074 * the window should be taken.
1075 */
1076 void setInsetProvider(@InternalInsetType int type, WindowState win,
1077 @Nullable TriConsumer<DisplayFrames, WindowState, Rect> frameProvider) {
1078 mInsetsStateController.getSourceProvider(type).setWindow(win, frameProvider);
1079 }
1080
1081 InsetsStateController getInsetsStateController() {
1082 return mInsetsStateController;
1083 }
1084
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001085 @VisibleForTesting
1086 void setDisplayRotation(DisplayRotation displayRotation) {
1087 mDisplayRotation = displayRotation;
1088 }
1089
Andrii Kulian8ee72852017-03-10 10:36:45 -08001090 int getRotation() {
1091 return mRotation;
1092 }
1093
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001094 @VisibleForTesting
Andrii Kulian8ee72852017-03-10 10:36:45 -08001095 void setRotation(int newRotation) {
1096 mRotation = newRotation;
Riddle Hsuad256a12018-07-18 16:11:30 +08001097 mDisplayRotation.setRotation(newRotation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08001098 }
1099
1100 int getLastOrientation() {
1101 return mLastOrientation;
1102 }
1103
Andrii Kulian8ee72852017-03-10 10:36:45 -08001104 int getLastWindowForcedOrientation() {
1105 return mLastWindowForcedOrientation;
1106 }
1107
Evan Rosky966759f2019-01-15 10:33:58 -08001108 void registerRemoteAnimations(RemoteAnimationDefinition definition) {
1109 mAppTransitionController.registerRemoteAnimations(definition);
1110 }
1111
Andrii Kulian06d07d62017-03-14 11:11:47 -07001112 /**
Riddle Hsu654a6f92018-07-13 22:59:36 +08001113 * Temporarily pauses rotation changes until resumed.
1114 *
1115 * This can be used to prevent rotation changes from occurring while the user is
1116 * performing certain operations, such as drag and drop.
1117 *
1118 * This call nests and must be matched by an equal number of calls to
1119 * {@link #resumeRotationLocked}.
1120 */
1121 void pauseRotationLocked() {
1122 mDeferredRotationPauseCount++;
1123 }
1124
1125 /**
1126 * Resumes normal rotation changes after being paused.
1127 */
1128 void resumeRotationLocked() {
1129 if (mDeferredRotationPauseCount <= 0) {
1130 return;
1131 }
1132
1133 mDeferredRotationPauseCount--;
1134 if (mDeferredRotationPauseCount == 0) {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001135 updateRotationAndSendNewConfigIfNeeded();
Riddle Hsu654a6f92018-07-13 22:59:36 +08001136 }
1137 }
1138
1139 /**
Riddle Hsuad256a12018-07-18 16:11:30 +08001140 * If this is true we have updated our desired orientation, but not yet changed the real
1141 * orientation our applied our screen rotation animation. For example, because a previous
1142 * screen rotation was in progress.
1143 *
1144 * @return {@code true} if the there is an ongoing rotation change.
1145 */
1146 boolean rotationNeedsUpdate() {
1147 final int lastOrientation = getLastOrientation();
1148 final int oldRotation = getRotation();
Riddle Hsuad256a12018-07-18 16:11:30 +08001149
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001150 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Garfield Tan4bb83472019-01-16 14:37:04 -08001151 return oldRotation != rotation;
Riddle Hsuad256a12018-07-18 16:11:30 +08001152 }
1153
Wale Ogunwale3a256e62018-12-06 14:41:18 -08001154 /**
1155 * The display content may have configuration set from {@link #DisplayWindowSettings}. This
1156 * callback let the owner of container know there is existing configuration to prevent the
1157 * values from being replaced by the initializing {@link #ActivityDisplay}.
1158 */
1159 void initializeDisplayOverrideConfiguration() {
1160 if (mAcitvityDisplay != null) {
1161 mAcitvityDisplay.onInitializeOverrideConfiguration(getRequestedOverrideConfiguration());
1162 }
1163 }
1164
Riddle Hsu4e611772018-10-31 18:58:28 +08001165 /** Notify the configuration change of this display. */
1166 void sendNewConfiguration() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001167 mWmService.mH.obtainMessage(SEND_NEW_CONFIGURATION, this).sendToTarget();
Riddle Hsu4e611772018-10-31 18:58:28 +08001168 }
1169
Garfield Tan90b04282018-12-11 14:04:42 -08001170 @Override
1171 boolean onDescendantOrientationChanged(IBinder freezeDisplayToken,
1172 ConfigurationContainer requestingContainer) {
1173 final Configuration config = updateOrientationFromAppTokens(
1174 getRequestedOverrideConfiguration(), freezeDisplayToken, false);
Garfield Tan49dae102019-02-04 09:51:59 -08001175 // If display rotation class tells us that it doesn't consider app requested orientation,
1176 // this display won't rotate just because of an app changes its requested orientation. Thus
1177 // it indicates that this display chooses not to handle this request.
1178 final boolean handled = getDisplayRotation().respectAppRequestedOrientation();
Garfield Tan90b04282018-12-11 14:04:42 -08001179 if (config == null) {
1180 return handled;
1181 }
1182
1183 if (handled && requestingContainer instanceof ActivityRecord) {
1184 final ActivityRecord activityRecord = (ActivityRecord) requestingContainer;
1185 final boolean kept = mWmService.mAtmService.updateDisplayOverrideConfigurationLocked(
1186 config, activityRecord, false /* deferResume */, getDisplayId());
1187 activityRecord.frozenBeforeDestroy = true;
1188 if (!kept) {
1189 mWmService.mAtmService.mRootActivityContainer.resumeFocusedStacksTopActivities();
1190 }
1191 } else {
1192 // We have a new configuration to push so we need to update ATMS for now.
1193 // TODO: Clean up display configuration push between ATMS and WMS after unification.
1194 mWmService.mAtmService.updateDisplayOverrideConfigurationLocked(
1195 config, null /* starting */, false /* deferResume */, getDisplayId());
1196 }
1197 return handled;
1198 }
1199
Garfield Tan49dae102019-02-04 09:51:59 -08001200 @Override
1201 boolean handlesOrientationChangeFromDescendant() {
1202 return getDisplayRotation().respectAppRequestedOrientation();
1203 }
1204
Riddle Hsu4e611772018-10-31 18:58:28 +08001205 /**
1206 * Determine the new desired orientation of this display.
1207 *
1208 * The orientation is computed from non-application windows first. If none of the
1209 * non-application windows specify orientation, the orientation is computed from application
1210 * tokens.
1211 *
1212 * @return {@code true} if the orientation is changed.
1213 */
1214 boolean updateOrientationFromAppTokens() {
1215 return updateOrientationFromAppTokens(false /* forceUpdate */);
1216 }
1217
Garfield Tan90b04282018-12-11 14:04:42 -08001218 /**
1219 * Update orientation of the target display, returning a non-null new Configuration if it has
1220 * changed from the current orientation. If a non-null configuration is returned, someone must
1221 * call {@link WindowManagerService#setNewDisplayOverrideConfiguration(Configuration,
1222 * DisplayContent)} to tell the window manager it can unfreeze the screen. This will typically
1223 * be done by calling {@link WindowManagerService#sendNewConfiguration(int)}.
1224 */
1225 Configuration updateOrientationFromAppTokens(Configuration currentConfig,
1226 IBinder freezeDisplayToken, boolean forceUpdate) {
1227 if (!mDisplayReady) {
1228 return null;
1229 }
1230
1231 Configuration config = null;
1232 if (updateOrientationFromAppTokens(forceUpdate)) {
1233 // If we changed the orientation but mOrientationChangeComplete is already true,
1234 // we used seamless rotation, and we don't need to freeze the screen.
1235 if (freezeDisplayToken != null && !mWmService.mRoot.mOrientationChangeComplete) {
1236 final AppWindowToken atoken = getAppWindowToken(freezeDisplayToken);
1237 if (atoken != null) {
1238 atoken.startFreezingScreen();
1239 }
1240 }
1241 config = new Configuration();
1242 computeScreenConfiguration(config);
1243 } else if (currentConfig != null) {
1244 // No obvious action we need to take, but if our current state mismatches the
1245 // activity manager's, update it, disregarding font scale, which should remain set
1246 // to the value of the previous configuration.
1247 // Here we're calling Configuration#unset() instead of setToDefaults() because we
1248 // need to keep override configs clear of non-empty values (e.g. fontSize).
1249 mTmpConfiguration.unset();
1250 mTmpConfiguration.updateFrom(currentConfig);
1251 computeScreenConfiguration(mTmpConfiguration);
1252 if (currentConfig.diff(mTmpConfiguration) != 0) {
1253 mWaitingForConfig = true;
1254 setLayoutNeeded();
1255 int[] anim = new int[2];
1256 getDisplayPolicy().selectRotationAnimationLw(anim);
1257
1258 mWmService.startFreezingDisplayLocked(anim[0], anim[1], this);
1259 config = new Configuration(mTmpConfiguration);
1260 }
1261 }
1262
1263 return config;
1264 }
1265
1266
1267 private boolean updateOrientationFromAppTokens(boolean forceUpdate) {
Riddle Hsu4e611772018-10-31 18:58:28 +08001268 final int req = getOrientation();
1269 if (req != mLastOrientation || forceUpdate) {
1270 mLastOrientation = req;
1271 mDisplayRotation.setCurrentOrientation(req);
1272 return updateRotationUnchecked(forceUpdate);
1273 }
1274 return false;
1275 }
1276
Riddle Hsuad256a12018-07-18 16:11:30 +08001277 /**
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001278 * Update rotation of the display and send configuration if the rotation is changed.
1279 *
1280 * @return {@code true} if the rotation has been changed and the new config is sent.
1281 */
1282 boolean updateRotationAndSendNewConfigIfNeeded() {
1283 final boolean changed = updateRotationUnchecked(false /* forceUpdate */);
1284 if (changed) {
Riddle Hsu4e611772018-10-31 18:58:28 +08001285 sendNewConfiguration();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001286 }
1287 return changed;
1288 }
1289
1290 /**
Andrii Kulian06d07d62017-03-14 11:11:47 -07001291 * Update rotation of the display.
1292 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001293 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
1294 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001295 */
Robert Carrae606b42018-02-15 15:36:23 -08001296 boolean updateRotationUnchecked() {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001297 return updateRotationUnchecked(false /* forceUpdate */);
1298 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001299
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001300 /**
Evan Roskye747c3e2018-10-30 20:06:41 -07001301 * Update rotation of the DisplayContent with an option to force the update. This updates
1302 * the container's perception of rotation and, depending on the top activities, will freeze
1303 * the screen or start seamless rotation. The display itself gets rotated in
1304 * {@link #applyRotationLocked} during {@link WindowManagerService#sendNewConfiguration}.
1305 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001306 * @param forceUpdate Force the rotation update. Sometimes in WM we might skip updating
1307 * orientation because we're waiting for some rotation to finish or display
1308 * to unfreeze, which results in configuration of the previously visible
1309 * activity being applied to a newly visible one. Forcing the rotation
1310 * update allows to workaround this issue.
1311 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
Evan Roskye747c3e2018-10-30 20:06:41 -07001312 * {@link WindowManagerService#sendNewConfiguration(int)} TO COMPLETE THE ROTATION AND
1313 * UNFREEZE THE SCREEN.
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001314 */
1315 boolean updateRotationUnchecked(boolean forceUpdate) {
1316 ScreenRotationAnimation screenRotationAnimation;
1317 if (!forceUpdate) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001318 if (mDeferredRotationPauseCount > 0) {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001319 // Rotation updates have been paused temporarily. Defer the update until
1320 // updates have been resumed.
1321 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
1322 return false;
1323 }
1324
1325 screenRotationAnimation =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001326 mWmService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001327 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
1328 // Rotation updates cannot be performed while the previous rotation change
1329 // animation is still in progress. Skip this update. We will try updating
1330 // again after the animation is finished and the display is unfrozen.
1331 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
1332 return false;
1333 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001334 if (mWmService.mDisplayFrozen) {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001335 // Even if the screen rotation animation has finished (e.g. isAnimating
1336 // returns false), there is still some time where we haven't yet unfrozen
1337 // the display. We also need to abort rotation here.
1338 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
1339 "Deferring rotation, still finishing previous rotation");
1340 return false;
1341 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001342 }
1343
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001344 if (!mWmService.mDisplayEnabled) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001345 // No point choosing a rotation if the display is not enabled.
1346 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
1347 return false;
1348 }
1349
1350 final int oldRotation = mRotation;
1351 final int lastOrientation = mLastOrientation;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001352 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001353 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Computed rotation=" + rotation + " for display id="
1354 + mDisplayId + " based on lastOrientation=" + lastOrientation
1355 + " and oldRotation=" + oldRotation);
Tiger Huang7c610aa2018-10-27 00:01:01 +08001356 boolean mayRotateSeamlessly = mDisplayPolicy.shouldRotateSeamlessly(mDisplayRotation,
Riddle Hsuad256a12018-07-18 16:11:30 +08001357 oldRotation, rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001358
Robert Carr0e007272017-10-02 13:00:55 -07001359 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001360 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001361 if (seamlessRotated != null && !forceUpdate) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001362 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
1363 // to complete (that is, waiting for windows to redraw). It's tempting to check
1364 // w.mSeamlessRotationCount but that could be incorrect in the case of
1365 // window-removal.
1366 return false;
1367 }
Robert Carr0e007272017-10-02 13:00:55 -07001368
1369 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001370 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -07001371 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -07001372 mayRotateSeamlessly = false;
1373 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001374 for (int i = 0; i < mWmService.mSessions.size(); i++) {
1375 if (mWmService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
Robert Carr0e007272017-10-02 13:00:55 -07001376 mayRotateSeamlessly = false;
1377 break;
1378 }
1379 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001380 }
Robert Carr0e007272017-10-02 13:00:55 -07001381 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001382
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001383 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1384 + " selected orientation " + lastOrientation
Garfield Tan49dae102019-02-04 09:51:59 -08001385 + ", got rotation " + rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001386
Garfield Tan4bb83472019-01-16 14:37:04 -08001387 if (oldRotation == rotation) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001388 // No change.
1389 return false;
1390 }
1391
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001392 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1393 + " rotation changed to " + rotation
Garfield Tan4bb83472019-01-16 14:37:04 -08001394 + " from " + oldRotation
1395 + ", lastOrientation=" + lastOrientation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001396
1397 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
Vishnu Nairba183352018-11-21 11:16:49 -08001398 mWaitingForConfig = true;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001399 }
1400
Evan Roskye747c3e2018-10-30 20:06:41 -07001401 mRotation = rotation;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001402
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001403 mWmService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1404 mWmService.mH.sendNewMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
Riddle Hsu654a6f92018-07-13 22:59:36 +08001405 this, WINDOW_FREEZE_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001406
1407 setLayoutNeeded();
1408 final int[] anim = new int[2];
Tiger Huang7c610aa2018-10-27 00:01:01 +08001409 mDisplayPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001410
1411 if (!rotateSeamlessly) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001412 mWmService.startFreezingDisplayLocked(anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001413 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001414 } else {
1415 // The screen rotation animation uses a screenshot to freeze the screen
1416 // while windows resize underneath.
1417 // When we are rotating seamlessly, we allow the elements to transition
1418 // to their rotated state independently and without a freeze required.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001419 mWmService.startSeamlessRotation();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001420 }
1421
Evan Roskye747c3e2018-10-30 20:06:41 -07001422 return true;
1423 }
1424
1425 /**
1426 * Applies the rotation transaction. This must be called after {@link #updateRotationUnchecked}
1427 * (if it returned {@code true}) to actually finish the rotation.
1428 *
1429 * @param oldRotation the rotation we are coming from.
1430 * @param rotation the rotation to apply.
1431 */
1432 void applyRotationLocked(final int oldRotation, final int rotation) {
1433 mDisplayRotation.setRotation(rotation);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001434 final boolean rotateSeamlessly = mWmService.isRotatingSeamlessly();
Evan Roskye747c3e2018-10-30 20:06:41 -07001435 ScreenRotationAnimation screenRotationAnimation = rotateSeamlessly
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001436 ? null : mWmService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001437 // We need to update our screen size information to match the new rotation. If the rotation
1438 // has actually changed then this method will return true and, according to the comment at
1439 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1440 // By updating the Display info here it will be available to
1441 // #computeScreenConfiguration() later.
1442 updateDisplayAndOrientation(getConfiguration().uiMode);
1443
Robert Carrae606b42018-02-15 15:36:23 -08001444 // NOTE: We disable the rotation in the emulator because
1445 // it doesn't support hardware OpenGL emulation yet.
1446 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1447 && screenRotationAnimation.hasScreenshot()) {
1448 if (screenRotationAnimation.setRotation(getPendingTransaction(), rotation,
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001449 MAX_ANIMATION_DURATION, mWmService.getTransitionAnimationScaleLocked(),
Robert Carrae606b42018-02-15 15:36:23 -08001450 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001451 mWmService.scheduleAnimationLocked();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001452 }
1453 }
1454
Vishnu Nair83537a72018-07-19 21:27:48 -07001455 forAllWindows(w -> {
Vishnu Nairddd80742018-08-21 14:12:46 -07001456 w.seamlesslyRotateIfAllowed(getPendingTransaction(), oldRotation, rotation,
1457 rotateSeamlessly);
Vishnu Nair83537a72018-07-19 21:27:48 -07001458 }, true /* traverseTopToBottom */);
1459
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001460 mWmService.mDisplayManagerInternal.performTraversal(getPendingTransaction());
Robert Carrae606b42018-02-15 15:36:23 -08001461 scheduleAnimation();
1462
Andrii Kulian06d07d62017-03-14 11:11:47 -07001463 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001464 if (w.mHasSurface && !rotateSeamlessly) {
1465 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001466 w.setOrientationChanging(true);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001467 mWmService.mRoot.mOrientationChangeComplete = false;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001468 w.mLastFreezeDuration = 0;
1469 }
1470 w.mReportOrientationChanged = true;
1471 }, true /* traverseTopToBottom */);
1472
1473 if (rotateSeamlessly) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001474 mWmService.mH.sendNewMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
Riddle Hsu654a6f92018-07-13 22:59:36 +08001475 this, SEAMLESS_ROTATION_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001476 }
1477
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001478 for (int i = mWmService.mRotationWatchers.size() - 1; i >= 0; i--) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001479 final WindowManagerService.RotationWatcher rotationWatcher
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001480 = mWmService.mRotationWatchers.get(i);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001481 if (rotationWatcher.mDisplayId == mDisplayId) {
1482 try {
1483 rotationWatcher.mWatcher.onRotationChanged(rotation);
1484 } catch (RemoteException e) {
1485 // Ignore
1486 }
1487 }
1488 }
1489
Andrii Kulian06d07d62017-03-14 11:11:47 -07001490 // Announce rotation only if we will not animate as we already have the
1491 // windows in final state. Otherwise, we make this call at the rotation end.
Rhed Jao02655dc2018-10-30 20:44:52 +08001492 if (screenRotationAnimation == null && mWmService.mAccessibilityController != null) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001493 mWmService.mAccessibilityController.onRotationChangedLocked(this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001494 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001495 }
1496
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001497 void configureDisplayPolicy() {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001498 final int width = mBaseDisplayWidth;
1499 final int height = mBaseDisplayHeight;
1500 final int shortSize;
1501 final int longSize;
1502 if (width > height) {
1503 shortSize = height;
1504 longSize = width;
1505 } else {
1506 shortSize = width;
1507 longSize = height;
1508 }
1509
1510 final int shortSizeDp = shortSize * DisplayMetrics.DENSITY_DEFAULT / mBaseDisplayDensity;
1511 final int longSizeDp = longSize * DisplayMetrics.DENSITY_DEFAULT / mBaseDisplayDensity;
1512
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001513 mDisplayPolicy.configure(width, height, shortSizeDp);
Tiger Huang3d2b8982019-01-29 22:56:48 +08001514 mDisplayRotation.configure(width, height, shortSizeDp, longSizeDp);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001515
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001516 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
1517 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001518 }
1519
Andrii Kulian06d07d62017-03-14 11:11:47 -07001520 /**
1521 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1522 * changed.
1523 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1524 */
1525 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1526 // Use the effective "visual" dimensions based on current rotation
1527 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
Garfield Tan4bb83472019-01-16 14:37:04 -08001528 final int dw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1529 final int dh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001530
1531 // Update application display metrics.
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001532 final WmDisplayCutout wmDisplayCutout = calculateDisplayCutoutForRotation(mRotation);
1533 final DisplayCutout displayCutout = wmDisplayCutout.getDisplayCutout();
1534
Tiger Huang7c610aa2018-10-27 00:01:01 +08001535 final int appWidth = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1536 displayCutout);
1537 final int appHeight = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1538 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001539 mDisplayInfo.rotation = mRotation;
1540 mDisplayInfo.logicalWidth = dw;
1541 mDisplayInfo.logicalHeight = dh;
1542 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1543 mDisplayInfo.appWidth = appWidth;
1544 mDisplayInfo.appHeight = appHeight;
1545 if (isDefaultDisplay) {
1546 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1547 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1548 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001549 mDisplayInfo.displayCutout = displayCutout.isEmpty() ? null : displayCutout;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001550 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1551 if (mDisplayScalingDisabled) {
1552 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1553 } else {
1554 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1555 }
1556
Andrii Kulianf0379de2018-03-14 16:24:07 -07001557 // We usually set the override info in DisplayManager so that we get consistent display
1558 // metrics values when displays are changing and don't send out new values until WM is aware
1559 // of them. However, we don't do this for displays that serve as containers for ActivityView
1560 // because we don't want letter-/pillar-boxing during resize.
1561 final DisplayInfo overrideDisplayInfo = mShouldOverrideDisplayConfiguration
1562 ? mDisplayInfo : null;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001563 mWmService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
Andrii Kulianf0379de2018-03-14 16:24:07 -07001564 overrideDisplayInfo);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001565
1566 mBaseDisplayRect.set(0, 0, dw, dh);
1567
1568 if (isDefaultDisplay) {
1569 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1570 mCompatDisplayMetrics);
1571 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001572
Andrii Kulian06d07d62017-03-14 11:11:47 -07001573 return mDisplayInfo;
1574 }
1575
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001576 WmDisplayCutout calculateDisplayCutoutForRotation(int rotation) {
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001577 return mDisplayCutoutCache.getOrCompute(mInitialDisplayCutout, rotation);
1578 }
1579
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001580 private WmDisplayCutout calculateDisplayCutoutForRotationUncached(
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001581 DisplayCutout cutout, int rotation) {
Adrian Roos24264212018-02-19 16:26:15 +01001582 if (cutout == null || cutout == DisplayCutout.NO_CUTOUT) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001583 return WmDisplayCutout.NO_CUTOUT;
Adrian Roos1cf585052018-01-03 18:43:27 +01001584 }
Adrian Roos11c25582018-02-19 18:06:36 +01001585 if (rotation == ROTATION_0) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001586 return WmDisplayCutout.computeSafeInsets(
1587 cutout, mInitialDisplayWidth, mInitialDisplayHeight);
Adrian Roos24264212018-02-19 16:26:15 +01001588 }
Adrian Roosbed538e2018-02-21 17:50:07 +01001589 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Issei Suzuki43190bd2018-08-20 17:28:41 +02001590 final Rect[] newBounds = mRotationUtil.getRotatedBounds(
1591 WmDisplayCutout.computeSafeInsets(
Jorim Jaggi60640512018-06-29 01:14:31 +02001592 cutout, mInitialDisplayWidth, mInitialDisplayHeight)
Issei Suzuki43190bd2018-08-20 17:28:41 +02001593 .getDisplayCutout().getBoundingRectsAll(),
1594 rotation, mInitialDisplayWidth, mInitialDisplayHeight);
1595 return WmDisplayCutout.computeSafeInsets(DisplayCutout.fromBounds(newBounds),
Adrian Roos24264212018-02-19 16:26:15 +01001596 rotated ? mInitialDisplayHeight : mInitialDisplayWidth,
1597 rotated ? mInitialDisplayWidth : mInitialDisplayHeight);
Adrian Roos1cf585052018-01-03 18:43:27 +01001598 }
1599
Andrii Kulian06d07d62017-03-14 11:11:47 -07001600 /**
1601 * Compute display configuration based on display properties and policy settings.
1602 * Do not call if mDisplayReady == false.
1603 */
1604 void computeScreenConfiguration(Configuration config) {
1605 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
Evan Roskye747c3e2018-10-30 20:06:41 -07001606 calculateBounds(displayInfo, mTmpBounds);
1607 config.windowConfiguration.setBounds(mTmpBounds);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001608
1609 final int dw = displayInfo.logicalWidth;
1610 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001611 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
Garfield Tane0846042018-07-26 13:42:04 -07001612 config.windowConfiguration.setWindowingMode(getWindowingMode());
Yunfan Chen7daa6ac2018-11-29 18:16:44 -08001613 config.windowConfiguration.setDisplayWindowingMode(getWindowingMode());
Evan Roskye747c3e2018-10-30 20:06:41 -07001614 config.windowConfiguration.setRotation(displayInfo.rotation);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001615
Adrian Roos11c25582018-02-19 18:06:36 +01001616 final float density = mDisplayMetrics.density;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001617 config.screenWidthDp =
Tiger Huang7c610aa2018-10-27 00:01:01 +08001618 (int)(mDisplayPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1619 config.uiMode, displayInfo.displayCutout) / density);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001620 config.screenHeightDp =
Tiger Huang7c610aa2018-10-27 00:01:01 +08001621 (int)(mDisplayPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1622 config.uiMode, displayInfo.displayCutout) / density);
Bryce Lee7566d762017-03-30 09:34:15 -07001623
Tiger Huang7c610aa2018-10-27 00:01:01 +08001624 mDisplayPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh,
Adrian Roos11c25582018-02-19 18:06:36 +01001625 displayInfo.displayCutout, mTmpRect);
Bryce Lee7566d762017-03-30 09:34:15 -07001626 final int leftInset = mTmpRect.left;
1627 final int topInset = mTmpRect.top;
1628 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001629 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1630 leftInset + displayInfo.appWidth /* right */,
1631 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001632 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1633 || displayInfo.rotation == Surface.ROTATION_270);
1634
Tiger Huang7c610aa2018-10-27 00:01:01 +08001635 computeSizeRangesAndScreenLayout(displayInfo, rotated, config.uiMode, dw, dh, density,
1636 config);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001637
1638 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1639 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1640 ? Configuration.SCREENLAYOUT_ROUND_YES
1641 : Configuration.SCREENLAYOUT_ROUND_NO);
1642
1643 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1644 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1645 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001646 dh, displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001647 config.densityDpi = displayInfo.logicalDensityDpi;
1648
1649 config.colorMode =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001650 ((displayInfo.isHdr() && mWmService.hasHdrSupport())
Andrii Kulian06d07d62017-03-14 11:11:47 -07001651 ? Configuration.COLOR_MODE_HDR_YES
1652 : Configuration.COLOR_MODE_HDR_NO)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001653 | (displayInfo.isWideColorGamut() && mWmService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001654 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1655 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1656
1657 // Update the configuration based on available input devices, lid switch,
1658 // and platform configuration.
1659 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1660 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1661 config.navigation = Configuration.NAVIGATION_NONAV;
1662
1663 int keyboardPresence = 0;
1664 int navigationPresence = 0;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001665 final InputDevice[] devices = mWmService.mInputManager.getInputDevices();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001666 final int len = devices != null ? devices.length : 0;
1667 for (int i = 0; i < len; i++) {
1668 InputDevice device = devices[i];
Arthur Hung82bbfc32018-11-29 20:24:51 +08001669 // Ignore virtual input device.
1670 if (device.isVirtual()) {
1671 continue;
1672 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001673
Arthur Hung82bbfc32018-11-29 20:24:51 +08001674 // Check if input device can dispatch events to current display.
1675 // If display type is virtual, will follow the default display.
1676 if (!mWmService.mInputManager.canDispatchToDisplay(device.getId(),
1677 displayInfo.type == Display.TYPE_VIRTUAL ? DEFAULT_DISPLAY : mDisplayId)) {
1678 continue;
1679 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001680
Arthur Hung82bbfc32018-11-29 20:24:51 +08001681 final int sources = device.getSources();
1682 final int presenceFlag = device.isExternal()
1683 ? WindowManagerPolicy.PRESENCE_EXTERNAL : WindowManagerPolicy.PRESENCE_INTERNAL;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001684
Arthur Hung82bbfc32018-11-29 20:24:51 +08001685 if (mWmService.mIsTouchDevice) {
1686 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) == InputDevice.SOURCE_TOUCHSCREEN) {
1687 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001688 }
Arthur Hung82bbfc32018-11-29 20:24:51 +08001689 } else {
1690 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1691 }
1692
1693 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1694 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1695 navigationPresence |= presenceFlag;
1696 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1697 && config.navigation == Configuration.NAVIGATION_NONAV) {
1698 config.navigation = Configuration.NAVIGATION_DPAD;
1699 navigationPresence |= presenceFlag;
1700 }
1701
1702 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1703 config.keyboard = Configuration.KEYBOARD_QWERTY;
1704 keyboardPresence |= presenceFlag;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001705 }
1706 }
1707
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001708 if (config.navigation == Configuration.NAVIGATION_NONAV && mWmService.mHasPermanentDpad) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001709 config.navigation = Configuration.NAVIGATION_DPAD;
1710 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1711 }
1712
1713 // Determine whether a hard keyboard is available and enabled.
1714 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1715 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001716 if (hardKeyboardAvailable != mWmService.mHardKeyboardAvailable) {
1717 mWmService.mHardKeyboardAvailable = hardKeyboardAvailable;
1718 mWmService.mH.removeMessages(REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1719 mWmService.mH.sendEmptyMessage(REPORT_HARD_KEYBOARD_STATUS_CHANGE);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001720 }
1721
Tiger Huang7c610aa2018-10-27 00:01:01 +08001722 mDisplayPolicy.updateConfigurationDependentBehaviors();
1723
Andrii Kulian06d07d62017-03-14 11:11:47 -07001724 // Let the policy update hidden states.
1725 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1726 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1727 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001728 mWmService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001729 }
1730
1731 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001732 DisplayCutout displayCutout) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001733 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1734 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1735 final int unrotDw, unrotDh;
1736 if (rotated) {
1737 unrotDw = dh;
1738 unrotDh = dw;
1739 } else {
1740 unrotDw = dw;
1741 unrotDh = dh;
1742 }
1743 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001744 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001745 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001746 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001747 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001748 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001749 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001750 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001751 return sw;
1752 }
1753
1754 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001755 DisplayMetrics dm, int dw, int dh, DisplayCutout displayCutout) {
1756 dm.noncompatWidthPixels = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1757 displayCutout);
1758 dm.noncompatHeightPixels = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode,
1759 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001760 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1761 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1762 if (curSize == 0 || size < curSize) {
1763 curSize = size;
1764 }
1765 return curSize;
1766 }
1767
Tiger Huang7c610aa2018-10-27 00:01:01 +08001768 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, boolean rotated,
1769 int uiMode, int dw, int dh, float density, Configuration outConfig) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001770
1771 // We need to determine the smallest width that will occur under normal
1772 // operation. To this, start with the base screen size and compute the
1773 // width under the different possible rotations. We need to un-rotate
1774 // the current screen dimensions before doing this.
1775 int unrotDw, unrotDh;
1776 if (rotated) {
1777 unrotDw = dh;
1778 unrotDh = dw;
1779 } else {
1780 unrotDw = dw;
1781 unrotDh = dh;
1782 }
1783 displayInfo.smallestNominalAppWidth = 1<<30;
1784 displayInfo.smallestNominalAppHeight = 1<<30;
1785 displayInfo.largestNominalAppWidth = 0;
1786 displayInfo.largestNominalAppHeight = 0;
Tiger Huang7c610aa2018-10-27 00:01:01 +08001787 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_0, uiMode, unrotDw, unrotDh);
1788 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_90, uiMode, unrotDh, unrotDw);
1789 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_180, uiMode, unrotDw, unrotDh);
1790 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_270, uiMode, unrotDh, unrotDw);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001791 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1792 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001793 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001794 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001795 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001796 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001797 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001798 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001799 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001800 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1801 outConfig.screenLayout = sl;
1802 }
1803
1804 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001805 int uiMode, DisplayCutout displayCutout) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001806 // Get the app screen size at this rotation.
Tiger Huang7c610aa2018-10-27 00:01:01 +08001807 int w = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayCutout);
1808 int h = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001809
1810 // Compute the screen layout size class for this rotation.
1811 int longSize = w;
1812 int shortSize = h;
1813 if (longSize < shortSize) {
1814 int tmp = longSize;
1815 longSize = shortSize;
1816 shortSize = tmp;
1817 }
1818 longSize = (int)(longSize/density);
1819 shortSize = (int)(shortSize/density);
1820 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1821 }
1822
Tiger Huang7c610aa2018-10-27 00:01:01 +08001823 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int rotation,
Andrii Kulian06d07d62017-03-14 11:11:47 -07001824 int uiMode, int dw, int dh) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001825 final DisplayCutout displayCutout = calculateDisplayCutoutForRotation(
1826 rotation).getDisplayCutout();
Tiger Huang7c610aa2018-10-27 00:01:01 +08001827 final int width = mDisplayPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1828 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001829 if (width < displayInfo.smallestNominalAppWidth) {
1830 displayInfo.smallestNominalAppWidth = width;
1831 }
1832 if (width > displayInfo.largestNominalAppWidth) {
1833 displayInfo.largestNominalAppWidth = width;
1834 }
Tiger Huang7c610aa2018-10-27 00:01:01 +08001835 final int height = mDisplayPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1836 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001837 if (height < displayInfo.smallestNominalAppHeight) {
1838 displayInfo.smallestNominalAppHeight = height;
1839 }
1840 if (height > displayInfo.largestNominalAppHeight) {
1841 displayInfo.largestNominalAppHeight = height;
1842 }
1843 }
1844
Riddle Hsua4d6fa22018-08-11 00:50:39 +08001845 /**
1846 * Apps that use the compact menu panel (as controlled by the panelMenuIsCompact
1847 * theme attribute) on devices that feature a physical options menu key attempt to position
1848 * their menu panel window along the edge of the screen nearest the physical menu key.
1849 * This lowers the travel distance between invoking the menu panel and selecting
1850 * a menu option.
1851 *
1852 * This method helps control where that menu is placed. Its current implementation makes
1853 * assumptions about the menu key and its relationship to the screen based on whether
1854 * the device's natural orientation is portrait (width < height) or landscape.
1855 *
1856 * The menu key is assumed to be located along the bottom edge of natural-portrait
1857 * devices and along the right edge of natural-landscape devices. If these assumptions
1858 * do not hold for the target device, this method should be changed to reflect that.
1859 *
1860 * @return A {@link Gravity} value for placing the options menu window.
1861 */
1862 int getPreferredOptionsPanelGravity() {
1863 final int rotation = getRotation();
1864 if (mInitialDisplayWidth < mInitialDisplayHeight) {
1865 // On devices with a natural orientation of portrait.
1866 switch (rotation) {
1867 default:
1868 case Surface.ROTATION_0:
1869 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1870 case Surface.ROTATION_90:
1871 return Gravity.RIGHT | Gravity.BOTTOM;
1872 case Surface.ROTATION_180:
1873 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1874 case Surface.ROTATION_270:
1875 return Gravity.START | Gravity.BOTTOM;
1876 }
1877 }
1878
1879 // On devices with a natural orientation of landscape.
1880 switch (rotation) {
1881 default:
1882 case Surface.ROTATION_0:
1883 return Gravity.RIGHT | Gravity.BOTTOM;
1884 case Surface.ROTATION_90:
1885 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1886 case Surface.ROTATION_180:
1887 return Gravity.START | Gravity.BOTTOM;
1888 case Surface.ROTATION_270:
1889 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1890 }
1891 }
1892
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001893 DockedStackDividerController getDockedDividerController() {
1894 return mDividerControllerLocked;
1895 }
1896
Winson Chung655332c2016-10-31 13:14:28 -07001897 PinnedStackController getPinnedStackController() {
1898 return mPinnedStackControllerLocked;
1899 }
1900
Jeff Browna506a6e2013-06-04 00:02:38 -07001901 /**
1902 * Returns true if the specified UID has access to this display.
1903 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001904 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001905 return mDisplay.hasAccess(uid);
1906 }
1907
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001908 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001909 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001910 }
1911
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001912 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001913 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001914 }
1915
Wale Ogunwale61911492017-10-11 08:50:50 -07001916 /**
1917 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1918 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001919 TaskStack getSplitScreenPrimaryStack() {
1920 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001921 return (stack != null && stack.isVisible()) ? stack : null;
1922 }
1923
Robert Carr9785cf32018-04-25 15:06:07 -07001924 boolean hasSplitScreenPrimaryStack() {
1925 return getSplitScreenPrimaryStack() != null;
1926 }
1927
Wale Ogunwale61911492017-10-11 08:50:50 -07001928 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001929 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001930 * not visible.
1931 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001932 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1933 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001934 }
1935
1936 TaskStack getPinnedStack() {
1937 return mTaskStackContainers.getPinnedStack();
1938 }
1939
1940 private boolean hasPinnedStack() {
1941 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001942 }
1943
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001944 /**
1945 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1946 * Null is no compatible stack on the display.
1947 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001948 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001949 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1950 }
1951
1952 /**
1953 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1954 * activity type. Null is no compatible stack on the display.
1955 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001956 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001957 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001958 }
1959
Bryce Lee48f4b572017-04-10 10:54:15 -07001960 @VisibleForTesting
Kazuki Takise148d00a2018-05-31 15:32:19 +09001961 WindowList<TaskStack> getStacks() {
1962 return mTaskStackContainers.mChildren;
1963 }
1964
1965 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001966 TaskStack getTopStack() {
1967 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001968 }
1969
Winson Chunge2d72172018-01-25 17:46:20 +00001970 ArrayList<Task> getVisibleTasks() {
1971 return mTaskStackContainers.getVisibleTasks();
1972 }
1973
Wale Ogunwale61911492017-10-11 08:50:50 -07001974 void onStackWindowingModeChanged(TaskStack stack) {
1975 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001976 }
1977
Andrii Kulian441e4492016-09-29 15:25:00 -07001978 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001979 public void onConfigurationChanged(Configuration newParentConfig) {
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -05001980 final int lastOrientation = getConfiguration().orientation;
Andrii Kulian441e4492016-09-29 15:25:00 -07001981 super.onConfigurationChanged(newParentConfig);
Tiger Huang7c610aa2018-10-27 00:01:01 +08001982 if (mDisplayPolicy != null) {
1983 mDisplayPolicy.onConfigurationChanged();
1984 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001985
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -05001986 if (lastOrientation != getConfiguration().orientation) {
1987 getMetricsLogger().write(
1988 new LogMaker(MetricsEvent.ACTION_PHONE_ORIENTATION_CHANGED)
1989 .setSubtype(getConfiguration().orientation)
1990 .addTaggedData(MetricsEvent.FIELD_DISPLAY_ID, getDisplayId()));
1991 }
1992
Evan Roskye747c3e2018-10-30 20:06:41 -07001993 // If there was no pinned stack, we still need to notify the controller of the display info
1994 // update as a result of the config change.
1995 if (mPinnedStackControllerLocked != null && !hasPinnedStack()) {
1996 mPinnedStackControllerLocked.onDisplayInfoChanged(getDisplayInfo());
1997 }
Evan Roskye747c3e2018-10-30 20:06:41 -07001998 }
1999
2000 /**
2001 * Updates the resources used by docked/pinned controllers. This needs to be called at the
2002 * beginning of a configuration update cascade since the metrics from these resources are used
2003 * for bounds calculations. Since ActivityDisplay initiates the configuration update, this
2004 * should be called from there instead of DisplayContent's onConfigurationChanged.
2005 */
2006 void preOnConfigurationChanged() {
Bryce Leef3c6a472017-11-14 14:53:06 -08002007 final DockedStackDividerController dividerController = getDockedDividerController();
2008
2009 if (dividerController != null) {
2010 getDockedDividerController().onConfigurationChanged();
2011 }
2012
2013 final PinnedStackController pinnedStackController = getPinnedStackController();
2014
2015 if (pinnedStackController != null) {
2016 getPinnedStackController().onConfigurationChanged();
2017 }
Andrii Kulian441e4492016-09-29 15:25:00 -07002018 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07002019
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07002020 @Override
2021 boolean fillsParent() {
2022 return true;
2023 }
2024
2025 @Override
2026 boolean isVisible() {
2027 return true;
2028 }
2029
2030 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002031 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002032 super.onAppTransitionDone();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002033 mWmService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002034 }
2035
Yunfan Chen7daa6ac2018-11-29 18:16:44 -08002036 @Override
2037 public void setWindowingMode(int windowingMode) {
2038 super.setWindowingMode(windowingMode);
2039 super.setDisplayWindowingMode(windowingMode);
2040 }
2041
2042 @Override
2043 void setDisplayWindowingMode(int windowingMode) {
2044 setWindowingMode(windowingMode);
2045 }
2046
Robert Carr9785cf32018-04-25 15:06:07 -07002047 /**
2048 * In split-screen mode we process the IME containers above the docked divider
2049 * rather than directly above their target.
2050 */
2051 private boolean skipTraverseChild(WindowContainer child) {
lumarkff0ab692018-11-05 20:32:30 +08002052 if (child == mImeWindowsContainers && mInputMethodTarget != null
Robert Carr9785cf32018-04-25 15:06:07 -07002053 && !hasSplitScreenPrimaryStack()) {
2054 return true;
2055 }
2056 return false;
2057 }
2058
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07002059 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08002060 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
2061 // Special handling so we can process IME windows with #forAllImeWindows above their IME
2062 // target, or here in order if there isn't an IME target.
2063 if (traverseTopToBottom) {
2064 for (int i = mChildren.size() - 1; i >= 0; --i) {
2065 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07002066 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08002067 continue;
2068 }
Robert Carr9785cf32018-04-25 15:06:07 -07002069
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08002070 if (child.forAllWindows(callback, traverseTopToBottom)) {
2071 return true;
2072 }
2073 }
2074 } else {
2075 final int count = mChildren.size();
2076 for (int i = 0; i < count; i++) {
2077 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07002078 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08002079 continue;
2080 }
Robert Carr9785cf32018-04-25 15:06:07 -07002081
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08002082 if (child.forAllWindows(callback, traverseTopToBottom)) {
2083 return true;
2084 }
2085 }
2086 }
2087 return false;
2088 }
2089
2090 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
2091 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
2092 }
2093
Wale Ogunwale399c8692017-05-08 14:22:42 -07002094 @Override
2095 int getOrientation() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002096 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002097
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002098 if (mWmService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08002099 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07002100 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
2101 + " is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002102 // If the display is frozen, some activities may be in the middle of restarting, and
2103 // thus have removed their old window. If the window has the flag to hide the lock
2104 // screen, then the lock screen can re-appear and inflict its own orientation on us.
2105 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08002106 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002107 } else if (policy.isKeyguardLocked()) {
2108 // Use the last orientation the while the display is frozen with the keyguard
2109 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
2110 // window. We don't want to check the show when locked window directly though as
2111 // things aren't stable while the display is frozen, for example the window could be
2112 // momentarily unavailable due to activity relaunch.
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07002113 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
2114 + " is frozen while keyguard locked, return " + mLastOrientation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08002115 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07002116 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002117 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002118 final int orientation = mAboveAppWindowsContainers.getOrientation();
2119 if (orientation != SCREEN_ORIENTATION_UNSET) {
2120 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002121 }
Wale Ogunwale51362492016-09-08 17:49:17 -07002122 }
2123
Wale Ogunwale399c8692017-05-08 14:22:42 -07002124 // Top system windows are not requesting an orientation. Start searching from apps.
2125 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07002126 }
2127
Craig Mautner46ac6fa2013-08-01 10:06:34 -07002128 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07002129 // Check if display metrics changed and update base values if needed.
2130 updateBaseDisplayMetricsIfNeeded();
2131
Craig Mautner722285e2012-09-07 13:55:58 -07002132 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07002133 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07002134
Garfield Tan2f145f22018-11-01 15:27:03 -07002135 onDisplayChanged(this);
Craig Mautner722285e2012-09-07 13:55:58 -07002136 }
2137
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002138 void initializeDisplayBaseInfo() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002139 final DisplayManagerInternal displayManagerInternal = mWmService.mDisplayManagerInternal;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002140 if (displayManagerInternal != null) {
2141 // Bootstrap the default logical display from the display manager.
2142 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
2143 if (newDisplayInfo != null) {
2144 mDisplayInfo.copyFrom(newDisplayInfo);
2145 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002146 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002147
Andrii Kuliancd097992017-03-23 18:31:59 -07002148 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
2149 mDisplayInfo.logicalDensityDpi);
2150 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
2151 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
2152 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002153 mInitialDisplayCutout = mDisplayInfo.displayCutout;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002154 }
2155
Andrii Kuliancd097992017-03-23 18:31:59 -07002156 /**
2157 * If display metrics changed, overrides are not set and it's not just a rotation - update base
2158 * values.
2159 */
2160 private void updateBaseDisplayMetricsIfNeeded() {
2161 // Get real display metrics without overrides from WM.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002162 mWmService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
Andrii Kuliancd097992017-03-23 18:31:59 -07002163 final int orientation = mDisplayInfo.rotation;
2164 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2165 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
2166 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
2167 final int newDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002168 final DisplayCutout newCutout = mDisplayInfo.displayCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07002169
2170 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
2171 || mInitialDisplayHeight != newHeight
Adrian Roos1cf585052018-01-03 18:43:27 +01002172 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi
2173 || !Objects.equals(mInitialDisplayCutout, newCutout);
Andrii Kuliancd097992017-03-23 18:31:59 -07002174
2175 if (displayMetricsChanged) {
2176 // Check if display size or density is forced.
2177 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
2178 || mBaseDisplayHeight != mInitialDisplayHeight;
2179 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
2180
2181 // If there is an override set for base values - use it, otherwise use new values.
2182 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
2183 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
2184 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
2185
2186 // Real display metrics changed, so we should also update initial values.
2187 mInitialDisplayWidth = newWidth;
2188 mInitialDisplayHeight = newHeight;
2189 mInitialDisplayDensity = newDensity;
Adrian Roos1cf585052018-01-03 18:43:27 +01002190 mInitialDisplayCutout = newCutout;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002191 mWmService.reconfigureDisplayLocked(this);
Andrii Kuliancd097992017-03-23 18:31:59 -07002192 }
2193 }
2194
Bryce Lee27cec322017-03-21 09:41:37 -07002195 /** Sets the maximum width the screen resolution can be */
2196 void setMaxUiWidth(int width) {
2197 if (DEBUG_DISPLAY) {
2198 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
2199 }
2200
2201 mMaxUiWidth = width;
2202
2203 // Update existing metrics.
2204 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
2205 }
2206
2207 /** Update base (override) display metrics. */
2208 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
2209 mBaseDisplayWidth = baseWidth;
2210 mBaseDisplayHeight = baseHeight;
2211 mBaseDisplayDensity = baseDensity;
2212
2213 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
2214 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
2215 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
2216 mBaseDisplayWidth = mMaxUiWidth;
2217
2218 if (DEBUG_DISPLAY) {
2219 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
2220 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
2221 + " on display:" + getDisplayId());
2222 }
2223 }
2224
2225 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08002226
2227 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07002228 }
2229
Riddle Hsuf53da812018-08-15 22:00:27 +08002230 /**
2231 * Forces this display to use the specified density.
2232 *
2233 * @param density The density in DPI to use. If the value equals to initial density, the setting
2234 * will be cleared.
2235 * @param userId The target user to apply. Only meaningful when this is default display. If the
2236 * user id is {@link UserHandle#USER_CURRENT}, it means to apply current settings
2237 * so only need to configure display.
2238 */
2239 void setForcedDensity(int density, int userId) {
2240 final boolean clear = density == mInitialDisplayDensity;
2241 final boolean updateCurrent = userId == UserHandle.USER_CURRENT;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002242 if (mWmService.mCurrentUserId == userId || updateCurrent) {
Riddle Hsuf53da812018-08-15 22:00:27 +08002243 mBaseDisplayDensity = density;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002244 mWmService.reconfigureDisplayLocked(this);
Riddle Hsuf53da812018-08-15 22:00:27 +08002245 }
2246 if (updateCurrent) {
2247 // We are applying existing settings so no need to save it again.
2248 return;
2249 }
2250
2251 if (density == mInitialDisplayDensity) {
2252 density = 0;
2253 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002254 mWmService.mDisplayWindowSettings.setForcedDensity(this, density, userId);
Riddle Hsuf53da812018-08-15 22:00:27 +08002255 }
2256
2257 /** @param mode {@link #FORCE_SCALING_MODE_AUTO} or {@link #FORCE_SCALING_MODE_DISABLED}. */
2258 void setForcedScalingMode(@ForceScalingMode int mode) {
2259 if (mode != FORCE_SCALING_MODE_DISABLED) {
2260 mode = FORCE_SCALING_MODE_AUTO;
2261 }
2262
2263 mDisplayScalingDisabled = (mode != FORCE_SCALING_MODE_AUTO);
2264 Slog.i(TAG_WM, "Using display scaling mode: " + (mDisplayScalingDisabled ? "off" : "auto"));
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002265 mWmService.reconfigureDisplayLocked(this);
Riddle Hsuf53da812018-08-15 22:00:27 +08002266
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002267 mWmService.mDisplayWindowSettings.setForcedScalingMode(this, mode);
Riddle Hsuf53da812018-08-15 22:00:27 +08002268 }
2269
2270 /** If the given width and height equal to initial size, the setting will be cleared. */
2271 void setForcedSize(int width, int height) {
2272 final boolean clear = mInitialDisplayWidth == width && mInitialDisplayHeight == height;
2273 if (!clear) {
2274 // Set some sort of reasonable bounds on the size of the display that we will try
2275 // to emulate.
2276 final int minSize = 200;
2277 final int maxScale = 2;
2278 width = Math.min(Math.max(width, minSize), mInitialDisplayWidth * maxScale);
2279 height = Math.min(Math.max(height, minSize), mInitialDisplayHeight * maxScale);
2280 }
2281
2282 Slog.i(TAG_WM, "Using new display size: " + width + "x" + height);
2283 updateBaseDisplayMetrics(width, height, mBaseDisplayDensity);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002284 mWmService.reconfigureDisplayLocked(this);
Riddle Hsuf53da812018-08-15 22:00:27 +08002285
2286 if (clear) {
2287 width = height = 0;
2288 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002289 mWmService.mDisplayWindowSettings.setForcedSize(this, width, height);
Riddle Hsuf53da812018-08-15 22:00:27 +08002290 }
2291
Wale Ogunwaledb506192017-12-08 10:57:32 -08002292 void getStableRect(Rect out) {
2293 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08002294 }
2295
Yunfan Chen279f5582018-12-12 15:24:50 -08002296 void setStackOnDisplay(int stackId, boolean onTop, TaskStack stack) {
2297 if (DEBUG_STACK) {
2298 Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId=" + mDisplayId);
2299 }
Andrii Kulian839def92016-11-02 10:58:58 -07002300
Wale Ogunwale61911492017-10-11 08:50:50 -07002301 mTaskStackContainers.addStackToDisplay(stack, onTop);
Andrii Kulian839def92016-11-02 10:58:58 -07002302 }
2303
Andrii Kulian51c1b672017-04-07 18:39:32 -07002304 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002305 final DisplayContent prevDc = stack.getDisplayContent();
2306 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07002307 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
2308 + " which is not currently attached to any display");
2309 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002310 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07002311 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
2312 + " to its current displayId=" + mDisplayId);
2313 }
2314
lumark588a3e82018-07-20 18:53:54 +08002315 // Clean up all pending transitions when stack reparent to another display.
2316 stack.forAllAppWindows(AppWindowToken::removeFromPendingTransition);
2317
Wale Ogunwale61911492017-10-11 08:50:50 -07002318 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07002319 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08002320 }
2321
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002322 @Override
2323 protected void addChild(DisplayChildWindowContainer child,
2324 Comparator<DisplayChildWindowContainer> comparator) {
2325 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2326 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002327
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002328 @Override
2329 protected void addChild(DisplayChildWindowContainer child, int index) {
2330 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2331 }
2332
2333 @Override
2334 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002335 // Only allow removal of direct children from this display if the display is in the process
2336 // of been removed.
2337 if (mRemovingDisplay) {
2338 super.removeChild(child);
2339 return;
2340 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002341 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002342 }
2343
Andrii Kuliand2765632016-12-12 22:26:34 -08002344 @Override
2345 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
2346 // Children of the display are statically ordered, so the real intention here is to perform
2347 // the operation on the display and not the static direct children.
2348 getParent().positionChildAt(position, this, includingParents);
2349 }
2350
Riddle Hsu57831b52018-07-27 00:31:48 +08002351 void positionStackAt(int position, TaskStack child, boolean includingParents) {
2352 mTaskStackContainers.positionChildAt(position, child, includingParents);
Winson Chung59a47ded2018-01-25 17:46:06 +00002353 layoutAndAssignWindowLayersIfNeeded();
2354 }
2355
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002356 /**
2357 * Used to obtain task ID when user taps on coordinate (x, y) in this display, and outside
2358 * current task in focus.
2359 *
2360 * This returns the task ID of the foremost task at (x, y) if the task is not home. Otherwise it
2361 * returns -1.
2362 *
2363 * @param x horizontal coordinate of the tap position
2364 * @param y vertical coordinate of the tap position
2365 * @return the task ID if a non-home task is found; -1 if not
2366 */
2367 int taskForTapOutside(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002368 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2369 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Garfield Tanf2e0a9e2018-10-16 09:42:02 -07002370 if (stack.isActivityTypeHome() && !stack.inMultiWindowMode()) {
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002371 // We skip not only home stack, but also everything behind home because user can't
Garfield Tanf2e0a9e2018-10-16 09:42:02 -07002372 // see them when home stack is isn't in multi-window mode.
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002373 break;
2374 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002375 final int taskId = stack.taskIdFromPoint(x, y);
2376 if (taskId != -1) {
2377 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07002378 }
2379 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002380 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07002381 }
2382
Chong Zhang8e89b312015-09-09 15:09:30 -07002383 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08002384 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07002385 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07002386 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07002387 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002388 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002389 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07002390 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2391 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002392 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002393 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002394 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002395
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002396 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
2397 if (mTmpTaskForResizePointSearchResult.searchDone) {
2398 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07002399 }
2400 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002401 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002402 }
2403
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002404 void updateTouchExcludeRegion() {
2405 final Task focusedTask = (mFocusedApp != null ? mFocusedApp.getTask() : null);
David Stevensee9e2772017-02-09 16:30:27 -08002406 if (focusedTask == null) {
2407 mTouchExcludeRegion.setEmpty();
2408 } else {
2409 mTouchExcludeRegion.set(mBaseDisplayRect);
2410 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
2411 mTmpRect2.setEmpty();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002412 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0;
2413 --stackNdx) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002414 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002415 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
2416 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08002417 }
2418 // If we removed the focused task above, add it back and only leave its
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002419 // outside touch area in the exclusion. TapDetector is not interested in
David Stevensee9e2772017-02-09 16:30:27 -08002420 // any touch inside the focused task itself.
2421 if (!mTmpRect2.isEmpty()) {
2422 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
2423 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08002424 }
lumark90120a82018-08-15 00:33:03 +08002425 if (mInputMethodWindow != null && mInputMethodWindow.isVisibleLw()) {
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002426 // If the input method is visible and the user is typing, we don't want these touch
2427 // events to be intercepted and used to change focus. This would likely cause a
2428 // disappearance of the input method.
lumark90120a82018-08-15 00:33:03 +08002429 mInputMethodWindow.getTouchableRegion(mTmpRegion);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002430 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002431 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002432 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002433 final WindowState win = mTapExcludedWindows.get(i);
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002434 win.getTouchableRegion(mTmpRegion);
2435 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
2436 }
Tiger Huang04dc4cc2019-01-17 18:41:41 +08002437 amendWindowTapExcludeRegion(mTouchExcludeRegion);
Andrii Kulian03c403d2016-11-11 11:14:12 -08002438 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07002439 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002440 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07002441 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002442 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
2443 }
Riddle Hsu2588ab02019-02-25 14:23:56 +08002444 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner6601b7b2013-04-29 10:29:11 -07002445 }
2446
Tiger Huang04dc4cc2019-01-17 18:41:41 +08002447 /**
2448 * Union the region with all the tap exclude region provided by windows on this display.
2449 *
2450 * @param inOutRegion The region to be amended.
2451 */
2452 void amendWindowTapExcludeRegion(Region inOutRegion) {
2453 for (int i = mTapExcludeProvidingWindows.size() - 1; i >= 0; i--) {
2454 final WindowState win = mTapExcludeProvidingWindows.valueAt(i);
2455 win.amendTapExcludeRegion(inOutRegion);
2456 }
2457 }
2458
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002459 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07002460 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002461 super.switchUser();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002462 mWmService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07002463 }
2464
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07002465 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002466 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2467 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07002468 }
2469 }
2470
Wale Ogunwale10124582016-09-15 20:25:50 -07002471 @Override
2472 void removeIfPossible() {
2473 if (isAnimating()) {
2474 mDeferredRemoval = true;
2475 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07002476 }
Wale Ogunwale10124582016-09-15 20:25:50 -07002477 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07002478 }
2479
Wale Ogunwale10124582016-09-15 20:25:50 -07002480 @Override
2481 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002482 mRemovingDisplay = true;
2483 try {
lumark588a3e82018-07-20 18:53:54 +08002484 // Clear all transitions & screen frozen states when removing display.
2485 mOpeningApps.clear();
2486 mClosingApps.clear();
Evan Rosky2289ba12018-11-19 18:28:18 -08002487 mChangingApps.clear();
lumark588a3e82018-07-20 18:53:54 +08002488 mUnknownAppVisibilityController.clear();
2489 mAppTransition.removeAppTransitionTimeoutCallbacks();
2490 handleAnimatingStoppedAndTransition();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002491 mWmService.stopFreezingDisplayLocked();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002492 super.removeImmediately();
2493 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Riddle Hsu2588ab02019-02-25 14:23:56 +08002494 mPointerEventDispatcher.dispose();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002495 mWmService.mAnimator.removeDisplayLocked(mDisplayId);
Louis Chang207f9112018-07-30 08:58:54 +08002496 mWindowingLayer.release();
2497 mOverlayLayer.release();
Jorim Jaggiae962e62018-12-27 17:23:48 +01002498 mInputMonitor.onDisplayRemoved();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002499 } finally {
Riddle Hsu4e611772018-10-31 18:58:28 +08002500 mDisplayReady = false;
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002501 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08002502 }
Robert Carr679ccb02018-08-08 15:32:35 -07002503
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002504 mWmService.mWindowPlacerLocked.requestTraversal();
Craig Mautner95da1082014-02-24 17:54:35 -08002505 }
2506
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002507 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07002508 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002509 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002510 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
2511
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002512 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002513 removeImmediately();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002514 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08002515 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002516 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08002517 }
2518
Andrii Kulian0214ed92017-05-16 13:44:05 -07002519 /** @return 'true' if removal of this display content is deferred due to active animation. */
2520 boolean isRemovalDeferred() {
2521 return mDeferredRemoval;
2522 }
2523
Wale Ogunwale10124582016-09-15 20:25:50 -07002524 boolean animateForIme(float interpolatedValue, float animationTarget,
2525 float dividerAnimationTarget) {
2526 boolean updated = false;
2527
Wale Ogunwale61911492017-10-11 08:50:50 -07002528 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2529 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002530 if (stack == null || !stack.isAdjustedForIme()) {
2531 continue;
2532 }
2533
2534 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
2535 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2536 updated = true;
2537 } else {
2538 mDividerControllerLocked.mLastAnimationProgress =
2539 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
2540 mDividerControllerLocked.mLastDividerProgress =
2541 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
2542 updated |= stack.updateAdjustForIme(
2543 mDividerControllerLocked.mLastAnimationProgress,
2544 mDividerControllerLocked.mLastDividerProgress,
2545 false /* force */);
2546 }
2547 if (interpolatedValue >= 1f) {
2548 stack.endImeAdjustAnimation();
2549 }
2550 }
2551
2552 return updated;
2553 }
2554
2555 boolean clearImeAdjustAnimation() {
2556 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002557 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2558 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002559 if (stack != null && stack.isAdjustedForIme()) {
2560 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2561 changed = true;
2562 }
2563 }
2564 return changed;
2565 }
2566
2567 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002568 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2569 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002570 if (stack.isVisible() && stack.isAdjustedForIme()) {
2571 stack.beginImeAdjustAnimation();
2572 }
2573 }
2574 }
2575
2576 void adjustForImeIfNeeded() {
lumark90120a82018-08-15 00:33:03 +08002577 final WindowState imeWin = mInputMethodWindow;
Wale Ogunwale10124582016-09-15 20:25:50 -07002578 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2579 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002580 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002581 final TaskStack imeTargetStack = mWmService.getImeFocusStackLocked();
Wale Ogunwale10124582016-09-15 20:25:50 -07002582 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2583 imeTargetStack.getDockSide() : DOCKED_INVALID;
2584 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2585 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2586 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002587 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002588 final boolean imeHeightChanged = imeVisible &&
2589 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2590
2591 // The divider could be adjusted for IME position, or be thinner than usual,
2592 // or both. There are three possible cases:
2593 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2594 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2595 // - If IME is not visible, divider is not moved and is normal width.
2596
2597 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002598 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2599 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002600 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002601 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2602 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002603 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2604 } else {
2605 stack.resetAdjustedForIme(false);
2606 }
2607 }
2608 mDividerControllerLocked.setAdjustedForIme(
2609 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2610 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002611 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2612 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002613 stack.resetAdjustedForIme(!dockVisible);
2614 }
2615 mDividerControllerLocked.setAdjustedForIme(
2616 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2617 }
Winson Chung655332c2016-10-31 13:14:28 -07002618 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002619 }
2620
2621 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002622 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2623 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002624 stack.prepareFreezingTaskBounds();
2625 }
2626 }
2627
Wale Ogunwale94744212015-09-21 19:01:47 -07002628 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002629 getBounds(mTmpRect, newRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002630 rotateBounds(mTmpRect, oldRotation, newRotation, bounds);
2631 }
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002632
Evan Rosky39b6f232018-10-30 18:35:41 -07002633 void rotateBounds(Rect parentBounds, int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002634 // Compute a transform matrix to undo the coordinate space transformation,
2635 // and present the window at the same physical position it previously occupied.
2636 final int deltaRotation = deltaRotation(newRotation, oldRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002637 createRotationMatrix(
2638 deltaRotation, parentBounds.width(), parentBounds.height(), mTmpMatrix);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002639
2640 mTmpRectF.set(bounds);
2641 mTmpMatrix.mapRect(mTmpRectF);
2642 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002643 }
2644
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002645 static int deltaRotation(int oldRotation, int newRotation) {
2646 int delta = newRotation - oldRotation;
2647 if (delta < 0) delta += 4;
2648 return delta;
2649 }
2650
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002651 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002652 Matrix outMatrix) {
2653 // For rotations without Z-ordering we don't need the target rectangle's position.
2654 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2655 displayHeight, outMatrix);
2656 }
2657
2658 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2659 float displayWidth, float displayHeight, Matrix outMatrix) {
2660 switch (rotation) {
2661 case ROTATION_0:
2662 outMatrix.reset();
2663 break;
2664 case ROTATION_270:
2665 outMatrix.setRotate(270, 0, 0);
2666 outMatrix.postTranslate(0, displayHeight);
2667 outMatrix.postTranslate(rectTop, 0);
2668 break;
2669 case ROTATION_180:
2670 outMatrix.reset();
2671 break;
2672 case ROTATION_90:
2673 outMatrix.setRotate(90, 0, 0);
2674 outMatrix.postTranslate(displayWidth, 0);
2675 outMatrix.postTranslate(-rectTop, rectLeft);
2676 break;
2677 }
2678 }
2679
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002680 @CallSuper
2681 @Override
Nataniel Borges023ecb52019-01-16 14:15:43 -08002682 public void writeToProto(ProtoOutputStream proto, long fieldId,
2683 @WindowTraceLogLevel int logLevel) {
2684 // Critical log level logs only visible elements to mitigate performance overheard
2685 if (logLevel == WindowTraceLogLevel.CRITICAL && !isVisible()) {
2686 return;
2687 }
2688
Steven Timotiusaf03df62017-07-18 16:56:43 -07002689 final long token = proto.start(fieldId);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002690 super.writeToProto(proto, WINDOW_CONTAINER, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002691 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002692 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2693 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002694 stack.writeToProto(proto, STACKS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002695 }
2696 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2697 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002698 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2699 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002700 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002701 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002702 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2703 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002704 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002705 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002706 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2707 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Nataniel Borges023ecb52019-01-16 14:15:43 -08002708 windowToken.writeToProto(proto, IME_WINDOWS, logLevel);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002709 }
2710 proto.write(DPI, mBaseDisplayDensity);
2711 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002712 proto.write(ROTATION, mRotation);
2713 final ScreenRotationAnimation screenRotationAnimation =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002714 mWmService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002715 if (screenRotationAnimation != null) {
2716 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2717 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002718 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
lumark588a3e82018-07-20 18:53:54 +08002719 mAppTransition.writeToProto(proto, APP_TRANSITION);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002720 if (mFocusedApp != null) {
2721 mFocusedApp.writeNameToProto(proto, FOCUSED_APP);
2722 }
Steven Timotiusaf03df62017-07-18 16:56:43 -07002723 proto.end(token);
2724 }
2725
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002726 @Override
2727 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2728 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002729 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2730 final String subPrefix = " " + prefix;
2731 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2732 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2733 pw.print("dpi");
2734 if (mInitialDisplayWidth != mBaseDisplayWidth
2735 || mInitialDisplayHeight != mBaseDisplayHeight
2736 || mInitialDisplayDensity != mBaseDisplayDensity) {
2737 pw.print(" base=");
2738 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2739 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2740 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002741 if (mDisplayScalingDisabled) {
2742 pw.println(" noscale");
2743 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002744 pw.print(" cur=");
2745 pw.print(mDisplayInfo.logicalWidth);
2746 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2747 pw.print(" app=");
2748 pw.print(mDisplayInfo.appWidth);
2749 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2750 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2751 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2752 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2753 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002754 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002755 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002756 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002757
Craig Mautnerdc548482014-02-05 13:35:24 -08002758 pw.println();
Adrian Roos5251b1d2018-03-23 18:57:43 +01002759 pw.print(prefix); pw.print("mLayoutSeq="); pw.println(mLayoutSeq);
Riddle Hsu654a6f92018-07-13 22:59:36 +08002760 pw.print(prefix);
2761 pw.print("mDeferredRotationPauseCount="); pw.println(mDeferredRotationPauseCount);
Adrian Roos5251b1d2018-03-23 18:57:43 +01002762
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002763 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
2764 if (mLastFocus != mCurrentFocus) {
2765 pw.print(" mLastFocus="); pw.println(mLastFocus);
2766 }
2767 if (mLosingFocus.size() > 0) {
2768 pw.println();
2769 pw.println(" Windows losing focus:");
2770 for (int i = mLosingFocus.size() - 1; i >= 0; i--) {
2771 final WindowState w = mLosingFocus.get(i);
2772 pw.print(" Losing #"); pw.print(i); pw.print(' ');
2773 pw.print(w);
2774 if (dumpAll) {
2775 pw.println(":");
2776 w.dump(pw, " ", true);
2777 } else {
2778 pw.println();
2779 }
2780 }
2781 }
2782 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
Tiger Huang7c610aa2018-10-27 00:01:01 +08002783 if (mLastStatusBarVisibility != 0) {
2784 pw.print(" mLastStatusBarVisibility=0x");
2785 pw.println(Integer.toHexString(mLastStatusBarVisibility));
2786 }
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002787
Adrian Roos5251b1d2018-03-23 18:57:43 +01002788 pw.println();
wilsonshihc32538e2018-11-07 17:27:34 +08002789 mWallpaperController.dump(pw, " ");
2790
2791 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002792 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002793 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2794 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002795 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002796 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002797
Craig Mautnerdc548482014-02-05 13:35:24 -08002798 pw.println();
2799 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002800 pw.println();
2801 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002802 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002803 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002804 pw.print(" Exiting #"); pw.print(i);
2805 pw.print(' '); pw.print(token);
2806 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002807 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002808 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002809 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002810
Jorim Jaggi31f71702016-05-04 16:43:04 -07002811 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002812
2813 // Dump stack references
2814 final TaskStack homeStack = getHomeStack();
2815 if (homeStack != null) {
2816 pw.println(prefix + "homeStack=" + homeStack.getName());
2817 }
2818 final TaskStack pinnedStack = getPinnedStack();
2819 if (pinnedStack != null) {
2820 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2821 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002822 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002823 if (splitScreenPrimaryStack != null) {
2824 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2825 }
2826
2827 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002828 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002829 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002830 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002831
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002832 pw.println();
2833 mDisplayFrames.dump(prefix, pw);
Arthur Hung95b38a92018-07-20 18:56:12 +08002834 pw.println();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08002835 mDisplayPolicy.dump(prefix, pw);
2836 pw.println();
Riddle Hsuad256a12018-07-18 16:11:30 +08002837 mDisplayRotation.dump(prefix, pw);
2838 pw.println();
Arthur Hung95b38a92018-07-20 18:56:12 +08002839 mInputMonitor.dump(pw, " ");
Jorim Jaggif96c90a2018-09-26 16:55:15 +02002840 pw.println();
2841 mInsetsStateController.dump(prefix, pw);
Craig Mautner59c00972012-07-30 12:10:24 -07002842 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002843
2844 @Override
2845 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002846 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002847 }
2848
2849 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002850 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002851 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002852
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002853 /** Returns true if the stack in the windowing mode is visible. */
2854 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002855 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002856 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002857 }
2858
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002859 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002860 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002861 final int x = (int) xf;
2862 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002863 final WindowState touchedWin = getWindow(w -> {
2864 final int flags = w.mAttrs.flags;
2865 if (!w.isVisibleLw()) {
2866 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002867 }
2868 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002869 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002870 }
2871
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002872 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002873 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002874 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002875 }
2876
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002877 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002878
2879 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002880 return mTmpRegion.contains(x, y) || touchFlags == 0;
2881 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002882
2883 return touchedWin;
2884 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002885
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002886 boolean canAddToastWindowForUid(int uid) {
2887 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002888 // Also if the app is focused adding more than one toast at
2889 // a time for better backwards compatibility.
2890 final WindowState focusedWindowForUid = getWindow(w ->
2891 w.mOwnerUid == uid && w.isFocused());
2892 if (focusedWindowForUid != null) {
2893 return true;
2894 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002895 final WindowState win = getWindow(w ->
2896 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2897 && !w.mWindowRemovalAllowed);
2898 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002899 }
2900
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002901 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002902 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2903 return;
2904 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002905
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002906 // Used to communicate the old focus to the callback method.
2907 mTmpWindow = oldFocus;
2908
2909 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002910 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002911
Tiger Huang51c5a1d2018-12-11 20:24:51 +08002912 WindowState findFocusedWindowIfNeeded() {
2913 return (mWmService.mPerDisplayFocusEnabled
2914 || mWmService.mRoot.mTopFocusedAppByProcess.isEmpty()) ? findFocusedWindow() : null;
2915 }
2916
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002917 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002918 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002919
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002920 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002921
2922 if (mTmpWindow == null) {
2923 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2924 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002925 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002926 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002927 }
2928
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002929 /**
2930 * Update the focused window and make some adjustments if the focus has changed.
2931 *
2932 * @param mode Indicates the situation we are in. Possible modes are:
2933 * {@link WindowManagerService#UPDATE_FOCUS_NORMAL},
2934 * {@link WindowManagerService#UPDATE_FOCUS_PLACING_SURFACES},
2935 * {@link WindowManagerService#UPDATE_FOCUS_WILL_PLACE_SURFACES},
2936 * {@link WindowManagerService#UPDATE_FOCUS_REMOVING_FOCUS}
2937 * @param updateInputWindows Whether to sync the window information to the input module.
2938 * @return {@code true} if the focused window has changed.
2939 */
Tiger Huang51c5a1d2018-12-11 20:24:51 +08002940 boolean updateFocusedWindowLocked(int mode, boolean updateInputWindows) {
2941 WindowState newFocus = findFocusedWindowIfNeeded();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002942 if (mCurrentFocus == newFocus) {
2943 return false;
2944 }
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002945 boolean imWindowChanged = false;
Tiger Huang51c5a1d2018-12-11 20:24:51 +08002946 final WindowState imWindow = mInputMethodWindow;
2947 if (imWindow != null) {
2948 final WindowState prevTarget = mInputMethodTarget;
2949 final WindowState newTarget = computeImeTarget(true /* updateImeTarget*/);
2950 imWindowChanged = prevTarget != newTarget;
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002951
Tiger Huang51c5a1d2018-12-11 20:24:51 +08002952 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS
2953 && mode != UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2954 assignWindowLayers(false /* setLayoutNeeded */);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002955 }
2956 }
2957
2958 if (imWindowChanged) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002959 mWmService.mWindowsChanged = true;
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002960 setLayoutNeeded();
Tiger Huang51c5a1d2018-12-11 20:24:51 +08002961 newFocus = findFocusedWindowIfNeeded();
2962 }
2963 if (mCurrentFocus != newFocus) {
2964 mWmService.mH.obtainMessage(REPORT_FOCUS_CHANGE, this).sendToTarget();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002965 }
2966
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002967 if (DEBUG_FOCUS_LIGHT || mWmService.localLOGV) Slog.v(TAG_WM, "Changing focus from "
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002968 + mCurrentFocus + " to " + newFocus + " displayId=" + getDisplayId()
2969 + " Callers=" + Debug.getCallers(4));
2970 final WindowState oldFocus = mCurrentFocus;
2971 mCurrentFocus = newFocus;
2972 mLosingFocus.remove(newFocus);
2973
2974 if (newFocus != null) {
2975 mWinAddedSinceNullFocus.clear();
2976 mWinRemovedSinceNullFocus.clear();
2977
2978 if (newFocus.canReceiveKeys()) {
2979 // Displaying a window implicitly causes dispatching to be unpaused.
2980 // This is to protect against bugs if someone pauses dispatching but
2981 // forgets to resume.
2982 newFocus.mToken.paused = false;
2983 }
2984 }
2985
Tiger Huang7c610aa2018-10-27 00:01:01 +08002986 int focusChanged = getDisplayPolicy().focusChangedLw(oldFocus, newFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002987
2988 if (imWindowChanged && oldFocus != mInputMethodWindow) {
2989 // Focus of the input method window changed. Perform layout if needed.
2990 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2991 performLayout(true /*initial*/, updateInputWindows);
2992 focusChanged &= ~FINISH_LAYOUT_REDO_LAYOUT;
2993 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2994 // Client will do the layout, but we need to assign layers
2995 // for handleNewWindowLocked() below.
2996 assignWindowLayers(false /* setLayoutNeeded */);
2997 }
2998 }
2999
3000 if ((focusChanged & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
3001 // The change in focus caused us to need to do a layout. Okay.
3002 setLayoutNeeded();
3003 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
3004 performLayout(true /*initial*/, updateInputWindows);
3005 } else if (mode == UPDATE_FOCUS_REMOVING_FOCUS) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003006 mWmService.mRoot.performSurfacePlacement(false);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003007 }
3008 }
3009
3010 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
3011 // If we defer assigning layers, then the caller is responsible for doing this part.
3012 getInputMonitor().setInputFocusLw(newFocus, updateInputWindows);
3013 }
3014
3015 adjustForImeIfNeeded();
3016
3017 // We may need to schedule some toast windows to be removed. The toasts for an app that
3018 // does not have input focus are removed within a timeout to prevent apps to redress
3019 // other apps' UI.
3020 scheduleToastWindowsTimeoutIfNeededLocked(oldFocus, newFocus);
3021
3022 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
3023 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
3024 }
3025 return true;
3026 }
3027
3028 /**
3029 * Set the new focused app to this display.
3030 *
3031 * @param newFocus the new focused AppWindowToken.
3032 * @return true if the focused app is changed.
3033 */
3034 boolean setFocusedApp(AppWindowToken newFocus) {
3035 if (newFocus != null) {
3036 final DisplayContent appDisplay = newFocus.getDisplayContent();
3037 if (appDisplay != this) {
3038 throw new IllegalStateException(newFocus + " is not on " + getName()
3039 + " but " + ((appDisplay != null) ? appDisplay.getName() : "none"));
3040 }
3041 }
3042 if (mFocusedApp == newFocus) {
3043 return false;
3044 }
3045 mFocusedApp = newFocus;
3046 getInputMonitor().setFocusedAppLw(newFocus);
3047 updateTouchExcludeRegion();
3048 return true;
3049 }
3050
Wale Ogunwalec69694a2016-10-18 13:51:15 -07003051 /** Updates the layer assignment of windows on this display. */
3052 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003053 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07003054 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07003055 if (setLayoutNeeded) {
3056 setLayoutNeeded();
3057 }
Robert Carrb1579c82017-09-05 14:54:47 -07003058
Robert Carrf59b8dd2017-10-02 18:58:36 -07003059 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07003060 // the application of this transaction until the animation pass triggers
3061 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
3062 // the hiding and showing of surfaces.
3063 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003064 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07003065 }
3066
Wale Ogunwale1666e312016-12-16 11:27:18 -08003067 // TODO: This should probably be called any time a visual change is made to the hierarchy like
3068 // moving containers or resizing them. Need to investigate the best way to have it automatically
3069 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003070 void layoutAndAssignWindowLayersIfNeeded() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003071 mWmService.mWindowsChanged = true;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003072 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003073
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003074 if (!mWmService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003075 false /*updateInputWindows*/)) {
3076 assignWindowLayers(false /* setLayoutNeeded */);
3077 }
3078
Arthur Hung95b38a92018-07-20 18:56:12 +08003079 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003080 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Arthur Hung95b38a92018-07-20 18:56:12 +08003081 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003082 }
3083
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003084 /** Returns true if a leaked surface was destroyed */
3085 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003086 // Used to indicate that a surface was leaked.
3087 mTmpWindow = null;
3088 forAllWindows(w -> {
3089 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003090 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003091 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003092 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003093 if (!mWmService.mSessions.contains(wsa.mSession)) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003094 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003095 + w + " surface=" + wsa.mSurfaceController
3096 + " token=" + w.mToken
3097 + " pid=" + w.mSession.mPid
3098 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003099 wsa.destroySurface();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003100 mWmService.mForceRemoves.add(w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003101 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07003102 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003103 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003104 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02003105 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003106 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003107 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003108 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003109 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003110 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003111
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003112 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003113 }
3114
3115 /**
lumark90120a82018-08-15 00:33:03 +08003116 * Set input method window for the display.
3117 * @param win Set when window added or Null when destroyed.
3118 */
3119 void setInputMethodWindowLocked(WindowState win) {
3120 mInputMethodWindow = win;
lumark70865a82018-09-17 16:56:26 +08003121 // Update display configuration for IME process.
3122 if (mInputMethodWindow != null) {
3123 final int imePid = mInputMethodWindow.mSession.mPid;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003124 mWmService.mAtmInternal.onImeWindowSetOnDisplay(imePid,
lumark70865a82018-09-17 16:56:26 +08003125 mInputMethodWindow.getDisplayId());
3126 }
lumark90120a82018-08-15 00:33:03 +08003127 computeImeTarget(true /* updateImeTarget */);
Jorim Jaggif96c90a2018-09-26 16:55:15 +02003128 mInsetsStateController.getSourceProvider(TYPE_IME).setWindow(win,
3129 null /* frameProvider */);
lumark90120a82018-08-15 00:33:03 +08003130 }
3131
3132 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003133 * Determine and return the window that should be the IME target.
3134 * @param updateImeTarget If true the system IME target will be updated to match what we found.
3135 * @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 +00003136 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003137 WindowState computeImeTarget(boolean updateImeTarget) {
lumark90120a82018-08-15 00:33:03 +08003138 if (mInputMethodWindow == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003139 // There isn't an IME so there shouldn't be a target...That was easy!
3140 if (updateImeTarget) {
3141 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
lumarkff0ab692018-11-05 20:32:30 +08003142 + mInputMethodTarget + " to null since mInputMethodWindow is null");
3143 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003144 }
3145 return null;
3146 }
3147
lumarkff0ab692018-11-05 20:32:30 +08003148 final WindowState curTarget = mInputMethodTarget;
Chavi Weingarten3a748552018-05-14 17:32:42 +00003149 if (!canUpdateImeTarget()) {
3150 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Defer updating IME target");
3151 return curTarget;
3152 }
3153
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003154 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
3155 // same display. Or even when the current IME/target are not on the same screen as the next
3156 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08003157 mUpdateImeTarget = updateImeTarget;
3158 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003159
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003160
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003161 // Yet more tricksyness! If this window is a "starting" window, we do actually want
3162 // to be on top of it, but it is not -really- where input will go. So look down below
3163 // for a real window to target...
3164 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
3165 final AppWindowToken token = target.mAppToken;
3166 if (token != null) {
3167 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
3168 if (betterTarget != null) {
3169 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003170 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003171 }
3172 }
3173
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003174 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
lumarkff0ab692018-11-05 20:32:30 +08003175 "Proposed new IME target: " + target + " for display: " + getDisplayId());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003176
chaviw1c13ad32018-06-12 16:44:23 -07003177 // Now, a special case -- if the last target's window is in the process of exiting, but
3178 // not removed, and the new target is home, keep on the last target to avoid flicker.
3179 // Home is a special case since its above other stacks in the ordering list, but layed
3180 // out below the others.
3181 if (curTarget != null && !curTarget.mRemoved && curTarget.isDisplayedLw()
3182 && curTarget.isClosing() && (target == null || target.isActivityTypeHome())) {
chaviw4ad54912018-05-30 11:05:44 -07003183 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "New target is home while current target is "
Chavi Weingarten3a748552018-05-14 17:32:42 +00003184 + "closing, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003185 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003186 }
3187
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003188 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
3189 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003190
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003191 if (target == null) {
3192 if (updateImeTarget) {
3193 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
3194 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
3195 + Debug.getCallers(4) : ""));
lumarkff0ab692018-11-05 20:32:30 +08003196 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003197 }
3198
3199 return null;
3200 }
3201
3202 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003203 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
3204 if (token != null) {
3205
3206 // Now some fun for dealing with window animations that modify the Z order. We need
3207 // to look at all windows below the current target that are in this app, finding the
3208 // highest visible one in layering.
3209 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003210 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003211 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003212 }
3213
3214 if (highestTarget != null) {
lumark588a3e82018-07-20 18:53:54 +08003215 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, mAppTransition + " " + highestTarget
Jorim Jaggi35d328a2018-08-14 17:00:20 +02003216 + " animating=" + highestTarget.isAnimating());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003217
lumark588a3e82018-07-20 18:53:54 +08003218 if (mAppTransition.isTransitionSet()) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003219 // If we are currently setting up for an animation, hold everything until we
3220 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003221 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003222 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003223 }
3224 }
3225 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003226
3227 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
3228 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003229 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003230 }
3231
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003232 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003233 }
3234
lumarkff0ab692018-11-05 20:32:30 +08003235 /**
3236 * Calling {@link #computeImeTarget(boolean)} to update the input method target window in
3237 * the candidate app window token if needed.
3238 */
3239 void computeImeTargetIfNeeded(AppWindowToken candidate) {
3240 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == candidate) {
3241 computeImeTarget(true /* updateImeTarget */);
3242 }
3243 }
3244
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003245 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
lumarkff0ab692018-11-05 20:32:30 +08003246 if (target == mInputMethodTarget && mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003247 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003248 }
3249
lumarkff0ab692018-11-05 20:32:30 +08003250 mInputMethodTarget = target;
3251 mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003252 assignWindowLayers(false /* setLayoutNeeded */);
Tarandeep Singh215929b2019-01-11 18:24:37 -08003253 mInsetsStateController.onImeTargetChanged(target);
Tarandeep Singha6f35612019-01-11 19:50:46 -08003254 updateImeParent();
3255 }
3256
3257 private void updateImeParent() {
Tarandeep Singha6f35612019-01-11 19:50:46 -08003258 final SurfaceControl newParent = computeImeParent();
3259 if (newParent != null) {
3260 mPendingTransaction.reparent(mImeWindowsContainers.mSurfaceControl, newParent);
3261 scheduleAnimation();
3262 }
3263 }
3264
3265 /**
3266 * Computes the window the IME should be attached to.
3267 */
3268 @VisibleForTesting
3269 SurfaceControl computeImeParent() {
3270
3271 // Attach it to app if the target is part of an app and such app is covering the entire
3272 // screen. If it's not covering the entire screen the IME might extend beyond the apps
3273 // bounds.
3274 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken != null &&
3275 mInputMethodTarget.getWindowingMode() == WINDOWING_MODE_FULLSCREEN) {
3276 return mInputMethodTarget.mAppToken.getSurfaceControl();
3277 }
3278
3279 // Otherwise, we just attach it to the display.
3280 return mWindowingLayer;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003281 }
3282
3283 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
3284 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3285 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
3286 }
3287
3288 // Used to indicate we have reached the first window in the range we are interested in.
3289 mTmpWindow = null;
3290
3291 // TODO: Figure-out a more efficient way to do this.
3292 final WindowState candidate = getWindow(w -> {
3293 if (w == top) {
3294 // Reached the first window in the range we are interested in.
3295 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003296 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003297 if (mTmpWindow == null) {
3298 return false;
3299 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003300
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003301 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3302 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003303 }
3304 // If we reached the bottom of the range of windows we are considering,
3305 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003306 if (w == bottom) {
3307 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003308 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003309 return false;
3310 });
3311
3312 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003313 }
3314
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003315 void setLayoutNeeded() {
3316 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
3317 mLayoutNeeded = true;
3318 }
3319
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003320 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003321 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
3322 mLayoutNeeded = false;
3323 }
3324
3325 boolean isLayoutNeeded() {
3326 return mLayoutNeeded;
3327 }
3328
Wale Ogunwale02319a62016-09-26 15:21:22 -07003329 void dumpTokens(PrintWriter pw, boolean dumpAll) {
3330 if (mTokenMap.isEmpty()) {
3331 return;
3332 }
3333 pw.println(" Display #" + mDisplayId);
3334 final Iterator<WindowToken> it = mTokenMap.values().iterator();
3335 while (it.hasNext()) {
3336 final WindowToken token = it.next();
3337 pw.print(" ");
3338 pw.print(token);
3339 if (dumpAll) {
3340 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003341 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07003342 } else {
3343 pw.println();
3344 }
3345 }
lumark588a3e82018-07-20 18:53:54 +08003346
Evan Rosky2289ba12018-11-19 18:28:18 -08003347 if (!mOpeningApps.isEmpty() || !mClosingApps.isEmpty() || !mChangingApps.isEmpty()) {
lumark588a3e82018-07-20 18:53:54 +08003348 pw.println();
3349 if (mOpeningApps.size() > 0) {
3350 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
3351 }
3352 if (mClosingApps.size() > 0) {
3353 pw.print(" mClosingApps="); pw.println(mClosingApps);
3354 }
Evan Rosky2289ba12018-11-19 18:28:18 -08003355 if (mChangingApps.size() > 0) {
3356 pw.print(" mChangingApps="); pw.println(mChangingApps);
3357 }
lumark588a3e82018-07-20 18:53:54 +08003358 }
3359
3360 mUnknownAppVisibilityController.dump(pw, " ");
Wale Ogunwale02319a62016-09-26 15:21:22 -07003361 }
3362
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003363 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003364 final int[] index = new int[1];
3365 forAllWindows(w -> {
3366 final WindowStateAnimator wAnim = w.mWinAnimator;
3367 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
3368 index[0] = index[0] + 1;
3369 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003370 }
3371
Jorim Jaggife762342016-10-13 14:33:27 +02003372 /**
3373 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
3374 */
3375 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003376 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003377 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02003378 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
3379 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01003380 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02003381 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003382 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02003383 }
3384
Wale Ogunwale494009b82016-10-21 09:01:38 -07003385 boolean checkWaitingForWindows() {
3386
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003387 mHaveBootMsg = false;
3388 mHaveApp = false;
3389 mHaveWallpaper = false;
3390 mHaveKeyguard = true;
3391
3392 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003393 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
3394 return true;
3395 }
3396 if (w.isDrawnLw()) {
3397 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003398 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003399 } else if (w.mAttrs.type == TYPE_APPLICATION
3400 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003401 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003402 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003403 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003404 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003405 mHaveKeyguard = mWmService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003406 }
3407 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003408 return false;
3409 });
3410
3411 if (visibleWindow != null) {
3412 // We have a visible window.
3413 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003414 }
3415
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003416 // if the wallpaper service is disabled on the device, we're never going to have
3417 // wallpaper, don't bother waiting for it
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003418 boolean wallpaperEnabled = mWmService.mContext.getResources().getBoolean(
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003419 com.android.internal.R.bool.config_enableWallpaperService)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003420 && mWmService.mContext.getResources().getBoolean(
Daichi Hironoc1432222018-03-29 13:06:07 +09003421 com.android.internal.R.bool.config_checkWallpaperAtBoot)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003422 && !mWmService.mOnlyCore;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003423
Wale Ogunwale494009b82016-10-21 09:01:38 -07003424 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003425 "******** booted=" + mWmService.mSystemBooted
3426 + " msg=" + mWmService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003427 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
3428 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
3429 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003430
3431 // If we are turning on the screen to show the boot message, don't do it until the boot
3432 // message is actually displayed.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003433 if (!mWmService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003434 return true;
3435 }
3436
3437 // If we are turning on the screen after the boot is completed normally, don't do so until
3438 // we have the application and wallpaper.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003439 if (mWmService.mSystemBooted
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003440 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003441 return true;
3442 }
3443
3444 return false;
3445 }
3446
Jorim Jaggi8f520872018-08-14 17:00:20 +02003447 void updateWindowsForAnimator() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003448 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003449 }
3450
Jorim Jaggi8f520872018-08-14 17:00:20 +02003451 /**
3452 * Updates the {@link TaskStack#setAnimationBackground} for all windows.
3453 */
3454 void updateBackgroundForAnimator() {
Jorim Jaggi7156b652016-10-25 08:31:58 -07003455 resetAnimationBackgroundAnimator();
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003456 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003457 }
3458
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003459 boolean isInputMethodClientFocus(int uid, int pid) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003460 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003461 if (imFocus == null) {
3462 return false;
3463 }
3464
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003465 if (DEBUG_INPUT_METHOD) {
3466 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003467 Slog.i(TAG_WM, "Current focus: " + mCurrentFocus + " displayId=" + mDisplayId);
3468 Slog.i(TAG_WM, "Last focus: " + mLastFocus + " displayId=" + mDisplayId);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003469 }
3470
Wale Ogunwale494009b82016-10-21 09:01:38 -07003471 if (DEBUG_INPUT_METHOD) {
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003472 Slog.i(TAG_WM, "IM target uid/pid: " + imFocus.mSession.mUid
3473 + "/" + imFocus.mSession.mPid);
3474 Slog.i(TAG_WM, "Requesting client uid/pid: " + uid + "/" + pid);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003475 }
3476
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003477 return imFocus.mSession.mUid == uid && imFocus.mSession.mPid == pid;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003478 }
3479
3480 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003481 final WindowState win = getWindow(
3482 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
3483 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003484 }
3485
Tiger Huang7c610aa2018-10-27 00:01:01 +08003486 void statusBarVisibilityChanged(int visibility) {
3487 mLastStatusBarVisibility = visibility;
3488 visibility = getDisplayPolicy().adjustSystemUiVisibilityLw(visibility);
3489 updateStatusBarVisibilityLocked(visibility);
3490 }
3491
3492 private boolean updateStatusBarVisibilityLocked(int visibility) {
3493 if (mLastDispatchedSystemUiVisibility == visibility) {
3494 return false;
3495 }
3496 final int globalDiff = (visibility ^ mLastDispatchedSystemUiVisibility)
3497 // We are only interested in differences of one of the
3498 // clearable flags...
3499 & View.SYSTEM_UI_CLEARABLE_FLAGS
3500 // ...if it has actually been cleared.
3501 & ~visibility;
3502
3503 mLastDispatchedSystemUiVisibility = visibility;
3504 if (isDefaultDisplay) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003505 mWmService.mInputManager.setSystemUiVisibility(visibility);
Tiger Huang7c610aa2018-10-27 00:01:01 +08003506 }
3507 updateSystemUiVisibility(visibility, globalDiff);
3508 return true;
3509 }
3510
Wale Ogunwale494009b82016-10-21 09:01:38 -07003511 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003512 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003513 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003514 final int curValue = w.mSystemUiVisibility;
3515 final int diff = (curValue ^ visibility) & globalDiff;
3516 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003517 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003518 w.mSeq++;
3519 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003520 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003521 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
3522 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07003523 visibility, newValue, diff);
3524 }
3525 } catch (RemoteException e) {
3526 // so sorry
3527 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003528 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003529 }
3530
Tiger Huang7c610aa2018-10-27 00:01:01 +08003531 void reevaluateStatusBarVisibility() {
3532 int visibility = getDisplayPolicy().adjustSystemUiVisibilityLw(mLastStatusBarVisibility);
3533 if (updateStatusBarVisibilityLocked(visibility)) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003534 mWmService.mWindowPlacerLocked.requestTraversal();
Tiger Huang7c610aa2018-10-27 00:01:01 +08003535 }
3536 }
3537
Wale Ogunwale494009b82016-10-21 09:01:38 -07003538 void onWindowFreezeTimeout() {
3539 Slog.w(TAG_WM, "Window freeze timeout expired.");
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003540 mWmService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003541
3542 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07003543 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003544 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003545 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07003546 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003547 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003548 - mWmService.mDisplayFreezeTime);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003549 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003550 }, true /* traverseTopToBottom */);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003551 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003552 }
3553
3554 void waitForAllWindowsDrawn() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003555 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003556 forAllWindows(w -> {
3557 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
3558 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
3559 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003560 // Force add to mResizingWindows.
chaviw9c81e632018-07-31 11:17:52 -07003561 w.resetLastContentInsets();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003562 mWmService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003563 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003564 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003565 }
3566
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003567 // TODO: Super crazy long method that should be broken down...
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003568 void applySurfaceChangesTransaction(boolean recoveringMemory) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003569 final WindowSurfacePlacer surfacePlacer = mWmService.mWindowPlacerLocked;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003570
3571 mTmpUpdateAllDrawn.clear();
3572
3573 int repeats = 0;
3574 do {
3575 repeats++;
3576 if (repeats > 6) {
3577 Slog.w(TAG, "Animation repeat aborted after too many iterations");
3578 clearLayoutNeeded();
3579 break;
3580 }
3581
3582 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
3583 pendingLayoutChanges);
3584
wilsonshihc32538e2018-11-07 17:27:34 +08003585 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
3586 mWallpaperController.adjustWallpaperWindows();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003587 }
3588
Riddle Hsu654a6f92018-07-13 22:59:36 +08003589 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003590 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
Riddle Hsu4e611772018-10-31 18:58:28 +08003591 if (updateOrientationFromAppTokens()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003592 setLayoutNeeded();
Riddle Hsu4e611772018-10-31 18:58:28 +08003593 sendNewConfiguration();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003594 }
3595 }
3596
3597 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
3598 setLayoutNeeded();
3599 }
3600
3601 // FIRST LOOP: Perform a layout, if needed.
3602 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
3603 performLayout(repeats == 1, false /* updateInputWindows */);
3604 } else {
3605 Slog.w(TAG, "Layout repeat skipped after too many iterations");
3606 }
3607
3608 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
3609 pendingLayoutChanges = 0;
3610
Tiger Huang7c610aa2018-10-27 00:01:01 +08003611 mDisplayPolicy.beginPostLayoutPolicyLw();
3612 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
3613 pendingLayoutChanges |= mDisplayPolicy.finishPostLayoutPolicyLw();
3614 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
3615 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
Jorim Jaggif96c90a2018-09-26 16:55:15 +02003616 mInsetsStateController.onPostLayout();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003617 } while (pendingLayoutChanges != 0);
3618
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003619 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003620
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003621 mTmpRecoveringMemory = recoveringMemory;
3622 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01003623 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003624
Jorim Jaggif1292892018-09-10 11:58:13 +02003625 mLastHasContent = mTmpApplySurfaceChangesTransactionState.displayHasContent;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003626 mWmService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Jorim Jaggif1292892018-09-10 11:58:13 +02003627 mLastHasContent,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003628 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
3629 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003630 true /* inTraversal, must call performTraversalInTrans... below */);
3631
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003632 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
3633 if (wallpaperVisible != mLastWallpaperVisible) {
3634 mLastWallpaperVisible = wallpaperVisible;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003635 mWmService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003636 }
3637
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003638 while (!mTmpUpdateAllDrawn.isEmpty()) {
3639 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
3640 // See if any windows have been drawn, so they (and others associated with them)
3641 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07003642 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003643 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003644 }
3645
Bryce Leef3c6a472017-11-14 14:53:06 -08003646 private void updateBounds() {
Evan Rosky39b6f232018-10-30 18:35:41 -07003647 calculateBounds(mDisplayInfo, mTmpBounds);
Bryce Leef3c6a472017-11-14 14:53:06 -08003648 setBounds(mTmpBounds);
Tiger Huang04dc4cc2019-01-17 18:41:41 +08003649 if (mPortalWindowHandle != null && mParentSurfaceControl != null) {
3650 mPortalWindowHandle.touchableRegion.getBounds(mTmpRect);
3651 if (!mTmpBounds.equals(mTmpRect)) {
3652 mPortalWindowHandle.touchableRegion.set(mTmpBounds);
3653 mPendingTransaction.setInputWindowInfo(mParentSurfaceControl, mPortalWindowHandle);
3654 }
3655 }
Bryce Leef3c6a472017-11-14 14:53:06 -08003656 }
3657
3658 // Determines the current display bounds based on the current state
Evan Rosky39b6f232018-10-30 18:35:41 -07003659 private void calculateBounds(DisplayInfo displayInfo, Rect out) {
Bryce Leef3c6a472017-11-14 14:53:06 -08003660 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Evan Rosky39b6f232018-10-30 18:35:41 -07003661 final int rotation = displayInfo.rotation;
3662 boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Bryce Leef3c6a472017-11-14 14:53:06 -08003663 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
3664 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Evan Rosky39b6f232018-10-30 18:35:41 -07003665 int width = displayInfo.logicalWidth;
Bryce Leef3c6a472017-11-14 14:53:06 -08003666 int left = (physWidth - width) / 2;
Evan Rosky39b6f232018-10-30 18:35:41 -07003667 int height = displayInfo.logicalHeight;
Bryce Leef3c6a472017-11-14 14:53:06 -08003668 int top = (physHeight - height) / 2;
3669 out.set(left, top, left + width, top + height);
3670 }
3671
3672 @Override
3673 public void getBounds(Rect out) {
Evan Rosky39b6f232018-10-30 18:35:41 -07003674 calculateBounds(mDisplayInfo, out);
Bryce Leef3c6a472017-11-14 14:53:06 -08003675 }
3676
3677 private void getBounds(Rect out, int orientation) {
3678 getBounds(out);
3679
3680 // Rotate the Rect if needed.
3681 final int currentRotation = mDisplayInfo.rotation;
3682 final int rotationDelta = deltaRotation(currentRotation, orientation);
3683 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
3684 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
3685 mTmpRectF.set(out);
3686 mTmpMatrix.mapRect(mTmpRectF);
3687 mTmpRectF.round(out);
3688 }
3689 }
3690
Evan Rosky730f6e82018-12-03 17:40:11 -08003691 /** @returns the orientation of the display when it's rotation is ROTATION_0. */
3692 int getNaturalOrientation() {
3693 return mBaseDisplayWidth < mBaseDisplayHeight
3694 ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
3695 }
3696
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003697 void performLayout(boolean initial, boolean updateInputWindows) {
3698 if (!isLayoutNeeded()) {
3699 return;
3700 }
3701 clearLayoutNeeded();
3702
3703 final int dw = mDisplayInfo.logicalWidth;
3704 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003705 if (DEBUG_LAYOUT) {
3706 Slog.v(TAG, "-------------------------------------");
3707 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
3708 }
3709
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01003710 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
3711 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00003712 // TODO: Not sure if we really need to set the rotation here since we are updating from the
3713 // display info above...
3714 mDisplayFrames.mRotation = mRotation;
Tiger Huang7c610aa2018-10-27 00:01:01 +08003715 mDisplayPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003716
Adrian Roos5251b1d2018-03-23 18:57:43 +01003717 int seq = mLayoutSeq + 1;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003718 if (seq < 0) seq = 0;
Adrian Roos5251b1d2018-03-23 18:57:43 +01003719 mLayoutSeq = seq;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003720
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003721 // Used to indicate that we have processed the dream window and all additional windows are
3722 // behind it.
3723 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003724 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003725
3726 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003727 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003728
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003729 // Used to indicate that we have processed the dream window and all additional attached
3730 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003731 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003732 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003733
3734 // Now perform layout of attached windows, which usually depend on the position of the
3735 // window they are attached to. XXX does not deal with windows that are attached to windows
3736 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003737 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003738
3739 // Window frames may have changed. Tell the input dispatcher about it.
Arthur Hung95b38a92018-07-20 18:56:12 +08003740 mInputMonitor.layoutInputConsumers(dw, dh);
3741 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003742 if (updateInputWindows) {
Arthur Hung95b38a92018-07-20 18:56:12 +08003743 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003744 }
3745
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003746 mWmService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003747 }
3748
3749 /**
3750 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
3751 * In portrait mode, it grabs the full screenshot.
3752 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003753 * @param config of the output bitmap
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003754 */
chaviw0315a1a2018-03-05 15:28:35 -08003755 Bitmap screenshotDisplayLocked(Bitmap.Config config) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003756 if (!mWmService.mPolicy.isScreenOn()) {
chaviw0315a1a2018-03-05 15:28:35 -08003757 if (DEBUG_SCREENSHOT) {
3758 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003759 }
chaviw0315a1a2018-03-05 15:28:35 -08003760 return null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003761 }
chaviwfbe47df2017-11-10 16:14:49 -08003762
chaviw0315a1a2018-03-05 15:28:35 -08003763 int dw = mDisplayInfo.logicalWidth;
3764 int dh = mDisplayInfo.logicalHeight;
chaviwfbe47df2017-11-10 16:14:49 -08003765
chaviw0315a1a2018-03-05 15:28:35 -08003766 if (dw <= 0 || dh <= 0) {
3767 return null;
3768 }
chaviwfbe47df2017-11-10 16:14:49 -08003769
chaviw0315a1a2018-03-05 15:28:35 -08003770 final Rect frame = new Rect(0, 0, dw, dh);
chaviwfbe47df2017-11-10 16:14:49 -08003771
chaviw0315a1a2018-03-05 15:28:35 -08003772 // The screenshot API does not apply the current screen rotation.
3773 int rot = mDisplay.getRotation();
chaviwfbe47df2017-11-10 16:14:49 -08003774
chaviw0315a1a2018-03-05 15:28:35 -08003775 if (rot == ROTATION_90 || rot == ROTATION_270) {
3776 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3777 }
chaviwfbe47df2017-11-10 16:14:49 -08003778
chaviw0315a1a2018-03-05 15:28:35 -08003779 // SurfaceFlinger is not aware of orientation, so convert our logical
3780 // crop to SurfaceFlinger's portrait orientation.
3781 convertCropForSurfaceFlinger(frame, rot, dw, dh);
3782
3783 final ScreenRotationAnimation screenRotationAnimation =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003784 mWmService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
chaviw0315a1a2018-03-05 15:28:35 -08003785 final boolean inRotation = screenRotationAnimation != null &&
3786 screenRotationAnimation.isAnimating();
3787 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
3788
3789 // TODO(b/68392460): We should screenshot Task controls directly
3790 // but it's difficult at the moment as the Task doesn't have the
3791 // correct size set.
chaviw08520a02018-09-10 16:44:56 -07003792 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, inRotation, rot);
chaviw0315a1a2018-03-05 15:28:35 -08003793 if (bitmap == null) {
3794 Slog.w(TAG_WM, "Failed to take screenshot");
3795 return null;
3796 }
3797
3798 // Create a copy of the screenshot that is immutable and backed in ashmem.
3799 // This greatly reduces the overhead of passing the bitmap between processes.
3800 final Bitmap ret = bitmap.createAshmemBitmap(config);
3801 bitmap.recycle();
3802 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003803 }
3804
3805 // TODO: Can this use createRotationMatrix()?
3806 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3807 if (rot == Surface.ROTATION_90) {
3808 final int tmp = crop.top;
3809 crop.top = dw - crop.right;
3810 crop.right = crop.bottom;
3811 crop.bottom = dw - crop.left;
3812 crop.left = tmp;
3813 } else if (rot == Surface.ROTATION_180) {
3814 int tmp = crop.top;
3815 crop.top = dh - crop.bottom;
3816 crop.bottom = dh - tmp;
3817 tmp = crop.right;
3818 crop.right = dw - crop.left;
3819 crop.left = dw - tmp;
3820 } else if (rot == Surface.ROTATION_270) {
3821 final int tmp = crop.top;
3822 crop.top = crop.left;
3823 crop.left = dh - crop.bottom;
3824 crop.bottom = crop.right;
3825 crop.right = dh - tmp;
3826 }
3827 }
3828
3829 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003830 // Used to indicate the layout is needed.
3831 mTmpWindow = null;
3832
3833 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003834 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003835 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003836 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003837 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003838 w.setDisplayLayoutNeeded();
Vishnu Nairba183352018-11-21 11:16:49 -08003839 w.finishSeamlessRotation(true /* timeout */);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003840 mWmService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003841 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003842
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003843 if (mTmpWindow != null) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003844 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003845 }
3846 }
3847
Wale Ogunwale1666e312016-12-16 11:27:18 -08003848 void setExitingTokensHasVisible(boolean hasVisible) {
3849 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3850 mExitingTokens.get(i).hasVisible = hasVisible;
3851 }
3852
3853 // Initialize state of exiting applications.
3854 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3855 }
3856
3857 void removeExistingTokensIfPossible() {
3858 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3859 final WindowToken token = mExitingTokens.get(i);
3860 if (!token.hasVisible) {
3861 mExitingTokens.remove(i);
3862 }
3863 }
3864
3865 // Time to remove any exiting applications?
3866 mTaskStackContainers.removeExistingAppTokensIfPossible();
3867 }
3868
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003869 @Override
3870 void onDescendantOverrideConfigurationChanged() {
3871 setLayoutNeeded();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003872 mWmService.requestTraversal();
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003873 }
3874
David Stevens9440dc82017-03-16 19:00:20 -07003875 boolean okToDisplay() {
3876 if (mDisplayId == DEFAULT_DISPLAY) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003877 return !mWmService.mDisplayFrozen
3878 && mWmService.mDisplayEnabled && mWmService.mPolicy.isScreenOn();
David Stevens9440dc82017-03-16 19:00:20 -07003879 }
3880 return mDisplayInfo.state == Display.STATE_ON;
3881 }
3882
3883 boolean okToAnimate() {
3884 return okToDisplay() &&
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003885 (mDisplayId != DEFAULT_DISPLAY || mWmService.mPolicy.okToAnimate());
David Stevens9440dc82017-03-16 19:00:20 -07003886 }
3887
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003888 static final class TaskForResizePointSearchResult {
3889 boolean searchDone;
3890 Task taskForResize;
3891
3892 void reset() {
3893 searchDone = false;
3894 taskForResize = null;
3895 }
3896 }
Robert Carr3b716242016-08-16 16:02:21 -07003897
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003898 private static final class ApplySurfaceChangesTransactionState {
3899 boolean displayHasContent;
3900 boolean obscured;
3901 boolean syswin;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003902 float preferredRefreshRate;
3903 int preferredModeId;
3904
3905 void reset() {
3906 displayHasContent = false;
3907 obscured = false;
3908 syswin = false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003909 preferredRefreshRate = 0;
3910 preferredModeId = 0;
3911 }
3912 }
3913
3914 private static final class ScreenshotApplicationState {
3915 WindowState appWin;
3916 int maxLayer;
3917 int minLayer;
3918 boolean screenshotReady;
3919
3920 void reset(boolean screenshotReady) {
3921 appWin = null;
3922 maxLayer = 0;
3923 minLayer = 0;
3924 this.screenshotReady = screenshotReady;
3925 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3926 }
3927 }
3928
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003929 /**
3930 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3931 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3932 * homogeneous children type which is currently required by sub-classes of
3933 * {@link WindowContainer} class.
3934 */
3935 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3936
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003937 DisplayChildWindowContainer(WindowManagerService service) {
3938 super(service);
3939 }
3940
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003941 @Override
3942 boolean fillsParent() {
3943 return true;
3944 }
3945
3946 @Override
3947 boolean isVisible() {
3948 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003949 }
3950 }
3951
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003952 /**
3953 * Window container class that contains all containers on this display relating to Apps.
3954 * I.e Activities.
3955 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003956 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003957 /**
3958 * A control placed at the appropriate level for transitions to occur.
3959 */
chaviw23ee71c2017-12-18 11:29:41 -08003960 SurfaceControl mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02003961 SurfaceControl mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02003962 SurfaceControl mHomeAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003963
Robert Carrf7a7ca82017-12-06 13:17:07 -08003964 /**
3965 * Given that the split-screen divider does not have an AppWindowToken, it
3966 * will have to live inside of a "NonAppWindowContainer", in particular
3967 * {@link DisplayContent#mAboveAppWindowsContainers}. However, in visual Z order
3968 * it will need to be interleaved with some of our children, appearing on top of
3969 * both docked stacks but underneath any assistant stacks.
3970 *
3971 * To solve this problem we have this anchor control, which will always exist so
3972 * we can always assign it the correct value in our {@link #assignChildLayers}.
3973 * Likewise since it always exists, {@link AboveAppWindowContainers} can always
3974 * assign the divider a layer relative to it. This way we prevent linking lifecycle
3975 * events between the two containers.
3976 */
3977 SurfaceControl mSplitScreenDividerAnchor = null;
3978
Wale Ogunwale61911492017-10-11 08:50:50 -07003979 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3980 // through the list to find them.
3981 private TaskStack mHomeStack = null;
3982 private TaskStack mPinnedStack = null;
3983 private TaskStack mSplitScreenPrimaryStack = null;
3984
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003985 TaskStackContainers(WindowManagerService service) {
3986 super(service);
3987 }
3988
Wale Ogunwale61911492017-10-11 08:50:50 -07003989 /**
3990 * Returns the topmost stack on the display that is compatible with the input windowing mode
3991 * and activity type. Null is no compatible stack on the display.
3992 */
3993 TaskStack getStack(int windowingMode, int activityType) {
3994 if (activityType == ACTIVITY_TYPE_HOME) {
3995 return mHomeStack;
3996 }
3997 if (windowingMode == WINDOWING_MODE_PINNED) {
3998 return mPinnedStack;
3999 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
4000 return mSplitScreenPrimaryStack;
4001 }
4002 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
4003 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004004 if (activityType == ACTIVITY_TYPE_UNDEFINED
4005 && windowingMode == stack.getWindowingMode()) {
4006 // Passing in undefined type means we want to match the topmost stack with the
4007 // windowing mode.
4008 return stack;
4009 }
Wale Ogunwale61911492017-10-11 08:50:50 -07004010 if (stack.isCompatible(windowingMode, activityType)) {
4011 return stack;
4012 }
4013 }
4014 return null;
4015 }
4016
4017 @VisibleForTesting
4018 TaskStack getTopStack() {
4019 return mTaskStackContainers.getChildCount() > 0
4020 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
4021 }
4022
4023 TaskStack getHomeStack() {
4024 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
4025 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
4026 }
4027 return mHomeStack;
4028 }
4029
4030 TaskStack getPinnedStack() {
4031 return mPinnedStack;
4032 }
4033
Matthew Ng64e77cf2017-10-31 14:01:31 -07004034 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07004035 return mSplitScreenPrimaryStack;
4036 }
4037
Winson Chunge2d72172018-01-25 17:46:20 +00004038 ArrayList<Task> getVisibleTasks() {
4039 final ArrayList<Task> visibleTasks = new ArrayList<>();
4040 forAllTasks(task -> {
4041 if (task.isVisible()) {
4042 visibleTasks.add(task);
4043 }
4044 });
4045 return visibleTasks;
4046 }
4047
Andrii Kulian839def92016-11-02 10:58:58 -07004048 /**
4049 * Adds the stack to this container.
Andrii Kulian839def92016-11-02 10:58:58 -07004050 */
4051 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07004052 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004053 addChild(stack, onTop);
4054 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07004055 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004056
Wale Ogunwale61911492017-10-11 08:50:50 -07004057 void onStackWindowingModeChanged(TaskStack stack) {
4058 removeStackReferenceIfNeeded(stack);
4059 addStackReferenceIfNeeded(stack);
4060 if (stack == mPinnedStack && getTopStack() != stack) {
4061 // Looks like this stack changed windowing mode to pinned. Move it to the top.
4062 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
4063 }
4064 }
4065
4066 private void addStackReferenceIfNeeded(TaskStack stack) {
4067 if (stack.isActivityTypeHome()) {
4068 if (mHomeStack != null) {
Louis Chang7d0037c2018-08-13 12:42:06 +08004069 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
Wale Ogunwale61911492017-10-11 08:50:50 -07004070 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
Louis Chang7d0037c2018-08-13 12:42:06 +08004071
Wale Ogunwale61911492017-10-11 08:50:50 -07004072 }
Louis Chang7d0037c2018-08-13 12:42:06 +08004073 mHomeStack = stack;
Wale Ogunwale61911492017-10-11 08:50:50 -07004074 }
4075 final int windowingMode = stack.getWindowingMode();
4076 if (windowingMode == WINDOWING_MODE_PINNED) {
4077 if (mPinnedStack != null) {
4078 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
4079 + mPinnedStack + " already exist on display=" + this
4080 + " stack=" + stack);
4081 }
4082 mPinnedStack = stack;
4083 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
4084 if (mSplitScreenPrimaryStack != null) {
4085 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
4086 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
4087 + " already exist on display=" + this + " stack=" + stack);
4088 }
4089 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004090 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07004091 }
4092 }
4093
4094 private void removeStackReferenceIfNeeded(TaskStack stack) {
4095 if (stack == mHomeStack) {
4096 mHomeStack = null;
4097 } else if (stack == mPinnedStack) {
4098 mPinnedStack = null;
4099 } else if (stack == mSplitScreenPrimaryStack) {
4100 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004101 // Re-set the split-screen create mode whenever the split-screen stack is removed.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004102 mWmService.setDockedStackCreateStateLocked(
Wale Ogunwale30e441d2017-11-09 08:28:45 -08004103 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
4104 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07004105 }
Andrii Kulian839def92016-11-02 10:58:58 -07004106 }
4107
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004108 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004109 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
4110 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004111 addChild(stack, addIndex);
4112 setLayoutNeeded();
4113 }
4114
Wale Ogunwale61911492017-10-11 08:50:50 -07004115 @Override
4116 protected void removeChild(TaskStack stack) {
4117 super.removeChild(stack);
4118 removeStackReferenceIfNeeded(stack);
4119 }
Bryce Lee00d586d2017-07-28 20:48:43 -07004120
4121 @Override
4122 boolean isOnTop() {
4123 // Considered always on top
4124 return true;
4125 }
4126
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004127 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08004128 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07004129 if (child.getWindowConfiguration().isAlwaysOnTop()
4130 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08004131 // This stack is always-on-top, override the default behavior.
4132 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
4133
4134 // Moving to its current position, as we must call super but we don't want to
4135 // perform any meaningful action.
4136 final int currentPosition = mChildren.indexOf(child);
4137 super.positionChildAt(currentPosition, child, false /* includingParents */);
4138 return;
4139 }
4140
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004141 final int targetPosition = findPositionForStack(position, child, false /* adding */);
4142 super.positionChildAt(targetPosition, child, includingParents);
4143
Riddle Hsu2f6e1742018-08-23 22:44:36 +08004144 if (includingParents) {
4145 // We still want to move the display of this stack container to top because even the
4146 // target position is adjusted to non-top, the intention of the condition is to have
4147 // higher z-order to gain focus (e.g. moving a task of a fullscreen stack to front
4148 // in a non-top display which is using picture-in-picture mode).
4149 final int topChildPosition = getChildCount() - 1;
4150 if (targetPosition < topChildPosition && position >= topChildPosition) {
4151 getParent().positionChildAt(POSITION_TOP, this /* child */,
4152 true /* includingParents */);
4153 }
4154 }
4155
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004156 setLayoutNeeded();
4157 }
4158
4159 /**
4160 * When stack is added or repositioned, find a proper position for it.
4161 * This will make sure that pinned stack always stays on top.
4162 * @param requestedPosition Position requested by caller.
4163 * @param stack Stack to be added or positioned.
4164 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
4165 * @return The proper position for the stack.
4166 */
4167 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09004168 if (stack.inPinnedWindowingMode()) {
Kazuki Takisef85197b2018-06-18 18:18:36 +09004169 return POSITION_TOP;
Kazuki Takise148d00a2018-05-31 15:32:19 +09004170 }
4171
Kazuki Takisef85197b2018-06-18 18:18:36 +09004172 final int topChildPosition = mChildren.size() - 1;
4173 int belowAlwaysOnTopPosition = POSITION_BOTTOM;
4174 for (int i = topChildPosition; i >= 0; --i) {
4175 if (getStacks().get(i) != stack && !getStacks().get(i).isAlwaysOnTop()) {
4176 belowAlwaysOnTopPosition = i;
4177 break;
4178 }
4179 }
4180
4181 // The max possible position we can insert the stack at.
4182 int maxPosition = POSITION_TOP;
4183 // The min possible position we can insert the stack at.
4184 int minPosition = POSITION_BOTTOM;
4185
4186 if (stack.isAlwaysOnTop()) {
4187 if (hasPinnedStack()) {
4188 // Always-on-top stacks go below the pinned stack.
4189 maxPosition = getStacks().indexOf(mPinnedStack) - 1;
4190 }
4191 // Always-on-top stacks need to be above all other stacks.
4192 minPosition = belowAlwaysOnTopPosition !=
4193 POSITION_BOTTOM ? belowAlwaysOnTopPosition : topChildPosition;
4194 } else {
4195 // Other stacks need to be below the always-on-top stacks.
4196 maxPosition = belowAlwaysOnTopPosition !=
Arthur Hung928d8dc2018-07-18 15:38:05 +08004197 POSITION_BOTTOM ? belowAlwaysOnTopPosition : 0;
Kazuki Takisef85197b2018-06-18 18:18:36 +09004198 }
4199
4200 int targetPosition = requestedPosition;
4201 targetPosition = Math.min(targetPosition, maxPosition);
4202 targetPosition = Math.max(targetPosition, minPosition);
4203
4204 int prevPosition = getStacks().indexOf(stack);
4205 // The positions we calculated above (maxPosition, minPosition) do not take into
4206 // consideration the following edge cases.
4207 // 1) We need to adjust the position depending on the value "adding".
4208 // 2) When we are moving a stack to another position, we also need to adjust the
4209 // position depending on whether the stack is moving to a higher or lower position.
4210 if ((targetPosition != requestedPosition) &&
4211 (adding || targetPosition < prevPosition)) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09004212 targetPosition++;
4213 }
4214
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004215 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08004216 }
4217
4218 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004219 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
4220 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004221 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004222 if (super.forAllWindows(callback, traverseTopToBottom)) {
4223 return true;
4224 }
4225 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4226 return true;
4227 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004228 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004229 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4230 return true;
4231 }
4232 if (super.forAllWindows(callback, traverseTopToBottom)) {
4233 return true;
4234 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004235 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004236 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004237 }
4238
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004239 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004240 boolean traverseTopToBottom) {
4241 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
4242 // app tokens.
4243 // TODO: Investigate if we need to continue to do this or if we can just process them
4244 // in-order.
4245 if (traverseTopToBottom) {
4246 for (int i = mChildren.size() - 1; i >= 0; --i) {
4247 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4248 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004249 if (appTokens.get(j).forAllWindowsUnchecked(callback,
4250 traverseTopToBottom)) {
4251 return true;
4252 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004253 }
4254 }
4255 } else {
4256 final int count = mChildren.size();
4257 for (int i = 0; i < count; ++i) {
4258 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4259 final int appTokensCount = appTokens.size();
4260 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004261 if (appTokens.get(j).forAllWindowsUnchecked(callback,
4262 traverseTopToBottom)) {
4263 return true;
4264 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004265 }
4266 }
4267 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004268 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004269 }
4270
Wale Ogunwale1666e312016-12-16 11:27:18 -08004271 void setExitingTokensHasVisible(boolean hasVisible) {
4272 for (int i = mChildren.size() - 1; i >= 0; --i) {
4273 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4274 for (int j = appTokens.size() - 1; j >= 0; --j) {
4275 appTokens.get(j).hasVisible = hasVisible;
4276 }
4277 }
4278 }
4279
4280 void removeExistingAppTokensIfPossible() {
4281 for (int i = mChildren.size() - 1; i >= 0; --i) {
4282 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4283 for (int j = appTokens.size() - 1; j >= 0; --j) {
4284 final AppWindowToken token = appTokens.get(j);
lumark588a3e82018-07-20 18:53:54 +08004285 if (!token.hasVisible && !mClosingApps.contains(token)
Wale Ogunwale1666e312016-12-16 11:27:18 -08004286 && (!token.mIsExiting || token.isEmpty())) {
4287 // Make sure there is no animation running on this token, so any windows
4288 // associated with it will be removed as soon as their animations are
4289 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02004290 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004291 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
4292 "performLayout: App token exiting now removed" + token);
4293 token.removeIfPossible();
4294 }
4295 }
4296 }
4297 }
4298
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004299 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004300 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004301 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
4302 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004303 // Apps and their containers are not allowed to specify an orientation while the
Wale Ogunwaled0c225c2018-06-15 14:47:46 -07004304 // docked or freeform stack is visible...except for the home stack if the docked
4305 // stack is minimized and it actually set something and the bounds is different from
4306 // the display.
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004307 if (mHomeStack != null && mHomeStack.isVisible()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004308 && mDividerControllerLocked.isMinimizedDock()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004309 && !(mDividerControllerLocked.isHomeStackResizable()
4310 && mHomeStack.matchParentBounds())) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004311 final int orientation = mHomeStack.getOrientation();
4312 if (orientation != SCREEN_ORIENTATION_UNSET) {
4313 return orientation;
4314 }
4315 }
4316 return SCREEN_ORIENTATION_UNSPECIFIED;
4317 }
4318
4319 final int orientation = super.getOrientation();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004320 boolean isCar = mWmService.mContext.getPackageManager().hasSystemFeature(
Dean Harding3e5a1522017-10-06 09:19:01 -07004321 PackageManager.FEATURE_AUTOMOTIVE);
4322 if (isCar) {
4323 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
4324 // allow anything but the default orientation.
4325 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004326 "Forcing UNSPECIFIED orientation in car for display id=" + mDisplayId
4327 + ". Ignoring " + orientation);
Dean Harding3e5a1522017-10-06 09:19:01 -07004328 return SCREEN_ORIENTATION_UNSPECIFIED;
4329 }
4330
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004331 if (orientation != SCREEN_ORIENTATION_UNSET
4332 && orientation != SCREEN_ORIENTATION_BEHIND) {
4333 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004334 "App is requesting an orientation, return " + orientation
4335 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004336 return orientation;
4337 }
4338
4339 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004340 "No app is requesting an orientation, return " + mLastOrientation
4341 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004342 // The next app has not been requested to be visible, so we keep the current orientation
4343 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08004344 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004345 }
Robert Carrb1579c82017-09-05 14:54:47 -07004346
4347 @Override
4348 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004349 assignStackOrdering(t);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004350
Robert Carr2f8aa392018-01-31 14:46:51 -08004351 for (int i = 0; i < mChildren.size(); i++) {
4352 final TaskStack s = mChildren.get(i);
4353 s.assignChildLayers(t);
4354 }
4355 }
4356
4357 void assignStackOrdering(SurfaceControl.Transaction t) {
Jorim Jaggibe418292018-03-26 16:14:12 +02004358
Robert Carrf7a7ca82017-12-06 13:17:07 -08004359 final int HOME_STACK_STATE = 0;
4360 final int NORMAL_STACK_STATE = 1;
4361 final int ALWAYS_ON_TOP_STATE = 2;
4362
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004363 int layer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08004364 int layerForAnimationLayer = 0;
Jorim Jaggibe418292018-03-26 16:14:12 +02004365 int layerForBoostedAnimationLayer = 0;
Jorim Jaggi391790622018-04-18 15:30:44 +02004366 int layerForHomeAnimationLayer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08004367
Robert Carrf7a7ca82017-12-06 13:17:07 -08004368 for (int state = 0; state <= ALWAYS_ON_TOP_STATE; state++) {
4369 for (int i = 0; i < mChildren.size(); i++) {
4370 final TaskStack s = mChildren.get(i);
Robert Carr2f8aa392018-01-31 14:46:51 -08004371 if (state == HOME_STACK_STATE && !s.isActivityTypeHome()) {
4372 continue;
4373 } else if (state == NORMAL_STACK_STATE && (s.isActivityTypeHome()
4374 || s.isAlwaysOnTop())) {
4375 continue;
4376 } else if (state == ALWAYS_ON_TOP_STATE && !s.isAlwaysOnTop()) {
4377 continue;
4378 }
4379 s.assignLayer(t, layer++);
4380 if (s.inSplitScreenWindowingMode() && mSplitScreenDividerAnchor != null) {
4381 t.setLayer(mSplitScreenDividerAnchor, layer++);
4382 }
Winson Chungd41f71d2018-03-16 15:26:07 -07004383 if ((s.isTaskAnimating() || s.isAppAnimating())
4384 && state != ALWAYS_ON_TOP_STATE) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004385 // Ensure the animation layer ends up above the
4386 // highest animating stack and no higher.
4387 layerForAnimationLayer = layer++;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004388 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004389 if (state != ALWAYS_ON_TOP_STATE) {
4390 layerForBoostedAnimationLayer = layer++;
4391 }
Robert Carrf7a7ca82017-12-06 13:17:07 -08004392 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004393 if (state == HOME_STACK_STATE) {
4394 layerForHomeAnimationLayer = layer++;
4395 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004396 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004397 if (mAppAnimationLayer != null) {
4398 t.setLayer(mAppAnimationLayer, layerForAnimationLayer);
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004399 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004400 if (mBoostedAppAnimationLayer != null) {
4401 t.setLayer(mBoostedAppAnimationLayer, layerForBoostedAnimationLayer);
4402 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004403 if (mHomeAppAnimationLayer != null) {
4404 t.setLayer(mHomeAppAnimationLayer, layerForHomeAnimationLayer);
4405 }
Robert Carrb1579c82017-09-05 14:54:47 -07004406 }
4407
4408 @Override
Jorim Jaggi391790622018-04-18 15:30:44 +02004409 SurfaceControl getAppAnimationLayer(@AnimationLayer int animationLayer) {
4410 switch (animationLayer) {
4411 case ANIMATION_LAYER_BOOSTED:
4412 return mBoostedAppAnimationLayer;
4413 case ANIMATION_LAYER_HOME:
4414 return mHomeAppAnimationLayer;
4415 case ANIMATION_LAYER_STANDARD:
4416 default:
4417 return mAppAnimationLayer;
4418 }
chaviw23ee71c2017-12-18 11:29:41 -08004419 }
4420
Robert Carrf7a7ca82017-12-06 13:17:07 -08004421 SurfaceControl getSplitScreenDividerAnchor() {
4422 return mSplitScreenDividerAnchor;
4423 }
4424
chaviw23ee71c2017-12-18 11:29:41 -08004425 @Override
Riddle Hsu3a4bb612019-01-31 00:02:22 +08004426 void onParentChanged() {
4427 super.onParentChanged();
Robert Carrb1579c82017-09-05 14:54:47 -07004428 if (getParent() != null) {
chaviw23ee71c2017-12-18 11:29:41 -08004429 mAppAnimationLayer = makeChildSurface(null)
4430 .setName("animationLayer")
4431 .build();
Jorim Jaggibe418292018-03-26 16:14:12 +02004432 mBoostedAppAnimationLayer = makeChildSurface(null)
4433 .setName("boostedAnimationLayer")
4434 .build();
Jorim Jaggi391790622018-04-18 15:30:44 +02004435 mHomeAppAnimationLayer = makeChildSurface(null)
4436 .setName("homeAnimationLayer")
4437 .build();
Robert Carrf7a7ca82017-12-06 13:17:07 -08004438 mSplitScreenDividerAnchor = makeChildSurface(null)
4439 .setName("splitScreenDividerAnchor")
4440 .build();
4441 getPendingTransaction()
4442 .show(mAppAnimationLayer)
Jorim Jaggibe418292018-03-26 16:14:12 +02004443 .show(mBoostedAppAnimationLayer)
Jorim Jaggi391790622018-04-18 15:30:44 +02004444 .show(mHomeAppAnimationLayer)
Robert Carrf7a7ca82017-12-06 13:17:07 -08004445 .show(mSplitScreenDividerAnchor);
chaviw23ee71c2017-12-18 11:29:41 -08004446 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07004447 } else {
Robert Carr5ea304d2019-02-04 16:04:55 -08004448 mAppAnimationLayer.remove();
chaviw23ee71c2017-12-18 11:29:41 -08004449 mAppAnimationLayer = null;
Robert Carr5ea304d2019-02-04 16:04:55 -08004450 mBoostedAppAnimationLayer.remove();
Jorim Jaggibe418292018-03-26 16:14:12 +02004451 mBoostedAppAnimationLayer = null;
Robert Carr5ea304d2019-02-04 16:04:55 -08004452 mHomeAppAnimationLayer.remove();
Jorim Jaggi391790622018-04-18 15:30:44 +02004453 mHomeAppAnimationLayer = null;
Robert Carr5ea304d2019-02-04 16:04:55 -08004454 mSplitScreenDividerAnchor.remove();
Robert Carrf7a7ca82017-12-06 13:17:07 -08004455 mSplitScreenDividerAnchor = null;
Robert Carrb1579c82017-09-05 14:54:47 -07004456 }
4457 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004458 }
4459
Robert Carree4d4b92017-11-22 12:21:46 -08004460 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004461 AboveAppWindowContainers(String name, WindowManagerService service) {
4462 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08004463 }
4464
Robert Carrb9506032018-02-13 13:54:00 -08004465 @Override
Vishnu Nair83537a72018-07-19 21:27:48 -07004466 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
4467 final SurfaceControl.Builder builder = super.makeChildSurface(child);
4468 if (child instanceof WindowToken && ((WindowToken) child).mRoundedCornerOverlay) {
4469 // To draw above the ColorFade layer during the screen off transition, the
4470 // rounded corner overlays need to be at the root of the surface hierarchy.
4471 // TODO: move the ColorLayer into the display overlay layer such that this is not
4472 // necessary anymore.
4473 builder.setParent(null);
4474 }
4475 return builder;
4476 }
4477
4478 @Override
Robert Carrb9506032018-02-13 13:54:00 -08004479 void assignChildLayers(SurfaceControl.Transaction t) {
4480 assignChildLayers(t, null /* imeContainer */);
4481 }
4482
Robert Carree4d4b92017-11-22 12:21:46 -08004483 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
4484 boolean needAssignIme = imeContainer != null
4485 && imeContainer.getSurfaceControl() != null;
4486 for (int j = 0; j < mChildren.size(); ++j) {
4487 final WindowToken wt = mChildren.get(j);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004488
4489 // See {@link mSplitScreenDividerAnchor}
4490 if (wt.windowType == TYPE_DOCK_DIVIDER) {
4491 wt.assignRelativeLayer(t, mTaskStackContainers.getSplitScreenDividerAnchor(), 1);
4492 continue;
4493 }
Vishnu Nair83537a72018-07-19 21:27:48 -07004494 if (wt.mRoundedCornerOverlay) {
4495 wt.assignLayer(t, WindowManagerPolicy.COLOR_FADE_LAYER + 1);
4496 continue;
4497 }
Robert Carree4d4b92017-11-22 12:21:46 -08004498 wt.assignLayer(t, j);
4499 wt.assignChildLayers(t);
4500
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004501 int layer = mWmService.mPolicy.getWindowLayerFromTypeLw(
Robert Carree4d4b92017-11-22 12:21:46 -08004502 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08004503
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004504 if (needAssignIme && layer >= mWmService.mPolicy.getWindowLayerFromTypeLw(
Robert Carrb8e78fa2017-11-28 13:07:58 -08004505 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004506 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08004507 needAssignIme = false;
4508 }
4509 }
4510 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004511 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08004512 }
4513 }
4514 }
4515
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004516 /**
4517 * Window container class that contains all containers on this display that are not related to
4518 * Apps. E.g. status bar.
4519 */
Robert Carree4d4b92017-11-22 12:21:46 -08004520 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07004521 /**
4522 * Compares two child window tokens returns -1 if the first is lesser than the second in
4523 * terms of z-order and 1 otherwise.
4524 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004525 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07004526 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004527 mWmService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004528 token1.mOwnerCanManageAppTokens)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004529 < mWmService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004530 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004531
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004532 private final Predicate<WindowState> mGetOrientingWindow = w -> {
4533 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
4534 return false;
4535 }
4536 final int req = w.mAttrs.screenOrientation;
4537 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
4538 || req == SCREEN_ORIENTATION_UNSET) {
4539 return false;
4540 }
4541 return true;
4542 };
4543
Wale Ogunwale3a931692016-11-02 16:49:48 -07004544 private final String mName;
chaviwf29223e2018-01-31 13:23:51 -08004545 private final Dimmer mDimmer = new Dimmer(this);
4546 private final Rect mTmpDimBoundsRect = new Rect();
4547
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004548 NonAppWindowContainers(String name, WindowManagerService service) {
4549 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07004550 mName = name;
4551 }
4552
4553 void addChild(WindowToken token) {
4554 addChild(token, mWindowComparator);
4555 }
4556
4557 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004558 int getOrientation() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004559 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004560 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004561 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004562
4563 if (win != null) {
4564 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004565 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004566 mLastKeyguardForcedOrientation = req;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004567 if (mWmService.mKeyguardGoingAway) {
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07004568 // Keyguard can't affect the orientation if it is going away...
4569 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
4570 return SCREEN_ORIENTATION_UNSET;
4571 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004572 }
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004573 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req
4574 + " for display id=" + mDisplayId);
Andrii Kulian8ee72852017-03-10 10:36:45 -08004575 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004576 }
4577
Andrii Kulian8ee72852017-03-10 10:36:45 -08004578 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004579
Jorim Jaggi1c530592018-04-06 15:11:47 +02004580 // Only allow force setting the orientation when all unknown visibilities have been
4581 // resolved, as otherwise we just may be starting another occluding activity.
4582 final boolean isUnoccluding =
lumark588a3e82018-07-20 18:53:54 +08004583 mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
4584 && mUnknownAppVisibilityController.allResolved();
Jorim Jaggi1c530592018-04-06 15:11:47 +02004585 if (policy.isKeyguardShowingAndNotOccluded() || isUnoccluding) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004586 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004587 }
4588
4589 return SCREEN_ORIENTATION_UNSET;
4590 }
4591
4592 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07004593 String getName() {
4594 return mName;
4595 }
chaviwf29223e2018-01-31 13:23:51 -08004596
4597 @Override
4598 Dimmer getDimmer() {
4599 return mDimmer;
4600 }
4601
4602 @Override
4603 void prepareSurfaces() {
4604 mDimmer.resetDimStates();
4605 super.prepareSurfaces();
4606 getBounds(mTmpDimBoundsRect);
4607
4608 if (mDimmer.updateDims(getPendingTransaction(), mTmpDimBoundsRect)) {
4609 scheduleAnimation();
4610 }
4611 }
Robert Carr3b716242016-08-16 16:02:21 -07004612 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01004613
Robert Carr9034d962018-01-04 18:27:42 -08004614 private class NonMagnifiableWindowContainers extends NonAppWindowContainers {
4615 NonMagnifiableWindowContainers(String name, WindowManagerService service) {
4616 super(name, service);
4617 }
4618
4619 @Override
4620 void applyMagnificationSpec(Transaction t, MagnificationSpec spec) {
4621 }
4622 };
4623
Robert Carrb1579c82017-09-05 14:54:47 -07004624 SurfaceControl.Builder makeSurface(SurfaceSession s) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004625 return mWmService.makeSurfaceBuilder(s)
Robert Carrb1579c82017-09-05 14:54:47 -07004626 .setParent(mWindowingLayer);
4627 }
4628
4629 @Override
4630 SurfaceSession getSession() {
4631 return mSession;
4632 }
4633
4634 @Override
4635 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07004636 SurfaceSession s = child != null ? child.getSession() : getSession();
Chavi Weingarten6ef9cc62019-02-07 16:28:45 +00004637 final SurfaceControl.Builder b = mWmService.makeSurfaceBuilder(s).setContainerLayer();
Robert Carrf59b8dd2017-10-02 18:58:36 -07004638 if (child == null) {
4639 return b;
4640 }
4641
Robert Carree4d4b92017-11-22 12:21:46 -08004642 return b.setName(child.getName())
4643 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07004644 }
4645
4646 /**
4647 * The makeSurface variants are for use by the window-container
4648 * hierarchy. makeOverlay here is a function for various non windowing
4649 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
4650 * and other potpourii.
4651 */
4652 SurfaceControl.Builder makeOverlay() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004653 return mWmService.makeSurfaceBuilder(mSession)
Robert Carrb1579c82017-09-05 14:54:47 -07004654 .setParent(mOverlayLayer);
4655 }
4656
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004657 /**
4658 * Reparents the given surface to mOverlayLayer.
4659 */
4660 void reparentToOverlay(Transaction transaction, SurfaceControl surface) {
Robert Carr10584fa2019-01-14 15:55:19 -08004661 transaction.reparent(surface, mOverlayLayer);
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004662 }
4663
Robert Carrb1579c82017-09-05 14:54:47 -07004664 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carr24be9ab2018-04-30 17:54:53 -07004665 if (spec.scale != 1.0) {
4666 mMagnificationSpec = spec;
4667 } else {
4668 mMagnificationSpec = null;
4669 }
4670
Robert Carrf59b8dd2017-10-02 18:58:36 -07004671 applyMagnificationSpec(getPendingTransaction(), spec);
4672 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07004673 }
4674
Robert Carr24be9ab2018-04-30 17:54:53 -07004675 void reapplyMagnificationSpec() {
4676 if (mMagnificationSpec != null) {
4677 applyMagnificationSpec(getPendingTransaction(), mMagnificationSpec);
4678 }
4679 }
4680
Robert Carrb1579c82017-09-05 14:54:47 -07004681 @Override
Riddle Hsu3a4bb612019-01-31 00:02:22 +08004682 void onParentChanged() {
Robert Carrb1579c82017-09-05 14:54:47 -07004683 // Since we are the top of the SurfaceControl hierarchy here
4684 // we create the root surfaces explicitly rather than chaining
Riddle Hsu3a4bb612019-01-31 00:02:22 +08004685 // up as the default implementation in onParentChanged does. So we
Robert Carrb1579c82017-09-05 14:54:47 -07004686 // explicitly do NOT call super here.
4687 }
4688
4689 @Override
4690 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07004691
4692 // These are layers as children of "mWindowingLayer"
4693 mBelowAppWindowsContainers.assignLayer(t, 0);
4694 mTaskStackContainers.assignLayer(t, 1);
4695 mAboveAppWindowsContainers.assignLayer(t, 2);
4696
lumarkff0ab692018-11-05 20:32:30 +08004697 final WindowState imeTarget = mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08004698 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08004699
Robert Carree4d4b92017-11-22 12:21:46 -08004700 // In the case where we have an IME target that is not in split-screen
4701 // mode IME assignment is easy. We just need the IME to go directly above
4702 // the target. This way children of the target will naturally go above the IME
4703 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08004704 //
Robert Carree4d4b92017-11-22 12:21:46 -08004705 // In the case of split-screen windowing mode, we need to elevate the IME above the
4706 // docked divider while keeping the app itself below the docked divider, so instead
4707 // we use relative layering of the IME targets child windows, and place the
4708 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08004709 //
Robert Carr234b6332018-03-20 13:38:16 -07004710 // In the case the IME target is animating, the animation Z order may be different
4711 // than the WindowContainer Z order, so it's difficult to be sure we have the correct
4712 // IME target. In this case we just layer the IME over all transitions by placing it in the
4713 // above applications layer.
4714 //
Robert Carree4d4b92017-11-22 12:21:46 -08004715 // In the case where we have no IME target we assign it where it's base layer would
4716 // place it in the AboveAppWindowContainers.
Robert Carr234b6332018-03-20 13:38:16 -07004717 if (imeTarget != null && !(imeTarget.inSplitScreenWindowingMode()
4718 || imeTarget.mToken.isAppAnimating())
Robert Carree4d4b92017-11-22 12:21:46 -08004719 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004720 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07004721 // TODO: We need to use an extra level on the app surface to ensure
4722 // this is always above SurfaceView but always below attached window.
4723 1);
Robert Carree4d4b92017-11-22 12:21:46 -08004724 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07004725 }
4726
4727 // Above we have assigned layers to our children, now we ask them to assign
4728 // layers to their children.
4729 mBelowAppWindowsContainers.assignChildLayers(t);
4730 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08004731 mAboveAppWindowsContainers.assignChildLayers(t,
4732 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07004733 mImeWindowsContainers.assignChildLayers(t);
4734 }
4735
4736 /**
4737 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
4738 * that the IME target is one of the docked applications. We'd like the docked divider to be
4739 * above both of the applications, and we'd like the IME to be above the docked divider.
4740 * However we need child windows of the applications to be above the IME (Text drag handles).
4741 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
4742 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
Tiger Huang7c610aa2018-10-27 00:01:01 +08004743 * with {@link WindowState#assignLayer}
Robert Carrb1579c82017-09-05 14:54:47 -07004744 */
4745 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
Robert Carrbb5de662018-04-26 13:29:51 -07004746 child.assignRelativeLayer(t, mImeWindowsContainers.getSurfaceControl(), 1);
Robert Carrb1579c82017-09-05 14:54:47 -07004747 }
4748
4749 @Override
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004750 void prepareSurfaces() {
4751 final ScreenRotationAnimation screenRotationAnimation =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004752 mWmService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004753 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
4754 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
4755 mPendingTransaction.setMatrix(mWindowingLayer,
4756 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
4757 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
4758 mPendingTransaction.setPosition(mWindowingLayer,
4759 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
4760 mPendingTransaction.setAlpha(mWindowingLayer,
4761 screenRotationAnimation.getEnterTransformation().getAlpha());
4762 }
Chavi Weingartenb736e322018-02-23 00:27:54 +00004763
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004764 super.prepareSurfaces();
4765 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004766
Jorim Jaggibe418292018-03-26 16:14:12 +02004767 void assignStackOrdering() {
4768 mTaskStackContainers.assignStackOrdering(getPendingTransaction());
Robert Carr2f8aa392018-01-31 14:46:51 -08004769 }
Chavi Weingarten3a748552018-05-14 17:32:42 +00004770
4771 /**
4772 * Increment the deferral count to determine whether to update the IME target.
4773 */
4774 void deferUpdateImeTarget() {
4775 mDeferUpdateImeTargetCount++;
4776 }
4777
4778 /**
4779 * Decrement the deferral count to determine whether to update the IME target. If the count
4780 * reaches 0, a new ime target will get computed.
4781 */
4782 void continueUpdateImeTarget() {
4783 if (mDeferUpdateImeTargetCount == 0) {
4784 return;
4785 }
4786
4787 mDeferUpdateImeTargetCount--;
4788 if (mDeferUpdateImeTargetCount == 0) {
4789 computeImeTarget(true /* updateImeTarget */);
4790 }
4791 }
4792
4793 /**
4794 * @return Whether a new IME target should be computed.
4795 */
4796 private boolean canUpdateImeTarget() {
4797 return mDeferUpdateImeTargetCount == 0;
4798 }
Arthur Hung95b38a92018-07-20 18:56:12 +08004799
4800 InputMonitor getInputMonitor() {
4801 return mInputMonitor;
4802 }
Jorim Jaggif1292892018-09-10 11:58:13 +02004803
4804 /**
4805 * @return Cached value whether we told display manager that we have content.
4806 */
4807 boolean getLastHasContent() {
4808 return mLastHasContent;
4809 }
Arthur Hungbe5ce212018-09-13 18:41:56 +08004810
4811 void registerPointerEventListener(@NonNull PointerEventListener listener) {
Riddle Hsu2588ab02019-02-25 14:23:56 +08004812 mPointerEventDispatcher.registerInputEventListener(listener);
Arthur Hungbe5ce212018-09-13 18:41:56 +08004813 }
4814
4815 void unregisterPointerEventListener(@NonNull PointerEventListener listener) {
Riddle Hsu2588ab02019-02-25 14:23:56 +08004816 mPointerEventDispatcher.unregisterInputEventListener(listener);
Arthur Hungbe5ce212018-09-13 18:41:56 +08004817 }
lumark588a3e82018-07-20 18:53:54 +08004818
4819 void prepareAppTransition(@WindowManager.TransitionType int transit,
Wale Ogunwale3a256e62018-12-06 14:41:18 -08004820 boolean alwaysKeepCurrent) {
4821 prepareAppTransition(transit, alwaysKeepCurrent, 0 /* flags */, false /* forceOverride */);
4822 }
4823
4824 void prepareAppTransition(@WindowManager.TransitionType int transit,
lumark588a3e82018-07-20 18:53:54 +08004825 boolean alwaysKeepCurrent, @WindowManager.TransitionFlags int flags,
4826 boolean forceOverride) {
4827 final boolean prepared = mAppTransition.prepareAppTransitionLocked(
4828 transit, alwaysKeepCurrent, flags, forceOverride);
4829 if (prepared && okToAnimate()) {
4830 mSkipAppTransitionAnimation = false;
4831 }
4832 }
4833
4834 void executeAppTransition() {
4835 if (mAppTransition.isTransitionSet()) {
4836 if (DEBUG_APP_TRANSITIONS) {
4837 Slog.w(TAG_WM, "Execute app transition: " + mAppTransition + ", displayId: "
4838 + mDisplayId + " Callers=" + Debug.getCallers(5));
4839 }
4840 mAppTransition.setReady();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004841 mWmService.mWindowPlacerLocked.requestTraversal();
lumark588a3e82018-07-20 18:53:54 +08004842 }
4843 }
4844
4845 /**
4846 * Update pendingLayoutChanges after app transition has finished.
4847 */
4848 void handleAnimatingStoppedAndTransition() {
4849 int changes = 0;
4850
4851 mAppTransition.setIdle();
4852
4853 for (int i = mNoAnimationNotifyOnTransitionFinished.size() - 1; i >= 0; i--) {
4854 final IBinder token = mNoAnimationNotifyOnTransitionFinished.get(i);
4855 mAppTransition.notifyAppTransitionFinishedLocked(token);
4856 }
4857 mNoAnimationNotifyOnTransitionFinished.clear();
4858
4859 mWallpaperController.hideDeferredWallpapersIfNeeded();
4860
4861 onAppTransitionDone();
4862
4863 changes |= FINISH_LAYOUT_REDO_LAYOUT;
4864 if (DEBUG_WALLPAPER_LIGHT) {
4865 Slog.v(TAG_WM, "Wallpaper layer changed: assigning layers + relayout");
4866 }
4867 computeImeTarget(true /* updateImeTarget */);
wilsonshihc32538e2018-11-07 17:27:34 +08004868 mWallpaperMayChange = true;
lumark588a3e82018-07-20 18:53:54 +08004869 // Since the window list has been rebuilt, focus might have to be recomputed since the
4870 // actual order of windows might have changed again.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004871 mWmService.mFocusMayChange = true;
lumark588a3e82018-07-20 18:53:54 +08004872
4873 pendingLayoutChanges |= changes;
4874 }
Andrii Kulian15cfb422018-11-07 13:38:49 -08004875
Wale Ogunwale3a256e62018-12-06 14:41:18 -08004876 /** Check if pending app transition is for activity / task launch. */
4877 boolean isNextTransitionForward() {
4878 final int transit = mAppTransition.getAppTransition();
4879 return transit == TRANSIT_ACTIVITY_OPEN
4880 || transit == TRANSIT_TASK_OPEN
4881 || transit == TRANSIT_TASK_TO_FRONT;
4882 }
4883
Andrii Kulian15cfb422018-11-07 13:38:49 -08004884 /**
4885 * @see Display#FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS
4886 */
4887 boolean supportsSystemDecorations() {
Andrii Kuliandd989612019-02-21 12:13:28 -08004888 return mWmService.mDisplayWindowSettings.shouldShowSystemDecorsLocked(this)
4889 || (mDisplay.getFlags() & FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS) != 0
Andrii Kulian15cfb422018-11-07 13:38:49 -08004890 // TODO (b/111363427): Remove this and set the new FLAG_SHOULD_SHOW_LAUNCHER flag
Andrii Kuliandd989612019-02-21 12:13:28 -08004891 // whenever vr 2d display id is set.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004892 || mDisplayId == mWmService.mVr2dDisplayId
4893 || mWmService.mForceDesktopModeOnExternalDisplays;
Andrii Kulian15cfb422018-11-07 13:38:49 -08004894 }
chaviwff2e7d82018-11-02 11:11:27 -07004895
Tiger Huang04dc4cc2019-01-17 18:41:41 +08004896 /**
chaviwff2e7d82018-11-02 11:11:27 -07004897 * Re-parent the DisplayContent's top surfaces, {@link #mWindowingLayer} and
4898 * {@link #mOverlayLayer} to the specified surfaceControl.
4899 *
Tiger Huang04dc4cc2019-01-17 18:41:41 +08004900 * @param sc The new SurfaceControl, where the DisplayContent's surfaces will be re-parented to.
chaviwff2e7d82018-11-02 11:11:27 -07004901 */
Robert Carr10584fa2019-01-14 15:55:19 -08004902 void reparentDisplayContent(SurfaceControl sc) {
Tiger Huang04dc4cc2019-01-17 18:41:41 +08004903 mParentSurfaceControl = sc;
4904 if (mPortalWindowHandle == null) {
4905 mPortalWindowHandle = createPortalWindowHandle(sc.toString());
4906 }
4907 mPendingTransaction.setInputWindowInfo(sc, mPortalWindowHandle)
4908 .reparent(mWindowingLayer, sc).reparent(mOverlayLayer, sc);
4909 }
4910
Tarandeep Singha6f35612019-01-11 19:50:46 -08004911 @VisibleForTesting
4912 SurfaceControl getWindowingLayer() {
4913 return mWindowingLayer;
4914 }
4915
Tiger Huang04dc4cc2019-01-17 18:41:41 +08004916 /**
4917 * Create a portal window handle for input. This window transports any touch to the display
4918 * indicated by {@link InputWindowHandle#portalToDisplayId} if the touch hits this window.
4919 *
4920 * @param name The name of the portal window handle.
4921 * @return the new portal window handle.
4922 */
4923 private InputWindowHandle createPortalWindowHandle(String name) {
4924 // Let surface flinger to set the display ID of this input window handle because we don't
4925 // know which display the parent surface control is on.
4926 final InputWindowHandle portalWindowHandle = new InputWindowHandle(
4927 null /* inputApplicationHandle */, null /* clientWindow */, INVALID_DISPLAY);
4928 portalWindowHandle.name = name;
4929 portalWindowHandle.token = new Binder();
4930 portalWindowHandle.layoutParamsFlags =
4931 FLAG_SPLIT_TOUCH | FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL;
4932 getBounds(mTmpBounds);
4933 portalWindowHandle.touchableRegion.set(mTmpBounds);
4934 portalWindowHandle.scaleFactor = 1f;
4935 portalWindowHandle.ownerPid = Process.myPid();
4936 portalWindowHandle.ownerUid = Process.myUid();
4937 portalWindowHandle.portalToDisplayId = mDisplayId;
4938 return portalWindowHandle;
chaviwff2e7d82018-11-02 11:11:27 -07004939 }
Issei Suzukia5dbf522019-02-01 17:58:15 +01004940
4941 /**
4942 * @see IWindowManager#setForwardedInsets
4943 */
4944 public void setForwardedInsets(Insets insets) {
4945 if (insets == null) {
4946 insets = Insets.NONE;
4947 }
4948 if (mDisplayPolicy.getForwardedInsets().equals(insets)) {
4949 return;
4950 }
4951 mDisplayPolicy.setForwardedInsets(insets);
4952 setLayoutNeeded();
4953 mWmService.mWindowPlacerLocked.requestTraversal();
4954 }
Susi Kharraz-Post9893b8c2019-02-12 14:21:29 -05004955
4956 protected MetricsLogger getMetricsLogger() {
4957 if (mMetricsLogger == null) {
4958 mMetricsLogger = new MetricsLogger();
4959 }
4960 return mMetricsLogger;
4961 }
Craig Mautner59c00972012-07-30 12:10:24 -07004962}