blob: 871ceaf4d231badcbe3b97d5806d4ed9371039bd [file] [log] [blame]
Craig Mautner59c00972012-07-30 12:10:24 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wm;
18
Wale Ogunwale65ebd952018-04-25 15:41:44 -070019import static android.app.ActivityTaskManager.SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT;
Wale Ogunwale61911492017-10-11 08:50:50 -070020import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070021import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070022import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070023import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
24import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwalea77e1462016-09-28 10:09:46 -070025import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070026import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
27import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale687b4272017-07-27 02:56:23 -070028import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
29import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070030import static android.view.Display.DEFAULT_DISPLAY;
31import static android.view.Display.FLAG_PRIVATE;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070032import static android.view.Surface.ROTATION_0;
33import static android.view.Surface.ROTATION_180;
34import static android.view.Surface.ROTATION_270;
35import static android.view.Surface.ROTATION_90;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070036import static android.view.View.GONE;
Jorim Jaggif96c90a2018-09-26 16:55:15 +020037import static android.view.InsetsState.TYPE_IME;
Wale Ogunwale10124582016-09-15 20:25:50 -070038import static android.view.WindowManager.DOCKED_BOTTOM;
39import static android.view.WindowManager.DOCKED_INVALID;
40import static android.view.WindowManager.DOCKED_TOP;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080041import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
42import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
43import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale494009b82016-10-21 09:01:38 -070044import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070045import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070046import static android.view.WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE;
47import static android.view.WindowManager.LayoutParams.NEEDS_MENU_UNSET;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070048import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale494009b82016-10-21 09:01:38 -070049import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070050import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale494009b82016-10-21 09:01:38 -070051import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
Robert Carrf7a7ca82017-12-06 13:17:07 -080052import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
Wale Ogunwale494009b82016-10-21 09:01:38 -070053import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070054import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070055import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
56import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Wale Ogunwale494009b82016-10-21 09:01:38 -070057import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070058import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
59import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070060import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070061import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080062import static android.view.WindowManager.TRANSIT_KEYGUARD_UNOCCLUDE;
63
Adrian Roose99bc052017-11-20 17:55:31 +010064import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
65import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
66import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
67import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080068import static com.android.server.wm.DisplayContentProto.ABOVE_APP_WINDOWS;
lumark588a3e82018-07-20 18:53:54 +080069import static com.android.server.wm.DisplayContentProto.APP_TRANSITION;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080070import static com.android.server.wm.DisplayContentProto.BELOW_APP_WINDOWS;
71import static com.android.server.wm.DisplayContentProto.DISPLAY_FRAMES;
72import static com.android.server.wm.DisplayContentProto.DISPLAY_INFO;
73import static com.android.server.wm.DisplayContentProto.DOCKED_STACK_DIVIDER_CONTROLLER;
74import static com.android.server.wm.DisplayContentProto.DPI;
Tiger Huang1e5b10a2018-07-30 20:19:51 +080075import static com.android.server.wm.DisplayContentProto.FOCUSED_APP;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080076import static com.android.server.wm.DisplayContentProto.ID;
77import static com.android.server.wm.DisplayContentProto.IME_WINDOWS;
78import static com.android.server.wm.DisplayContentProto.PINNED_STACK_CONTROLLER;
79import static com.android.server.wm.DisplayContentProto.ROTATION;
80import static com.android.server.wm.DisplayContentProto.SCREEN_ROTATION_ANIMATION;
81import static com.android.server.wm.DisplayContentProto.STACKS;
82import static com.android.server.wm.DisplayContentProto.WINDOW_CONTAINER;
Wale Ogunwale1666e312016-12-16 11:27:18 -080083import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
lumark588a3e82018-07-20 18:53:54 +080084import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_APP_TRANSITIONS;
Wale Ogunwale494009b82016-10-21 09:01:38 -070085import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070086import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070087import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
88import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070089import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070090import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070091import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020092import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070093import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070094import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070095import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -080096import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070097import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070098import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070099import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700100import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800101import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700102import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800103import static com.android.server.wm.WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800104import static com.android.server.wm.WindowManagerService.H.REPORT_LOSING_FOCUS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700105import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
106import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700107import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700108import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700109import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
110import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800111import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_PLACING_SURFACES;
112import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_REMOVING_FOCUS;
113import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_ASSIGN_LAYERS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700114import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700115import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700116import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700117import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700118import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700119import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700120import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700121import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700122import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700123
lumark588a3e82018-07-20 18:53:54 +0800124import android.animation.AnimationHandler;
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700125import android.annotation.CallSuper;
Riddle Hsuf53da812018-08-15 22:00:27 +0800126import android.annotation.IntDef;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700127import android.annotation.NonNull;
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200128import android.annotation.Nullable;
Dean Harding3e5a1522017-10-06 09:19:01 -0700129import android.content.pm.PackageManager;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700130import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700131import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700132import android.graphics.Bitmap;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700133import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800134import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700135import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700136import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100137import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700138import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700139import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700140import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700141import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700142import android.os.RemoteException;
143import android.os.SystemClock;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100144import android.os.Trace;
Riddle Hsuf53da812018-08-15 22:00:27 +0800145import android.os.UserHandle;
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800146import android.util.ArraySet;
Chong Zhang8e89b312015-09-09 15:09:30 -0700147import android.util.DisplayMetrics;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700148import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700149import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700150import android.view.Display;
Adrian Roos1cf585052018-01-03 18:43:27 +0100151import android.view.DisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700152import android.view.DisplayInfo;
Riddle Hsua4d6fa22018-08-11 00:50:39 +0800153import android.view.Gravity;
Arthur Hungbe5ce212018-09-13 18:41:56 +0800154import android.view.InputChannel;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700155import android.view.InputDevice;
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200156import android.view.InsetsState.InternalInsetType;
Robert Carrb1579c82017-09-05 14:54:47 -0700157import android.view.MagnificationSpec;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700158import android.view.Surface;
159import android.view.SurfaceControl;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100160import android.view.SurfaceControl.Transaction;
Robert Carrb1579c82017-09-05 14:54:47 -0700161import android.view.SurfaceSession;
Tiger Huang7c610aa2018-10-27 00:01:01 +0800162import android.view.View;
lumark588a3e82018-07-20 18:53:54 +0800163import android.view.WindowManager;
Evan Rosky39b6f232018-10-30 18:35:41 -0700164import android.view.WindowManagerPolicyConstants.PointerEventListener;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700165
Bryce Lee48f4b572017-04-10 10:54:15 -0700166import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800167import com.android.internal.util.ToBooleanFunction;
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200168import com.android.internal.util.function.TriConsumer;
Adrian Roose99bc052017-11-20 17:55:31 +0100169import com.android.server.policy.WindowManagerPolicy;
Issei Suzuki43190bd2018-08-20 17:28:41 +0200170import com.android.server.wm.utils.DisplayRotationUtil;
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100171import com.android.server.wm.utils.RotationCache;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100172import com.android.server.wm.utils.WmDisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700173
174import java.io.PrintWriter;
Riddle Hsuf53da812018-08-15 22:00:27 +0800175import java.lang.annotation.Retention;
176import java.lang.annotation.RetentionPolicy;
Craig Mautner59c00972012-07-30 12:10:24 -0700177import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700178import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700179import java.util.HashMap;
180import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700181import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700182import java.util.List;
Adrian Roos1cf585052018-01-03 18:43:27 +0100183import java.util.Objects;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800184import java.util.function.Consumer;
185import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700186
Craig Mautner59c00972012-07-30 12:10:24 -0700187/**
188 * Utility class for keeping track of the WindowStates and other pertinent contents of a
189 * particular Display.
Craig Mautner59c00972012-07-30 12:10:24 -0700190 */
Riddle Hsuad256a12018-07-18 16:11:30 +0800191class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer>
192 implements WindowManagerPolicy.DisplayContentInfo {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700193 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700194
Riddle Hsuf53da812018-08-15 22:00:27 +0800195 /** The default scaling mode that scales content automatically. */
196 static final int FORCE_SCALING_MODE_AUTO = 0;
197 /** For {@link #setForcedScalingMode} to apply flag {@link Display#FLAG_SCALING_DISABLED}. */
198 static final int FORCE_SCALING_MODE_DISABLED = 1;
199
200 @IntDef(prefix = { "FORCE_SCALING_MODE_" }, value = {
201 FORCE_SCALING_MODE_AUTO,
202 FORCE_SCALING_MODE_DISABLED
203 })
204 @Retention(RetentionPolicy.SOURCE)
205 @interface ForceScalingMode {}
206
Craig Mautner59c00972012-07-30 12:10:24 -0700207 /** Unique identifier of this stack. */
208 private final int mDisplayId;
209
Wale Ogunwale3a931692016-11-02 16:49:48 -0700210 /** The containers below are the only child containers the display can have. */
211 // Contains all window containers that are related to apps (Activities)
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800212 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers(mWmService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700213 // Contains all non-app window containers that should be displayed above the app containers
214 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800215 private final AboveAppWindowContainers mAboveAppWindowsContainers =
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800216 new AboveAppWindowContainers("mAboveAppWindowsContainers", mWmService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700217 // Contains all non-app window containers that should be displayed below the app containers
218 // (e.g. Wallpaper).
219 private final NonAppWindowContainers mBelowAppWindowsContainers =
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800220 new NonAppWindowContainers("mBelowAppWindowsContainers", mWmService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700221 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
222 // 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 -0800223 // window containers together and move them in-sync if/when needed. We use a subclass of
224 // WindowContainer which is omitted from screen magnification, as the IME is never magnified.
225 private final NonMagnifiableWindowContainers mImeWindowsContainers =
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800226 new NonMagnifiableWindowContainers("mImeWindowsContainers", mWmService);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700227
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000228 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800229 private WindowState mTmpWindow2;
230 private WindowAnimator mTmpWindowAnimator;
231 private boolean mTmpRecoveringMemory;
232 private boolean mUpdateImeTarget;
233 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700234 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700235
lumark588a3e82018-07-20 18:53:54 +0800236 final AppTransition mAppTransition;
237 final AppTransitionController mAppTransitionController;
238 boolean mSkipAppTransitionAnimation = false;
239
240 final ArraySet<AppWindowToken> mOpeningApps = new ArraySet<>();
241 final ArraySet<AppWindowToken> mClosingApps = new ArraySet<>();
242 final UnknownAppVisibilityController mUnknownAppVisibilityController;
243 BoundsAnimationController mBoundsAnimationController;
244
245 /**
246 * List of clients without a transtiton animation that we notify once we are done
247 * transitioning since they won't be notified through the app window animator.
248 */
249 final List<IBinder> mNoAnimationNotifyOnTransitionFinished = new ArrayList<>();
250
Wale Ogunwale02319a62016-09-26 15:21:22 -0700251 // Mapping from a token IBinder to a WindowToken object on this display.
252 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
253
Andrii Kuliancd097992017-03-23 18:31:59 -0700254 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700255 int mInitialDisplayWidth = 0;
256 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700257 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700258
Adrian Roos1cf585052018-01-03 18:43:27 +0100259 DisplayCutout mInitialDisplayCutout;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100260 private final RotationCache<DisplayCutout, WmDisplayCutout> mDisplayCutoutCache
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100261 = new RotationCache<>(this::calculateDisplayCutoutForRotationUncached);
Adrian Roos1cf585052018-01-03 18:43:27 +0100262
Andrii Kuliancd097992017-03-23 18:31:59 -0700263 /**
264 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
265 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
266 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
267 */
Craig Mautner59c00972012-07-30 12:10:24 -0700268 int mBaseDisplayWidth = 0;
269 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700270 /**
271 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
272 * but can be set from Settings or via shell command "adb shell wm density".
273 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
274 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700275 int mBaseDisplayDensity = 0;
Riddle Hsuf53da812018-08-15 22:00:27 +0800276
277 /**
278 * Whether to disable display scaling. This can be set via shell command "adb shell wm scaling".
279 * @see WindowManagerService#setForcedDisplayScalingMode(int, int)
280 */
Jeff Brownd46747a2015-04-15 19:02:36 -0700281 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700282 private final DisplayInfo mDisplayInfo = new DisplayInfo();
283 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700284 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800285 private final DisplayPolicy mDisplayPolicy;
286 private DisplayRotation mDisplayRotation;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000287 DisplayFrames mDisplayFrames;
288
Andrii Kulian06d07d62017-03-14 11:11:47 -0700289 /**
290 * For default display it contains real metrics, empty for others.
291 * @see WindowManagerService#createWatermarkInTransaction()
292 */
293 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800294
295 /** @see #computeCompatSmallestWidth(boolean, int, int, int, DisplayCutout) */
Andrii Kulian06d07d62017-03-14 11:11:47 -0700296 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700297
Andrii Kulian06d07d62017-03-14 11:11:47 -0700298 /**
299 * Compat metrics computed based on {@link #mDisplayMetrics}.
300 * @see #updateDisplayAndOrientation(int)
301 */
302 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
303
304 /** The desired scaling factor for compatible apps. */
305 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700306
Andrii Kulian8ee72852017-03-10 10:36:45 -0800307 /**
308 * Current rotation of the display.
309 * Constants as per {@link android.view.Surface.Rotation}.
310 *
Robert Carrae606b42018-02-15 15:36:23 -0800311 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800312 */
313 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700314
Andrii Kulian8ee72852017-03-10 10:36:45 -0800315 /**
316 * Last applied orientation of the display.
317 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
318 *
Riddle Hsu4e611772018-10-31 18:58:28 +0800319 * @see #updateOrientationFromAppTokens()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800320 */
321 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700322
Andrii Kulian8ee72852017-03-10 10:36:45 -0800323 /**
324 * Flag indicating that the application is receiving an orientation that has different metrics
325 * than it expected. E.g. Portrait instead of Landscape.
326 *
Robert Carrae606b42018-02-15 15:36:23 -0800327 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800328 */
329 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700330
Andrii Kulian8ee72852017-03-10 10:36:45 -0800331 /**
332 * Orientation forced by some window. If there is no visible window that specifies orientation
333 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
334 *
335 * @see NonAppWindowContainers#getOrientation()
336 */
337 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700338
Andrii Kulian8ee72852017-03-10 10:36:45 -0800339 /**
340 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
341 * occluded.
342 *
343 * @see NonAppWindowContainers#getOrientation()
344 */
345 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
346
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700347 /**
348 * Keep track of wallpaper visibility to notify changes.
349 */
350 private boolean mLastWallpaperVisible = false;
351
Andrii Kulian06d07d62017-03-14 11:11:47 -0700352 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700353
Craig Mautner39834192012-09-02 07:47:24 -0700354 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700355 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700356 int pendingLayoutChanges;
Riddle Hsu654a6f92018-07-13 22:59:36 +0800357 int mDeferredRotationPauseCount;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800358
Vishnu Nairba183352018-11-21 11:16:49 -0800359 /**
360 * Used to gate application window layout until we have sent the complete configuration.
361 * TODO: There are still scenarios where we may be out of sync with the client. Ideally
362 * we want to replace this flag with a mechanism that will confirm the configuration
363 * applied by the client is the one expected by the system server.
364 */
365 boolean mWaitingForConfig;
366
Andrii Kulian839def92016-11-02 10:58:58 -0700367 // TODO(multi-display): remove some of the usages.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800368 @VisibleForTesting
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800369 boolean isDefaultDisplay;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800370
Andrii Kulianf0379de2018-03-14 16:24:07 -0700371 /**
372 * Flag indicating whether WindowManager should override info for this display in
373 * DisplayManager.
374 */
375 boolean mShouldOverrideDisplayConfiguration = true;
Craig Mautner39834192012-09-02 07:47:24 -0700376
Craig Mautnerdc548482014-02-05 13:35:24 -0800377 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700378 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800379
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700380 /** Detect user tapping outside of current focused task bounds .*/
381 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700382
Craig Mautner6601b7b2013-04-29 10:29:11 -0700383 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700384 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700385
Craig Mautner6601b7b2013-04-29 10:29:11 -0700386 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800387 private final Rect mTmpRect = new Rect();
388 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700389 private final RectF mTmpRectF = new RectF();
390 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800391 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700392
Issei Suzuki43190bd2018-08-20 17:28:41 +0200393
Bryce Leef3c6a472017-11-14 14:53:06 -0800394 /** Used for handing back size of display */
395 private final Rect mTmpBounds = new Rect();
396
Craig Mautner95da1082014-02-24 17:54:35 -0800397 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700398 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800399
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700400 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700401 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700402
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800403 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800404 /** A collection of windows that provide tap exclude regions inside of them. */
405 final ArraySet<WindowState> mTapExcludeProvidingWindows = new ArraySet<>();
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800406
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000407 private boolean mHaveBootMsg = false;
408 private boolean mHaveApp = false;
409 private boolean mHaveWallpaper = false;
410 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700411
412 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
413
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700414 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
415 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000416 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
417 new ApplySurfaceChangesTransactionState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700418
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700419 // True if this display is in the process of being removed. Used to determine if the removal of
420 // the display's direct children should be allowed.
421 private boolean mRemovingDisplay = false;
422
Bryce Leed1871262017-06-12 14:12:29 -0700423 // {@code false} if this display is in the processing of being created.
424 private boolean mDisplayReady = false;
425
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800426 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700427
wilsonshihc32538e2018-11-07 17:27:34 +0800428 boolean mWallpaperMayChange = false;
429
Robert Carrb1579c82017-09-05 14:54:47 -0700430 private final SurfaceSession mSession = new SurfaceSession();
431
432 /**
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800433 * Window that is currently interacting with the user. This window is responsible for receiving
434 * key events and pointer events from the user.
435 */
436 WindowState mCurrentFocus = null;
437
438 /**
439 * The last focused window that we've notified the client that the focus is changed.
440 */
441 WindowState mLastFocus = null;
442
443 /**
444 * Windows that have lost input focus and are waiting for the new focus window to be displayed
445 * before they are told about this.
446 */
447 ArrayList<WindowState> mLosingFocus = new ArrayList<>();
448
449 /**
450 * The foreground app of this display. Windows below this app cannot be the focused window. If
451 * the user taps on the area outside of the task of the focused app, we will notify AM about the
452 * new task the user wants to interact with.
453 */
454 AppWindowToken mFocusedApp = null;
455
456 /** Windows added since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
457 final ArrayList<WindowState> mWinAddedSinceNullFocus = new ArrayList<>();
458
459 /** Windows removed since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
460 final ArrayList<WindowState> mWinRemovedSinceNullFocus = new ArrayList<>();
461
462 /**
Robert Carrb1579c82017-09-05 14:54:47 -0700463 * We organize all top-level Surfaces in to the following layers.
464 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800465 * and omitted from Screen-Magnification, for example the strict mode flash or
466 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700467 * {@link #mWindowingLayer} contains everything else.
468 */
469 private SurfaceControl mOverlayLayer;
470
471 /**
472 * See {@link #mOverlayLayer}
473 */
474 private SurfaceControl mWindowingLayer;
475
476 /**
Adrian Roos5251b1d2018-03-23 18:57:43 +0100477 * Sequence number for the current layout pass.
478 */
479 int mLayoutSeq = 0;
480
Chavi Weingarten3a748552018-05-14 17:32:42 +0000481 /**
482 * Specifies the count to determine whether to defer updating the IME target until ready.
483 */
484 private int mDeferUpdateImeTargetCount;
485
Jorim Jaggi9af095b2017-12-12 17:18:57 +0100486 /** Temporary float array to retrieve 3x3 matrix values. */
487 private final float[] mTmpFloats = new float[9];
488
Robert Carr24be9ab2018-04-30 17:54:53 -0700489 private MagnificationSpec mMagnificationSpec;
490
Arthur Hung95b38a92018-07-20 18:56:12 +0800491 private InputMonitor mInputMonitor;
492
Jorim Jaggif1292892018-09-10 11:58:13 +0200493 /** Caches the value whether told display manager that we have content. */
494 private boolean mLastHasContent;
495
Issei Suzuki43190bd2018-08-20 17:28:41 +0200496 private DisplayRotationUtil mRotationUtil = new DisplayRotationUtil();
497
lumark90120a82018-08-15 00:33:03 +0800498 /**
499 * The input method window for this display.
500 */
501 WindowState mInputMethodWindow;
502
lumarkff0ab692018-11-05 20:32:30 +0800503 /**
504 * This just indicates the window the input method is on top of, not
505 * necessarily the window its input is going to.
506 */
507 WindowState mInputMethodTarget;
508
509 /** If true hold off on modifying the animation layer of mInputMethodTarget */
510 boolean mInputMethodTargetWaitingAnim;
511
Arthur Hungbe5ce212018-09-13 18:41:56 +0800512 private final PointerEventDispatcher mPointerEventDispatcher;
513
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200514 private final InsetsStateController mInsetsStateController;
515
Tiger Huang7c610aa2018-10-27 00:01:01 +0800516 // Last systemUiVisibility we received from status bar.
517 private int mLastStatusBarVisibility = 0;
518 // Last systemUiVisibility we dispatched to windows.
519 private int mLastDispatchedSystemUiVisibility = 0;
520
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800521 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
522 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800523 final AppWindowToken atoken = w.mAppToken;
524 if (winAnimator.mDrawState == READY_TO_SHOW) {
Jorim Jaggiaf0d6d22018-06-08 15:25:35 +0200525 if (atoken == null || atoken.canShowWindows()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800526 if (w.performShowLocked()) {
527 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
528 if (DEBUG_LAYOUT_REPEATS) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800529 mWmService.mWindowPlacerLocked.debugLayoutRepeats(
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800530 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
531 }
532 }
533 }
534 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800535 };
536
537 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
538 final WindowStateAnimator winAnimator = w.mWinAnimator;
539 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
540 return;
541 }
542
Jorim Jaggi8f520872018-08-14 17:00:20 +0200543 // If this window is animating, ensure the animation background is set.
544 final AnimationAdapter anim = w.mAppToken != null
545 ? w.mAppToken.getAnimation()
546 : w.getAnimation();
547 if (anim != null) {
548 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800549 if (color != 0) {
550 final TaskStack stack = w.getStack();
551 if (stack != null) {
552 stack.setAnimationBackground(winAnimator, color);
553 }
554 }
555 }
556 };
557
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800558 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
559 final int lostFocusUid = mTmpWindow.mOwnerUid;
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800560 final Handler handler = mWmService.mH;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800561 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
562 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
563 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
564 w.mAttrs.hideTimeoutMilliseconds);
565 }
566 }
567 };
568
569 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800570 final AppWindowToken focusedApp = mFocusedApp;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800571 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
572 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
573
574 if (!w.canReceiveKeys()) {
575 return false;
576 }
577
578 final AppWindowToken wtoken = w.mAppToken;
579
580 // If this window's application has been removed, just skip it.
581 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
582 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
583 + (wtoken.removed ? "removed" : "sendingToBottom"));
584 return false;
585 }
586
587 if (focusedApp == null) {
588 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
589 + " using new focus @ " + w);
590 mTmpWindow = w;
591 return true;
592 }
593
594 if (!focusedApp.windowsAreFocusable()) {
595 // Current focused app windows aren't focusable...
596 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
597 + " focusable using new focus @ " + w);
598 mTmpWindow = w;
599 return true;
600 }
601
602 // Descend through all of the app tokens and find the first that either matches
603 // win.mAppToken (return win) or mFocusedApp (return null).
604 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
605 if (focusedApp.compareTo(wtoken) > 0) {
606 // App stack below focused app stack. No focus for you!!!
607 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
608 "findFocusedWindow: Reached focused app=" + focusedApp);
609 mTmpWindow = null;
610 return true;
611 }
612 }
613
614 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
615 mTmpWindow = w;
616 return true;
617 };
618
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800619 private final Consumer<WindowState> mPerformLayout = w -> {
620 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
621 // wasting time and funky changes while a window is animating away.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800622 final boolean gone = (mTmpWindow != null && mWmService.mPolicy.canBeHiddenByKeyguardLw(w))
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800623 || w.isGoneForLayoutLw();
624
625 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
626 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
627 + " mLayoutAttached=" + w.mLayoutAttached
628 + " screen changed=" + w.isConfigChanged());
629 final AppWindowToken atoken = w.mAppToken;
630 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200631 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800632 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
633 + " parentHidden=" + w.isParentWindowHidden());
634 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200635 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800636 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
637 + " parentHidden=" + w.isParentWindowHidden());
638 }
639
640 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
641 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
642 // since that means "perform layout as normal, just don't display").
643 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
644 || ((w.isConfigChanged() || w.setReportResizeHints())
645 && !w.isGoneForLayoutLw() &&
646 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
647 (w.mHasSurface && w.mAppToken != null &&
648 w.mAppToken.layoutConfigChanges)))) {
649 if (!w.mLayoutAttached) {
650 if (mTmpInitial) {
651 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700652 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800653 }
654 if (w.mAttrs.type == TYPE_DREAM) {
655 // Don't layout windows behind a dream, so that if it does stuff like hide
656 // the status bar we won't get a bad transition when it goes away.
657 mTmpWindow = w;
658 }
659 w.mLayoutNeeded = false;
660 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200661 final boolean firstLayout = !w.isLaidOut();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800662 getDisplayPolicy().layoutWindowLw(w, null, mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100663 w.mLayoutSeq = mLayoutSeq;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800664
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200665 // If this is the first layout, we need to initialize the last inset values as
666 // otherwise we'd immediately cause an unnecessary resize.
667 if (firstLayout) {
668 w.updateLastInsetValues();
669 }
670
Adrian Roos23df3a32018-03-15 15:41:13 +0100671 if (w.mAppToken != null) {
672 w.mAppToken.layoutLetterbox(w);
673 }
674
chaviw492139a2018-07-16 16:07:35 -0700675 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700676 + " mContainingFrame=" + w.getContainingFrame()
677 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800678 }
679 }
680 };
681
682 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
683 if (w.mLayoutAttached) {
684 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
685 + " mViewVisibility=" + w.mViewVisibility
686 + " mRelayoutCalled=" + w.mRelayoutCalled);
687 // If this view is GONE, then skip it -- keep the current frame, and let the caller
688 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
689 // windows, since that means "perform layout as normal, just don't display").
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800690 if (mTmpWindow != null && mWmService.mPolicy.canBeHiddenByKeyguardLw(w)) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800691 return;
692 }
693 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
694 || w.mLayoutNeeded) {
695 if (mTmpInitial) {
696 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700697 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800698 }
699 w.mLayoutNeeded = false;
700 w.prelayout();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800701 getDisplayPolicy().layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100702 w.mLayoutSeq = mLayoutSeq;
chaviw492139a2018-07-16 16:07:35 -0700703 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700704 + " mContainingFrame=" + w.getContainingFrame()
705 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800706 }
707 } else if (w.mAttrs.type == TYPE_DREAM) {
708 // Don't layout windows behind a dream, so that if it does stuff like hide the
709 // status bar we won't get a bad transition when it goes away.
710 mTmpWindow = mTmpWindow2;
711 }
712 };
713
714 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
715 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
716 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
717 return w.canBeImeTarget();
718 };
719
720 private final Consumer<WindowState> mApplyPostLayoutPolicy =
Tiger Huang7c610aa2018-10-27 00:01:01 +0800721 w -> getDisplayPolicy().applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
lumarkff0ab692018-11-05 20:32:30 +0800722 mInputMethodTarget);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800723
724 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800725 final WindowSurfacePlacer surfacePlacer = mWmService.mWindowPlacerLocked;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800726 final boolean obscuredChanged = w.mObscured !=
727 mTmpApplySurfaceChangesTransactionState.obscured;
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800728 final RootWindowContainer root = mWmService.mRoot;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800729
730 // Update effect.
731 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
732 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
733 final boolean isDisplayed = w.isDisplayedLw();
734
735 if (isDisplayed && w.isObscuringDisplay()) {
736 // This window completely covers everything behind it, so we want to leave all
737 // of them as undimmed (for performance reasons).
738 root.mObscuringWindow = w;
739 mTmpApplySurfaceChangesTransactionState.obscured = true;
740 }
741
742 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
743 root.handleNotObscuredLocked(w,
744 mTmpApplySurfaceChangesTransactionState.obscured,
745 mTmpApplySurfaceChangesTransactionState.syswin);
746
747 if (w.mHasSurface && isDisplayed) {
748 final int type = w.mAttrs.type;
749 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
750 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
751 mTmpApplySurfaceChangesTransactionState.syswin = true;
752 }
753 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
754 && w.mAttrs.preferredRefreshRate != 0) {
755 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
756 = w.mAttrs.preferredRefreshRate;
757 }
758 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
759 && w.mAttrs.preferredDisplayModeId != 0) {
760 mTmpApplySurfaceChangesTransactionState.preferredModeId
761 = w.mAttrs.preferredDisplayModeId;
762 }
763 }
764 }
765
wilsonshihc32538e2018-11-07 17:27:34 +0800766 if (obscuredChanged && w.isVisibleLw() && mWallpaperController.isWallpaperTarget(w)) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800767 // This is the wallpaper target and its obscured state changed... make sure the
768 // current wallpaper's visibility has been updated accordingly.
769 mWallpaperController.updateWallpaperVisibility();
770 }
771
chaviw161ea3e2018-01-31 12:01:12 -0800772 w.handleWindowMovedIfNeeded();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800773
774 final WindowStateAnimator winAnimator = w.mWinAnimator;
775
776 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
chaviw1454b392018-08-06 09:54:04 -0700777 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800778
779 // Moved from updateWindowsAndWallpaperLocked().
780 if (w.mHasSurface) {
781 // Take care of the window being ready to display.
782 final boolean committed = winAnimator.commitFinishDrawingLocked();
783 if (isDefaultDisplay && committed) {
784 if (w.mAttrs.type == TYPE_DREAM) {
785 // HACK: When a dream is shown, it may at that point hide the lock screen.
786 // So we need to redo the layout to let the phone window manager make this
787 // happen.
788 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
789 if (DEBUG_LAYOUT_REPEATS) {
790 surfacePlacer.debugLayoutRepeats(
791 "dream and commitFinishDrawingLocked true",
792 pendingLayoutChanges);
793 }
794 }
795 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
796 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
797 "First draw done in potential wallpaper target " + w);
wilsonshihc32538e2018-11-07 17:27:34 +0800798 mWallpaperMayChange = true;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800799 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
800 if (DEBUG_LAYOUT_REPEATS) {
801 surfacePlacer.debugLayoutRepeats(
802 "wallpaper and commitFinishDrawingLocked true",
803 pendingLayoutChanges);
804 }
805 }
806 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800807 }
808
809 final AppWindowToken atoken = w.mAppToken;
810 if (atoken != null) {
Adrian Roos23df3a32018-03-15 15:41:13 +0100811 atoken.updateLetterboxSurface(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800812 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
813 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
814 mTmpUpdateAllDrawn.add(atoken);
815 }
816 }
817
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800818 if (!mLosingFocus.isEmpty() && w.isFocused() && w.isDisplayedLw()) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800819 mWmService.mH.obtainMessage(REPORT_LOSING_FOCUS, this).sendToTarget();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800820 }
821
822 w.updateResizingWindowIfNeeded();
823 };
824
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800825 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800826 * Create new {@link DisplayContent} instance, add itself to the root window container and
827 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700828 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800829 * @param service You know.
wilsonshihc32538e2018-11-07 17:27:34 +0800830 * @param controller The controller for the display container.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700831 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700832 DisplayContent(Display display, WindowManagerService service,
wilsonshihc32538e2018-11-07 17:27:34 +0800833 DisplayWindowController controller) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100834 super(service);
Bryce Leef19cbe22018-02-02 15:09:21 -0800835 setController(controller);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800836 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
837 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
838 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
839 + " new=" + display);
840 }
841
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700842 mDisplay = display;
843 mDisplayId = display.getDisplayId();
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800844 mWallpaperController = new WallpaperController(mWmService, this);
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700845 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700846 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700847 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100848 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo,
849 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700850 initializeDisplayBaseInfo();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700851
lumark588a3e82018-07-20 18:53:54 +0800852 mAppTransition = new AppTransition(service.mContext, service, this);
853 mAppTransition.registerListenerLocked(service.mActivityManagerAppTransitionNotifier);
854 mAppTransitionController = new AppTransitionController(service, this);
855 mUnknownAppVisibilityController = new UnknownAppVisibilityController(service, this);
856
857 AnimationHandler animationHandler = new AnimationHandler();
858 mBoundsAnimationController = new BoundsAnimationController(service.mContext,
859 mAppTransition, SurfaceAnimationThread.getHandler(), animationHandler);
860
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800861 if (mWmService.mInputManager != null) {
862 final InputChannel inputChannel = mWmService.mInputManager.monitorInput("Display "
Tiger Huang7c610aa2018-10-27 00:01:01 +0800863 + mDisplayId, mDisplayId);
864 mPointerEventDispatcher = inputChannel != null
865 ? new PointerEventDispatcher(inputChannel) : null;
866 } else {
867 mPointerEventDispatcher = null;
868 }
869 mDisplayPolicy = new DisplayPolicy(service, this);
870 mDisplayRotation = new DisplayRotation(service, this);
871 if (isDefaultDisplay) {
872 // The policy may be invoked right after here, so it requires the necessary default
873 // fields of this display content.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800874 mWmService.mPolicy.setDefaultDisplay(this);
Tiger Huang7c610aa2018-10-27 00:01:01 +0800875 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800876 if (mWmService.mDisplayReady) {
Tiger Huang7c610aa2018-10-27 00:01:01 +0800877 mDisplayPolicy.onConfigurationChanged();
878 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800879 if (mWmService.mSystemReady) {
Tiger Huang7c610aa2018-10-27 00:01:01 +0800880 mDisplayPolicy.systemReady();
881 }
882 mDividerControllerLocked = new DockedStackDividerController(service, this);
883 mPinnedStackControllerLocked = new PinnedStackController(service, this);
884
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800885 final SurfaceControl.Builder b = mWmService.makeSurfaceBuilder(mSession).setOpaque(true);
Robert Carrb1579c82017-09-05 14:54:47 -0700886 mWindowingLayer = b.setName("Display Root").build();
887 mOverlayLayer = b.setName("Display Overlays").build();
888
Robert Carrf59b8dd2017-10-02 18:58:36 -0700889 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700890 .setLayerStack(mWindowingLayer, mDisplayId)
891 .show(mWindowingLayer)
892 .setLayer(mOverlayLayer, 1)
893 .setLayerStack(mOverlayLayer, mDisplayId)
894 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700895 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700896
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700897 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700898 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700899 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700900 super.addChild(mAboveAppWindowsContainers, null);
901 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800902
903 // Add itself as a child to the root container.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800904 mWmService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700905
906 // TODO(b/62541591): evaluate whether this is the best spot to declare the
907 // {@link DisplayContent} ready for use.
908 mDisplayReady = true;
Arthur Hung95b38a92018-07-20 18:56:12 +0800909
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800910 mWmService.mAnimator.addDisplayLocked(mDisplayId);
Arthur Hung39134b22018-08-14 11:58:28 +0800911 mInputMonitor = new InputMonitor(service, mDisplayId);
Jorim Jaggif96c90a2018-09-26 16:55:15 +0200912 mInsetsStateController = new InsetsStateController(this);
Bryce Leed1871262017-06-12 14:12:29 -0700913 }
914
915 boolean isReady() {
916 // The display is ready when the system and the individual display are both ready.
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800917 return mWmService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700918 }
919
920 int getDisplayId() {
921 return mDisplayId;
922 }
923
Wale Ogunwale02319a62016-09-26 15:21:22 -0700924 WindowToken getWindowToken(IBinder binder) {
925 return mTokenMap.get(binder);
926 }
927
928 AppWindowToken getAppWindowToken(IBinder binder) {
929 final WindowToken token = getWindowToken(binder);
930 if (token == null) {
931 return null;
932 }
933 return token.asAppWindowToken();
934 }
935
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700936 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -0800937 final DisplayContent dc = mWmService.mRoot.getWindowTokenDisplay(token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700938 if (dc != null) {
939 // We currently don't support adding a window token to the display if the display
940 // already has the binder mapped to another token. If there is a use case for supporting
941 // this moving forward we will either need to merge the WindowTokens some how or have
942 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700943 throw new IllegalArgumentException("Can't map token=" + token + " to display="
944 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700945 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700946 if (binder == null) {
947 throw new IllegalArgumentException("Can't map token=" + token + " to display="
948 + getName() + " binder is null");
949 }
950 if (token == null) {
951 throw new IllegalArgumentException("Can't map null token to display="
952 + getName() + " binder=" + binder);
953 }
954
Wale Ogunwale02319a62016-09-26 15:21:22 -0700955 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700956
957 if (token.asAppWindowToken() == null) {
958 // Add non-app token to container hierarchy on the display. App tokens are added through
959 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700960 switch (token.windowType) {
961 case TYPE_WALLPAPER:
962 mBelowAppWindowsContainers.addChild(token);
963 break;
964 case TYPE_INPUT_METHOD:
965 case TYPE_INPUT_METHOD_DIALOG:
966 mImeWindowsContainers.addChild(token);
967 break;
968 default:
969 mAboveAppWindowsContainers.addChild(token);
970 break;
971 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700972 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700973 }
974
975 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700976 final WindowToken token = mTokenMap.remove(binder);
977 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800978 token.setExiting();
979 }
980 return token;
981 }
982
983 /** Changes the display the input window token is housed on to this one. */
984 void reParentWindowToken(WindowToken token) {
985 final DisplayContent prevDc = token.getDisplayContent();
986 if (prevDc == this) {
987 return;
988 }
Riddle Hsu85bd04b2018-11-17 00:34:36 +0800989 if (prevDc != null) {
990 if (prevDc.mTokenMap.remove(token.token) != null && token.asAppWindowToken() == null) {
991 // Removed the token from the map, but made sure it's not an app token before
992 // removing from parent.
993 token.getParent().removeChild(token);
994 }
995 if (prevDc.mLastFocus == mCurrentFocus) {
996 // The window has become the focus of this display, so it should not be notified
997 // that it lost focus from the previous display.
998 prevDc.mLastFocus = null;
999 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001000 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -08001001
1002 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -07001003 }
1004
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001005 void removeAppToken(IBinder binder) {
1006 final WindowToken token = removeWindowToken(binder);
1007 if (token == null) {
1008 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
1009 return;
1010 }
1011
1012 final AppWindowToken appToken = token.asAppWindowToken();
1013
1014 if (appToken == null) {
1015 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
1016 return;
1017 }
1018
1019 appToken.onRemovedFromDisplay();
1020 }
1021
Riddle Hsuad256a12018-07-18 16:11:30 +08001022 @Override
Riddle Hsu86cb7de2018-08-13 23:29:58 +08001023 DisplayWindowController getController() {
1024 return (DisplayWindowController) super.getController();
1025 }
1026
1027 @Override
Riddle Hsuad256a12018-07-18 16:11:30 +08001028 public Display getDisplay() {
Craig Mautnerb47bbc32012-08-22 17:41:48 -07001029 return mDisplay;
1030 }
1031
Craig Mautner59c00972012-07-30 12:10:24 -07001032 DisplayInfo getDisplayInfo() {
1033 return mDisplayInfo;
1034 }
1035
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001036 DisplayMetrics getDisplayMetrics() {
1037 return mDisplayMetrics;
1038 }
1039
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001040 DisplayPolicy getDisplayPolicy() {
1041 return mDisplayPolicy;
1042 }
1043
Riddle Hsuad256a12018-07-18 16:11:30 +08001044 @Override
1045 public DisplayRotation getDisplayRotation() {
1046 return mDisplayRotation;
1047 }
1048
Jorim Jaggif96c90a2018-09-26 16:55:15 +02001049 /**
1050 * Marks a window as providing insets for the rest of the windows in the system.
1051 *
1052 * @param type The type of inset this window provides.
1053 * @param win The window.
1054 * @param frameProvider Function to compute the frame, or {@code null} if the just the frame of
1055 * the window should be taken.
1056 */
1057 void setInsetProvider(@InternalInsetType int type, WindowState win,
1058 @Nullable TriConsumer<DisplayFrames, WindowState, Rect> frameProvider) {
1059 mInsetsStateController.getSourceProvider(type).setWindow(win, frameProvider);
1060 }
1061
1062 InsetsStateController getInsetsStateController() {
1063 return mInsetsStateController;
1064 }
1065
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001066 @VisibleForTesting
1067 void setDisplayRotation(DisplayRotation displayRotation) {
1068 mDisplayRotation = displayRotation;
1069 }
1070
Andrii Kulian8ee72852017-03-10 10:36:45 -08001071 int getRotation() {
1072 return mRotation;
1073 }
1074
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001075 @VisibleForTesting
Andrii Kulian8ee72852017-03-10 10:36:45 -08001076 void setRotation(int newRotation) {
1077 mRotation = newRotation;
Riddle Hsuad256a12018-07-18 16:11:30 +08001078 mDisplayRotation.setRotation(newRotation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08001079 }
1080
1081 int getLastOrientation() {
1082 return mLastOrientation;
1083 }
1084
Andrii Kulian8ee72852017-03-10 10:36:45 -08001085 boolean getAltOrientation() {
1086 return mAltOrientation;
1087 }
1088
Andrii Kulian8ee72852017-03-10 10:36:45 -08001089 int getLastWindowForcedOrientation() {
1090 return mLastWindowForcedOrientation;
1091 }
1092
Andrii Kulian06d07d62017-03-14 11:11:47 -07001093 /**
Riddle Hsu654a6f92018-07-13 22:59:36 +08001094 * Temporarily pauses rotation changes until resumed.
1095 *
1096 * This can be used to prevent rotation changes from occurring while the user is
1097 * performing certain operations, such as drag and drop.
1098 *
1099 * This call nests and must be matched by an equal number of calls to
1100 * {@link #resumeRotationLocked}.
1101 */
1102 void pauseRotationLocked() {
1103 mDeferredRotationPauseCount++;
1104 }
1105
1106 /**
1107 * Resumes normal rotation changes after being paused.
1108 */
1109 void resumeRotationLocked() {
1110 if (mDeferredRotationPauseCount <= 0) {
1111 return;
1112 }
1113
1114 mDeferredRotationPauseCount--;
1115 if (mDeferredRotationPauseCount == 0) {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001116 updateRotationAndSendNewConfigIfNeeded();
Riddle Hsu654a6f92018-07-13 22:59:36 +08001117 }
1118 }
1119
1120 /**
Riddle Hsuad256a12018-07-18 16:11:30 +08001121 * If this is true we have updated our desired orientation, but not yet changed the real
1122 * orientation our applied our screen rotation animation. For example, because a previous
1123 * screen rotation was in progress.
1124 *
1125 * @return {@code true} if the there is an ongoing rotation change.
1126 */
1127 boolean rotationNeedsUpdate() {
1128 final int lastOrientation = getLastOrientation();
1129 final int oldRotation = getRotation();
1130 final boolean oldAltOrientation = getAltOrientation();
1131
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001132 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Riddle Hsuad256a12018-07-18 16:11:30 +08001133 final boolean altOrientation = !mDisplayRotation.rotationHasCompatibleMetrics(
1134 lastOrientation, rotation);
1135 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1136 return false;
1137 }
1138 return true;
1139 }
1140
Riddle Hsu4e611772018-10-31 18:58:28 +08001141 /** Notify the configuration change of this display. */
1142 void sendNewConfiguration() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001143 mWmService.mH.obtainMessage(SEND_NEW_CONFIGURATION, this).sendToTarget();
Riddle Hsu4e611772018-10-31 18:58:28 +08001144 }
1145
1146 /**
1147 * Determine the new desired orientation of this display.
1148 *
1149 * The orientation is computed from non-application windows first. If none of the
1150 * non-application windows specify orientation, the orientation is computed from application
1151 * tokens.
1152 *
1153 * @return {@code true} if the orientation is changed.
1154 */
1155 boolean updateOrientationFromAppTokens() {
1156 return updateOrientationFromAppTokens(false /* forceUpdate */);
1157 }
1158
1159 boolean updateOrientationFromAppTokens(boolean forceUpdate) {
1160 final int req = getOrientation();
1161 if (req != mLastOrientation || forceUpdate) {
1162 mLastOrientation = req;
1163 mDisplayRotation.setCurrentOrientation(req);
1164 return updateRotationUnchecked(forceUpdate);
1165 }
1166 return false;
1167 }
1168
Riddle Hsuad256a12018-07-18 16:11:30 +08001169 /**
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001170 * Update rotation of the display and send configuration if the rotation is changed.
1171 *
1172 * @return {@code true} if the rotation has been changed and the new config is sent.
1173 */
1174 boolean updateRotationAndSendNewConfigIfNeeded() {
1175 final boolean changed = updateRotationUnchecked(false /* forceUpdate */);
1176 if (changed) {
Riddle Hsu4e611772018-10-31 18:58:28 +08001177 sendNewConfiguration();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001178 }
1179 return changed;
1180 }
1181
1182 /**
Andrii Kulian06d07d62017-03-14 11:11:47 -07001183 * Update rotation of the display.
1184 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001185 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
1186 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001187 */
Robert Carrae606b42018-02-15 15:36:23 -08001188 boolean updateRotationUnchecked() {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001189 return updateRotationUnchecked(false /* forceUpdate */);
1190 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001191
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001192 /**
Evan Roskye747c3e2018-10-30 20:06:41 -07001193 * Update rotation of the DisplayContent with an option to force the update. This updates
1194 * the container's perception of rotation and, depending on the top activities, will freeze
1195 * the screen or start seamless rotation. The display itself gets rotated in
1196 * {@link #applyRotationLocked} during {@link WindowManagerService#sendNewConfiguration}.
1197 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001198 * @param forceUpdate Force the rotation update. Sometimes in WM we might skip updating
1199 * orientation because we're waiting for some rotation to finish or display
1200 * to unfreeze, which results in configuration of the previously visible
1201 * activity being applied to a newly visible one. Forcing the rotation
1202 * update allows to workaround this issue.
1203 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
Evan Roskye747c3e2018-10-30 20:06:41 -07001204 * {@link WindowManagerService#sendNewConfiguration(int)} TO COMPLETE THE ROTATION AND
1205 * UNFREEZE THE SCREEN.
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001206 */
1207 boolean updateRotationUnchecked(boolean forceUpdate) {
1208 ScreenRotationAnimation screenRotationAnimation;
1209 if (!forceUpdate) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001210 if (mDeferredRotationPauseCount > 0) {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001211 // Rotation updates have been paused temporarily. Defer the update until
1212 // updates have been resumed.
1213 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
1214 return false;
1215 }
1216
1217 screenRotationAnimation =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001218 mWmService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001219 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
1220 // Rotation updates cannot be performed while the previous rotation change
1221 // animation is still in progress. Skip this update. We will try updating
1222 // again after the animation is finished and the display is unfrozen.
1223 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
1224 return false;
1225 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001226 if (mWmService.mDisplayFrozen) {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001227 // Even if the screen rotation animation has finished (e.g. isAnimating
1228 // returns false), there is still some time where we haven't yet unfrozen
1229 // the display. We also need to abort rotation here.
1230 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
1231 "Deferring rotation, still finishing previous rotation");
1232 return false;
1233 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001234 }
1235
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001236 if (!mWmService.mDisplayEnabled) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001237 // No point choosing a rotation if the display is not enabled.
1238 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
1239 return false;
1240 }
1241
1242 final int oldRotation = mRotation;
1243 final int lastOrientation = mLastOrientation;
1244 final boolean oldAltOrientation = mAltOrientation;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001245 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001246 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Computed rotation=" + rotation + " for display id="
1247 + mDisplayId + " based on lastOrientation=" + lastOrientation
1248 + " and oldRotation=" + oldRotation);
Tiger Huang7c610aa2018-10-27 00:01:01 +08001249 boolean mayRotateSeamlessly = mDisplayPolicy.shouldRotateSeamlessly(mDisplayRotation,
Riddle Hsuad256a12018-07-18 16:11:30 +08001250 oldRotation, rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001251
Robert Carr0e007272017-10-02 13:00:55 -07001252 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001253 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001254 if (seamlessRotated != null && !forceUpdate) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001255 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
1256 // to complete (that is, waiting for windows to redraw). It's tempting to check
1257 // w.mSeamlessRotationCount but that could be incorrect in the case of
1258 // window-removal.
1259 return false;
1260 }
Robert Carr0e007272017-10-02 13:00:55 -07001261
1262 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001263 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -07001264 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -07001265 mayRotateSeamlessly = false;
1266 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001267 for (int i = 0; i < mWmService.mSessions.size(); i++) {
1268 if (mWmService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
Robert Carr0e007272017-10-02 13:00:55 -07001269 mayRotateSeamlessly = false;
1270 break;
1271 }
1272 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001273 }
Robert Carr0e007272017-10-02 13:00:55 -07001274 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001275
1276 // TODO: Implement forced rotation changes.
1277 // Set mAltOrientation to indicate that the application is receiving
1278 // an orientation that has different metrics than it expected.
1279 // eg. Portrait instead of Landscape.
1280
Riddle Hsuad256a12018-07-18 16:11:30 +08001281 final boolean altOrientation = !mDisplayRotation.rotationHasCompatibleMetrics(
Andrii Kulian06d07d62017-03-14 11:11:47 -07001282 lastOrientation, rotation);
1283
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001284 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1285 + " selected orientation " + lastOrientation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001286 + ", got rotation " + rotation + " which has "
1287 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1288
1289 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1290 // No change.
1291 return false;
1292 }
1293
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001294 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1295 + " rotation changed to " + rotation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001296 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1297 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1298
1299 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
Vishnu Nairba183352018-11-21 11:16:49 -08001300 mWaitingForConfig = true;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001301 }
1302
Evan Roskye747c3e2018-10-30 20:06:41 -07001303 mRotation = rotation;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001304 mAltOrientation = altOrientation;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001305
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001306 mWmService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1307 mWmService.mH.sendNewMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
Riddle Hsu654a6f92018-07-13 22:59:36 +08001308 this, WINDOW_FREEZE_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001309
1310 setLayoutNeeded();
1311 final int[] anim = new int[2];
Tiger Huang7c610aa2018-10-27 00:01:01 +08001312 mDisplayPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001313
1314 if (!rotateSeamlessly) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001315 mWmService.startFreezingDisplayLocked(anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001316 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001317 } else {
1318 // The screen rotation animation uses a screenshot to freeze the screen
1319 // while windows resize underneath.
1320 // When we are rotating seamlessly, we allow the elements to transition
1321 // to their rotated state independently and without a freeze required.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001322 mWmService.startSeamlessRotation();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001323 }
1324
Evan Roskye747c3e2018-10-30 20:06:41 -07001325 return true;
1326 }
1327
1328 /**
1329 * Applies the rotation transaction. This must be called after {@link #updateRotationUnchecked}
1330 * (if it returned {@code true}) to actually finish the rotation.
1331 *
1332 * @param oldRotation the rotation we are coming from.
1333 * @param rotation the rotation to apply.
1334 */
1335 void applyRotationLocked(final int oldRotation, final int rotation) {
1336 mDisplayRotation.setRotation(rotation);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001337 final boolean rotateSeamlessly = mWmService.isRotatingSeamlessly();
Evan Roskye747c3e2018-10-30 20:06:41 -07001338 ScreenRotationAnimation screenRotationAnimation = rotateSeamlessly
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001339 ? null : mWmService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001340 // We need to update our screen size information to match the new rotation. If the rotation
1341 // has actually changed then this method will return true and, according to the comment at
1342 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1343 // By updating the Display info here it will be available to
1344 // #computeScreenConfiguration() later.
1345 updateDisplayAndOrientation(getConfiguration().uiMode);
1346
Robert Carrae606b42018-02-15 15:36:23 -08001347 // NOTE: We disable the rotation in the emulator because
1348 // it doesn't support hardware OpenGL emulation yet.
1349 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1350 && screenRotationAnimation.hasScreenshot()) {
1351 if (screenRotationAnimation.setRotation(getPendingTransaction(), rotation,
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001352 MAX_ANIMATION_DURATION, mWmService.getTransitionAnimationScaleLocked(),
Robert Carrae606b42018-02-15 15:36:23 -08001353 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001354 mWmService.scheduleAnimationLocked();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001355 }
1356 }
1357
Vishnu Nair83537a72018-07-19 21:27:48 -07001358 forAllWindows(w -> {
Vishnu Nairddd80742018-08-21 14:12:46 -07001359 w.seamlesslyRotateIfAllowed(getPendingTransaction(), oldRotation, rotation,
1360 rotateSeamlessly);
Vishnu Nair83537a72018-07-19 21:27:48 -07001361 }, true /* traverseTopToBottom */);
1362
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001363 mWmService.mDisplayManagerInternal.performTraversal(getPendingTransaction());
Robert Carrae606b42018-02-15 15:36:23 -08001364 scheduleAnimation();
1365
Andrii Kulian06d07d62017-03-14 11:11:47 -07001366 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001367 if (w.mHasSurface && !rotateSeamlessly) {
1368 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001369 w.setOrientationChanging(true);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001370 mWmService.mRoot.mOrientationChangeComplete = false;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001371 w.mLastFreezeDuration = 0;
1372 }
1373 w.mReportOrientationChanged = true;
1374 }, true /* traverseTopToBottom */);
1375
1376 if (rotateSeamlessly) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001377 mWmService.mH.sendNewMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
Riddle Hsu654a6f92018-07-13 22:59:36 +08001378 this, SEAMLESS_ROTATION_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001379 }
1380
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001381 for (int i = mWmService.mRotationWatchers.size() - 1; i >= 0; i--) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001382 final WindowManagerService.RotationWatcher rotationWatcher
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001383 = mWmService.mRotationWatchers.get(i);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001384 if (rotationWatcher.mDisplayId == mDisplayId) {
1385 try {
1386 rotationWatcher.mWatcher.onRotationChanged(rotation);
1387 } catch (RemoteException e) {
1388 // Ignore
1389 }
1390 }
1391 }
1392
1393 // TODO (multi-display): Magnification is supported only for the default display.
1394 // Announce rotation only if we will not animate as we already have the
1395 // windows in final state. Otherwise, we make this call at the rotation end.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001396 if (screenRotationAnimation == null && mWmService.mAccessibilityController != null
Andrii Kulian06d07d62017-03-14 11:11:47 -07001397 && isDefaultDisplay) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001398 mWmService.mAccessibilityController.onRotationChangedLocked(this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001399 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001400 }
1401
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001402 void configureDisplayPolicy() {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001403 final int width = mBaseDisplayWidth;
1404 final int height = mBaseDisplayHeight;
1405 final int shortSize;
1406 final int longSize;
1407 if (width > height) {
1408 shortSize = height;
1409 longSize = width;
1410 } else {
1411 shortSize = width;
1412 longSize = height;
1413 }
1414
1415 final int shortSizeDp = shortSize * DisplayMetrics.DENSITY_DEFAULT / mBaseDisplayDensity;
1416 final int longSizeDp = longSize * DisplayMetrics.DENSITY_DEFAULT / mBaseDisplayDensity;
1417
1418 mDisplayRotation.configure(width, height, shortSizeDp, longSizeDp);
1419 mDisplayPolicy.configure(width, height, shortSizeDp);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001420
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001421 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
1422 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Arthur Hungbe5ce212018-09-13 18:41:56 +08001423
1424 // Tap Listeners are supported for:
1425 // 1. All physical displays (multi-display).
1426 // 2. VirtualDisplays on VR, AA (and everything else).
1427 if (mPointerEventDispatcher != null && mTapDetector == null) {
1428 if (DEBUG_DISPLAY) {
1429 Slog.d(TAG,
1430 "Registering PointerEventListener for DisplayId: " + mDisplayId);
1431 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001432 mTapDetector = new TaskTapPointerEventListener(mWmService, this);
Arthur Hungbe5ce212018-09-13 18:41:56 +08001433 registerPointerEventListener(mTapDetector);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001434 registerPointerEventListener(mWmService.mMousePositionTracker);
Arthur Hungbe5ce212018-09-13 18:41:56 +08001435 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001436 }
1437
Andrii Kulian06d07d62017-03-14 11:11:47 -07001438 /**
1439 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1440 * changed.
1441 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1442 */
1443 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1444 // Use the effective "visual" dimensions based on current rotation
1445 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1446 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1447 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1448 int dw = realdw;
1449 int dh = realdh;
1450
1451 if (mAltOrientation) {
1452 if (realdw > realdh) {
1453 // Turn landscape into portrait.
1454 int maxw = (int)(realdh/1.3f);
1455 if (maxw < realdw) {
1456 dw = maxw;
1457 }
1458 } else {
1459 // Turn portrait into landscape.
1460 int maxh = (int)(realdw/1.3f);
1461 if (maxh < realdh) {
1462 dh = maxh;
1463 }
1464 }
1465 }
1466
1467 // Update application display metrics.
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001468 final WmDisplayCutout wmDisplayCutout = calculateDisplayCutoutForRotation(mRotation);
1469 final DisplayCutout displayCutout = wmDisplayCutout.getDisplayCutout();
1470
Tiger Huang7c610aa2018-10-27 00:01:01 +08001471 final int appWidth = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1472 displayCutout);
1473 final int appHeight = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1474 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001475 mDisplayInfo.rotation = mRotation;
1476 mDisplayInfo.logicalWidth = dw;
1477 mDisplayInfo.logicalHeight = dh;
1478 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1479 mDisplayInfo.appWidth = appWidth;
1480 mDisplayInfo.appHeight = appHeight;
1481 if (isDefaultDisplay) {
1482 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1483 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1484 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001485 mDisplayInfo.displayCutout = displayCutout.isEmpty() ? null : displayCutout;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001486 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1487 if (mDisplayScalingDisabled) {
1488 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1489 } else {
1490 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1491 }
1492
Andrii Kulianf0379de2018-03-14 16:24:07 -07001493 // We usually set the override info in DisplayManager so that we get consistent display
1494 // metrics values when displays are changing and don't send out new values until WM is aware
1495 // of them. However, we don't do this for displays that serve as containers for ActivityView
1496 // because we don't want letter-/pillar-boxing during resize.
1497 final DisplayInfo overrideDisplayInfo = mShouldOverrideDisplayConfiguration
1498 ? mDisplayInfo : null;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001499 mWmService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
Andrii Kulianf0379de2018-03-14 16:24:07 -07001500 overrideDisplayInfo);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001501
1502 mBaseDisplayRect.set(0, 0, dw, dh);
1503
1504 if (isDefaultDisplay) {
1505 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1506 mCompatDisplayMetrics);
1507 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001508
Andrii Kulian06d07d62017-03-14 11:11:47 -07001509 return mDisplayInfo;
1510 }
1511
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001512 WmDisplayCutout calculateDisplayCutoutForRotation(int rotation) {
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001513 return mDisplayCutoutCache.getOrCompute(mInitialDisplayCutout, rotation);
1514 }
1515
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001516 private WmDisplayCutout calculateDisplayCutoutForRotationUncached(
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001517 DisplayCutout cutout, int rotation) {
Adrian Roos24264212018-02-19 16:26:15 +01001518 if (cutout == null || cutout == DisplayCutout.NO_CUTOUT) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001519 return WmDisplayCutout.NO_CUTOUT;
Adrian Roos1cf585052018-01-03 18:43:27 +01001520 }
Adrian Roos11c25582018-02-19 18:06:36 +01001521 if (rotation == ROTATION_0) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001522 return WmDisplayCutout.computeSafeInsets(
1523 cutout, mInitialDisplayWidth, mInitialDisplayHeight);
Adrian Roos24264212018-02-19 16:26:15 +01001524 }
Adrian Roosbed538e2018-02-21 17:50:07 +01001525 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Issei Suzuki43190bd2018-08-20 17:28:41 +02001526 final Rect[] newBounds = mRotationUtil.getRotatedBounds(
1527 WmDisplayCutout.computeSafeInsets(
Jorim Jaggi60640512018-06-29 01:14:31 +02001528 cutout, mInitialDisplayWidth, mInitialDisplayHeight)
Issei Suzuki43190bd2018-08-20 17:28:41 +02001529 .getDisplayCutout().getBoundingRectsAll(),
1530 rotation, mInitialDisplayWidth, mInitialDisplayHeight);
1531 return WmDisplayCutout.computeSafeInsets(DisplayCutout.fromBounds(newBounds),
Adrian Roos24264212018-02-19 16:26:15 +01001532 rotated ? mInitialDisplayHeight : mInitialDisplayWidth,
1533 rotated ? mInitialDisplayWidth : mInitialDisplayHeight);
Adrian Roos1cf585052018-01-03 18:43:27 +01001534 }
1535
Andrii Kulian06d07d62017-03-14 11:11:47 -07001536 /**
1537 * Compute display configuration based on display properties and policy settings.
1538 * Do not call if mDisplayReady == false.
1539 */
1540 void computeScreenConfiguration(Configuration config) {
1541 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
Evan Roskye747c3e2018-10-30 20:06:41 -07001542 calculateBounds(displayInfo, mTmpBounds);
1543 config.windowConfiguration.setBounds(mTmpBounds);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001544
1545 final int dw = displayInfo.logicalWidth;
1546 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001547 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
Garfield Tane0846042018-07-26 13:42:04 -07001548 config.windowConfiguration.setWindowingMode(getWindowingMode());
Evan Roskye747c3e2018-10-30 20:06:41 -07001549 config.windowConfiguration.setRotation(displayInfo.rotation);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001550
Adrian Roos11c25582018-02-19 18:06:36 +01001551 final float density = mDisplayMetrics.density;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001552 config.screenWidthDp =
Tiger Huang7c610aa2018-10-27 00:01:01 +08001553 (int)(mDisplayPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1554 config.uiMode, displayInfo.displayCutout) / density);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001555 config.screenHeightDp =
Tiger Huang7c610aa2018-10-27 00:01:01 +08001556 (int)(mDisplayPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1557 config.uiMode, displayInfo.displayCutout) / density);
Bryce Lee7566d762017-03-30 09:34:15 -07001558
Tiger Huang7c610aa2018-10-27 00:01:01 +08001559 mDisplayPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh,
Adrian Roos11c25582018-02-19 18:06:36 +01001560 displayInfo.displayCutout, mTmpRect);
Bryce Lee7566d762017-03-30 09:34:15 -07001561 final int leftInset = mTmpRect.left;
1562 final int topInset = mTmpRect.top;
1563 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001564 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1565 leftInset + displayInfo.appWidth /* right */,
1566 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001567 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1568 || displayInfo.rotation == Surface.ROTATION_270);
1569
Tiger Huang7c610aa2018-10-27 00:01:01 +08001570 computeSizeRangesAndScreenLayout(displayInfo, rotated, config.uiMode, dw, dh, density,
1571 config);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001572
1573 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1574 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1575 ? Configuration.SCREENLAYOUT_ROUND_YES
1576 : Configuration.SCREENLAYOUT_ROUND_NO);
1577
1578 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1579 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1580 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001581 dh, displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001582 config.densityDpi = displayInfo.logicalDensityDpi;
1583
1584 config.colorMode =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001585 ((displayInfo.isHdr() && mWmService.hasHdrSupport())
Andrii Kulian06d07d62017-03-14 11:11:47 -07001586 ? Configuration.COLOR_MODE_HDR_YES
1587 : Configuration.COLOR_MODE_HDR_NO)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001588 | (displayInfo.isWideColorGamut() && mWmService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001589 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1590 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1591
1592 // Update the configuration based on available input devices, lid switch,
1593 // and platform configuration.
1594 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1595 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1596 config.navigation = Configuration.NAVIGATION_NONAV;
1597
1598 int keyboardPresence = 0;
1599 int navigationPresence = 0;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001600 final InputDevice[] devices = mWmService.mInputManager.getInputDevices();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001601 final int len = devices != null ? devices.length : 0;
1602 for (int i = 0; i < len; i++) {
1603 InputDevice device = devices[i];
1604 if (!device.isVirtual()) {
1605 final int sources = device.getSources();
1606 final int presenceFlag = device.isExternal() ?
1607 WindowManagerPolicy.PRESENCE_EXTERNAL :
1608 WindowManagerPolicy.PRESENCE_INTERNAL;
1609
1610 // TODO(multi-display): Configure on per-display basis.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001611 if (mWmService.mIsTouchDevice) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001612 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1613 InputDevice.SOURCE_TOUCHSCREEN) {
1614 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1615 }
1616 } else {
1617 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1618 }
1619
1620 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1621 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1622 navigationPresence |= presenceFlag;
1623 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1624 && config.navigation == Configuration.NAVIGATION_NONAV) {
1625 config.navigation = Configuration.NAVIGATION_DPAD;
1626 navigationPresence |= presenceFlag;
1627 }
1628
1629 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1630 config.keyboard = Configuration.KEYBOARD_QWERTY;
1631 keyboardPresence |= presenceFlag;
1632 }
1633 }
1634 }
1635
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001636 if (config.navigation == Configuration.NAVIGATION_NONAV && mWmService.mHasPermanentDpad) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001637 config.navigation = Configuration.NAVIGATION_DPAD;
1638 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1639 }
1640
1641 // Determine whether a hard keyboard is available and enabled.
1642 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1643 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001644 if (hardKeyboardAvailable != mWmService.mHardKeyboardAvailable) {
1645 mWmService.mHardKeyboardAvailable = hardKeyboardAvailable;
1646 mWmService.mH.removeMessages(REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1647 mWmService.mH.sendEmptyMessage(REPORT_HARD_KEYBOARD_STATUS_CHANGE);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001648 }
1649
Tiger Huang7c610aa2018-10-27 00:01:01 +08001650 mDisplayPolicy.updateConfigurationDependentBehaviors();
1651
Andrii Kulian06d07d62017-03-14 11:11:47 -07001652 // Let the policy update hidden states.
1653 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1654 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1655 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001656 mWmService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001657 }
1658
1659 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001660 DisplayCutout displayCutout) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001661 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1662 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1663 final int unrotDw, unrotDh;
1664 if (rotated) {
1665 unrotDw = dh;
1666 unrotDh = dw;
1667 } else {
1668 unrotDw = dw;
1669 unrotDh = dh;
1670 }
1671 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001672 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001673 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001674 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001675 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001676 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001677 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001678 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001679 return sw;
1680 }
1681
1682 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001683 DisplayMetrics dm, int dw, int dh, DisplayCutout displayCutout) {
1684 dm.noncompatWidthPixels = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1685 displayCutout);
1686 dm.noncompatHeightPixels = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode,
1687 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001688 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1689 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1690 if (curSize == 0 || size < curSize) {
1691 curSize = size;
1692 }
1693 return curSize;
1694 }
1695
Tiger Huang7c610aa2018-10-27 00:01:01 +08001696 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, boolean rotated,
1697 int uiMode, int dw, int dh, float density, Configuration outConfig) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001698
1699 // We need to determine the smallest width that will occur under normal
1700 // operation. To this, start with the base screen size and compute the
1701 // width under the different possible rotations. We need to un-rotate
1702 // the current screen dimensions before doing this.
1703 int unrotDw, unrotDh;
1704 if (rotated) {
1705 unrotDw = dh;
1706 unrotDh = dw;
1707 } else {
1708 unrotDw = dw;
1709 unrotDh = dh;
1710 }
1711 displayInfo.smallestNominalAppWidth = 1<<30;
1712 displayInfo.smallestNominalAppHeight = 1<<30;
1713 displayInfo.largestNominalAppWidth = 0;
1714 displayInfo.largestNominalAppHeight = 0;
Tiger Huang7c610aa2018-10-27 00:01:01 +08001715 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_0, uiMode, unrotDw, unrotDh);
1716 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_90, uiMode, unrotDh, unrotDw);
1717 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_180, uiMode, unrotDw, unrotDh);
1718 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_270, uiMode, unrotDh, unrotDw);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001719 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1720 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001721 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001722 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001723 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001724 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001725 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001726 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001727 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001728 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1729 outConfig.screenLayout = sl;
1730 }
1731
1732 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001733 int uiMode, DisplayCutout displayCutout) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001734 // Get the app screen size at this rotation.
Tiger Huang7c610aa2018-10-27 00:01:01 +08001735 int w = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayCutout);
1736 int h = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001737
1738 // Compute the screen layout size class for this rotation.
1739 int longSize = w;
1740 int shortSize = h;
1741 if (longSize < shortSize) {
1742 int tmp = longSize;
1743 longSize = shortSize;
1744 shortSize = tmp;
1745 }
1746 longSize = (int)(longSize/density);
1747 shortSize = (int)(shortSize/density);
1748 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1749 }
1750
Tiger Huang7c610aa2018-10-27 00:01:01 +08001751 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int rotation,
Andrii Kulian06d07d62017-03-14 11:11:47 -07001752 int uiMode, int dw, int dh) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001753 final DisplayCutout displayCutout = calculateDisplayCutoutForRotation(
1754 rotation).getDisplayCutout();
Tiger Huang7c610aa2018-10-27 00:01:01 +08001755 final int width = mDisplayPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1756 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001757 if (width < displayInfo.smallestNominalAppWidth) {
1758 displayInfo.smallestNominalAppWidth = width;
1759 }
1760 if (width > displayInfo.largestNominalAppWidth) {
1761 displayInfo.largestNominalAppWidth = width;
1762 }
Tiger Huang7c610aa2018-10-27 00:01:01 +08001763 final int height = mDisplayPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1764 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001765 if (height < displayInfo.smallestNominalAppHeight) {
1766 displayInfo.smallestNominalAppHeight = height;
1767 }
1768 if (height > displayInfo.largestNominalAppHeight) {
1769 displayInfo.largestNominalAppHeight = height;
1770 }
1771 }
1772
Riddle Hsua4d6fa22018-08-11 00:50:39 +08001773 /**
1774 * Apps that use the compact menu panel (as controlled by the panelMenuIsCompact
1775 * theme attribute) on devices that feature a physical options menu key attempt to position
1776 * their menu panel window along the edge of the screen nearest the physical menu key.
1777 * This lowers the travel distance between invoking the menu panel and selecting
1778 * a menu option.
1779 *
1780 * This method helps control where that menu is placed. Its current implementation makes
1781 * assumptions about the menu key and its relationship to the screen based on whether
1782 * the device's natural orientation is portrait (width < height) or landscape.
1783 *
1784 * The menu key is assumed to be located along the bottom edge of natural-portrait
1785 * devices and along the right edge of natural-landscape devices. If these assumptions
1786 * do not hold for the target device, this method should be changed to reflect that.
1787 *
1788 * @return A {@link Gravity} value for placing the options menu window.
1789 */
1790 int getPreferredOptionsPanelGravity() {
1791 final int rotation = getRotation();
1792 if (mInitialDisplayWidth < mInitialDisplayHeight) {
1793 // On devices with a natural orientation of portrait.
1794 switch (rotation) {
1795 default:
1796 case Surface.ROTATION_0:
1797 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1798 case Surface.ROTATION_90:
1799 return Gravity.RIGHT | Gravity.BOTTOM;
1800 case Surface.ROTATION_180:
1801 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1802 case Surface.ROTATION_270:
1803 return Gravity.START | Gravity.BOTTOM;
1804 }
1805 }
1806
1807 // On devices with a natural orientation of landscape.
1808 switch (rotation) {
1809 default:
1810 case Surface.ROTATION_0:
1811 return Gravity.RIGHT | Gravity.BOTTOM;
1812 case Surface.ROTATION_90:
1813 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1814 case Surface.ROTATION_180:
1815 return Gravity.START | Gravity.BOTTOM;
1816 case Surface.ROTATION_270:
1817 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1818 }
1819 }
1820
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001821 DockedStackDividerController getDockedDividerController() {
1822 return mDividerControllerLocked;
1823 }
1824
Winson Chung655332c2016-10-31 13:14:28 -07001825 PinnedStackController getPinnedStackController() {
1826 return mPinnedStackControllerLocked;
1827 }
1828
Jeff Browna506a6e2013-06-04 00:02:38 -07001829 /**
1830 * Returns true if the specified UID has access to this display.
1831 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001832 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001833 return mDisplay.hasAccess(uid);
1834 }
1835
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001836 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001837 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001838 }
1839
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001840 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001841 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001842 }
1843
Wale Ogunwale61911492017-10-11 08:50:50 -07001844 /**
1845 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1846 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001847 TaskStack getSplitScreenPrimaryStack() {
1848 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001849 return (stack != null && stack.isVisible()) ? stack : null;
1850 }
1851
Robert Carr9785cf32018-04-25 15:06:07 -07001852 boolean hasSplitScreenPrimaryStack() {
1853 return getSplitScreenPrimaryStack() != null;
1854 }
1855
Wale Ogunwale61911492017-10-11 08:50:50 -07001856 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001857 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001858 * not visible.
1859 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001860 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1861 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001862 }
1863
1864 TaskStack getPinnedStack() {
1865 return mTaskStackContainers.getPinnedStack();
1866 }
1867
1868 private boolean hasPinnedStack() {
1869 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001870 }
1871
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001872 /**
1873 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1874 * Null is no compatible stack on the display.
1875 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001876 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001877 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1878 }
1879
1880 /**
1881 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1882 * activity type. Null is no compatible stack on the display.
1883 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001884 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001885 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001886 }
1887
Bryce Lee48f4b572017-04-10 10:54:15 -07001888 @VisibleForTesting
Kazuki Takise148d00a2018-05-31 15:32:19 +09001889 WindowList<TaskStack> getStacks() {
1890 return mTaskStackContainers.mChildren;
1891 }
1892
1893 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001894 TaskStack getTopStack() {
1895 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001896 }
1897
Winson Chunge2d72172018-01-25 17:46:20 +00001898 ArrayList<Task> getVisibleTasks() {
1899 return mTaskStackContainers.getVisibleTasks();
1900 }
1901
Wale Ogunwale61911492017-10-11 08:50:50 -07001902 void onStackWindowingModeChanged(TaskStack stack) {
1903 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001904 }
1905
Andrii Kulian441e4492016-09-29 15:25:00 -07001906 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001907 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001908 super.onConfigurationChanged(newParentConfig);
Tiger Huang7c610aa2018-10-27 00:01:01 +08001909 if (mDisplayPolicy != null) {
1910 mDisplayPolicy.onConfigurationChanged();
1911 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001912
Evan Roskye747c3e2018-10-30 20:06:41 -07001913 // If there was no pinned stack, we still need to notify the controller of the display info
1914 // update as a result of the config change.
1915 if (mPinnedStackControllerLocked != null && !hasPinnedStack()) {
1916 mPinnedStackControllerLocked.onDisplayInfoChanged(getDisplayInfo());
1917 }
Evan Roskye747c3e2018-10-30 20:06:41 -07001918 }
1919
1920 /**
1921 * Updates the resources used by docked/pinned controllers. This needs to be called at the
1922 * beginning of a configuration update cascade since the metrics from these resources are used
1923 * for bounds calculations. Since ActivityDisplay initiates the configuration update, this
1924 * should be called from there instead of DisplayContent's onConfigurationChanged.
1925 */
1926 void preOnConfigurationChanged() {
Bryce Leef3c6a472017-11-14 14:53:06 -08001927 final DockedStackDividerController dividerController = getDockedDividerController();
1928
1929 if (dividerController != null) {
1930 getDockedDividerController().onConfigurationChanged();
1931 }
1932
1933 final PinnedStackController pinnedStackController = getPinnedStackController();
1934
1935 if (pinnedStackController != null) {
1936 getPinnedStackController().onConfigurationChanged();
1937 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001938 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001939
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001940 @Override
1941 boolean fillsParent() {
1942 return true;
1943 }
1944
1945 @Override
1946 boolean isVisible() {
1947 return true;
1948 }
1949
1950 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001951 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001952 super.onAppTransitionDone();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08001953 mWmService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001954 }
1955
Robert Carr9785cf32018-04-25 15:06:07 -07001956 /**
1957 * In split-screen mode we process the IME containers above the docked divider
1958 * rather than directly above their target.
1959 */
1960 private boolean skipTraverseChild(WindowContainer child) {
lumarkff0ab692018-11-05 20:32:30 +08001961 if (child == mImeWindowsContainers && mInputMethodTarget != null
Robert Carr9785cf32018-04-25 15:06:07 -07001962 && !hasSplitScreenPrimaryStack()) {
1963 return true;
1964 }
1965 return false;
1966 }
1967
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001968 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001969 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1970 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1971 // target, or here in order if there isn't an IME target.
1972 if (traverseTopToBottom) {
1973 for (int i = mChildren.size() - 1; i >= 0; --i) {
1974 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001975 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001976 continue;
1977 }
Robert Carr9785cf32018-04-25 15:06:07 -07001978
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001979 if (child.forAllWindows(callback, traverseTopToBottom)) {
1980 return true;
1981 }
1982 }
1983 } else {
1984 final int count = mChildren.size();
1985 for (int i = 0; i < count; i++) {
1986 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001987 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001988 continue;
1989 }
Robert Carr9785cf32018-04-25 15:06:07 -07001990
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001991 if (child.forAllWindows(callback, traverseTopToBottom)) {
1992 return true;
1993 }
1994 }
1995 }
1996 return false;
1997 }
1998
1999 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
2000 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
2001 }
2002
Wale Ogunwale399c8692017-05-08 14:22:42 -07002003 @Override
2004 int getOrientation() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002005 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002006
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002007 if (mWmService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08002008 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07002009 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
2010 + " is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002011 // If the display is frozen, some activities may be in the middle of restarting, and
2012 // thus have removed their old window. If the window has the flag to hide the lock
2013 // screen, then the lock screen can re-appear and inflict its own orientation on us.
2014 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08002015 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002016 } else if (policy.isKeyguardLocked()) {
2017 // Use the last orientation the while the display is frozen with the keyguard
2018 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
2019 // window. We don't want to check the show when locked window directly though as
2020 // things aren't stable while the display is frozen, for example the window could be
2021 // momentarily unavailable due to activity relaunch.
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07002022 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
2023 + " is frozen while keyguard locked, return " + mLastOrientation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08002024 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07002025 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002026 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002027 final int orientation = mAboveAppWindowsContainers.getOrientation();
2028 if (orientation != SCREEN_ORIENTATION_UNSET) {
2029 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002030 }
Wale Ogunwale51362492016-09-08 17:49:17 -07002031 }
2032
Wale Ogunwale399c8692017-05-08 14:22:42 -07002033 // Top system windows are not requesting an orientation. Start searching from apps.
2034 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07002035 }
2036
Craig Mautner46ac6fa2013-08-01 10:06:34 -07002037 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07002038 // Check if display metrics changed and update base values if needed.
2039 updateBaseDisplayMetricsIfNeeded();
2040
Craig Mautner722285e2012-09-07 13:55:58 -07002041 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07002042 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07002043
Garfield Tan2f145f22018-11-01 15:27:03 -07002044 onDisplayChanged(this);
Craig Mautner722285e2012-09-07 13:55:58 -07002045 }
2046
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002047 void initializeDisplayBaseInfo() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002048 final DisplayManagerInternal displayManagerInternal = mWmService.mDisplayManagerInternal;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002049 if (displayManagerInternal != null) {
2050 // Bootstrap the default logical display from the display manager.
2051 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
2052 if (newDisplayInfo != null) {
2053 mDisplayInfo.copyFrom(newDisplayInfo);
2054 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002055 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002056
Andrii Kuliancd097992017-03-23 18:31:59 -07002057 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
2058 mDisplayInfo.logicalDensityDpi);
2059 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
2060 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
2061 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002062 mInitialDisplayCutout = mDisplayInfo.displayCutout;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002063 }
2064
Andrii Kuliancd097992017-03-23 18:31:59 -07002065 /**
2066 * If display metrics changed, overrides are not set and it's not just a rotation - update base
2067 * values.
2068 */
2069 private void updateBaseDisplayMetricsIfNeeded() {
2070 // Get real display metrics without overrides from WM.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002071 mWmService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
Andrii Kuliancd097992017-03-23 18:31:59 -07002072 final int orientation = mDisplayInfo.rotation;
2073 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2074 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
2075 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
2076 final int newDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002077 final DisplayCutout newCutout = mDisplayInfo.displayCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07002078
2079 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
2080 || mInitialDisplayHeight != newHeight
Adrian Roos1cf585052018-01-03 18:43:27 +01002081 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi
2082 || !Objects.equals(mInitialDisplayCutout, newCutout);
Andrii Kuliancd097992017-03-23 18:31:59 -07002083
2084 if (displayMetricsChanged) {
2085 // Check if display size or density is forced.
2086 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
2087 || mBaseDisplayHeight != mInitialDisplayHeight;
2088 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
2089
2090 // If there is an override set for base values - use it, otherwise use new values.
2091 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
2092 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
2093 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
2094
2095 // Real display metrics changed, so we should also update initial values.
2096 mInitialDisplayWidth = newWidth;
2097 mInitialDisplayHeight = newHeight;
2098 mInitialDisplayDensity = newDensity;
Adrian Roos1cf585052018-01-03 18:43:27 +01002099 mInitialDisplayCutout = newCutout;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002100 mWmService.reconfigureDisplayLocked(this);
Andrii Kuliancd097992017-03-23 18:31:59 -07002101 }
2102 }
2103
Bryce Lee27cec322017-03-21 09:41:37 -07002104 /** Sets the maximum width the screen resolution can be */
2105 void setMaxUiWidth(int width) {
2106 if (DEBUG_DISPLAY) {
2107 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
2108 }
2109
2110 mMaxUiWidth = width;
2111
2112 // Update existing metrics.
2113 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
2114 }
2115
2116 /** Update base (override) display metrics. */
2117 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
2118 mBaseDisplayWidth = baseWidth;
2119 mBaseDisplayHeight = baseHeight;
2120 mBaseDisplayDensity = baseDensity;
2121
2122 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
2123 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
2124 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
2125 mBaseDisplayWidth = mMaxUiWidth;
2126
2127 if (DEBUG_DISPLAY) {
2128 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
2129 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
2130 + " on display:" + getDisplayId());
2131 }
2132 }
2133
2134 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08002135
2136 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07002137 }
2138
Riddle Hsuf53da812018-08-15 22:00:27 +08002139 /**
2140 * Forces this display to use the specified density.
2141 *
2142 * @param density The density in DPI to use. If the value equals to initial density, the setting
2143 * will be cleared.
2144 * @param userId The target user to apply. Only meaningful when this is default display. If the
2145 * user id is {@link UserHandle#USER_CURRENT}, it means to apply current settings
2146 * so only need to configure display.
2147 */
2148 void setForcedDensity(int density, int userId) {
2149 final boolean clear = density == mInitialDisplayDensity;
2150 final boolean updateCurrent = userId == UserHandle.USER_CURRENT;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002151 if (mWmService.mCurrentUserId == userId || updateCurrent) {
Riddle Hsuf53da812018-08-15 22:00:27 +08002152 mBaseDisplayDensity = density;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002153 mWmService.reconfigureDisplayLocked(this);
Riddle Hsuf53da812018-08-15 22:00:27 +08002154 }
2155 if (updateCurrent) {
2156 // We are applying existing settings so no need to save it again.
2157 return;
2158 }
2159
2160 if (density == mInitialDisplayDensity) {
2161 density = 0;
2162 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002163 mWmService.mDisplayWindowSettings.setForcedDensity(this, density, userId);
Riddle Hsuf53da812018-08-15 22:00:27 +08002164 }
2165
2166 /** @param mode {@link #FORCE_SCALING_MODE_AUTO} or {@link #FORCE_SCALING_MODE_DISABLED}. */
2167 void setForcedScalingMode(@ForceScalingMode int mode) {
2168 if (mode != FORCE_SCALING_MODE_DISABLED) {
2169 mode = FORCE_SCALING_MODE_AUTO;
2170 }
2171
2172 mDisplayScalingDisabled = (mode != FORCE_SCALING_MODE_AUTO);
2173 Slog.i(TAG_WM, "Using display scaling mode: " + (mDisplayScalingDisabled ? "off" : "auto"));
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002174 mWmService.reconfigureDisplayLocked(this);
Riddle Hsuf53da812018-08-15 22:00:27 +08002175
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002176 mWmService.mDisplayWindowSettings.setForcedScalingMode(this, mode);
Riddle Hsuf53da812018-08-15 22:00:27 +08002177 }
2178
2179 /** If the given width and height equal to initial size, the setting will be cleared. */
2180 void setForcedSize(int width, int height) {
2181 final boolean clear = mInitialDisplayWidth == width && mInitialDisplayHeight == height;
2182 if (!clear) {
2183 // Set some sort of reasonable bounds on the size of the display that we will try
2184 // to emulate.
2185 final int minSize = 200;
2186 final int maxScale = 2;
2187 width = Math.min(Math.max(width, minSize), mInitialDisplayWidth * maxScale);
2188 height = Math.min(Math.max(height, minSize), mInitialDisplayHeight * maxScale);
2189 }
2190
2191 Slog.i(TAG_WM, "Using new display size: " + width + "x" + height);
2192 updateBaseDisplayMetrics(width, height, mBaseDisplayDensity);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002193 mWmService.reconfigureDisplayLocked(this);
Riddle Hsuf53da812018-08-15 22:00:27 +08002194
2195 if (clear) {
2196 width = height = 0;
2197 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002198 mWmService.mDisplayWindowSettings.setForcedSize(this, width, height);
Riddle Hsuf53da812018-08-15 22:00:27 +08002199 }
2200
Wale Ogunwaledb506192017-12-08 10:57:32 -08002201 void getStableRect(Rect out) {
2202 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08002203 }
2204
Wale Ogunwale61911492017-10-11 08:50:50 -07002205 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002206 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
2207 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07002208
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002209 final TaskStack stack = new TaskStack(mWmService, stackId, controller);
Wale Ogunwale61911492017-10-11 08:50:50 -07002210 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002211 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07002212 }
2213
Andrii Kulian51c1b672017-04-07 18:39:32 -07002214 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002215 final DisplayContent prevDc = stack.getDisplayContent();
2216 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07002217 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
2218 + " which is not currently attached to any display");
2219 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002220 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07002221 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
2222 + " to its current displayId=" + mDisplayId);
2223 }
2224
lumark588a3e82018-07-20 18:53:54 +08002225 // Clean up all pending transitions when stack reparent to another display.
2226 stack.forAllAppWindows(AppWindowToken::removeFromPendingTransition);
2227
Wale Ogunwale61911492017-10-11 08:50:50 -07002228 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07002229 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08002230 }
2231
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002232 @Override
2233 protected void addChild(DisplayChildWindowContainer child,
2234 Comparator<DisplayChildWindowContainer> comparator) {
2235 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2236 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002237
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002238 @Override
2239 protected void addChild(DisplayChildWindowContainer child, int index) {
2240 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2241 }
2242
2243 @Override
2244 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002245 // Only allow removal of direct children from this display if the display is in the process
2246 // of been removed.
2247 if (mRemovingDisplay) {
2248 super.removeChild(child);
2249 return;
2250 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002251 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002252 }
2253
Andrii Kuliand2765632016-12-12 22:26:34 -08002254 @Override
2255 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
2256 // Children of the display are statically ordered, so the real intention here is to perform
2257 // the operation on the display and not the static direct children.
2258 getParent().positionChildAt(position, this, includingParents);
2259 }
2260
Riddle Hsu57831b52018-07-27 00:31:48 +08002261 void positionStackAt(int position, TaskStack child, boolean includingParents) {
2262 mTaskStackContainers.positionChildAt(position, child, includingParents);
Winson Chung59a47ded2018-01-25 17:46:06 +00002263 layoutAndAssignWindowLayersIfNeeded();
2264 }
2265
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002266 /**
2267 * Used to obtain task ID when user taps on coordinate (x, y) in this display, and outside
2268 * current task in focus.
2269 *
2270 * This returns the task ID of the foremost task at (x, y) if the task is not home. Otherwise it
2271 * returns -1.
2272 *
2273 * @param x horizontal coordinate of the tap position
2274 * @param y vertical coordinate of the tap position
2275 * @return the task ID if a non-home task is found; -1 if not
2276 */
2277 int taskForTapOutside(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002278 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2279 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Garfield Tanf2e0a9e2018-10-16 09:42:02 -07002280 if (stack.isActivityTypeHome() && !stack.inMultiWindowMode()) {
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002281 // We skip not only home stack, but also everything behind home because user can't
Garfield Tanf2e0a9e2018-10-16 09:42:02 -07002282 // see them when home stack is isn't in multi-window mode.
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002283 break;
2284 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002285 final int taskId = stack.taskIdFromPoint(x, y);
2286 if (taskId != -1) {
2287 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07002288 }
2289 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002290 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07002291 }
2292
Chong Zhang8e89b312015-09-09 15:09:30 -07002293 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08002294 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07002295 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07002296 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07002297 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002298 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002299 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07002300 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2301 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002302 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002303 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002304 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002305
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002306 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
2307 if (mTmpTaskForResizePointSearchResult.searchDone) {
2308 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07002309 }
2310 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002311 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002312 }
2313
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002314 void updateTouchExcludeRegion() {
2315 final Task focusedTask = (mFocusedApp != null ? mFocusedApp.getTask() : null);
David Stevensee9e2772017-02-09 16:30:27 -08002316 if (focusedTask == null) {
2317 mTouchExcludeRegion.setEmpty();
2318 } else {
2319 mTouchExcludeRegion.set(mBaseDisplayRect);
2320 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
2321 mTmpRect2.setEmpty();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002322 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0;
2323 --stackNdx) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002324 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002325 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
2326 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08002327 }
2328 // If we removed the focused task above, add it back and only leave its
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002329 // outside touch area in the exclusion. TapDetector is not interested in
David Stevensee9e2772017-02-09 16:30:27 -08002330 // any touch inside the focused task itself.
2331 if (!mTmpRect2.isEmpty()) {
2332 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
2333 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08002334 }
lumark90120a82018-08-15 00:33:03 +08002335 if (mInputMethodWindow != null && mInputMethodWindow.isVisibleLw()) {
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002336 // If the input method is visible and the user is typing, we don't want these touch
2337 // events to be intercepted and used to change focus. This would likely cause a
2338 // disappearance of the input method.
lumark90120a82018-08-15 00:33:03 +08002339 mInputMethodWindow.getTouchableRegion(mTmpRegion);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002340 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002341 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002342 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002343 final WindowState win = mTapExcludedWindows.get(i);
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002344 win.getTouchableRegion(mTmpRegion);
2345 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
2346 }
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002347 for (int i = mTapExcludeProvidingWindows.size() - 1; i >= 0; i--) {
2348 final WindowState win = mTapExcludeProvidingWindows.valueAt(i);
2349 win.amendTapExcludeRegion(mTouchExcludeRegion);
2350 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08002351 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07002352 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002353 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07002354 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002355 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
2356 }
Craig Mautner1bef3892015-02-17 15:09:47 -08002357 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07002358 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08002359 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07002360 }
2361
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002362 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07002363 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002364 super.switchUser();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002365 mWmService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07002366 }
2367
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07002368 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002369 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2370 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07002371 }
2372 }
2373
Wale Ogunwale10124582016-09-15 20:25:50 -07002374 @Override
2375 void removeIfPossible() {
2376 if (isAnimating()) {
2377 mDeferredRemoval = true;
2378 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07002379 }
Wale Ogunwale10124582016-09-15 20:25:50 -07002380 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07002381 }
2382
Wale Ogunwale10124582016-09-15 20:25:50 -07002383 @Override
2384 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002385 mRemovingDisplay = true;
2386 try {
lumark588a3e82018-07-20 18:53:54 +08002387 // Clear all transitions & screen frozen states when removing display.
2388 mOpeningApps.clear();
2389 mClosingApps.clear();
2390 mUnknownAppVisibilityController.clear();
2391 mAppTransition.removeAppTransitionTimeoutCallbacks();
2392 handleAnimatingStoppedAndTransition();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002393 mWmService.stopFreezingDisplayLocked();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002394 super.removeImmediately();
2395 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Arthur Hungbe5ce212018-09-13 18:41:56 +08002396 if (mPointerEventDispatcher != null && mTapDetector != null) {
2397 unregisterPointerEventListener(mTapDetector);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002398 unregisterPointerEventListener(mWmService.mMousePositionTracker);
Arthur Hungbe5ce212018-09-13 18:41:56 +08002399 mTapDetector = null;
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002400 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002401 mWmService.mAnimator.removeDisplayLocked(mDisplayId);
Louis Chang207f9112018-07-30 08:58:54 +08002402 mWindowingLayer.release();
2403 mOverlayLayer.release();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002404 } finally {
Riddle Hsu4e611772018-10-31 18:58:28 +08002405 mDisplayReady = false;
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002406 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08002407 }
Robert Carr679ccb02018-08-08 15:32:35 -07002408
Tiger Huang7c610aa2018-10-27 00:01:01 +08002409 mDisplayPolicy.onDisplayRemoved();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002410 mWmService.mWindowPlacerLocked.requestTraversal();
Craig Mautner95da1082014-02-24 17:54:35 -08002411 }
2412
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002413 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07002414 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002415 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002416 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
2417
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002418 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002419 removeImmediately();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002420 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08002421 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002422 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08002423 }
2424
Andrii Kulian0214ed92017-05-16 13:44:05 -07002425 /** @return 'true' if removal of this display content is deferred due to active animation. */
2426 boolean isRemovalDeferred() {
2427 return mDeferredRemoval;
2428 }
2429
Wale Ogunwale10124582016-09-15 20:25:50 -07002430 boolean animateForIme(float interpolatedValue, float animationTarget,
2431 float dividerAnimationTarget) {
2432 boolean updated = false;
2433
Wale Ogunwale61911492017-10-11 08:50:50 -07002434 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2435 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002436 if (stack == null || !stack.isAdjustedForIme()) {
2437 continue;
2438 }
2439
2440 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
2441 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2442 updated = true;
2443 } else {
2444 mDividerControllerLocked.mLastAnimationProgress =
2445 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
2446 mDividerControllerLocked.mLastDividerProgress =
2447 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
2448 updated |= stack.updateAdjustForIme(
2449 mDividerControllerLocked.mLastAnimationProgress,
2450 mDividerControllerLocked.mLastDividerProgress,
2451 false /* force */);
2452 }
2453 if (interpolatedValue >= 1f) {
2454 stack.endImeAdjustAnimation();
2455 }
2456 }
2457
2458 return updated;
2459 }
2460
2461 boolean clearImeAdjustAnimation() {
2462 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002463 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2464 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002465 if (stack != null && stack.isAdjustedForIme()) {
2466 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2467 changed = true;
2468 }
2469 }
2470 return changed;
2471 }
2472
2473 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002474 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2475 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002476 if (stack.isVisible() && stack.isAdjustedForIme()) {
2477 stack.beginImeAdjustAnimation();
2478 }
2479 }
2480 }
2481
2482 void adjustForImeIfNeeded() {
lumark90120a82018-08-15 00:33:03 +08002483 final WindowState imeWin = mInputMethodWindow;
Wale Ogunwale10124582016-09-15 20:25:50 -07002484 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2485 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002486 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002487 final TaskStack imeTargetStack = mWmService.getImeFocusStackLocked();
Wale Ogunwale10124582016-09-15 20:25:50 -07002488 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2489 imeTargetStack.getDockSide() : DOCKED_INVALID;
2490 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2491 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2492 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002493 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002494 final boolean imeHeightChanged = imeVisible &&
2495 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2496
2497 // The divider could be adjusted for IME position, or be thinner than usual,
2498 // or both. There are three possible cases:
2499 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2500 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2501 // - If IME is not visible, divider is not moved and is normal width.
2502
2503 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002504 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2505 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002506 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002507 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2508 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002509 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2510 } else {
2511 stack.resetAdjustedForIme(false);
2512 }
2513 }
2514 mDividerControllerLocked.setAdjustedForIme(
2515 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2516 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002517 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2518 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002519 stack.resetAdjustedForIme(!dockVisible);
2520 }
2521 mDividerControllerLocked.setAdjustedForIme(
2522 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2523 }
Winson Chung655332c2016-10-31 13:14:28 -07002524 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002525 }
2526
2527 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002528 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2529 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002530 stack.prepareFreezingTaskBounds();
2531 }
2532 }
2533
Wale Ogunwale94744212015-09-21 19:01:47 -07002534 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002535 getBounds(mTmpRect, newRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002536 rotateBounds(mTmpRect, oldRotation, newRotation, bounds);
2537 }
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002538
Evan Rosky39b6f232018-10-30 18:35:41 -07002539 void rotateBounds(Rect parentBounds, int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002540 // Compute a transform matrix to undo the coordinate space transformation,
2541 // and present the window at the same physical position it previously occupied.
2542 final int deltaRotation = deltaRotation(newRotation, oldRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002543 createRotationMatrix(
2544 deltaRotation, parentBounds.width(), parentBounds.height(), mTmpMatrix);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002545
2546 mTmpRectF.set(bounds);
2547 mTmpMatrix.mapRect(mTmpRectF);
2548 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002549 }
2550
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002551 static int deltaRotation(int oldRotation, int newRotation) {
2552 int delta = newRotation - oldRotation;
2553 if (delta < 0) delta += 4;
2554 return delta;
2555 }
2556
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002557 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002558 Matrix outMatrix) {
2559 // For rotations without Z-ordering we don't need the target rectangle's position.
2560 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2561 displayHeight, outMatrix);
2562 }
2563
2564 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2565 float displayWidth, float displayHeight, Matrix outMatrix) {
2566 switch (rotation) {
2567 case ROTATION_0:
2568 outMatrix.reset();
2569 break;
2570 case ROTATION_270:
2571 outMatrix.setRotate(270, 0, 0);
2572 outMatrix.postTranslate(0, displayHeight);
2573 outMatrix.postTranslate(rectTop, 0);
2574 break;
2575 case ROTATION_180:
2576 outMatrix.reset();
2577 break;
2578 case ROTATION_90:
2579 outMatrix.setRotate(90, 0, 0);
2580 outMatrix.postTranslate(displayWidth, 0);
2581 outMatrix.postTranslate(-rectTop, rectLeft);
2582 break;
2583 }
2584 }
2585
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002586 @CallSuper
2587 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002588 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002589 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002590 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002591 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002592 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2593 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002594 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002595 }
2596 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2597 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002598 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2599 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002600 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002601 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002602 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2603 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002604 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002605 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002606 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2607 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002608 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002609 }
2610 proto.write(DPI, mBaseDisplayDensity);
2611 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002612 proto.write(ROTATION, mRotation);
2613 final ScreenRotationAnimation screenRotationAnimation =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002614 mWmService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002615 if (screenRotationAnimation != null) {
2616 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2617 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002618 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
lumark588a3e82018-07-20 18:53:54 +08002619 mAppTransition.writeToProto(proto, APP_TRANSITION);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002620 if (mFocusedApp != null) {
2621 mFocusedApp.writeNameToProto(proto, FOCUSED_APP);
2622 }
Steven Timotiusaf03df62017-07-18 16:56:43 -07002623 proto.end(token);
2624 }
2625
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002626 @Override
2627 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2628 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002629 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2630 final String subPrefix = " " + prefix;
2631 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2632 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2633 pw.print("dpi");
2634 if (mInitialDisplayWidth != mBaseDisplayWidth
2635 || mInitialDisplayHeight != mBaseDisplayHeight
2636 || mInitialDisplayDensity != mBaseDisplayDensity) {
2637 pw.print(" base=");
2638 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2639 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2640 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002641 if (mDisplayScalingDisabled) {
2642 pw.println(" noscale");
2643 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002644 pw.print(" cur=");
2645 pw.print(mDisplayInfo.logicalWidth);
2646 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2647 pw.print(" app=");
2648 pw.print(mDisplayInfo.appWidth);
2649 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2650 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2651 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2652 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2653 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002654 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002655 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002656 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002657
Craig Mautnerdc548482014-02-05 13:35:24 -08002658 pw.println();
Adrian Roos5251b1d2018-03-23 18:57:43 +01002659 pw.print(prefix); pw.print("mLayoutSeq="); pw.println(mLayoutSeq);
Riddle Hsu654a6f92018-07-13 22:59:36 +08002660 pw.print(prefix);
2661 pw.print("mDeferredRotationPauseCount="); pw.println(mDeferredRotationPauseCount);
Adrian Roos5251b1d2018-03-23 18:57:43 +01002662
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002663 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
2664 if (mLastFocus != mCurrentFocus) {
2665 pw.print(" mLastFocus="); pw.println(mLastFocus);
2666 }
2667 if (mLosingFocus.size() > 0) {
2668 pw.println();
2669 pw.println(" Windows losing focus:");
2670 for (int i = mLosingFocus.size() - 1; i >= 0; i--) {
2671 final WindowState w = mLosingFocus.get(i);
2672 pw.print(" Losing #"); pw.print(i); pw.print(' ');
2673 pw.print(w);
2674 if (dumpAll) {
2675 pw.println(":");
2676 w.dump(pw, " ", true);
2677 } else {
2678 pw.println();
2679 }
2680 }
2681 }
2682 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
Tiger Huang7c610aa2018-10-27 00:01:01 +08002683 if (mLastStatusBarVisibility != 0) {
2684 pw.print(" mLastStatusBarVisibility=0x");
2685 pw.println(Integer.toHexString(mLastStatusBarVisibility));
2686 }
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002687
Adrian Roos5251b1d2018-03-23 18:57:43 +01002688 pw.println();
wilsonshihc32538e2018-11-07 17:27:34 +08002689 mWallpaperController.dump(pw, " ");
2690
2691 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002692 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002693 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2694 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002695 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002696 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002697
Craig Mautnerdc548482014-02-05 13:35:24 -08002698 pw.println();
2699 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002700 pw.println();
2701 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002702 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002703 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002704 pw.print(" Exiting #"); pw.print(i);
2705 pw.print(' '); pw.print(token);
2706 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002707 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002708 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002709 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002710
Jorim Jaggi31f71702016-05-04 16:43:04 -07002711 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002712
2713 // Dump stack references
2714 final TaskStack homeStack = getHomeStack();
2715 if (homeStack != null) {
2716 pw.println(prefix + "homeStack=" + homeStack.getName());
2717 }
2718 final TaskStack pinnedStack = getPinnedStack();
2719 if (pinnedStack != null) {
2720 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2721 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002722 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002723 if (splitScreenPrimaryStack != null) {
2724 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2725 }
2726
2727 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002728 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002729 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002730 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002731
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002732 pw.println();
2733 mDisplayFrames.dump(prefix, pw);
Arthur Hung95b38a92018-07-20 18:56:12 +08002734 pw.println();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08002735 mDisplayPolicy.dump(prefix, pw);
2736 pw.println();
Riddle Hsuad256a12018-07-18 16:11:30 +08002737 mDisplayRotation.dump(prefix, pw);
2738 pw.println();
Arthur Hung95b38a92018-07-20 18:56:12 +08002739 mInputMonitor.dump(pw, " ");
Jorim Jaggif96c90a2018-09-26 16:55:15 +02002740 pw.println();
2741 mInsetsStateController.dump(prefix, pw);
Craig Mautner59c00972012-07-30 12:10:24 -07002742 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002743
2744 @Override
2745 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002746 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002747 }
2748
2749 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002750 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002751 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002752
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002753 /** Returns true if the stack in the windowing mode is visible. */
2754 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002755 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002756 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002757 }
2758
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002759 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002760 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002761 final int x = (int) xf;
2762 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002763 final WindowState touchedWin = getWindow(w -> {
2764 final int flags = w.mAttrs.flags;
2765 if (!w.isVisibleLw()) {
2766 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002767 }
2768 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002769 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002770 }
2771
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002772 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002773 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002774 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002775 }
2776
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002777 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002778
2779 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002780 return mTmpRegion.contains(x, y) || touchFlags == 0;
2781 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002782
2783 return touchedWin;
2784 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002785
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002786 boolean canAddToastWindowForUid(int uid) {
2787 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002788 // Also if the app is focused adding more than one toast at
2789 // a time for better backwards compatibility.
2790 final WindowState focusedWindowForUid = getWindow(w ->
2791 w.mOwnerUid == uid && w.isFocused());
2792 if (focusedWindowForUid != null) {
2793 return true;
2794 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002795 final WindowState win = getWindow(w ->
2796 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2797 && !w.mWindowRemovalAllowed);
2798 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002799 }
2800
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002801 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002802 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2803 return;
2804 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002805
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002806 // Used to communicate the old focus to the callback method.
2807 mTmpWindow = oldFocus;
2808
2809 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002810 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002811
2812 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002813 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002814
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002815 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002816
2817 if (mTmpWindow == null) {
2818 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2819 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002820 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002821 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002822 }
2823
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002824
2825 /**
2826 * Update the focused window and make some adjustments if the focus has changed.
2827 *
2828 * @param mode Indicates the situation we are in. Possible modes are:
2829 * {@link WindowManagerService#UPDATE_FOCUS_NORMAL},
2830 * {@link WindowManagerService#UPDATE_FOCUS_PLACING_SURFACES},
2831 * {@link WindowManagerService#UPDATE_FOCUS_WILL_PLACE_SURFACES},
2832 * {@link WindowManagerService#UPDATE_FOCUS_REMOVING_FOCUS}
2833 * @param updateInputWindows Whether to sync the window information to the input module.
2834 * @return {@code true} if the focused window has changed.
2835 */
2836 boolean updateFocusedWindowLocked(int mode, boolean updateInputWindows, boolean focusFound) {
2837 final WindowState newFocus = findFocusedWindow();
2838 if (mCurrentFocus == newFocus) {
2839 return false;
2840 }
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002841 boolean imWindowChanged = false;
2842 // TODO (b/111080190): Multi-Session IME
2843 if (!focusFound) {
2844 final WindowState imWindow = mInputMethodWindow;
2845 if (imWindow != null) {
lumarkff0ab692018-11-05 20:32:30 +08002846 final WindowState prevTarget = mInputMethodTarget;
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002847
2848 final WindowState newTarget = computeImeTarget(true /* updateImeTarget*/);
2849 imWindowChanged = prevTarget != newTarget;
2850
2851 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS
2852 && mode != UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2853 assignWindowLayers(false /* setLayoutNeeded */);
2854 }
2855 }
2856 }
2857
2858 if (imWindowChanged) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002859 mWmService.mWindowsChanged = true;
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002860 setLayoutNeeded();
2861 }
2862
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002863 if (DEBUG_FOCUS_LIGHT || mWmService.localLOGV) Slog.v(TAG_WM, "Changing focus from "
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002864 + mCurrentFocus + " to " + newFocus + " displayId=" + getDisplayId()
2865 + " Callers=" + Debug.getCallers(4));
2866 final WindowState oldFocus = mCurrentFocus;
2867 mCurrentFocus = newFocus;
2868 mLosingFocus.remove(newFocus);
2869
2870 if (newFocus != null) {
2871 mWinAddedSinceNullFocus.clear();
2872 mWinRemovedSinceNullFocus.clear();
2873
2874 if (newFocus.canReceiveKeys()) {
2875 // Displaying a window implicitly causes dispatching to be unpaused.
2876 // This is to protect against bugs if someone pauses dispatching but
2877 // forgets to resume.
2878 newFocus.mToken.paused = false;
2879 }
2880 }
2881
Tiger Huang7c610aa2018-10-27 00:01:01 +08002882 int focusChanged = getDisplayPolicy().focusChangedLw(oldFocus, newFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002883
2884 if (imWindowChanged && oldFocus != mInputMethodWindow) {
2885 // Focus of the input method window changed. Perform layout if needed.
2886 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2887 performLayout(true /*initial*/, updateInputWindows);
2888 focusChanged &= ~FINISH_LAYOUT_REDO_LAYOUT;
2889 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2890 // Client will do the layout, but we need to assign layers
2891 // for handleNewWindowLocked() below.
2892 assignWindowLayers(false /* setLayoutNeeded */);
2893 }
2894 }
2895
2896 if ((focusChanged & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2897 // The change in focus caused us to need to do a layout. Okay.
2898 setLayoutNeeded();
2899 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2900 performLayout(true /*initial*/, updateInputWindows);
2901 } else if (mode == UPDATE_FOCUS_REMOVING_FOCUS) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002902 mWmService.mRoot.performSurfacePlacement(false);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002903 }
2904 }
2905
2906 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
2907 // If we defer assigning layers, then the caller is responsible for doing this part.
2908 getInputMonitor().setInputFocusLw(newFocus, updateInputWindows);
2909 }
2910
2911 adjustForImeIfNeeded();
2912
2913 // We may need to schedule some toast windows to be removed. The toasts for an app that
2914 // does not have input focus are removed within a timeout to prevent apps to redress
2915 // other apps' UI.
2916 scheduleToastWindowsTimeoutIfNeededLocked(oldFocus, newFocus);
2917
2918 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2919 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
2920 }
2921 return true;
2922 }
2923
2924 /**
2925 * Set the new focused app to this display.
2926 *
2927 * @param newFocus the new focused AppWindowToken.
2928 * @return true if the focused app is changed.
2929 */
2930 boolean setFocusedApp(AppWindowToken newFocus) {
2931 if (newFocus != null) {
2932 final DisplayContent appDisplay = newFocus.getDisplayContent();
2933 if (appDisplay != this) {
2934 throw new IllegalStateException(newFocus + " is not on " + getName()
2935 + " but " + ((appDisplay != null) ? appDisplay.getName() : "none"));
2936 }
2937 }
2938 if (mFocusedApp == newFocus) {
2939 return false;
2940 }
2941 mFocusedApp = newFocus;
2942 getInputMonitor().setFocusedAppLw(newFocus);
2943 updateTouchExcludeRegion();
2944 return true;
2945 }
2946
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002947 /** Updates the layer assignment of windows on this display. */
2948 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002949 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07002950 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002951 if (setLayoutNeeded) {
2952 setLayoutNeeded();
2953 }
Robert Carrb1579c82017-09-05 14:54:47 -07002954
Robert Carrf59b8dd2017-10-02 18:58:36 -07002955 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002956 // the application of this transaction until the animation pass triggers
2957 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2958 // the hiding and showing of surfaces.
2959 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002960 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002961 }
2962
Wale Ogunwale1666e312016-12-16 11:27:18 -08002963 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2964 // moving containers or resizing them. Need to investigate the best way to have it automatically
2965 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002966 void layoutAndAssignWindowLayersIfNeeded() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002967 mWmService.mWindowsChanged = true;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002968 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002969
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002970 if (!mWmService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002971 false /*updateInputWindows*/)) {
2972 assignWindowLayers(false /* setLayoutNeeded */);
2973 }
2974
Arthur Hung95b38a92018-07-20 18:56:12 +08002975 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002976 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Arthur Hung95b38a92018-07-20 18:56:12 +08002977 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002978 }
2979
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002980 /** Returns true if a leaked surface was destroyed */
2981 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002982 // Used to indicate that a surface was leaked.
2983 mTmpWindow = null;
2984 forAllWindows(w -> {
2985 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002986 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002987 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002988 }
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002989 if (!mWmService.mSessions.contains(wsa.mSession)) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002990 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002991 + w + " surface=" + wsa.mSurfaceController
2992 + " token=" + w.mToken
2993 + " pid=" + w.mSession.mPid
2994 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002995 wsa.destroySurface();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08002996 mWmService.mForceRemoves.add(w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002997 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002998 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002999 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003000 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02003001 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003002 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003003 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003004 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003005 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003006 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003007
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003008 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003009 }
3010
3011 /**
lumark90120a82018-08-15 00:33:03 +08003012 * Set input method window for the display.
3013 * @param win Set when window added or Null when destroyed.
3014 */
3015 void setInputMethodWindowLocked(WindowState win) {
3016 mInputMethodWindow = win;
lumark70865a82018-09-17 16:56:26 +08003017 // Update display configuration for IME process.
3018 if (mInputMethodWindow != null) {
3019 final int imePid = mInputMethodWindow.mSession.mPid;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003020 mWmService.mAtmInternal.onImeWindowSetOnDisplay(imePid,
lumark70865a82018-09-17 16:56:26 +08003021 mInputMethodWindow.getDisplayId());
3022 }
lumark90120a82018-08-15 00:33:03 +08003023 computeImeTarget(true /* updateImeTarget */);
Jorim Jaggif96c90a2018-09-26 16:55:15 +02003024 mInsetsStateController.getSourceProvider(TYPE_IME).setWindow(win,
3025 null /* frameProvider */);
lumark90120a82018-08-15 00:33:03 +08003026 }
3027
3028 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003029 * Determine and return the window that should be the IME target.
3030 * @param updateImeTarget If true the system IME target will be updated to match what we found.
3031 * @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 +00003032 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003033 WindowState computeImeTarget(boolean updateImeTarget) {
lumark90120a82018-08-15 00:33:03 +08003034 if (mInputMethodWindow == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003035 // There isn't an IME so there shouldn't be a target...That was easy!
3036 if (updateImeTarget) {
3037 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
lumarkff0ab692018-11-05 20:32:30 +08003038 + mInputMethodTarget + " to null since mInputMethodWindow is null");
3039 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003040 }
3041 return null;
3042 }
3043
lumarkff0ab692018-11-05 20:32:30 +08003044 final WindowState curTarget = mInputMethodTarget;
Chavi Weingarten3a748552018-05-14 17:32:42 +00003045 if (!canUpdateImeTarget()) {
3046 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Defer updating IME target");
3047 return curTarget;
3048 }
3049
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003050 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
3051 // same display. Or even when the current IME/target are not on the same screen as the next
3052 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08003053 mUpdateImeTarget = updateImeTarget;
3054 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003055
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003056
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003057 // Yet more tricksyness! If this window is a "starting" window, we do actually want
3058 // to be on top of it, but it is not -really- where input will go. So look down below
3059 // for a real window to target...
3060 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
3061 final AppWindowToken token = target.mAppToken;
3062 if (token != null) {
3063 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
3064 if (betterTarget != null) {
3065 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003066 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003067 }
3068 }
3069
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003070 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
lumarkff0ab692018-11-05 20:32:30 +08003071 "Proposed new IME target: " + target + " for display: " + getDisplayId());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003072
chaviw1c13ad32018-06-12 16:44:23 -07003073 // Now, a special case -- if the last target's window is in the process of exiting, but
3074 // not removed, and the new target is home, keep on the last target to avoid flicker.
3075 // Home is a special case since its above other stacks in the ordering list, but layed
3076 // out below the others.
3077 if (curTarget != null && !curTarget.mRemoved && curTarget.isDisplayedLw()
3078 && curTarget.isClosing() && (target == null || target.isActivityTypeHome())) {
chaviw4ad54912018-05-30 11:05:44 -07003079 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "New target is home while current target is "
Chavi Weingarten3a748552018-05-14 17:32:42 +00003080 + "closing, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003081 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003082 }
3083
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003084 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
3085 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003086
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003087 if (target == null) {
3088 if (updateImeTarget) {
3089 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
3090 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
3091 + Debug.getCallers(4) : ""));
lumarkff0ab692018-11-05 20:32:30 +08003092 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003093 }
3094
3095 return null;
3096 }
3097
3098 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003099 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
3100 if (token != null) {
3101
3102 // Now some fun for dealing with window animations that modify the Z order. We need
3103 // to look at all windows below the current target that are in this app, finding the
3104 // highest visible one in layering.
3105 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003106 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003107 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003108 }
3109
3110 if (highestTarget != null) {
lumark588a3e82018-07-20 18:53:54 +08003111 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, mAppTransition + " " + highestTarget
Jorim Jaggi35d328a2018-08-14 17:00:20 +02003112 + " animating=" + highestTarget.isAnimating());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003113
lumark588a3e82018-07-20 18:53:54 +08003114 if (mAppTransition.isTransitionSet()) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003115 // If we are currently setting up for an animation, hold everything until we
3116 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003117 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003118 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003119 }
3120 }
3121 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003122
3123 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
3124 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003125 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003126 }
3127
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003128 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003129 }
3130
lumarkff0ab692018-11-05 20:32:30 +08003131 /**
3132 * Calling {@link #computeImeTarget(boolean)} to update the input method target window in
3133 * the candidate app window token if needed.
3134 */
3135 void computeImeTargetIfNeeded(AppWindowToken candidate) {
3136 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == candidate) {
3137 computeImeTarget(true /* updateImeTarget */);
3138 }
3139 }
3140
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003141 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
lumarkff0ab692018-11-05 20:32:30 +08003142 if (target == mInputMethodTarget && mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003143 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003144 }
3145
lumarkff0ab692018-11-05 20:32:30 +08003146 mInputMethodTarget = target;
3147 mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003148 assignWindowLayers(false /* setLayoutNeeded */);
3149 }
3150
3151 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
3152 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3153 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
3154 }
3155
3156 // Used to indicate we have reached the first window in the range we are interested in.
3157 mTmpWindow = null;
3158
3159 // TODO: Figure-out a more efficient way to do this.
3160 final WindowState candidate = getWindow(w -> {
3161 if (w == top) {
3162 // Reached the first window in the range we are interested in.
3163 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003164 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003165 if (mTmpWindow == null) {
3166 return false;
3167 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003168
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003169 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3170 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003171 }
3172 // If we reached the bottom of the range of windows we are considering,
3173 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003174 if (w == bottom) {
3175 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003176 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003177 return false;
3178 });
3179
3180 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003181 }
3182
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003183 void setLayoutNeeded() {
3184 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
3185 mLayoutNeeded = true;
3186 }
3187
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003188 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003189 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
3190 mLayoutNeeded = false;
3191 }
3192
3193 boolean isLayoutNeeded() {
3194 return mLayoutNeeded;
3195 }
3196
Wale Ogunwale02319a62016-09-26 15:21:22 -07003197 void dumpTokens(PrintWriter pw, boolean dumpAll) {
3198 if (mTokenMap.isEmpty()) {
3199 return;
3200 }
3201 pw.println(" Display #" + mDisplayId);
3202 final Iterator<WindowToken> it = mTokenMap.values().iterator();
3203 while (it.hasNext()) {
3204 final WindowToken token = it.next();
3205 pw.print(" ");
3206 pw.print(token);
3207 if (dumpAll) {
3208 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003209 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07003210 } else {
3211 pw.println();
3212 }
3213 }
lumark588a3e82018-07-20 18:53:54 +08003214
3215 if (!mOpeningApps.isEmpty() || !mClosingApps.isEmpty()) {
3216 pw.println();
3217 if (mOpeningApps.size() > 0) {
3218 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
3219 }
3220 if (mClosingApps.size() > 0) {
3221 pw.print(" mClosingApps="); pw.println(mClosingApps);
3222 }
3223 }
3224
3225 mUnknownAppVisibilityController.dump(pw, " ");
Wale Ogunwale02319a62016-09-26 15:21:22 -07003226 }
3227
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003228 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003229 final int[] index = new int[1];
3230 forAllWindows(w -> {
3231 final WindowStateAnimator wAnim = w.mWinAnimator;
3232 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
3233 index[0] = index[0] + 1;
3234 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003235 }
3236
Jorim Jaggife762342016-10-13 14:33:27 +02003237 /**
3238 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
3239 */
3240 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003241 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003242 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02003243 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
3244 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01003245 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02003246 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003247 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02003248 }
3249
Wale Ogunwale494009b82016-10-21 09:01:38 -07003250 boolean checkWaitingForWindows() {
3251
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003252 mHaveBootMsg = false;
3253 mHaveApp = false;
3254 mHaveWallpaper = false;
3255 mHaveKeyguard = true;
3256
3257 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003258 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
3259 return true;
3260 }
3261 if (w.isDrawnLw()) {
3262 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003263 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003264 } else if (w.mAttrs.type == TYPE_APPLICATION
3265 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003266 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003267 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003268 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003269 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003270 mHaveKeyguard = mWmService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003271 }
3272 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003273 return false;
3274 });
3275
3276 if (visibleWindow != null) {
3277 // We have a visible window.
3278 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003279 }
3280
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003281 // if the wallpaper service is disabled on the device, we're never going to have
3282 // wallpaper, don't bother waiting for it
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003283 boolean wallpaperEnabled = mWmService.mContext.getResources().getBoolean(
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003284 com.android.internal.R.bool.config_enableWallpaperService)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003285 && mWmService.mContext.getResources().getBoolean(
Daichi Hironoc1432222018-03-29 13:06:07 +09003286 com.android.internal.R.bool.config_checkWallpaperAtBoot)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003287 && !mWmService.mOnlyCore;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003288
Wale Ogunwale494009b82016-10-21 09:01:38 -07003289 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003290 "******** booted=" + mWmService.mSystemBooted
3291 + " msg=" + mWmService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003292 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
3293 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
3294 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003295
3296 // If we are turning on the screen to show the boot message, don't do it until the boot
3297 // message is actually displayed.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003298 if (!mWmService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003299 return true;
3300 }
3301
3302 // If we are turning on the screen after the boot is completed normally, don't do so until
3303 // we have the application and wallpaper.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003304 if (mWmService.mSystemBooted
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003305 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003306 return true;
3307 }
3308
3309 return false;
3310 }
3311
Jorim Jaggi8f520872018-08-14 17:00:20 +02003312 void updateWindowsForAnimator() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003313 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003314 }
3315
Jorim Jaggi8f520872018-08-14 17:00:20 +02003316 /**
3317 * Updates the {@link TaskStack#setAnimationBackground} for all windows.
3318 */
3319 void updateBackgroundForAnimator() {
Jorim Jaggi7156b652016-10-25 08:31:58 -07003320 resetAnimationBackgroundAnimator();
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003321 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003322 }
3323
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003324 boolean isInputMethodClientFocus(int uid, int pid) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003325 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003326 if (imFocus == null) {
3327 return false;
3328 }
3329
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003330 if (DEBUG_INPUT_METHOD) {
3331 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003332 Slog.i(TAG_WM, "Current focus: " + mCurrentFocus + " displayId=" + mDisplayId);
3333 Slog.i(TAG_WM, "Last focus: " + mLastFocus + " displayId=" + mDisplayId);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003334 }
3335
Wale Ogunwale494009b82016-10-21 09:01:38 -07003336 if (DEBUG_INPUT_METHOD) {
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003337 Slog.i(TAG_WM, "IM target uid/pid: " + imFocus.mSession.mUid
3338 + "/" + imFocus.mSession.mPid);
3339 Slog.i(TAG_WM, "Requesting client uid/pid: " + uid + "/" + pid);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003340 }
3341
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003342 return imFocus.mSession.mUid == uid && imFocus.mSession.mPid == pid;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003343 }
3344
3345 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003346 final WindowState win = getWindow(
3347 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
3348 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003349 }
3350
Tiger Huang7c610aa2018-10-27 00:01:01 +08003351 void statusBarVisibilityChanged(int visibility) {
3352 mLastStatusBarVisibility = visibility;
3353 visibility = getDisplayPolicy().adjustSystemUiVisibilityLw(visibility);
3354 updateStatusBarVisibilityLocked(visibility);
3355 }
3356
3357 private boolean updateStatusBarVisibilityLocked(int visibility) {
3358 if (mLastDispatchedSystemUiVisibility == visibility) {
3359 return false;
3360 }
3361 final int globalDiff = (visibility ^ mLastDispatchedSystemUiVisibility)
3362 // We are only interested in differences of one of the
3363 // clearable flags...
3364 & View.SYSTEM_UI_CLEARABLE_FLAGS
3365 // ...if it has actually been cleared.
3366 & ~visibility;
3367
3368 mLastDispatchedSystemUiVisibility = visibility;
3369 if (isDefaultDisplay) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003370 mWmService.mInputManager.setSystemUiVisibility(visibility);
Tiger Huang7c610aa2018-10-27 00:01:01 +08003371 }
3372 updateSystemUiVisibility(visibility, globalDiff);
3373 return true;
3374 }
3375
Wale Ogunwale494009b82016-10-21 09:01:38 -07003376 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003377 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003378 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003379 final int curValue = w.mSystemUiVisibility;
3380 final int diff = (curValue ^ visibility) & globalDiff;
3381 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003382 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003383 w.mSeq++;
3384 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003385 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003386 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
3387 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07003388 visibility, newValue, diff);
3389 }
3390 } catch (RemoteException e) {
3391 // so sorry
3392 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003393 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003394 }
3395
Tiger Huang7c610aa2018-10-27 00:01:01 +08003396 void reevaluateStatusBarVisibility() {
3397 int visibility = getDisplayPolicy().adjustSystemUiVisibilityLw(mLastStatusBarVisibility);
3398 if (updateStatusBarVisibilityLocked(visibility)) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003399 mWmService.mWindowPlacerLocked.requestTraversal();
Tiger Huang7c610aa2018-10-27 00:01:01 +08003400 }
3401 }
3402
Wale Ogunwale494009b82016-10-21 09:01:38 -07003403 void onWindowFreezeTimeout() {
3404 Slog.w(TAG_WM, "Window freeze timeout expired.");
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003405 mWmService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003406
3407 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07003408 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003409 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003410 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07003411 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003412 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003413 - mWmService.mDisplayFreezeTime);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003414 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003415 }, true /* traverseTopToBottom */);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003416 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003417 }
3418
3419 void waitForAllWindowsDrawn() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003420 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003421 forAllWindows(w -> {
3422 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
3423 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
3424 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003425 // Force add to mResizingWindows.
chaviw9c81e632018-07-31 11:17:52 -07003426 w.resetLastContentInsets();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003427 mWmService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003428 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003429 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003430 }
3431
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003432 // TODO: Super crazy long method that should be broken down...
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003433 void applySurfaceChangesTransaction(boolean recoveringMemory) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003434 final WindowSurfacePlacer surfacePlacer = mWmService.mWindowPlacerLocked;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003435
3436 mTmpUpdateAllDrawn.clear();
3437
3438 int repeats = 0;
3439 do {
3440 repeats++;
3441 if (repeats > 6) {
3442 Slog.w(TAG, "Animation repeat aborted after too many iterations");
3443 clearLayoutNeeded();
3444 break;
3445 }
3446
3447 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
3448 pendingLayoutChanges);
3449
wilsonshihc32538e2018-11-07 17:27:34 +08003450 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
3451 mWallpaperController.adjustWallpaperWindows();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003452 }
3453
Riddle Hsu654a6f92018-07-13 22:59:36 +08003454 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003455 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
Riddle Hsu4e611772018-10-31 18:58:28 +08003456 if (updateOrientationFromAppTokens()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003457 setLayoutNeeded();
Riddle Hsu4e611772018-10-31 18:58:28 +08003458 sendNewConfiguration();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003459 }
3460 }
3461
3462 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
3463 setLayoutNeeded();
3464 }
3465
3466 // FIRST LOOP: Perform a layout, if needed.
3467 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
3468 performLayout(repeats == 1, false /* updateInputWindows */);
3469 } else {
3470 Slog.w(TAG, "Layout repeat skipped after too many iterations");
3471 }
3472
3473 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
3474 pendingLayoutChanges = 0;
3475
Tiger Huang7c610aa2018-10-27 00:01:01 +08003476 mDisplayPolicy.beginPostLayoutPolicyLw();
3477 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
3478 pendingLayoutChanges |= mDisplayPolicy.finishPostLayoutPolicyLw();
3479 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
3480 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
Jorim Jaggif96c90a2018-09-26 16:55:15 +02003481 mInsetsStateController.onPostLayout();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003482 } while (pendingLayoutChanges != 0);
3483
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003484 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003485
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003486 mTmpRecoveringMemory = recoveringMemory;
3487 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01003488 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003489
Jorim Jaggif1292892018-09-10 11:58:13 +02003490 mLastHasContent = mTmpApplySurfaceChangesTransactionState.displayHasContent;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003491 mWmService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Jorim Jaggif1292892018-09-10 11:58:13 +02003492 mLastHasContent,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003493 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
3494 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003495 true /* inTraversal, must call performTraversalInTrans... below */);
3496
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003497 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
3498 if (wallpaperVisible != mLastWallpaperVisible) {
3499 mLastWallpaperVisible = wallpaperVisible;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003500 mWmService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003501 }
3502
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003503 while (!mTmpUpdateAllDrawn.isEmpty()) {
3504 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
3505 // See if any windows have been drawn, so they (and others associated with them)
3506 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07003507 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003508 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003509 }
3510
Bryce Leef3c6a472017-11-14 14:53:06 -08003511 private void updateBounds() {
Evan Rosky39b6f232018-10-30 18:35:41 -07003512 calculateBounds(mDisplayInfo, mTmpBounds);
Bryce Leef3c6a472017-11-14 14:53:06 -08003513 setBounds(mTmpBounds);
3514 }
3515
3516 // Determines the current display bounds based on the current state
Evan Rosky39b6f232018-10-30 18:35:41 -07003517 private void calculateBounds(DisplayInfo displayInfo, Rect out) {
Bryce Leef3c6a472017-11-14 14:53:06 -08003518 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Evan Rosky39b6f232018-10-30 18:35:41 -07003519 final int rotation = displayInfo.rotation;
3520 boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Bryce Leef3c6a472017-11-14 14:53:06 -08003521 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
3522 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Evan Rosky39b6f232018-10-30 18:35:41 -07003523 int width = displayInfo.logicalWidth;
Bryce Leef3c6a472017-11-14 14:53:06 -08003524 int left = (physWidth - width) / 2;
Evan Rosky39b6f232018-10-30 18:35:41 -07003525 int height = displayInfo.logicalHeight;
Bryce Leef3c6a472017-11-14 14:53:06 -08003526 int top = (physHeight - height) / 2;
3527 out.set(left, top, left + width, top + height);
3528 }
3529
3530 @Override
3531 public void getBounds(Rect out) {
Evan Rosky39b6f232018-10-30 18:35:41 -07003532 calculateBounds(mDisplayInfo, out);
Bryce Leef3c6a472017-11-14 14:53:06 -08003533 }
3534
3535 private void getBounds(Rect out, int orientation) {
3536 getBounds(out);
3537
3538 // Rotate the Rect if needed.
3539 final int currentRotation = mDisplayInfo.rotation;
3540 final int rotationDelta = deltaRotation(currentRotation, orientation);
3541 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
3542 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
3543 mTmpRectF.set(out);
3544 mTmpMatrix.mapRect(mTmpRectF);
3545 mTmpRectF.round(out);
3546 }
3547 }
3548
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003549 void performLayout(boolean initial, boolean updateInputWindows) {
3550 if (!isLayoutNeeded()) {
3551 return;
3552 }
3553 clearLayoutNeeded();
3554
3555 final int dw = mDisplayInfo.logicalWidth;
3556 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003557 if (DEBUG_LAYOUT) {
3558 Slog.v(TAG, "-------------------------------------");
3559 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
3560 }
3561
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01003562 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
3563 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00003564 // TODO: Not sure if we really need to set the rotation here since we are updating from the
3565 // display info above...
3566 mDisplayFrames.mRotation = mRotation;
Tiger Huang7c610aa2018-10-27 00:01:01 +08003567 mDisplayPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003568
Adrian Roos5251b1d2018-03-23 18:57:43 +01003569 int seq = mLayoutSeq + 1;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003570 if (seq < 0) seq = 0;
Adrian Roos5251b1d2018-03-23 18:57:43 +01003571 mLayoutSeq = seq;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003572
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003573 // Used to indicate that we have processed the dream window and all additional windows are
3574 // behind it.
3575 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003576 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003577
3578 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003579 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003580
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003581 // Used to indicate that we have processed the dream window and all additional attached
3582 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003583 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003584 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003585
3586 // Now perform layout of attached windows, which usually depend on the position of the
3587 // window they are attached to. XXX does not deal with windows that are attached to windows
3588 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003589 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003590
3591 // Window frames may have changed. Tell the input dispatcher about it.
Arthur Hung95b38a92018-07-20 18:56:12 +08003592 mInputMonitor.layoutInputConsumers(dw, dh);
3593 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003594 if (updateInputWindows) {
Arthur Hung95b38a92018-07-20 18:56:12 +08003595 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003596 }
3597
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003598 mWmService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003599 }
3600
3601 /**
3602 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
3603 * In portrait mode, it grabs the full screenshot.
3604 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003605 * @param config of the output bitmap
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003606 */
chaviw0315a1a2018-03-05 15:28:35 -08003607 Bitmap screenshotDisplayLocked(Bitmap.Config config) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003608 if (!mWmService.mPolicy.isScreenOn()) {
chaviw0315a1a2018-03-05 15:28:35 -08003609 if (DEBUG_SCREENSHOT) {
3610 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003611 }
chaviw0315a1a2018-03-05 15:28:35 -08003612 return null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003613 }
chaviwfbe47df2017-11-10 16:14:49 -08003614
chaviw0315a1a2018-03-05 15:28:35 -08003615 int dw = mDisplayInfo.logicalWidth;
3616 int dh = mDisplayInfo.logicalHeight;
chaviwfbe47df2017-11-10 16:14:49 -08003617
chaviw0315a1a2018-03-05 15:28:35 -08003618 if (dw <= 0 || dh <= 0) {
3619 return null;
3620 }
chaviwfbe47df2017-11-10 16:14:49 -08003621
chaviw0315a1a2018-03-05 15:28:35 -08003622 final Rect frame = new Rect(0, 0, dw, dh);
chaviwfbe47df2017-11-10 16:14:49 -08003623
chaviw0315a1a2018-03-05 15:28:35 -08003624 // The screenshot API does not apply the current screen rotation.
3625 int rot = mDisplay.getRotation();
chaviwfbe47df2017-11-10 16:14:49 -08003626
chaviw0315a1a2018-03-05 15:28:35 -08003627 if (rot == ROTATION_90 || rot == ROTATION_270) {
3628 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3629 }
chaviwfbe47df2017-11-10 16:14:49 -08003630
chaviw0315a1a2018-03-05 15:28:35 -08003631 // SurfaceFlinger is not aware of orientation, so convert our logical
3632 // crop to SurfaceFlinger's portrait orientation.
3633 convertCropForSurfaceFlinger(frame, rot, dw, dh);
3634
3635 final ScreenRotationAnimation screenRotationAnimation =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003636 mWmService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
chaviw0315a1a2018-03-05 15:28:35 -08003637 final boolean inRotation = screenRotationAnimation != null &&
3638 screenRotationAnimation.isAnimating();
3639 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
3640
3641 // TODO(b/68392460): We should screenshot Task controls directly
3642 // but it's difficult at the moment as the Task doesn't have the
3643 // correct size set.
chaviw08520a02018-09-10 16:44:56 -07003644 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, inRotation, rot);
chaviw0315a1a2018-03-05 15:28:35 -08003645 if (bitmap == null) {
3646 Slog.w(TAG_WM, "Failed to take screenshot");
3647 return null;
3648 }
3649
3650 // Create a copy of the screenshot that is immutable and backed in ashmem.
3651 // This greatly reduces the overhead of passing the bitmap between processes.
3652 final Bitmap ret = bitmap.createAshmemBitmap(config);
3653 bitmap.recycle();
3654 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003655 }
3656
3657 // TODO: Can this use createRotationMatrix()?
3658 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3659 if (rot == Surface.ROTATION_90) {
3660 final int tmp = crop.top;
3661 crop.top = dw - crop.right;
3662 crop.right = crop.bottom;
3663 crop.bottom = dw - crop.left;
3664 crop.left = tmp;
3665 } else if (rot == Surface.ROTATION_180) {
3666 int tmp = crop.top;
3667 crop.top = dh - crop.bottom;
3668 crop.bottom = dh - tmp;
3669 tmp = crop.right;
3670 crop.right = dw - crop.left;
3671 crop.left = dw - tmp;
3672 } else if (rot == Surface.ROTATION_270) {
3673 final int tmp = crop.top;
3674 crop.top = crop.left;
3675 crop.left = dh - crop.bottom;
3676 crop.bottom = crop.right;
3677 crop.right = dh - tmp;
3678 }
3679 }
3680
3681 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003682 // Used to indicate the layout is needed.
3683 mTmpWindow = null;
3684
3685 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003686 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003687 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003688 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003689 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003690 w.setDisplayLayoutNeeded();
Vishnu Nairba183352018-11-21 11:16:49 -08003691 w.finishSeamlessRotation(true /* timeout */);
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003692 mWmService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003693 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003694
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003695 if (mTmpWindow != null) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003696 mWmService.mWindowPlacerLocked.performSurfacePlacement();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003697 }
3698 }
3699
Wale Ogunwale1666e312016-12-16 11:27:18 -08003700 void setExitingTokensHasVisible(boolean hasVisible) {
3701 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3702 mExitingTokens.get(i).hasVisible = hasVisible;
3703 }
3704
3705 // Initialize state of exiting applications.
3706 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3707 }
3708
3709 void removeExistingTokensIfPossible() {
3710 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3711 final WindowToken token = mExitingTokens.get(i);
3712 if (!token.hasVisible) {
3713 mExitingTokens.remove(i);
3714 }
3715 }
3716
3717 // Time to remove any exiting applications?
3718 mTaskStackContainers.removeExistingAppTokensIfPossible();
3719 }
3720
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003721 @Override
3722 void onDescendantOverrideConfigurationChanged() {
3723 setLayoutNeeded();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003724 mWmService.requestTraversal();
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003725 }
3726
David Stevens9440dc82017-03-16 19:00:20 -07003727 boolean okToDisplay() {
3728 if (mDisplayId == DEFAULT_DISPLAY) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003729 return !mWmService.mDisplayFrozen
3730 && mWmService.mDisplayEnabled && mWmService.mPolicy.isScreenOn();
David Stevens9440dc82017-03-16 19:00:20 -07003731 }
3732 return mDisplayInfo.state == Display.STATE_ON;
3733 }
3734
3735 boolean okToAnimate() {
3736 return okToDisplay() &&
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003737 (mDisplayId != DEFAULT_DISPLAY || mWmService.mPolicy.okToAnimate());
David Stevens9440dc82017-03-16 19:00:20 -07003738 }
3739
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003740 static final class TaskForResizePointSearchResult {
3741 boolean searchDone;
3742 Task taskForResize;
3743
3744 void reset() {
3745 searchDone = false;
3746 taskForResize = null;
3747 }
3748 }
Robert Carr3b716242016-08-16 16:02:21 -07003749
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003750 private static final class ApplySurfaceChangesTransactionState {
3751 boolean displayHasContent;
3752 boolean obscured;
3753 boolean syswin;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003754 float preferredRefreshRate;
3755 int preferredModeId;
3756
3757 void reset() {
3758 displayHasContent = false;
3759 obscured = false;
3760 syswin = false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003761 preferredRefreshRate = 0;
3762 preferredModeId = 0;
3763 }
3764 }
3765
3766 private static final class ScreenshotApplicationState {
3767 WindowState appWin;
3768 int maxLayer;
3769 int minLayer;
3770 boolean screenshotReady;
3771
3772 void reset(boolean screenshotReady) {
3773 appWin = null;
3774 maxLayer = 0;
3775 minLayer = 0;
3776 this.screenshotReady = screenshotReady;
3777 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3778 }
3779 }
3780
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003781 /**
3782 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3783 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3784 * homogeneous children type which is currently required by sub-classes of
3785 * {@link WindowContainer} class.
3786 */
3787 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3788
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003789 DisplayChildWindowContainer(WindowManagerService service) {
3790 super(service);
3791 }
3792
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003793 @Override
3794 boolean fillsParent() {
3795 return true;
3796 }
3797
3798 @Override
3799 boolean isVisible() {
3800 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003801 }
3802 }
3803
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003804 /**
3805 * Window container class that contains all containers on this display relating to Apps.
3806 * I.e Activities.
3807 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003808 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003809 /**
3810 * A control placed at the appropriate level for transitions to occur.
3811 */
chaviw23ee71c2017-12-18 11:29:41 -08003812 SurfaceControl mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02003813 SurfaceControl mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02003814 SurfaceControl mHomeAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003815
Robert Carrf7a7ca82017-12-06 13:17:07 -08003816 /**
3817 * Given that the split-screen divider does not have an AppWindowToken, it
3818 * will have to live inside of a "NonAppWindowContainer", in particular
3819 * {@link DisplayContent#mAboveAppWindowsContainers}. However, in visual Z order
3820 * it will need to be interleaved with some of our children, appearing on top of
3821 * both docked stacks but underneath any assistant stacks.
3822 *
3823 * To solve this problem we have this anchor control, which will always exist so
3824 * we can always assign it the correct value in our {@link #assignChildLayers}.
3825 * Likewise since it always exists, {@link AboveAppWindowContainers} can always
3826 * assign the divider a layer relative to it. This way we prevent linking lifecycle
3827 * events between the two containers.
3828 */
3829 SurfaceControl mSplitScreenDividerAnchor = null;
3830
Wale Ogunwale61911492017-10-11 08:50:50 -07003831 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3832 // through the list to find them.
3833 private TaskStack mHomeStack = null;
3834 private TaskStack mPinnedStack = null;
3835 private TaskStack mSplitScreenPrimaryStack = null;
3836
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003837 TaskStackContainers(WindowManagerService service) {
3838 super(service);
3839 }
3840
Wale Ogunwale61911492017-10-11 08:50:50 -07003841 /**
3842 * Returns the topmost stack on the display that is compatible with the input windowing mode
3843 * and activity type. Null is no compatible stack on the display.
3844 */
3845 TaskStack getStack(int windowingMode, int activityType) {
3846 if (activityType == ACTIVITY_TYPE_HOME) {
3847 return mHomeStack;
3848 }
3849 if (windowingMode == WINDOWING_MODE_PINNED) {
3850 return mPinnedStack;
3851 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3852 return mSplitScreenPrimaryStack;
3853 }
3854 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3855 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003856 if (activityType == ACTIVITY_TYPE_UNDEFINED
3857 && windowingMode == stack.getWindowingMode()) {
3858 // Passing in undefined type means we want to match the topmost stack with the
3859 // windowing mode.
3860 return stack;
3861 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003862 if (stack.isCompatible(windowingMode, activityType)) {
3863 return stack;
3864 }
3865 }
3866 return null;
3867 }
3868
3869 @VisibleForTesting
3870 TaskStack getTopStack() {
3871 return mTaskStackContainers.getChildCount() > 0
3872 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3873 }
3874
3875 TaskStack getHomeStack() {
3876 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3877 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3878 }
3879 return mHomeStack;
3880 }
3881
3882 TaskStack getPinnedStack() {
3883 return mPinnedStack;
3884 }
3885
Matthew Ng64e77cf2017-10-31 14:01:31 -07003886 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003887 return mSplitScreenPrimaryStack;
3888 }
3889
Winson Chunge2d72172018-01-25 17:46:20 +00003890 ArrayList<Task> getVisibleTasks() {
3891 final ArrayList<Task> visibleTasks = new ArrayList<>();
3892 forAllTasks(task -> {
3893 if (task.isVisible()) {
3894 visibleTasks.add(task);
3895 }
3896 });
3897 return visibleTasks;
3898 }
3899
Andrii Kulian839def92016-11-02 10:58:58 -07003900 /**
3901 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003902 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003903 */
3904 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003905 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003906 addChild(stack, onTop);
3907 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003908 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003909
Wale Ogunwale61911492017-10-11 08:50:50 -07003910 void onStackWindowingModeChanged(TaskStack stack) {
3911 removeStackReferenceIfNeeded(stack);
3912 addStackReferenceIfNeeded(stack);
3913 if (stack == mPinnedStack && getTopStack() != stack) {
3914 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3915 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3916 }
3917 }
3918
3919 private void addStackReferenceIfNeeded(TaskStack stack) {
3920 if (stack.isActivityTypeHome()) {
3921 if (mHomeStack != null) {
Louis Chang7d0037c2018-08-13 12:42:06 +08003922 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
Wale Ogunwale61911492017-10-11 08:50:50 -07003923 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
Louis Chang7d0037c2018-08-13 12:42:06 +08003924
Wale Ogunwale61911492017-10-11 08:50:50 -07003925 }
Louis Chang7d0037c2018-08-13 12:42:06 +08003926 mHomeStack = stack;
Wale Ogunwale61911492017-10-11 08:50:50 -07003927 }
3928 final int windowingMode = stack.getWindowingMode();
3929 if (windowingMode == WINDOWING_MODE_PINNED) {
3930 if (mPinnedStack != null) {
3931 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3932 + mPinnedStack + " already exist on display=" + this
3933 + " stack=" + stack);
3934 }
3935 mPinnedStack = stack;
3936 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3937 if (mSplitScreenPrimaryStack != null) {
3938 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3939 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3940 + " already exist on display=" + this + " stack=" + stack);
3941 }
3942 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003943 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003944 }
3945 }
3946
3947 private void removeStackReferenceIfNeeded(TaskStack stack) {
3948 if (stack == mHomeStack) {
3949 mHomeStack = null;
3950 } else if (stack == mPinnedStack) {
3951 mPinnedStack = null;
3952 } else if (stack == mSplitScreenPrimaryStack) {
3953 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003954 // Re-set the split-screen create mode whenever the split-screen stack is removed.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08003955 mWmService.setDockedStackCreateStateLocked(
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003956 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3957 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003958 }
Andrii Kulian839def92016-11-02 10:58:58 -07003959 }
3960
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003961 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003962 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3963 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003964 addChild(stack, addIndex);
3965 setLayoutNeeded();
3966 }
3967
Wale Ogunwale61911492017-10-11 08:50:50 -07003968 @Override
3969 protected void removeChild(TaskStack stack) {
3970 super.removeChild(stack);
3971 removeStackReferenceIfNeeded(stack);
3972 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003973
3974 @Override
3975 boolean isOnTop() {
3976 // Considered always on top
3977 return true;
3978 }
3979
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003980 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003981 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003982 if (child.getWindowConfiguration().isAlwaysOnTop()
3983 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003984 // This stack is always-on-top, override the default behavior.
3985 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3986
3987 // Moving to its current position, as we must call super but we don't want to
3988 // perform any meaningful action.
3989 final int currentPosition = mChildren.indexOf(child);
3990 super.positionChildAt(currentPosition, child, false /* includingParents */);
3991 return;
3992 }
3993
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003994 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3995 super.positionChildAt(targetPosition, child, includingParents);
3996
Riddle Hsu2f6e1742018-08-23 22:44:36 +08003997 if (includingParents) {
3998 // We still want to move the display of this stack container to top because even the
3999 // target position is adjusted to non-top, the intention of the condition is to have
4000 // higher z-order to gain focus (e.g. moving a task of a fullscreen stack to front
4001 // in a non-top display which is using picture-in-picture mode).
4002 final int topChildPosition = getChildCount() - 1;
4003 if (targetPosition < topChildPosition && position >= topChildPosition) {
4004 getParent().positionChildAt(POSITION_TOP, this /* child */,
4005 true /* includingParents */);
4006 }
4007 }
4008
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004009 setLayoutNeeded();
4010 }
4011
4012 /**
4013 * When stack is added or repositioned, find a proper position for it.
4014 * This will make sure that pinned stack always stays on top.
4015 * @param requestedPosition Position requested by caller.
4016 * @param stack Stack to be added or positioned.
4017 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
4018 * @return The proper position for the stack.
4019 */
4020 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09004021 if (stack.inPinnedWindowingMode()) {
Kazuki Takisef85197b2018-06-18 18:18:36 +09004022 return POSITION_TOP;
Kazuki Takise148d00a2018-05-31 15:32:19 +09004023 }
4024
Kazuki Takisef85197b2018-06-18 18:18:36 +09004025 final int topChildPosition = mChildren.size() - 1;
4026 int belowAlwaysOnTopPosition = POSITION_BOTTOM;
4027 for (int i = topChildPosition; i >= 0; --i) {
4028 if (getStacks().get(i) != stack && !getStacks().get(i).isAlwaysOnTop()) {
4029 belowAlwaysOnTopPosition = i;
4030 break;
4031 }
4032 }
4033
4034 // The max possible position we can insert the stack at.
4035 int maxPosition = POSITION_TOP;
4036 // The min possible position we can insert the stack at.
4037 int minPosition = POSITION_BOTTOM;
4038
4039 if (stack.isAlwaysOnTop()) {
4040 if (hasPinnedStack()) {
4041 // Always-on-top stacks go below the pinned stack.
4042 maxPosition = getStacks().indexOf(mPinnedStack) - 1;
4043 }
4044 // Always-on-top stacks need to be above all other stacks.
4045 minPosition = belowAlwaysOnTopPosition !=
4046 POSITION_BOTTOM ? belowAlwaysOnTopPosition : topChildPosition;
4047 } else {
4048 // Other stacks need to be below the always-on-top stacks.
4049 maxPosition = belowAlwaysOnTopPosition !=
Arthur Hung928d8dc2018-07-18 15:38:05 +08004050 POSITION_BOTTOM ? belowAlwaysOnTopPosition : 0;
Kazuki Takisef85197b2018-06-18 18:18:36 +09004051 }
4052
4053 int targetPosition = requestedPosition;
4054 targetPosition = Math.min(targetPosition, maxPosition);
4055 targetPosition = Math.max(targetPosition, minPosition);
4056
4057 int prevPosition = getStacks().indexOf(stack);
4058 // The positions we calculated above (maxPosition, minPosition) do not take into
4059 // consideration the following edge cases.
4060 // 1) We need to adjust the position depending on the value "adding".
4061 // 2) When we are moving a stack to another position, we also need to adjust the
4062 // position depending on whether the stack is moving to a higher or lower position.
4063 if ((targetPosition != requestedPosition) &&
4064 (adding || targetPosition < prevPosition)) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09004065 targetPosition++;
4066 }
4067
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004068 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08004069 }
4070
4071 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004072 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
4073 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004074 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004075 if (super.forAllWindows(callback, traverseTopToBottom)) {
4076 return true;
4077 }
4078 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4079 return true;
4080 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004081 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004082 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4083 return true;
4084 }
4085 if (super.forAllWindows(callback, traverseTopToBottom)) {
4086 return true;
4087 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004088 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004089 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004090 }
4091
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004092 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004093 boolean traverseTopToBottom) {
4094 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
4095 // app tokens.
4096 // TODO: Investigate if we need to continue to do this or if we can just process them
4097 // in-order.
4098 if (traverseTopToBottom) {
4099 for (int i = mChildren.size() - 1; i >= 0; --i) {
4100 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4101 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004102 if (appTokens.get(j).forAllWindowsUnchecked(callback,
4103 traverseTopToBottom)) {
4104 return true;
4105 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004106 }
4107 }
4108 } else {
4109 final int count = mChildren.size();
4110 for (int i = 0; i < count; ++i) {
4111 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4112 final int appTokensCount = appTokens.size();
4113 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004114 if (appTokens.get(j).forAllWindowsUnchecked(callback,
4115 traverseTopToBottom)) {
4116 return true;
4117 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004118 }
4119 }
4120 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004121 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004122 }
4123
Wale Ogunwale1666e312016-12-16 11:27:18 -08004124 void setExitingTokensHasVisible(boolean hasVisible) {
4125 for (int i = mChildren.size() - 1; i >= 0; --i) {
4126 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4127 for (int j = appTokens.size() - 1; j >= 0; --j) {
4128 appTokens.get(j).hasVisible = hasVisible;
4129 }
4130 }
4131 }
4132
4133 void removeExistingAppTokensIfPossible() {
4134 for (int i = mChildren.size() - 1; i >= 0; --i) {
4135 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4136 for (int j = appTokens.size() - 1; j >= 0; --j) {
4137 final AppWindowToken token = appTokens.get(j);
lumark588a3e82018-07-20 18:53:54 +08004138 if (!token.hasVisible && !mClosingApps.contains(token)
Wale Ogunwale1666e312016-12-16 11:27:18 -08004139 && (!token.mIsExiting || token.isEmpty())) {
4140 // Make sure there is no animation running on this token, so any windows
4141 // associated with it will be removed as soon as their animations are
4142 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02004143 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004144 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
4145 "performLayout: App token exiting now removed" + token);
4146 token.removeIfPossible();
4147 }
4148 }
4149 }
4150 }
4151
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004152 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004153 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004154 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
4155 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004156 // Apps and their containers are not allowed to specify an orientation while the
Wale Ogunwaled0c225c2018-06-15 14:47:46 -07004157 // docked or freeform stack is visible...except for the home stack if the docked
4158 // stack is minimized and it actually set something and the bounds is different from
4159 // the display.
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004160 if (mHomeStack != null && mHomeStack.isVisible()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004161 && mDividerControllerLocked.isMinimizedDock()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004162 && !(mDividerControllerLocked.isHomeStackResizable()
4163 && mHomeStack.matchParentBounds())) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004164 final int orientation = mHomeStack.getOrientation();
4165 if (orientation != SCREEN_ORIENTATION_UNSET) {
4166 return orientation;
4167 }
4168 }
4169 return SCREEN_ORIENTATION_UNSPECIFIED;
4170 }
4171
4172 final int orientation = super.getOrientation();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004173 boolean isCar = mWmService.mContext.getPackageManager().hasSystemFeature(
Dean Harding3e5a1522017-10-06 09:19:01 -07004174 PackageManager.FEATURE_AUTOMOTIVE);
4175 if (isCar) {
4176 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
4177 // allow anything but the default orientation.
4178 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004179 "Forcing UNSPECIFIED orientation in car for display id=" + mDisplayId
4180 + ". Ignoring " + orientation);
Dean Harding3e5a1522017-10-06 09:19:01 -07004181 return SCREEN_ORIENTATION_UNSPECIFIED;
4182 }
4183
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004184 if (orientation != SCREEN_ORIENTATION_UNSET
4185 && orientation != SCREEN_ORIENTATION_BEHIND) {
4186 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004187 "App is requesting an orientation, return " + orientation
4188 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004189 return orientation;
4190 }
4191
4192 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004193 "No app is requesting an orientation, return " + mLastOrientation
4194 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004195 // The next app has not been requested to be visible, so we keep the current orientation
4196 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08004197 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004198 }
Robert Carrb1579c82017-09-05 14:54:47 -07004199
4200 @Override
4201 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004202 assignStackOrdering(t);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004203
Robert Carr2f8aa392018-01-31 14:46:51 -08004204 for (int i = 0; i < mChildren.size(); i++) {
4205 final TaskStack s = mChildren.get(i);
4206 s.assignChildLayers(t);
4207 }
4208 }
4209
4210 void assignStackOrdering(SurfaceControl.Transaction t) {
Jorim Jaggibe418292018-03-26 16:14:12 +02004211
Robert Carrf7a7ca82017-12-06 13:17:07 -08004212 final int HOME_STACK_STATE = 0;
4213 final int NORMAL_STACK_STATE = 1;
4214 final int ALWAYS_ON_TOP_STATE = 2;
4215
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004216 int layer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08004217 int layerForAnimationLayer = 0;
Jorim Jaggibe418292018-03-26 16:14:12 +02004218 int layerForBoostedAnimationLayer = 0;
Jorim Jaggi391790622018-04-18 15:30:44 +02004219 int layerForHomeAnimationLayer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08004220
Robert Carrf7a7ca82017-12-06 13:17:07 -08004221 for (int state = 0; state <= ALWAYS_ON_TOP_STATE; state++) {
4222 for (int i = 0; i < mChildren.size(); i++) {
4223 final TaskStack s = mChildren.get(i);
Robert Carr2f8aa392018-01-31 14:46:51 -08004224 if (state == HOME_STACK_STATE && !s.isActivityTypeHome()) {
4225 continue;
4226 } else if (state == NORMAL_STACK_STATE && (s.isActivityTypeHome()
4227 || s.isAlwaysOnTop())) {
4228 continue;
4229 } else if (state == ALWAYS_ON_TOP_STATE && !s.isAlwaysOnTop()) {
4230 continue;
4231 }
4232 s.assignLayer(t, layer++);
4233 if (s.inSplitScreenWindowingMode() && mSplitScreenDividerAnchor != null) {
4234 t.setLayer(mSplitScreenDividerAnchor, layer++);
4235 }
Winson Chungd41f71d2018-03-16 15:26:07 -07004236 if ((s.isTaskAnimating() || s.isAppAnimating())
4237 && state != ALWAYS_ON_TOP_STATE) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004238 // Ensure the animation layer ends up above the
4239 // highest animating stack and no higher.
4240 layerForAnimationLayer = layer++;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004241 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004242 if (state != ALWAYS_ON_TOP_STATE) {
4243 layerForBoostedAnimationLayer = layer++;
4244 }
Robert Carrf7a7ca82017-12-06 13:17:07 -08004245 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004246 if (state == HOME_STACK_STATE) {
4247 layerForHomeAnimationLayer = layer++;
4248 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004249 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004250 if (mAppAnimationLayer != null) {
4251 t.setLayer(mAppAnimationLayer, layerForAnimationLayer);
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004252 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004253 if (mBoostedAppAnimationLayer != null) {
4254 t.setLayer(mBoostedAppAnimationLayer, layerForBoostedAnimationLayer);
4255 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004256 if (mHomeAppAnimationLayer != null) {
4257 t.setLayer(mHomeAppAnimationLayer, layerForHomeAnimationLayer);
4258 }
Robert Carrb1579c82017-09-05 14:54:47 -07004259 }
4260
4261 @Override
Jorim Jaggi391790622018-04-18 15:30:44 +02004262 SurfaceControl getAppAnimationLayer(@AnimationLayer int animationLayer) {
4263 switch (animationLayer) {
4264 case ANIMATION_LAYER_BOOSTED:
4265 return mBoostedAppAnimationLayer;
4266 case ANIMATION_LAYER_HOME:
4267 return mHomeAppAnimationLayer;
4268 case ANIMATION_LAYER_STANDARD:
4269 default:
4270 return mAppAnimationLayer;
4271 }
chaviw23ee71c2017-12-18 11:29:41 -08004272 }
4273
Robert Carrf7a7ca82017-12-06 13:17:07 -08004274 SurfaceControl getSplitScreenDividerAnchor() {
4275 return mSplitScreenDividerAnchor;
4276 }
4277
chaviw23ee71c2017-12-18 11:29:41 -08004278 @Override
Robert Carrb1579c82017-09-05 14:54:47 -07004279 void onParentSet() {
4280 super.onParentSet();
4281 if (getParent() != null) {
chaviw23ee71c2017-12-18 11:29:41 -08004282 mAppAnimationLayer = makeChildSurface(null)
4283 .setName("animationLayer")
4284 .build();
Jorim Jaggibe418292018-03-26 16:14:12 +02004285 mBoostedAppAnimationLayer = makeChildSurface(null)
4286 .setName("boostedAnimationLayer")
4287 .build();
Jorim Jaggi391790622018-04-18 15:30:44 +02004288 mHomeAppAnimationLayer = makeChildSurface(null)
4289 .setName("homeAnimationLayer")
4290 .build();
Robert Carrf7a7ca82017-12-06 13:17:07 -08004291 mSplitScreenDividerAnchor = makeChildSurface(null)
4292 .setName("splitScreenDividerAnchor")
4293 .build();
4294 getPendingTransaction()
4295 .show(mAppAnimationLayer)
Jorim Jaggibe418292018-03-26 16:14:12 +02004296 .show(mBoostedAppAnimationLayer)
Jorim Jaggi391790622018-04-18 15:30:44 +02004297 .show(mHomeAppAnimationLayer)
Robert Carrf7a7ca82017-12-06 13:17:07 -08004298 .show(mSplitScreenDividerAnchor);
chaviw23ee71c2017-12-18 11:29:41 -08004299 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07004300 } else {
chaviw23ee71c2017-12-18 11:29:41 -08004301 mAppAnimationLayer.destroy();
4302 mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02004303 mBoostedAppAnimationLayer.destroy();
4304 mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02004305 mHomeAppAnimationLayer.destroy();
4306 mHomeAppAnimationLayer = null;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004307 mSplitScreenDividerAnchor.destroy();
4308 mSplitScreenDividerAnchor = null;
Robert Carrb1579c82017-09-05 14:54:47 -07004309 }
4310 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004311 }
4312
Robert Carree4d4b92017-11-22 12:21:46 -08004313 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004314 AboveAppWindowContainers(String name, WindowManagerService service) {
4315 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08004316 }
4317
Robert Carrb9506032018-02-13 13:54:00 -08004318 @Override
Vishnu Nair83537a72018-07-19 21:27:48 -07004319 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
4320 final SurfaceControl.Builder builder = super.makeChildSurface(child);
4321 if (child instanceof WindowToken && ((WindowToken) child).mRoundedCornerOverlay) {
4322 // To draw above the ColorFade layer during the screen off transition, the
4323 // rounded corner overlays need to be at the root of the surface hierarchy.
4324 // TODO: move the ColorLayer into the display overlay layer such that this is not
4325 // necessary anymore.
4326 builder.setParent(null);
4327 }
4328 return builder;
4329 }
4330
4331 @Override
Robert Carrb9506032018-02-13 13:54:00 -08004332 void assignChildLayers(SurfaceControl.Transaction t) {
4333 assignChildLayers(t, null /* imeContainer */);
4334 }
4335
Robert Carree4d4b92017-11-22 12:21:46 -08004336 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
4337 boolean needAssignIme = imeContainer != null
4338 && imeContainer.getSurfaceControl() != null;
4339 for (int j = 0; j < mChildren.size(); ++j) {
4340 final WindowToken wt = mChildren.get(j);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004341
4342 // See {@link mSplitScreenDividerAnchor}
4343 if (wt.windowType == TYPE_DOCK_DIVIDER) {
4344 wt.assignRelativeLayer(t, mTaskStackContainers.getSplitScreenDividerAnchor(), 1);
4345 continue;
4346 }
Vishnu Nair83537a72018-07-19 21:27:48 -07004347 if (wt.mRoundedCornerOverlay) {
4348 wt.assignLayer(t, WindowManagerPolicy.COLOR_FADE_LAYER + 1);
4349 continue;
4350 }
Robert Carree4d4b92017-11-22 12:21:46 -08004351 wt.assignLayer(t, j);
4352 wt.assignChildLayers(t);
4353
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004354 int layer = mWmService.mPolicy.getWindowLayerFromTypeLw(
Robert Carree4d4b92017-11-22 12:21:46 -08004355 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08004356
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004357 if (needAssignIme && layer >= mWmService.mPolicy.getWindowLayerFromTypeLw(
Robert Carrb8e78fa2017-11-28 13:07:58 -08004358 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004359 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08004360 needAssignIme = false;
4361 }
4362 }
4363 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004364 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08004365 }
4366 }
4367 }
4368
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004369 /**
4370 * Window container class that contains all containers on this display that are not related to
4371 * Apps. E.g. status bar.
4372 */
Robert Carree4d4b92017-11-22 12:21:46 -08004373 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07004374 /**
4375 * Compares two child window tokens returns -1 if the first is lesser than the second in
4376 * terms of z-order and 1 otherwise.
4377 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004378 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07004379 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004380 mWmService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004381 token1.mOwnerCanManageAppTokens)
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004382 < mWmService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004383 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004384
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004385 private final Predicate<WindowState> mGetOrientingWindow = w -> {
4386 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
4387 return false;
4388 }
4389 final int req = w.mAttrs.screenOrientation;
4390 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
4391 || req == SCREEN_ORIENTATION_UNSET) {
4392 return false;
4393 }
4394 return true;
4395 };
4396
Wale Ogunwale3a931692016-11-02 16:49:48 -07004397 private final String mName;
chaviwf29223e2018-01-31 13:23:51 -08004398 private final Dimmer mDimmer = new Dimmer(this);
4399 private final Rect mTmpDimBoundsRect = new Rect();
4400
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004401 NonAppWindowContainers(String name, WindowManagerService service) {
4402 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07004403 mName = name;
4404 }
4405
4406 void addChild(WindowToken token) {
4407 addChild(token, mWindowComparator);
4408 }
4409
4410 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004411 int getOrientation() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004412 final WindowManagerPolicy policy = mWmService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004413 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004414 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004415
4416 if (win != null) {
4417 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004418 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004419 mLastKeyguardForcedOrientation = req;
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004420 if (mWmService.mKeyguardGoingAway) {
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07004421 // Keyguard can't affect the orientation if it is going away...
4422 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
4423 return SCREEN_ORIENTATION_UNSET;
4424 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004425 }
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004426 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req
4427 + " for display id=" + mDisplayId);
Andrii Kulian8ee72852017-03-10 10:36:45 -08004428 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004429 }
4430
Andrii Kulian8ee72852017-03-10 10:36:45 -08004431 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004432
Jorim Jaggi1c530592018-04-06 15:11:47 +02004433 // Only allow force setting the orientation when all unknown visibilities have been
4434 // resolved, as otherwise we just may be starting another occluding activity.
4435 final boolean isUnoccluding =
lumark588a3e82018-07-20 18:53:54 +08004436 mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
4437 && mUnknownAppVisibilityController.allResolved();
Jorim Jaggi1c530592018-04-06 15:11:47 +02004438 if (policy.isKeyguardShowingAndNotOccluded() || isUnoccluding) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004439 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004440 }
4441
4442 return SCREEN_ORIENTATION_UNSET;
4443 }
4444
4445 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07004446 String getName() {
4447 return mName;
4448 }
chaviwf29223e2018-01-31 13:23:51 -08004449
4450 @Override
4451 Dimmer getDimmer() {
4452 return mDimmer;
4453 }
4454
4455 @Override
4456 void prepareSurfaces() {
4457 mDimmer.resetDimStates();
4458 super.prepareSurfaces();
4459 getBounds(mTmpDimBoundsRect);
4460
4461 if (mDimmer.updateDims(getPendingTransaction(), mTmpDimBoundsRect)) {
4462 scheduleAnimation();
4463 }
4464 }
Robert Carr3b716242016-08-16 16:02:21 -07004465 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01004466
Robert Carr9034d962018-01-04 18:27:42 -08004467 private class NonMagnifiableWindowContainers extends NonAppWindowContainers {
4468 NonMagnifiableWindowContainers(String name, WindowManagerService service) {
4469 super(name, service);
4470 }
4471
4472 @Override
4473 void applyMagnificationSpec(Transaction t, MagnificationSpec spec) {
4474 }
4475 };
4476
Robert Carrb1579c82017-09-05 14:54:47 -07004477 SurfaceControl.Builder makeSurface(SurfaceSession s) {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004478 return mWmService.makeSurfaceBuilder(s)
Robert Carrb1579c82017-09-05 14:54:47 -07004479 .setParent(mWindowingLayer);
4480 }
4481
4482 @Override
4483 SurfaceSession getSession() {
4484 return mSession;
4485 }
4486
4487 @Override
4488 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07004489 SurfaceSession s = child != null ? child.getSession() : getSession();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004490 final SurfaceControl.Builder b = mWmService.makeSurfaceBuilder(s);
Robert Carrf59b8dd2017-10-02 18:58:36 -07004491 if (child == null) {
4492 return b;
4493 }
4494
Robert Carree4d4b92017-11-22 12:21:46 -08004495 return b.setName(child.getName())
4496 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07004497 }
4498
4499 /**
4500 * The makeSurface variants are for use by the window-container
4501 * hierarchy. makeOverlay here is a function for various non windowing
4502 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
4503 * and other potpourii.
4504 */
4505 SurfaceControl.Builder makeOverlay() {
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004506 return mWmService.makeSurfaceBuilder(mSession)
Robert Carrb1579c82017-09-05 14:54:47 -07004507 .setParent(mOverlayLayer);
4508 }
4509
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004510 /**
4511 * Reparents the given surface to mOverlayLayer.
4512 */
4513 void reparentToOverlay(Transaction transaction, SurfaceControl surface) {
4514 transaction.reparent(surface, mOverlayLayer.getHandle());
4515 }
4516
Robert Carrb1579c82017-09-05 14:54:47 -07004517 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carr24be9ab2018-04-30 17:54:53 -07004518 if (spec.scale != 1.0) {
4519 mMagnificationSpec = spec;
4520 } else {
4521 mMagnificationSpec = null;
4522 }
4523
Robert Carrf59b8dd2017-10-02 18:58:36 -07004524 applyMagnificationSpec(getPendingTransaction(), spec);
4525 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07004526 }
4527
Robert Carr24be9ab2018-04-30 17:54:53 -07004528 void reapplyMagnificationSpec() {
4529 if (mMagnificationSpec != null) {
4530 applyMagnificationSpec(getPendingTransaction(), mMagnificationSpec);
4531 }
4532 }
4533
Robert Carrb1579c82017-09-05 14:54:47 -07004534 @Override
4535 void onParentSet() {
4536 // Since we are the top of the SurfaceControl hierarchy here
4537 // we create the root surfaces explicitly rather than chaining
4538 // up as the default implementation in onParentSet does. So we
4539 // explicitly do NOT call super here.
4540 }
4541
4542 @Override
4543 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07004544
4545 // These are layers as children of "mWindowingLayer"
4546 mBelowAppWindowsContainers.assignLayer(t, 0);
4547 mTaskStackContainers.assignLayer(t, 1);
4548 mAboveAppWindowsContainers.assignLayer(t, 2);
4549
lumarkff0ab692018-11-05 20:32:30 +08004550 final WindowState imeTarget = mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08004551 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08004552
Robert Carree4d4b92017-11-22 12:21:46 -08004553 // In the case where we have an IME target that is not in split-screen
4554 // mode IME assignment is easy. We just need the IME to go directly above
4555 // the target. This way children of the target will naturally go above the IME
4556 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08004557 //
Robert Carree4d4b92017-11-22 12:21:46 -08004558 // In the case of split-screen windowing mode, we need to elevate the IME above the
4559 // docked divider while keeping the app itself below the docked divider, so instead
4560 // we use relative layering of the IME targets child windows, and place the
4561 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08004562 //
Robert Carr234b6332018-03-20 13:38:16 -07004563 // In the case the IME target is animating, the animation Z order may be different
4564 // than the WindowContainer Z order, so it's difficult to be sure we have the correct
4565 // IME target. In this case we just layer the IME over all transitions by placing it in the
4566 // above applications layer.
4567 //
Robert Carree4d4b92017-11-22 12:21:46 -08004568 // In the case where we have no IME target we assign it where it's base layer would
4569 // place it in the AboveAppWindowContainers.
Robert Carr234b6332018-03-20 13:38:16 -07004570 if (imeTarget != null && !(imeTarget.inSplitScreenWindowingMode()
4571 || imeTarget.mToken.isAppAnimating())
Robert Carree4d4b92017-11-22 12:21:46 -08004572 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004573 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07004574 // TODO: We need to use an extra level on the app surface to ensure
4575 // this is always above SurfaceView but always below attached window.
4576 1);
Robert Carree4d4b92017-11-22 12:21:46 -08004577 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07004578 }
4579
4580 // Above we have assigned layers to our children, now we ask them to assign
4581 // layers to their children.
4582 mBelowAppWindowsContainers.assignChildLayers(t);
4583 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08004584 mAboveAppWindowsContainers.assignChildLayers(t,
4585 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07004586 mImeWindowsContainers.assignChildLayers(t);
4587 }
4588
4589 /**
4590 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
4591 * that the IME target is one of the docked applications. We'd like the docked divider to be
4592 * above both of the applications, and we'd like the IME to be above the docked divider.
4593 * However we need child windows of the applications to be above the IME (Text drag handles).
4594 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
4595 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
Tiger Huang7c610aa2018-10-27 00:01:01 +08004596 * with {@link WindowState#assignLayer}
Robert Carrb1579c82017-09-05 14:54:47 -07004597 */
4598 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
Robert Carrbb5de662018-04-26 13:29:51 -07004599 child.assignRelativeLayer(t, mImeWindowsContainers.getSurfaceControl(), 1);
Robert Carrb1579c82017-09-05 14:54:47 -07004600 }
4601
4602 @Override
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004603 void prepareSurfaces() {
4604 final ScreenRotationAnimation screenRotationAnimation =
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004605 mWmService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004606 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
4607 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
4608 mPendingTransaction.setMatrix(mWindowingLayer,
4609 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
4610 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
4611 mPendingTransaction.setPosition(mWindowingLayer,
4612 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
4613 mPendingTransaction.setAlpha(mWindowingLayer,
4614 screenRotationAnimation.getEnterTransformation().getAlpha());
4615 }
Chavi Weingartenb736e322018-02-23 00:27:54 +00004616
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004617 super.prepareSurfaces();
4618 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004619
Jorim Jaggibe418292018-03-26 16:14:12 +02004620 void assignStackOrdering() {
4621 mTaskStackContainers.assignStackOrdering(getPendingTransaction());
Robert Carr2f8aa392018-01-31 14:46:51 -08004622 }
Chavi Weingarten3a748552018-05-14 17:32:42 +00004623
4624 /**
4625 * Increment the deferral count to determine whether to update the IME target.
4626 */
4627 void deferUpdateImeTarget() {
4628 mDeferUpdateImeTargetCount++;
4629 }
4630
4631 /**
4632 * Decrement the deferral count to determine whether to update the IME target. If the count
4633 * reaches 0, a new ime target will get computed.
4634 */
4635 void continueUpdateImeTarget() {
4636 if (mDeferUpdateImeTargetCount == 0) {
4637 return;
4638 }
4639
4640 mDeferUpdateImeTargetCount--;
4641 if (mDeferUpdateImeTargetCount == 0) {
4642 computeImeTarget(true /* updateImeTarget */);
4643 }
4644 }
4645
4646 /**
4647 * @return Whether a new IME target should be computed.
4648 */
4649 private boolean canUpdateImeTarget() {
4650 return mDeferUpdateImeTargetCount == 0;
4651 }
Arthur Hung95b38a92018-07-20 18:56:12 +08004652
4653 InputMonitor getInputMonitor() {
4654 return mInputMonitor;
4655 }
Jorim Jaggif1292892018-09-10 11:58:13 +02004656
4657 /**
4658 * @return Cached value whether we told display manager that we have content.
4659 */
4660 boolean getLastHasContent() {
4661 return mLastHasContent;
4662 }
Arthur Hungbe5ce212018-09-13 18:41:56 +08004663
4664 void registerPointerEventListener(@NonNull PointerEventListener listener) {
4665 if (mPointerEventDispatcher != null) {
4666 mPointerEventDispatcher.registerInputEventListener(listener);
4667 }
4668 }
4669
4670 void unregisterPointerEventListener(@NonNull PointerEventListener listener) {
4671 if (mPointerEventDispatcher != null) {
4672 mPointerEventDispatcher.unregisterInputEventListener(listener);
4673 }
4674 }
lumark588a3e82018-07-20 18:53:54 +08004675
4676 void prepareAppTransition(@WindowManager.TransitionType int transit,
4677 boolean alwaysKeepCurrent, @WindowManager.TransitionFlags int flags,
4678 boolean forceOverride) {
4679 final boolean prepared = mAppTransition.prepareAppTransitionLocked(
4680 transit, alwaysKeepCurrent, flags, forceOverride);
4681 if (prepared && okToAnimate()) {
4682 mSkipAppTransitionAnimation = false;
4683 }
4684 }
4685
4686 void executeAppTransition() {
4687 if (mAppTransition.isTransitionSet()) {
4688 if (DEBUG_APP_TRANSITIONS) {
4689 Slog.w(TAG_WM, "Execute app transition: " + mAppTransition + ", displayId: "
4690 + mDisplayId + " Callers=" + Debug.getCallers(5));
4691 }
4692 mAppTransition.setReady();
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004693 mWmService.mWindowPlacerLocked.requestTraversal();
lumark588a3e82018-07-20 18:53:54 +08004694 }
4695 }
4696
4697 /**
4698 * Update pendingLayoutChanges after app transition has finished.
4699 */
4700 void handleAnimatingStoppedAndTransition() {
4701 int changes = 0;
4702
4703 mAppTransition.setIdle();
4704
4705 for (int i = mNoAnimationNotifyOnTransitionFinished.size() - 1; i >= 0; i--) {
4706 final IBinder token = mNoAnimationNotifyOnTransitionFinished.get(i);
4707 mAppTransition.notifyAppTransitionFinishedLocked(token);
4708 }
4709 mNoAnimationNotifyOnTransitionFinished.clear();
4710
4711 mWallpaperController.hideDeferredWallpapersIfNeeded();
4712
4713 onAppTransitionDone();
4714
4715 changes |= FINISH_LAYOUT_REDO_LAYOUT;
4716 if (DEBUG_WALLPAPER_LIGHT) {
4717 Slog.v(TAG_WM, "Wallpaper layer changed: assigning layers + relayout");
4718 }
4719 computeImeTarget(true /* updateImeTarget */);
wilsonshihc32538e2018-11-07 17:27:34 +08004720 mWallpaperMayChange = true;
lumark588a3e82018-07-20 18:53:54 +08004721 // Since the window list has been rebuilt, focus might have to be recomputed since the
4722 // actual order of windows might have changed again.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004723 mWmService.mFocusMayChange = true;
lumark588a3e82018-07-20 18:53:54 +08004724
4725 pendingLayoutChanges |= changes;
4726 }
Andrii Kulian15cfb422018-11-07 13:38:49 -08004727
4728 /**
4729 * @see Display#FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS
4730 */
4731 boolean supportsSystemDecorations() {
4732 // TODO(b/114338689): Read the setting from DisplaySettings.
4733 return mDisplay.supportsSystemDecorations()
4734 // TODO (b/111363427): Remove this and set the new FLAG_SHOULD_SHOW_LAUNCHER flag
4735 // (b/114338689) whenever vr 2d display id is set.
Wale Ogunwale8b19de92018-11-29 19:58:26 -08004736 || mDisplayId == mWmService.mVr2dDisplayId
4737 || mWmService.mForceDesktopModeOnExternalDisplays;
Andrii Kulian15cfb422018-11-07 13:38:49 -08004738 }
Craig Mautner59c00972012-07-30 12:10:24 -07004739}