blob: 886b2ff1ce54add9355ac3771d29172a4b3d7b6d [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;
Wale Ogunwale10124582016-09-15 20:25:50 -070037import static android.view.WindowManager.DOCKED_BOTTOM;
38import static android.view.WindowManager.DOCKED_INVALID;
39import static android.view.WindowManager.DOCKED_TOP;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080040import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
41import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
42import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale494009b82016-10-21 09:01:38 -070043import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070044import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070045import static android.view.WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE;
46import static android.view.WindowManager.LayoutParams.NEEDS_MENU_UNSET;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070047import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale494009b82016-10-21 09:01:38 -070048import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070049import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale494009b82016-10-21 09:01:38 -070050import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
Robert Carrf7a7ca82017-12-06 13:17:07 -080051import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
Wale Ogunwale494009b82016-10-21 09:01:38 -070052import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070053import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070054import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
55import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Wale Ogunwale494009b82016-10-21 09:01:38 -070056import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070057import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
58import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070059import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070060import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080061import static android.view.WindowManager.TRANSIT_KEYGUARD_UNOCCLUDE;
62
Adrian Roose99bc052017-11-20 17:55:31 +010063import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
64import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
65import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
66import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080067import static com.android.server.wm.DisplayContentProto.ABOVE_APP_WINDOWS;
lumark588a3e82018-07-20 18:53:54 +080068import static com.android.server.wm.DisplayContentProto.APP_TRANSITION;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080069import static com.android.server.wm.DisplayContentProto.BELOW_APP_WINDOWS;
70import static com.android.server.wm.DisplayContentProto.DISPLAY_FRAMES;
71import static com.android.server.wm.DisplayContentProto.DISPLAY_INFO;
72import static com.android.server.wm.DisplayContentProto.DOCKED_STACK_DIVIDER_CONTROLLER;
73import static com.android.server.wm.DisplayContentProto.DPI;
Tiger Huang1e5b10a2018-07-30 20:19:51 +080074import static com.android.server.wm.DisplayContentProto.FOCUSED_APP;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080075import static com.android.server.wm.DisplayContentProto.ID;
76import static com.android.server.wm.DisplayContentProto.IME_WINDOWS;
77import static com.android.server.wm.DisplayContentProto.PINNED_STACK_CONTROLLER;
78import static com.android.server.wm.DisplayContentProto.ROTATION;
79import static com.android.server.wm.DisplayContentProto.SCREEN_ROTATION_ANIMATION;
80import static com.android.server.wm.DisplayContentProto.STACKS;
Louis Chang7501e332018-08-20 13:08:39 +080081import static com.android.server.wm.DisplayContentProto.SURFACE_SIZE;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080082import 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;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800103import static com.android.server.wm.WindowManagerService.H.REPORT_LOSING_FOCUS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700104import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
105import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700106import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700107import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700108import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
109import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800110import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_PLACING_SURFACES;
111import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_REMOVING_FOCUS;
112import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_ASSIGN_LAYERS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700113import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700114import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700115import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700116import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700117import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700118import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700119import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700120import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700121import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700122
lumark588a3e82018-07-20 18:53:54 +0800123import android.animation.AnimationHandler;
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700124import android.annotation.CallSuper;
Riddle Hsuf53da812018-08-15 22:00:27 +0800125import android.annotation.IntDef;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700126import android.annotation.NonNull;
Dean Harding3e5a1522017-10-06 09:19:01 -0700127import android.content.pm.PackageManager;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700128import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700129import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700130import android.graphics.Bitmap;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700131import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800132import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700133import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700134import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100135import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700136import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700137import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700138import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700139import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700140import android.os.RemoteException;
141import android.os.SystemClock;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100142import android.os.Trace;
Riddle Hsuf53da812018-08-15 22:00:27 +0800143import android.os.UserHandle;
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800144import android.util.ArraySet;
Chong Zhang8e89b312015-09-09 15:09:30 -0700145import android.util.DisplayMetrics;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700146import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700147import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700148import android.view.Display;
Adrian Roos1cf585052018-01-03 18:43:27 +0100149import android.view.DisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700150import android.view.DisplayInfo;
Riddle Hsua4d6fa22018-08-11 00:50:39 +0800151import android.view.Gravity;
Arthur Hungbe5ce212018-09-13 18:41:56 +0800152import android.view.InputChannel;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700153import android.view.InputDevice;
Robert Carrb1579c82017-09-05 14:54:47 -0700154import android.view.MagnificationSpec;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700155import android.view.Surface;
156import android.view.SurfaceControl;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100157import android.view.SurfaceControl.Transaction;
Robert Carrb1579c82017-09-05 14:54:47 -0700158import android.view.SurfaceSession;
Tiger Huang7c610aa2018-10-27 00:01:01 +0800159import android.view.View;
lumark588a3e82018-07-20 18:53:54 +0800160import android.view.WindowManager;
Evan Rosky39b6f232018-10-30 18:35:41 -0700161import android.view.WindowManagerPolicyConstants.PointerEventListener;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700162
Bryce Lee48f4b572017-04-10 10:54:15 -0700163import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800164import com.android.internal.util.ToBooleanFunction;
Adrian Roose99bc052017-11-20 17:55:31 +0100165import com.android.server.policy.WindowManagerPolicy;
Issei Suzuki43190bd2018-08-20 17:28:41 +0200166import com.android.server.wm.utils.DisplayRotationUtil;
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100167import com.android.server.wm.utils.RotationCache;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100168import com.android.server.wm.utils.WmDisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700169
170import java.io.PrintWriter;
Riddle Hsuf53da812018-08-15 22:00:27 +0800171import java.lang.annotation.Retention;
172import java.lang.annotation.RetentionPolicy;
Craig Mautner59c00972012-07-30 12:10:24 -0700173import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700174import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700175import java.util.HashMap;
176import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700177import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700178import java.util.List;
Adrian Roos1cf585052018-01-03 18:43:27 +0100179import java.util.Objects;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800180import java.util.function.Consumer;
181import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700182
Craig Mautner59c00972012-07-30 12:10:24 -0700183/**
184 * Utility class for keeping track of the WindowStates and other pertinent contents of a
185 * particular Display.
Craig Mautner59c00972012-07-30 12:10:24 -0700186 */
Riddle Hsuad256a12018-07-18 16:11:30 +0800187class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer>
188 implements WindowManagerPolicy.DisplayContentInfo {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700189 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700190
Riddle Hsuf53da812018-08-15 22:00:27 +0800191 /** The default scaling mode that scales content automatically. */
192 static final int FORCE_SCALING_MODE_AUTO = 0;
193 /** For {@link #setForcedScalingMode} to apply flag {@link Display#FLAG_SCALING_DISABLED}. */
194 static final int FORCE_SCALING_MODE_DISABLED = 1;
195
196 @IntDef(prefix = { "FORCE_SCALING_MODE_" }, value = {
197 FORCE_SCALING_MODE_AUTO,
198 FORCE_SCALING_MODE_DISABLED
199 })
200 @Retention(RetentionPolicy.SOURCE)
201 @interface ForceScalingMode {}
202
Craig Mautner59c00972012-07-30 12:10:24 -0700203 /** Unique identifier of this stack. */
204 private final int mDisplayId;
205
Wale Ogunwale3a931692016-11-02 16:49:48 -0700206 /** The containers below are the only child containers the display can have. */
207 // Contains all window containers that are related to apps (Activities)
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100208 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers(mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700209 // Contains all non-app window containers that should be displayed above the app containers
210 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800211 private final AboveAppWindowContainers mAboveAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100212 new AboveAppWindowContainers("mAboveAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700213 // Contains all non-app window containers that should be displayed below the app containers
214 // (e.g. Wallpaper).
215 private final NonAppWindowContainers mBelowAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100216 new NonAppWindowContainers("mBelowAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700217 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
218 // 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 -0800219 // window containers together and move them in-sync if/when needed. We use a subclass of
220 // WindowContainer which is omitted from screen magnification, as the IME is never magnified.
221 private final NonMagnifiableWindowContainers mImeWindowsContainers =
222 new NonMagnifiableWindowContainers("mImeWindowsContainers", mService);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700223
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000224 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800225 private WindowState mTmpWindow2;
226 private WindowAnimator mTmpWindowAnimator;
227 private boolean mTmpRecoveringMemory;
228 private boolean mUpdateImeTarget;
229 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700230 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700231
lumark588a3e82018-07-20 18:53:54 +0800232 final AppTransition mAppTransition;
233 final AppTransitionController mAppTransitionController;
234 boolean mSkipAppTransitionAnimation = false;
235
236 final ArraySet<AppWindowToken> mOpeningApps = new ArraySet<>();
237 final ArraySet<AppWindowToken> mClosingApps = new ArraySet<>();
238 final UnknownAppVisibilityController mUnknownAppVisibilityController;
239 BoundsAnimationController mBoundsAnimationController;
240
241 /**
242 * List of clients without a transtiton animation that we notify once we are done
243 * transitioning since they won't be notified through the app window animator.
244 */
245 final List<IBinder> mNoAnimationNotifyOnTransitionFinished = new ArrayList<>();
246
Wale Ogunwale02319a62016-09-26 15:21:22 -0700247 // Mapping from a token IBinder to a WindowToken object on this display.
248 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
249
Andrii Kuliancd097992017-03-23 18:31:59 -0700250 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700251 int mInitialDisplayWidth = 0;
252 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700253 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700254
Adrian Roos1cf585052018-01-03 18:43:27 +0100255 DisplayCutout mInitialDisplayCutout;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100256 private final RotationCache<DisplayCutout, WmDisplayCutout> mDisplayCutoutCache
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100257 = new RotationCache<>(this::calculateDisplayCutoutForRotationUncached);
Adrian Roos1cf585052018-01-03 18:43:27 +0100258
Andrii Kuliancd097992017-03-23 18:31:59 -0700259 /**
260 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
261 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
262 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
263 */
Craig Mautner59c00972012-07-30 12:10:24 -0700264 int mBaseDisplayWidth = 0;
265 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700266 /**
267 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
268 * but can be set from Settings or via shell command "adb shell wm density".
269 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
270 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700271 int mBaseDisplayDensity = 0;
Riddle Hsuf53da812018-08-15 22:00:27 +0800272
273 /**
274 * Whether to disable display scaling. This can be set via shell command "adb shell wm scaling".
275 * @see WindowManagerService#setForcedDisplayScalingMode(int, int)
276 */
Jeff Brownd46747a2015-04-15 19:02:36 -0700277 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700278 private final DisplayInfo mDisplayInfo = new DisplayInfo();
279 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700280 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800281 private final DisplayPolicy mDisplayPolicy;
282 private DisplayRotation mDisplayRotation;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000283 DisplayFrames mDisplayFrames;
284
Andrii Kulian06d07d62017-03-14 11:11:47 -0700285 /**
286 * For default display it contains real metrics, empty for others.
287 * @see WindowManagerService#createWatermarkInTransaction()
288 */
289 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800290
291 /** @see #computeCompatSmallestWidth(boolean, int, int, int, DisplayCutout) */
Andrii Kulian06d07d62017-03-14 11:11:47 -0700292 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700293
Andrii Kulian06d07d62017-03-14 11:11:47 -0700294 /**
295 * Compat metrics computed based on {@link #mDisplayMetrics}.
296 * @see #updateDisplayAndOrientation(int)
297 */
298 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
299
300 /** The desired scaling factor for compatible apps. */
301 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700302
Andrii Kulian8ee72852017-03-10 10:36:45 -0800303 /**
304 * Current rotation of the display.
305 * Constants as per {@link android.view.Surface.Rotation}.
306 *
Robert Carrae606b42018-02-15 15:36:23 -0800307 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800308 */
309 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700310
Andrii Kulian8ee72852017-03-10 10:36:45 -0800311 /**
312 * Last applied orientation of the display.
313 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
314 *
Riddle Hsu4e611772018-10-31 18:58:28 +0800315 * @see #updateOrientationFromAppTokens()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800316 */
317 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700318
Andrii Kulian8ee72852017-03-10 10:36:45 -0800319 /**
320 * Flag indicating that the application is receiving an orientation that has different metrics
321 * than it expected. E.g. Portrait instead of Landscape.
322 *
Robert Carrae606b42018-02-15 15:36:23 -0800323 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800324 */
325 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700326
Andrii Kulian8ee72852017-03-10 10:36:45 -0800327 /**
328 * Orientation forced by some window. If there is no visible window that specifies orientation
329 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
330 *
331 * @see NonAppWindowContainers#getOrientation()
332 */
333 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700334
Andrii Kulian8ee72852017-03-10 10:36:45 -0800335 /**
336 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
337 * occluded.
338 *
339 * @see NonAppWindowContainers#getOrientation()
340 */
341 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
342
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700343 /**
344 * Keep track of wallpaper visibility to notify changes.
345 */
346 private boolean mLastWallpaperVisible = false;
347
Andrii Kulian06d07d62017-03-14 11:11:47 -0700348 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700349
Craig Mautner39834192012-09-02 07:47:24 -0700350 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700351 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700352 int pendingLayoutChanges;
Riddle Hsu654a6f92018-07-13 22:59:36 +0800353 int mDeferredRotationPauseCount;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800354
Andrii Kulian839def92016-11-02 10:58:58 -0700355 // TODO(multi-display): remove some of the usages.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800356 @VisibleForTesting
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800357 boolean isDefaultDisplay;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800358
Andrii Kulianf0379de2018-03-14 16:24:07 -0700359 /**
360 * Flag indicating whether WindowManager should override info for this display in
361 * DisplayManager.
362 */
363 boolean mShouldOverrideDisplayConfiguration = true;
Craig Mautner39834192012-09-02 07:47:24 -0700364
Craig Mautnerdc548482014-02-05 13:35:24 -0800365 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700366 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800367
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700368 /** Detect user tapping outside of current focused task bounds .*/
369 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700370
Craig Mautner6601b7b2013-04-29 10:29:11 -0700371 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700372 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700373
Craig Mautner6601b7b2013-04-29 10:29:11 -0700374 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800375 private final Rect mTmpRect = new Rect();
376 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700377 private final RectF mTmpRectF = new RectF();
378 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800379 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700380
Issei Suzuki43190bd2018-08-20 17:28:41 +0200381
Bryce Leef3c6a472017-11-14 14:53:06 -0800382 /** Used for handing back size of display */
383 private final Rect mTmpBounds = new Rect();
384
Craig Mautner95da1082014-02-24 17:54:35 -0800385 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700386 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800387
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700388 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700389 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700390
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800391 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800392 /** A collection of windows that provide tap exclude regions inside of them. */
393 final ArraySet<WindowState> mTapExcludeProvidingWindows = new ArraySet<>();
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800394
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000395 private boolean mHaveBootMsg = false;
396 private boolean mHaveApp = false;
397 private boolean mHaveWallpaper = false;
398 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700399
400 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
401
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700402 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
403 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000404 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
405 new ApplySurfaceChangesTransactionState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700406
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700407 // True if this display is in the process of being removed. Used to determine if the removal of
408 // the display's direct children should be allowed.
409 private boolean mRemovingDisplay = false;
410
Bryce Leed1871262017-06-12 14:12:29 -0700411 // {@code false} if this display is in the processing of being created.
412 private boolean mDisplayReady = false;
413
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800414 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700415
wilsonshihc32538e2018-11-07 17:27:34 +0800416 boolean mWallpaperMayChange = false;
417
Robert Carrb1579c82017-09-05 14:54:47 -0700418 private final SurfaceSession mSession = new SurfaceSession();
419
420 /**
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800421 * Window that is currently interacting with the user. This window is responsible for receiving
422 * key events and pointer events from the user.
423 */
424 WindowState mCurrentFocus = null;
425
426 /**
427 * The last focused window that we've notified the client that the focus is changed.
428 */
429 WindowState mLastFocus = null;
430
431 /**
432 * Windows that have lost input focus and are waiting for the new focus window to be displayed
433 * before they are told about this.
434 */
435 ArrayList<WindowState> mLosingFocus = new ArrayList<>();
436
437 /**
438 * The foreground app of this display. Windows below this app cannot be the focused window. If
439 * the user taps on the area outside of the task of the focused app, we will notify AM about the
440 * new task the user wants to interact with.
441 */
442 AppWindowToken mFocusedApp = null;
443
444 /** Windows added since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
445 final ArrayList<WindowState> mWinAddedSinceNullFocus = new ArrayList<>();
446
447 /** Windows removed since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
448 final ArrayList<WindowState> mWinRemovedSinceNullFocus = new ArrayList<>();
449
450 /**
Robert Carrb1579c82017-09-05 14:54:47 -0700451 * We organize all top-level Surfaces in to the following layers.
452 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800453 * and omitted from Screen-Magnification, for example the strict mode flash or
454 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700455 * {@link #mWindowingLayer} contains everything else.
456 */
457 private SurfaceControl mOverlayLayer;
458
459 /**
460 * See {@link #mOverlayLayer}
461 */
462 private SurfaceControl mWindowingLayer;
463
464 /**
465 * Specifies the size of the surfaces in {@link #mOverlayLayer} and {@link #mWindowingLayer}.
466 * <p>
467 * For these surfaces currently we use a surface based on the larger of width or height so we
468 * don't have to resize when rotating the display.
469 */
470 private int mSurfaceSize;
471
Adrian Roos5251b1d2018-03-23 18:57:43 +0100472 /**
473 * Sequence number for the current layout pass.
474 */
475 int mLayoutSeq = 0;
476
Chavi Weingarten3a748552018-05-14 17:32:42 +0000477 /**
478 * Specifies the count to determine whether to defer updating the IME target until ready.
479 */
480 private int mDeferUpdateImeTargetCount;
481
Jorim Jaggi9af095b2017-12-12 17:18:57 +0100482 /** Temporary float array to retrieve 3x3 matrix values. */
483 private final float[] mTmpFloats = new float[9];
484
Robert Carr24be9ab2018-04-30 17:54:53 -0700485 private MagnificationSpec mMagnificationSpec;
486
Arthur Hung95b38a92018-07-20 18:56:12 +0800487 private InputMonitor mInputMonitor;
488
Jorim Jaggif1292892018-09-10 11:58:13 +0200489 /** Caches the value whether told display manager that we have content. */
490 private boolean mLastHasContent;
491
Issei Suzuki43190bd2018-08-20 17:28:41 +0200492 private DisplayRotationUtil mRotationUtil = new DisplayRotationUtil();
493
lumark90120a82018-08-15 00:33:03 +0800494 /**
495 * The input method window for this display.
496 */
497 WindowState mInputMethodWindow;
498
lumarkff0ab692018-11-05 20:32:30 +0800499 /**
500 * This just indicates the window the input method is on top of, not
501 * necessarily the window its input is going to.
502 */
503 WindowState mInputMethodTarget;
504
505 /** If true hold off on modifying the animation layer of mInputMethodTarget */
506 boolean mInputMethodTargetWaitingAnim;
507
Arthur Hungbe5ce212018-09-13 18:41:56 +0800508 private final PointerEventDispatcher mPointerEventDispatcher;
509
Tiger Huang7c610aa2018-10-27 00:01:01 +0800510 // Last systemUiVisibility we received from status bar.
511 private int mLastStatusBarVisibility = 0;
512 // Last systemUiVisibility we dispatched to windows.
513 private int mLastDispatchedSystemUiVisibility = 0;
514
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800515 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
516 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800517 final AppWindowToken atoken = w.mAppToken;
518 if (winAnimator.mDrawState == READY_TO_SHOW) {
Jorim Jaggiaf0d6d22018-06-08 15:25:35 +0200519 if (atoken == null || atoken.canShowWindows()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800520 if (w.performShowLocked()) {
521 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
522 if (DEBUG_LAYOUT_REPEATS) {
523 mService.mWindowPlacerLocked.debugLayoutRepeats(
524 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
525 }
526 }
527 }
528 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800529 };
530
531 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
532 final WindowStateAnimator winAnimator = w.mWinAnimator;
533 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
534 return;
535 }
536
Jorim Jaggi8f520872018-08-14 17:00:20 +0200537 // If this window is animating, ensure the animation background is set.
538 final AnimationAdapter anim = w.mAppToken != null
539 ? w.mAppToken.getAnimation()
540 : w.getAnimation();
541 if (anim != null) {
542 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800543 if (color != 0) {
544 final TaskStack stack = w.getStack();
545 if (stack != null) {
546 stack.setAnimationBackground(winAnimator, color);
547 }
548 }
549 }
550 };
551
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800552 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
553 final int lostFocusUid = mTmpWindow.mOwnerUid;
554 final Handler handler = mService.mH;
555 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
556 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
557 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
558 w.mAttrs.hideTimeoutMilliseconds);
559 }
560 }
561 };
562
563 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800564 final AppWindowToken focusedApp = mFocusedApp;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800565 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
566 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
567
568 if (!w.canReceiveKeys()) {
569 return false;
570 }
571
572 final AppWindowToken wtoken = w.mAppToken;
573
574 // If this window's application has been removed, just skip it.
575 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
576 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
577 + (wtoken.removed ? "removed" : "sendingToBottom"));
578 return false;
579 }
580
581 if (focusedApp == null) {
582 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
583 + " using new focus @ " + w);
584 mTmpWindow = w;
585 return true;
586 }
587
588 if (!focusedApp.windowsAreFocusable()) {
589 // Current focused app windows aren't focusable...
590 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
591 + " focusable using new focus @ " + w);
592 mTmpWindow = w;
593 return true;
594 }
595
596 // Descend through all of the app tokens and find the first that either matches
597 // win.mAppToken (return win) or mFocusedApp (return null).
598 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
599 if (focusedApp.compareTo(wtoken) > 0) {
600 // App stack below focused app stack. No focus for you!!!
601 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
602 "findFocusedWindow: Reached focused app=" + focusedApp);
603 mTmpWindow = null;
604 return true;
605 }
606 }
607
608 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
609 mTmpWindow = w;
610 return true;
611 };
612
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800613 private final Consumer<WindowState> mPerformLayout = w -> {
614 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
615 // wasting time and funky changes while a window is animating away.
616 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
617 || w.isGoneForLayoutLw();
618
619 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
620 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
621 + " mLayoutAttached=" + w.mLayoutAttached
622 + " screen changed=" + w.isConfigChanged());
623 final AppWindowToken atoken = w.mAppToken;
624 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200625 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800626 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
627 + " parentHidden=" + w.isParentWindowHidden());
628 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200629 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800630 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
631 + " parentHidden=" + w.isParentWindowHidden());
632 }
633
634 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
635 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
636 // since that means "perform layout as normal, just don't display").
637 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
638 || ((w.isConfigChanged() || w.setReportResizeHints())
639 && !w.isGoneForLayoutLw() &&
640 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
641 (w.mHasSurface && w.mAppToken != null &&
642 w.mAppToken.layoutConfigChanges)))) {
643 if (!w.mLayoutAttached) {
644 if (mTmpInitial) {
645 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700646 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800647 }
648 if (w.mAttrs.type == TYPE_DREAM) {
649 // Don't layout windows behind a dream, so that if it does stuff like hide
650 // the status bar we won't get a bad transition when it goes away.
651 mTmpWindow = w;
652 }
653 w.mLayoutNeeded = false;
654 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200655 final boolean firstLayout = !w.isLaidOut();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800656 getDisplayPolicy().layoutWindowLw(w, null, mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100657 w.mLayoutSeq = mLayoutSeq;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800658
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200659 // If this is the first layout, we need to initialize the last inset values as
660 // otherwise we'd immediately cause an unnecessary resize.
661 if (firstLayout) {
662 w.updateLastInsetValues();
663 }
664
Adrian Roos23df3a32018-03-15 15:41:13 +0100665 if (w.mAppToken != null) {
666 w.mAppToken.layoutLetterbox(w);
667 }
668
chaviw492139a2018-07-16 16:07:35 -0700669 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700670 + " mContainingFrame=" + w.getContainingFrame()
671 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800672 }
673 }
674 };
675
676 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
677 if (w.mLayoutAttached) {
678 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
679 + " mViewVisibility=" + w.mViewVisibility
680 + " mRelayoutCalled=" + w.mRelayoutCalled);
681 // If this view is GONE, then skip it -- keep the current frame, and let the caller
682 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
683 // windows, since that means "perform layout as normal, just don't display").
684 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
685 return;
686 }
687 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
688 || w.mLayoutNeeded) {
689 if (mTmpInitial) {
690 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700691 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800692 }
693 w.mLayoutNeeded = false;
694 w.prelayout();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800695 getDisplayPolicy().layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100696 w.mLayoutSeq = mLayoutSeq;
chaviw492139a2018-07-16 16:07:35 -0700697 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700698 + " mContainingFrame=" + w.getContainingFrame()
699 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800700 }
701 } else if (w.mAttrs.type == TYPE_DREAM) {
702 // Don't layout windows behind a dream, so that if it does stuff like hide the
703 // status bar we won't get a bad transition when it goes away.
704 mTmpWindow = mTmpWindow2;
705 }
706 };
707
708 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
709 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
710 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
711 return w.canBeImeTarget();
712 };
713
714 private final Consumer<WindowState> mApplyPostLayoutPolicy =
Tiger Huang7c610aa2018-10-27 00:01:01 +0800715 w -> getDisplayPolicy().applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
lumarkff0ab692018-11-05 20:32:30 +0800716 mInputMethodTarget);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800717
718 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
719 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
720 final boolean obscuredChanged = w.mObscured !=
721 mTmpApplySurfaceChangesTransactionState.obscured;
722 final RootWindowContainer root = mService.mRoot;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800723
724 // Update effect.
725 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
726 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
727 final boolean isDisplayed = w.isDisplayedLw();
728
729 if (isDisplayed && w.isObscuringDisplay()) {
730 // This window completely covers everything behind it, so we want to leave all
731 // of them as undimmed (for performance reasons).
732 root.mObscuringWindow = w;
733 mTmpApplySurfaceChangesTransactionState.obscured = true;
734 }
735
736 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
737 root.handleNotObscuredLocked(w,
738 mTmpApplySurfaceChangesTransactionState.obscured,
739 mTmpApplySurfaceChangesTransactionState.syswin);
740
741 if (w.mHasSurface && isDisplayed) {
742 final int type = w.mAttrs.type;
743 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
744 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
745 mTmpApplySurfaceChangesTransactionState.syswin = true;
746 }
747 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
748 && w.mAttrs.preferredRefreshRate != 0) {
749 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
750 = w.mAttrs.preferredRefreshRate;
751 }
752 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
753 && w.mAttrs.preferredDisplayModeId != 0) {
754 mTmpApplySurfaceChangesTransactionState.preferredModeId
755 = w.mAttrs.preferredDisplayModeId;
756 }
757 }
758 }
759
wilsonshihc32538e2018-11-07 17:27:34 +0800760 if (obscuredChanged && w.isVisibleLw() && mWallpaperController.isWallpaperTarget(w)) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800761 // This is the wallpaper target and its obscured state changed... make sure the
762 // current wallpaper's visibility has been updated accordingly.
763 mWallpaperController.updateWallpaperVisibility();
764 }
765
chaviw161ea3e2018-01-31 12:01:12 -0800766 w.handleWindowMovedIfNeeded();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800767
768 final WindowStateAnimator winAnimator = w.mWinAnimator;
769
770 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
chaviw1454b392018-08-06 09:54:04 -0700771 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800772
773 // Moved from updateWindowsAndWallpaperLocked().
774 if (w.mHasSurface) {
775 // Take care of the window being ready to display.
776 final boolean committed = winAnimator.commitFinishDrawingLocked();
777 if (isDefaultDisplay && committed) {
778 if (w.mAttrs.type == TYPE_DREAM) {
779 // HACK: When a dream is shown, it may at that point hide the lock screen.
780 // So we need to redo the layout to let the phone window manager make this
781 // happen.
782 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
783 if (DEBUG_LAYOUT_REPEATS) {
784 surfacePlacer.debugLayoutRepeats(
785 "dream and commitFinishDrawingLocked true",
786 pendingLayoutChanges);
787 }
788 }
789 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
790 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
791 "First draw done in potential wallpaper target " + w);
wilsonshihc32538e2018-11-07 17:27:34 +0800792 mWallpaperMayChange = true;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800793 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
794 if (DEBUG_LAYOUT_REPEATS) {
795 surfacePlacer.debugLayoutRepeats(
796 "wallpaper and commitFinishDrawingLocked true",
797 pendingLayoutChanges);
798 }
799 }
800 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800801 }
802
803 final AppWindowToken atoken = w.mAppToken;
804 if (atoken != null) {
Adrian Roos23df3a32018-03-15 15:41:13 +0100805 atoken.updateLetterboxSurface(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800806 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
807 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
808 mTmpUpdateAllDrawn.add(atoken);
809 }
810 }
811
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800812 if (!mLosingFocus.isEmpty() && w.isFocused() && w.isDisplayedLw()) {
813 mService.mH.obtainMessage(REPORT_LOSING_FOCUS, this).sendToTarget();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800814 }
815
816 w.updateResizingWindowIfNeeded();
817 };
818
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800819 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800820 * Create new {@link DisplayContent} instance, add itself to the root window container and
821 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700822 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800823 * @param service You know.
wilsonshihc32538e2018-11-07 17:27:34 +0800824 * @param controller The controller for the display container.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700825 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700826 DisplayContent(Display display, WindowManagerService service,
wilsonshihc32538e2018-11-07 17:27:34 +0800827 DisplayWindowController controller) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100828 super(service);
Bryce Leef19cbe22018-02-02 15:09:21 -0800829 setController(controller);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800830 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
831 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
832 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
833 + " new=" + display);
834 }
835
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700836 mDisplay = display;
837 mDisplayId = display.getDisplayId();
wilsonshihc32538e2018-11-07 17:27:34 +0800838 mWallpaperController = new WallpaperController(mService, this);
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700839 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700840 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700841 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100842 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo,
843 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700844 initializeDisplayBaseInfo();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700845
lumark588a3e82018-07-20 18:53:54 +0800846 mAppTransition = new AppTransition(service.mContext, service, this);
847 mAppTransition.registerListenerLocked(service.mActivityManagerAppTransitionNotifier);
848 mAppTransitionController = new AppTransitionController(service, this);
849 mUnknownAppVisibilityController = new UnknownAppVisibilityController(service, this);
850
851 AnimationHandler animationHandler = new AnimationHandler();
852 mBoundsAnimationController = new BoundsAnimationController(service.mContext,
853 mAppTransition, SurfaceAnimationThread.getHandler(), animationHandler);
854
Tiger Huang7c610aa2018-10-27 00:01:01 +0800855 if (mService.mInputManager != null) {
856 final InputChannel inputChannel = mService.mInputManager.monitorInput("Display "
857 + mDisplayId, mDisplayId);
858 mPointerEventDispatcher = inputChannel != null
859 ? new PointerEventDispatcher(inputChannel) : null;
860 } else {
861 mPointerEventDispatcher = null;
862 }
863 mDisplayPolicy = new DisplayPolicy(service, this);
864 mDisplayRotation = new DisplayRotation(service, this);
865 if (isDefaultDisplay) {
866 // The policy may be invoked right after here, so it requires the necessary default
867 // fields of this display content.
868 mService.mPolicy.setDefaultDisplay(this);
869 }
870 if (mService.mDisplayReady) {
871 mDisplayPolicy.onConfigurationChanged();
872 }
873 if (mService.mSystemReady) {
874 mDisplayPolicy.systemReady();
875 }
876 mDividerControllerLocked = new DockedStackDividerController(service, this);
877 mPinnedStackControllerLocked = new PinnedStackController(service, this);
878
Robert Carr74a66a22018-02-23 12:17:51 -0800879 // We use this as our arbitrary surface size for buffer-less parents
880 // that don't impose cropping on their children. It may need to be larger
881 // than the display size because fullscreen windows can be shifted offscreen
882 // due to surfaceInsets. 2 times the largest display dimension feels like an
883 // appropriately arbitrary number. Eventually we would like to give SurfaceFlinger
884 // layers the ability to match their parent sizes and be able to skip
885 // such arbitrary size settings.
886 mSurfaceSize = Math.max(mBaseDisplayHeight, mBaseDisplayWidth) * 2;
Robert Carrb1579c82017-09-05 14:54:47 -0700887
888 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession)
889 .setSize(mSurfaceSize, mSurfaceSize)
890 .setOpaque(true);
891 mWindowingLayer = b.setName("Display Root").build();
892 mOverlayLayer = b.setName("Display Overlays").build();
893
Robert Carrf59b8dd2017-10-02 18:58:36 -0700894 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700895 .setLayerStack(mWindowingLayer, mDisplayId)
896 .show(mWindowingLayer)
897 .setLayer(mOverlayLayer, 1)
898 .setLayerStack(mOverlayLayer, mDisplayId)
899 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700900 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700901
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700902 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700903 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700904 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700905 super.addChild(mAboveAppWindowsContainers, null);
906 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800907
908 // Add itself as a child to the root container.
909 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700910
911 // TODO(b/62541591): evaluate whether this is the best spot to declare the
912 // {@link DisplayContent} ready for use.
913 mDisplayReady = true;
Arthur Hung95b38a92018-07-20 18:56:12 +0800914
Riddle Hsuf53da812018-08-15 22:00:27 +0800915 mService.mAnimator.addDisplayLocked(mDisplayId);
Arthur Hung39134b22018-08-14 11:58:28 +0800916 mInputMonitor = new InputMonitor(service, mDisplayId);
Bryce Leed1871262017-06-12 14:12:29 -0700917 }
918
919 boolean isReady() {
920 // The display is ready when the system and the individual display are both ready.
921 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700922 }
923
924 int getDisplayId() {
925 return mDisplayId;
926 }
927
Wale Ogunwale02319a62016-09-26 15:21:22 -0700928 WindowToken getWindowToken(IBinder binder) {
929 return mTokenMap.get(binder);
930 }
931
932 AppWindowToken getAppWindowToken(IBinder binder) {
933 final WindowToken token = getWindowToken(binder);
934 if (token == null) {
935 return null;
936 }
937 return token.asAppWindowToken();
938 }
939
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700940 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700941 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
942 if (dc != null) {
943 // We currently don't support adding a window token to the display if the display
944 // already has the binder mapped to another token. If there is a use case for supporting
945 // this moving forward we will either need to merge the WindowTokens some how or have
946 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700947 throw new IllegalArgumentException("Can't map token=" + token + " to display="
948 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700949 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700950 if (binder == null) {
951 throw new IllegalArgumentException("Can't map token=" + token + " to display="
952 + getName() + " binder is null");
953 }
954 if (token == null) {
955 throw new IllegalArgumentException("Can't map null token to display="
956 + getName() + " binder=" + binder);
957 }
958
Wale Ogunwale02319a62016-09-26 15:21:22 -0700959 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700960
961 if (token.asAppWindowToken() == null) {
962 // Add non-app token to container hierarchy on the display. App tokens are added through
963 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700964 switch (token.windowType) {
965 case TYPE_WALLPAPER:
966 mBelowAppWindowsContainers.addChild(token);
967 break;
968 case TYPE_INPUT_METHOD:
969 case TYPE_INPUT_METHOD_DIALOG:
970 mImeWindowsContainers.addChild(token);
971 break;
972 default:
973 mAboveAppWindowsContainers.addChild(token);
974 break;
975 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700976 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700977 }
978
979 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700980 final WindowToken token = mTokenMap.remove(binder);
981 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800982 token.setExiting();
983 }
984 return token;
985 }
986
987 /** Changes the display the input window token is housed on to this one. */
988 void reParentWindowToken(WindowToken token) {
989 final DisplayContent prevDc = token.getDisplayContent();
990 if (prevDc == this) {
991 return;
992 }
Riddle Hsu85bd04b2018-11-17 00:34:36 +0800993 if (prevDc != null) {
994 if (prevDc.mTokenMap.remove(token.token) != null && token.asAppWindowToken() == null) {
995 // Removed the token from the map, but made sure it's not an app token before
996 // removing from parent.
997 token.getParent().removeChild(token);
998 }
999 if (prevDc.mLastFocus == mCurrentFocus) {
1000 // The window has become the focus of this display, so it should not be notified
1001 // that it lost focus from the previous display.
1002 prevDc.mLastFocus = null;
1003 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001004 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -08001005
1006 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -07001007 }
1008
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001009 void removeAppToken(IBinder binder) {
1010 final WindowToken token = removeWindowToken(binder);
1011 if (token == null) {
1012 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
1013 return;
1014 }
1015
1016 final AppWindowToken appToken = token.asAppWindowToken();
1017
1018 if (appToken == null) {
1019 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
1020 return;
1021 }
1022
1023 appToken.onRemovedFromDisplay();
1024 }
1025
Riddle Hsuad256a12018-07-18 16:11:30 +08001026 @Override
Riddle Hsu86cb7de2018-08-13 23:29:58 +08001027 DisplayWindowController getController() {
1028 return (DisplayWindowController) super.getController();
1029 }
1030
1031 @Override
Riddle Hsuad256a12018-07-18 16:11:30 +08001032 public Display getDisplay() {
Craig Mautnerb47bbc32012-08-22 17:41:48 -07001033 return mDisplay;
1034 }
1035
Craig Mautner59c00972012-07-30 12:10:24 -07001036 DisplayInfo getDisplayInfo() {
1037 return mDisplayInfo;
1038 }
1039
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001040 DisplayMetrics getDisplayMetrics() {
1041 return mDisplayMetrics;
1042 }
1043
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001044 DisplayPolicy getDisplayPolicy() {
1045 return mDisplayPolicy;
1046 }
1047
Riddle Hsuad256a12018-07-18 16:11:30 +08001048 @Override
1049 public DisplayRotation getDisplayRotation() {
1050 return mDisplayRotation;
1051 }
1052
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001053 @VisibleForTesting
1054 void setDisplayRotation(DisplayRotation displayRotation) {
1055 mDisplayRotation = displayRotation;
1056 }
1057
Andrii Kulian8ee72852017-03-10 10:36:45 -08001058 int getRotation() {
1059 return mRotation;
1060 }
1061
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001062 @VisibleForTesting
Andrii Kulian8ee72852017-03-10 10:36:45 -08001063 void setRotation(int newRotation) {
1064 mRotation = newRotation;
Riddle Hsuad256a12018-07-18 16:11:30 +08001065 mDisplayRotation.setRotation(newRotation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08001066 }
1067
1068 int getLastOrientation() {
1069 return mLastOrientation;
1070 }
1071
Andrii Kulian8ee72852017-03-10 10:36:45 -08001072 boolean getAltOrientation() {
1073 return mAltOrientation;
1074 }
1075
Andrii Kulian8ee72852017-03-10 10:36:45 -08001076 int getLastWindowForcedOrientation() {
1077 return mLastWindowForcedOrientation;
1078 }
1079
Andrii Kulian06d07d62017-03-14 11:11:47 -07001080 /**
Riddle Hsu654a6f92018-07-13 22:59:36 +08001081 * Temporarily pauses rotation changes until resumed.
1082 *
1083 * This can be used to prevent rotation changes from occurring while the user is
1084 * performing certain operations, such as drag and drop.
1085 *
1086 * This call nests and must be matched by an equal number of calls to
1087 * {@link #resumeRotationLocked}.
1088 */
1089 void pauseRotationLocked() {
1090 mDeferredRotationPauseCount++;
1091 }
1092
1093 /**
1094 * Resumes normal rotation changes after being paused.
1095 */
1096 void resumeRotationLocked() {
1097 if (mDeferredRotationPauseCount <= 0) {
1098 return;
1099 }
1100
1101 mDeferredRotationPauseCount--;
1102 if (mDeferredRotationPauseCount == 0) {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001103 updateRotationAndSendNewConfigIfNeeded();
Riddle Hsu654a6f92018-07-13 22:59:36 +08001104 }
1105 }
1106
1107 /**
Riddle Hsuad256a12018-07-18 16:11:30 +08001108 * If this is true we have updated our desired orientation, but not yet changed the real
1109 * orientation our applied our screen rotation animation. For example, because a previous
1110 * screen rotation was in progress.
1111 *
1112 * @return {@code true} if the there is an ongoing rotation change.
1113 */
1114 boolean rotationNeedsUpdate() {
1115 final int lastOrientation = getLastOrientation();
1116 final int oldRotation = getRotation();
1117 final boolean oldAltOrientation = getAltOrientation();
1118
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001119 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Riddle Hsuad256a12018-07-18 16:11:30 +08001120 final boolean altOrientation = !mDisplayRotation.rotationHasCompatibleMetrics(
1121 lastOrientation, rotation);
1122 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1123 return false;
1124 }
1125 return true;
1126 }
1127
Riddle Hsu4e611772018-10-31 18:58:28 +08001128 /** Notify the configuration change of this display. */
1129 void sendNewConfiguration() {
1130 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, this).sendToTarget();
1131 }
1132
1133 /**
1134 * Determine the new desired orientation of this display.
1135 *
1136 * The orientation is computed from non-application windows first. If none of the
1137 * non-application windows specify orientation, the orientation is computed from application
1138 * tokens.
1139 *
1140 * @return {@code true} if the orientation is changed.
1141 */
1142 boolean updateOrientationFromAppTokens() {
1143 return updateOrientationFromAppTokens(false /* forceUpdate */);
1144 }
1145
1146 boolean updateOrientationFromAppTokens(boolean forceUpdate) {
1147 final int req = getOrientation();
1148 if (req != mLastOrientation || forceUpdate) {
1149 mLastOrientation = req;
1150 mDisplayRotation.setCurrentOrientation(req);
1151 return updateRotationUnchecked(forceUpdate);
1152 }
1153 return false;
1154 }
1155
Riddle Hsuad256a12018-07-18 16:11:30 +08001156 /**
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001157 * Update rotation of the display and send configuration if the rotation is changed.
1158 *
1159 * @return {@code true} if the rotation has been changed and the new config is sent.
1160 */
1161 boolean updateRotationAndSendNewConfigIfNeeded() {
1162 final boolean changed = updateRotationUnchecked(false /* forceUpdate */);
1163 if (changed) {
Riddle Hsu4e611772018-10-31 18:58:28 +08001164 sendNewConfiguration();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001165 }
1166 return changed;
1167 }
1168
1169 /**
Andrii Kulian06d07d62017-03-14 11:11:47 -07001170 * Update rotation of the display.
1171 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001172 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
1173 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001174 */
Robert Carrae606b42018-02-15 15:36:23 -08001175 boolean updateRotationUnchecked() {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001176 return updateRotationUnchecked(false /* forceUpdate */);
1177 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001178
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001179 /**
Evan Roskye747c3e2018-10-30 20:06:41 -07001180 * Update rotation of the DisplayContent with an option to force the update. This updates
1181 * the container's perception of rotation and, depending on the top activities, will freeze
1182 * the screen or start seamless rotation. The display itself gets rotated in
1183 * {@link #applyRotationLocked} during {@link WindowManagerService#sendNewConfiguration}.
1184 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001185 * @param forceUpdate Force the rotation update. Sometimes in WM we might skip updating
1186 * orientation because we're waiting for some rotation to finish or display
1187 * to unfreeze, which results in configuration of the previously visible
1188 * activity being applied to a newly visible one. Forcing the rotation
1189 * update allows to workaround this issue.
1190 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
Evan Roskye747c3e2018-10-30 20:06:41 -07001191 * {@link WindowManagerService#sendNewConfiguration(int)} TO COMPLETE THE ROTATION AND
1192 * UNFREEZE THE SCREEN.
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001193 */
1194 boolean updateRotationUnchecked(boolean forceUpdate) {
1195 ScreenRotationAnimation screenRotationAnimation;
1196 if (!forceUpdate) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001197 if (mDeferredRotationPauseCount > 0) {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001198 // Rotation updates have been paused temporarily. Defer the update until
1199 // updates have been resumed.
1200 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
1201 return false;
1202 }
1203
1204 screenRotationAnimation =
1205 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
1206 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
1207 // Rotation updates cannot be performed while the previous rotation change
1208 // animation is still in progress. Skip this update. We will try updating
1209 // again after the animation is finished and the display is unfrozen.
1210 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
1211 return false;
1212 }
1213 if (mService.mDisplayFrozen) {
1214 // Even if the screen rotation animation has finished (e.g. isAnimating
1215 // returns false), there is still some time where we haven't yet unfrozen
1216 // the display. We also need to abort rotation here.
1217 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
1218 "Deferring rotation, still finishing previous rotation");
1219 return false;
1220 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001221 }
1222
1223 if (!mService.mDisplayEnabled) {
1224 // No point choosing a rotation if the display is not enabled.
1225 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
1226 return false;
1227 }
1228
1229 final int oldRotation = mRotation;
1230 final int lastOrientation = mLastOrientation;
1231 final boolean oldAltOrientation = mAltOrientation;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001232 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001233 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Computed rotation=" + rotation + " for display id="
1234 + mDisplayId + " based on lastOrientation=" + lastOrientation
1235 + " and oldRotation=" + oldRotation);
Tiger Huang7c610aa2018-10-27 00:01:01 +08001236 boolean mayRotateSeamlessly = mDisplayPolicy.shouldRotateSeamlessly(mDisplayRotation,
Riddle Hsuad256a12018-07-18 16:11:30 +08001237 oldRotation, rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001238
Robert Carr0e007272017-10-02 13:00:55 -07001239 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001240 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001241 if (seamlessRotated != null && !forceUpdate) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001242 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
1243 // to complete (that is, waiting for windows to redraw). It's tempting to check
1244 // w.mSeamlessRotationCount but that could be incorrect in the case of
1245 // window-removal.
1246 return false;
1247 }
Robert Carr0e007272017-10-02 13:00:55 -07001248
1249 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001250 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -07001251 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -07001252 mayRotateSeamlessly = false;
1253 }
1254 for (int i = 0; i < mService.mSessions.size(); i++) {
1255 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
1256 mayRotateSeamlessly = false;
1257 break;
1258 }
1259 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001260 }
Robert Carr0e007272017-10-02 13:00:55 -07001261 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001262
1263 // TODO: Implement forced rotation changes.
1264 // Set mAltOrientation to indicate that the application is receiving
1265 // an orientation that has different metrics than it expected.
1266 // eg. Portrait instead of Landscape.
1267
Riddle Hsuad256a12018-07-18 16:11:30 +08001268 final boolean altOrientation = !mDisplayRotation.rotationHasCompatibleMetrics(
Andrii Kulian06d07d62017-03-14 11:11:47 -07001269 lastOrientation, rotation);
1270
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001271 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1272 + " selected orientation " + lastOrientation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001273 + ", got rotation " + rotation + " which has "
1274 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1275
1276 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1277 // No change.
1278 return false;
1279 }
1280
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001281 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1282 + " rotation changed to " + rotation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001283 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1284 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1285
1286 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1287 mService.mWaitingForConfig = true;
1288 }
1289
Evan Roskye747c3e2018-10-30 20:06:41 -07001290 mRotation = rotation;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001291 mAltOrientation = altOrientation;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001292
1293 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
Riddle Hsu654a6f92018-07-13 22:59:36 +08001294 mService.mH.sendNewMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1295 this, WINDOW_FREEZE_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001296
1297 setLayoutNeeded();
1298 final int[] anim = new int[2];
Tiger Huang7c610aa2018-10-27 00:01:01 +08001299 mDisplayPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001300
1301 if (!rotateSeamlessly) {
Robert Carrae606b42018-02-15 15:36:23 -08001302 mService.startFreezingDisplayLocked(anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001303 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001304 } else {
1305 // The screen rotation animation uses a screenshot to freeze the screen
1306 // while windows resize underneath.
1307 // When we are rotating seamlessly, we allow the elements to transition
1308 // to their rotated state independently and without a freeze required.
Robert Carrefc75702018-02-16 11:46:16 -08001309 mService.startSeamlessRotation();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001310 }
1311
Evan Roskye747c3e2018-10-30 20:06:41 -07001312 return true;
1313 }
1314
1315 /**
1316 * Applies the rotation transaction. This must be called after {@link #updateRotationUnchecked}
1317 * (if it returned {@code true}) to actually finish the rotation.
1318 *
1319 * @param oldRotation the rotation we are coming from.
1320 * @param rotation the rotation to apply.
1321 */
1322 void applyRotationLocked(final int oldRotation, final int rotation) {
1323 mDisplayRotation.setRotation(rotation);
1324 final boolean rotateSeamlessly = mService.isRotatingSeamlessly();
1325 ScreenRotationAnimation screenRotationAnimation = rotateSeamlessly
1326 ? null : mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001327 // We need to update our screen size information to match the new rotation. If the rotation
1328 // has actually changed then this method will return true and, according to the comment at
1329 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1330 // By updating the Display info here it will be available to
1331 // #computeScreenConfiguration() later.
1332 updateDisplayAndOrientation(getConfiguration().uiMode);
1333
Robert Carrae606b42018-02-15 15:36:23 -08001334 // NOTE: We disable the rotation in the emulator because
1335 // it doesn't support hardware OpenGL emulation yet.
1336 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1337 && screenRotationAnimation.hasScreenshot()) {
1338 if (screenRotationAnimation.setRotation(getPendingTransaction(), rotation,
1339 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1340 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1341 mService.scheduleAnimationLocked();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001342 }
1343 }
1344
Vishnu Nair83537a72018-07-19 21:27:48 -07001345 forAllWindows(w -> {
Vishnu Nairddd80742018-08-21 14:12:46 -07001346 w.seamlesslyRotateIfAllowed(getPendingTransaction(), oldRotation, rotation,
1347 rotateSeamlessly);
Vishnu Nair83537a72018-07-19 21:27:48 -07001348 }, true /* traverseTopToBottom */);
1349
Robert Carrae606b42018-02-15 15:36:23 -08001350 mService.mDisplayManagerInternal.performTraversal(getPendingTransaction());
1351 scheduleAnimation();
1352
Andrii Kulian06d07d62017-03-14 11:11:47 -07001353 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001354 if (w.mHasSurface && !rotateSeamlessly) {
1355 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001356 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001357 mService.mRoot.mOrientationChangeComplete = false;
1358 w.mLastFreezeDuration = 0;
1359 }
1360 w.mReportOrientationChanged = true;
1361 }, true /* traverseTopToBottom */);
1362
1363 if (rotateSeamlessly) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001364 mService.mH.sendNewMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1365 this, SEAMLESS_ROTATION_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001366 }
1367
1368 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1369 final WindowManagerService.RotationWatcher rotationWatcher
1370 = mService.mRotationWatchers.get(i);
1371 if (rotationWatcher.mDisplayId == mDisplayId) {
1372 try {
1373 rotationWatcher.mWatcher.onRotationChanged(rotation);
1374 } catch (RemoteException e) {
1375 // Ignore
1376 }
1377 }
1378 }
1379
1380 // TODO (multi-display): Magnification is supported only for the default display.
1381 // Announce rotation only if we will not animate as we already have the
1382 // windows in final state. Otherwise, we make this call at the rotation end.
1383 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1384 && isDefaultDisplay) {
1385 mService.mAccessibilityController.onRotationChangedLocked(this);
1386 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001387 }
1388
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001389 void configureDisplayPolicy() {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001390 final int width = mBaseDisplayWidth;
1391 final int height = mBaseDisplayHeight;
1392 final int shortSize;
1393 final int longSize;
1394 if (width > height) {
1395 shortSize = height;
1396 longSize = width;
1397 } else {
1398 shortSize = width;
1399 longSize = height;
1400 }
1401
1402 final int shortSizeDp = shortSize * DisplayMetrics.DENSITY_DEFAULT / mBaseDisplayDensity;
1403 final int longSizeDp = longSize * DisplayMetrics.DENSITY_DEFAULT / mBaseDisplayDensity;
1404
1405 mDisplayRotation.configure(width, height, shortSizeDp, longSizeDp);
1406 mDisplayPolicy.configure(width, height, shortSizeDp);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001407
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001408 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
1409 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Arthur Hungbe5ce212018-09-13 18:41:56 +08001410
1411 // Tap Listeners are supported for:
1412 // 1. All physical displays (multi-display).
1413 // 2. VirtualDisplays on VR, AA (and everything else).
1414 if (mPointerEventDispatcher != null && mTapDetector == null) {
1415 if (DEBUG_DISPLAY) {
1416 Slog.d(TAG,
1417 "Registering PointerEventListener for DisplayId: " + mDisplayId);
1418 }
1419 mTapDetector = new TaskTapPointerEventListener(mService, this);
1420 registerPointerEventListener(mTapDetector);
1421 registerPointerEventListener(mService.mMousePositionTracker);
1422 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001423 }
1424
Andrii Kulian06d07d62017-03-14 11:11:47 -07001425 /**
1426 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1427 * changed.
1428 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1429 */
1430 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1431 // Use the effective "visual" dimensions based on current rotation
1432 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1433 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1434 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1435 int dw = realdw;
1436 int dh = realdh;
1437
1438 if (mAltOrientation) {
1439 if (realdw > realdh) {
1440 // Turn landscape into portrait.
1441 int maxw = (int)(realdh/1.3f);
1442 if (maxw < realdw) {
1443 dw = maxw;
1444 }
1445 } else {
1446 // Turn portrait into landscape.
1447 int maxh = (int)(realdw/1.3f);
1448 if (maxh < realdh) {
1449 dh = maxh;
1450 }
1451 }
1452 }
1453
1454 // Update application display metrics.
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001455 final WmDisplayCutout wmDisplayCutout = calculateDisplayCutoutForRotation(mRotation);
1456 final DisplayCutout displayCutout = wmDisplayCutout.getDisplayCutout();
1457
Tiger Huang7c610aa2018-10-27 00:01:01 +08001458 final int appWidth = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1459 displayCutout);
1460 final int appHeight = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1461 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001462 mDisplayInfo.rotation = mRotation;
1463 mDisplayInfo.logicalWidth = dw;
1464 mDisplayInfo.logicalHeight = dh;
1465 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1466 mDisplayInfo.appWidth = appWidth;
1467 mDisplayInfo.appHeight = appHeight;
1468 if (isDefaultDisplay) {
1469 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1470 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1471 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001472 mDisplayInfo.displayCutout = displayCutout.isEmpty() ? null : displayCutout;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001473 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1474 if (mDisplayScalingDisabled) {
1475 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1476 } else {
1477 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1478 }
1479
Andrii Kulianf0379de2018-03-14 16:24:07 -07001480 // We usually set the override info in DisplayManager so that we get consistent display
1481 // metrics values when displays are changing and don't send out new values until WM is aware
1482 // of them. However, we don't do this for displays that serve as containers for ActivityView
1483 // because we don't want letter-/pillar-boxing during resize.
1484 final DisplayInfo overrideDisplayInfo = mShouldOverrideDisplayConfiguration
1485 ? mDisplayInfo : null;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001486 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
Andrii Kulianf0379de2018-03-14 16:24:07 -07001487 overrideDisplayInfo);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001488
1489 mBaseDisplayRect.set(0, 0, dw, dh);
1490
1491 if (isDefaultDisplay) {
1492 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1493 mCompatDisplayMetrics);
1494 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001495
Andrii Kulian06d07d62017-03-14 11:11:47 -07001496 return mDisplayInfo;
1497 }
1498
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001499 WmDisplayCutout calculateDisplayCutoutForRotation(int rotation) {
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001500 return mDisplayCutoutCache.getOrCompute(mInitialDisplayCutout, rotation);
1501 }
1502
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001503 private WmDisplayCutout calculateDisplayCutoutForRotationUncached(
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001504 DisplayCutout cutout, int rotation) {
Adrian Roos24264212018-02-19 16:26:15 +01001505 if (cutout == null || cutout == DisplayCutout.NO_CUTOUT) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001506 return WmDisplayCutout.NO_CUTOUT;
Adrian Roos1cf585052018-01-03 18:43:27 +01001507 }
Adrian Roos11c25582018-02-19 18:06:36 +01001508 if (rotation == ROTATION_0) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001509 return WmDisplayCutout.computeSafeInsets(
1510 cutout, mInitialDisplayWidth, mInitialDisplayHeight);
Adrian Roos24264212018-02-19 16:26:15 +01001511 }
Adrian Roosbed538e2018-02-21 17:50:07 +01001512 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Issei Suzuki43190bd2018-08-20 17:28:41 +02001513 final Rect[] newBounds = mRotationUtil.getRotatedBounds(
1514 WmDisplayCutout.computeSafeInsets(
Jorim Jaggi60640512018-06-29 01:14:31 +02001515 cutout, mInitialDisplayWidth, mInitialDisplayHeight)
Issei Suzuki43190bd2018-08-20 17:28:41 +02001516 .getDisplayCutout().getBoundingRectsAll(),
1517 rotation, mInitialDisplayWidth, mInitialDisplayHeight);
1518 return WmDisplayCutout.computeSafeInsets(DisplayCutout.fromBounds(newBounds),
Adrian Roos24264212018-02-19 16:26:15 +01001519 rotated ? mInitialDisplayHeight : mInitialDisplayWidth,
1520 rotated ? mInitialDisplayWidth : mInitialDisplayHeight);
Adrian Roos1cf585052018-01-03 18:43:27 +01001521 }
1522
Andrii Kulian06d07d62017-03-14 11:11:47 -07001523 /**
1524 * Compute display configuration based on display properties and policy settings.
1525 * Do not call if mDisplayReady == false.
1526 */
1527 void computeScreenConfiguration(Configuration config) {
1528 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
Evan Roskye747c3e2018-10-30 20:06:41 -07001529 calculateBounds(displayInfo, mTmpBounds);
1530 config.windowConfiguration.setBounds(mTmpBounds);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001531
1532 final int dw = displayInfo.logicalWidth;
1533 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001534 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
Garfield Tane0846042018-07-26 13:42:04 -07001535 config.windowConfiguration.setWindowingMode(getWindowingMode());
Evan Roskye747c3e2018-10-30 20:06:41 -07001536 config.windowConfiguration.setRotation(displayInfo.rotation);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001537
Adrian Roos11c25582018-02-19 18:06:36 +01001538 final float density = mDisplayMetrics.density;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001539 config.screenWidthDp =
Tiger Huang7c610aa2018-10-27 00:01:01 +08001540 (int)(mDisplayPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1541 config.uiMode, displayInfo.displayCutout) / density);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001542 config.screenHeightDp =
Tiger Huang7c610aa2018-10-27 00:01:01 +08001543 (int)(mDisplayPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1544 config.uiMode, displayInfo.displayCutout) / density);
Bryce Lee7566d762017-03-30 09:34:15 -07001545
Tiger Huang7c610aa2018-10-27 00:01:01 +08001546 mDisplayPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh,
Adrian Roos11c25582018-02-19 18:06:36 +01001547 displayInfo.displayCutout, mTmpRect);
Bryce Lee7566d762017-03-30 09:34:15 -07001548 final int leftInset = mTmpRect.left;
1549 final int topInset = mTmpRect.top;
1550 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001551 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1552 leftInset + displayInfo.appWidth /* right */,
1553 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001554 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1555 || displayInfo.rotation == Surface.ROTATION_270);
1556
Tiger Huang7c610aa2018-10-27 00:01:01 +08001557 computeSizeRangesAndScreenLayout(displayInfo, rotated, config.uiMode, dw, dh, density,
1558 config);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001559
1560 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1561 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1562 ? Configuration.SCREENLAYOUT_ROUND_YES
1563 : Configuration.SCREENLAYOUT_ROUND_NO);
1564
1565 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1566 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1567 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001568 dh, displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001569 config.densityDpi = displayInfo.logicalDensityDpi;
1570
1571 config.colorMode =
Chia-I Wuf739bad2018-05-09 12:36:57 -07001572 ((displayInfo.isHdr() && mService.hasHdrSupport())
Andrii Kulian06d07d62017-03-14 11:11:47 -07001573 ? Configuration.COLOR_MODE_HDR_YES
1574 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001575 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001576 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1577 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1578
1579 // Update the configuration based on available input devices, lid switch,
1580 // and platform configuration.
1581 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1582 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1583 config.navigation = Configuration.NAVIGATION_NONAV;
1584
1585 int keyboardPresence = 0;
1586 int navigationPresence = 0;
1587 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1588 final int len = devices != null ? devices.length : 0;
1589 for (int i = 0; i < len; i++) {
1590 InputDevice device = devices[i];
1591 if (!device.isVirtual()) {
1592 final int sources = device.getSources();
1593 final int presenceFlag = device.isExternal() ?
1594 WindowManagerPolicy.PRESENCE_EXTERNAL :
1595 WindowManagerPolicy.PRESENCE_INTERNAL;
1596
1597 // TODO(multi-display): Configure on per-display basis.
1598 if (mService.mIsTouchDevice) {
1599 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1600 InputDevice.SOURCE_TOUCHSCREEN) {
1601 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1602 }
1603 } else {
1604 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1605 }
1606
1607 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1608 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1609 navigationPresence |= presenceFlag;
1610 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1611 && config.navigation == Configuration.NAVIGATION_NONAV) {
1612 config.navigation = Configuration.NAVIGATION_DPAD;
1613 navigationPresence |= presenceFlag;
1614 }
1615
1616 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1617 config.keyboard = Configuration.KEYBOARD_QWERTY;
1618 keyboardPresence |= presenceFlag;
1619 }
1620 }
1621 }
1622
1623 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1624 config.navigation = Configuration.NAVIGATION_DPAD;
1625 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1626 }
1627
1628 // Determine whether a hard keyboard is available and enabled.
1629 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1630 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1631 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1632 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1633 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1634 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1635 }
1636
Tiger Huang7c610aa2018-10-27 00:01:01 +08001637 mDisplayPolicy.updateConfigurationDependentBehaviors();
1638
Andrii Kulian06d07d62017-03-14 11:11:47 -07001639 // Let the policy update hidden states.
1640 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1641 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1642 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1643 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1644 }
1645
1646 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001647 DisplayCutout displayCutout) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001648 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1649 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1650 final int unrotDw, unrotDh;
1651 if (rotated) {
1652 unrotDw = dh;
1653 unrotDh = dw;
1654 } else {
1655 unrotDw = dw;
1656 unrotDh = dh;
1657 }
1658 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001659 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001660 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001661 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001662 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001663 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001664 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001665 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001666 return sw;
1667 }
1668
1669 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001670 DisplayMetrics dm, int dw, int dh, DisplayCutout displayCutout) {
1671 dm.noncompatWidthPixels = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1672 displayCutout);
1673 dm.noncompatHeightPixels = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode,
1674 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001675 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1676 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1677 if (curSize == 0 || size < curSize) {
1678 curSize = size;
1679 }
1680 return curSize;
1681 }
1682
Tiger Huang7c610aa2018-10-27 00:01:01 +08001683 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, boolean rotated,
1684 int uiMode, int dw, int dh, float density, Configuration outConfig) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001685
1686 // We need to determine the smallest width that will occur under normal
1687 // operation. To this, start with the base screen size and compute the
1688 // width under the different possible rotations. We need to un-rotate
1689 // the current screen dimensions before doing this.
1690 int unrotDw, unrotDh;
1691 if (rotated) {
1692 unrotDw = dh;
1693 unrotDh = dw;
1694 } else {
1695 unrotDw = dw;
1696 unrotDh = dh;
1697 }
1698 displayInfo.smallestNominalAppWidth = 1<<30;
1699 displayInfo.smallestNominalAppHeight = 1<<30;
1700 displayInfo.largestNominalAppWidth = 0;
1701 displayInfo.largestNominalAppHeight = 0;
Tiger Huang7c610aa2018-10-27 00:01:01 +08001702 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_0, uiMode, unrotDw, unrotDh);
1703 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_90, uiMode, unrotDh, unrotDw);
1704 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_180, uiMode, unrotDw, unrotDh);
1705 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_270, uiMode, unrotDh, unrotDw);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001706 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1707 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001708 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001709 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001710 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001711 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001712 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001713 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001714 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001715 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1716 outConfig.screenLayout = sl;
1717 }
1718
1719 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001720 int uiMode, DisplayCutout displayCutout) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001721 // Get the app screen size at this rotation.
Tiger Huang7c610aa2018-10-27 00:01:01 +08001722 int w = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayCutout);
1723 int h = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001724
1725 // Compute the screen layout size class for this rotation.
1726 int longSize = w;
1727 int shortSize = h;
1728 if (longSize < shortSize) {
1729 int tmp = longSize;
1730 longSize = shortSize;
1731 shortSize = tmp;
1732 }
1733 longSize = (int)(longSize/density);
1734 shortSize = (int)(shortSize/density);
1735 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1736 }
1737
Tiger Huang7c610aa2018-10-27 00:01:01 +08001738 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int rotation,
Andrii Kulian06d07d62017-03-14 11:11:47 -07001739 int uiMode, int dw, int dh) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001740 final DisplayCutout displayCutout = calculateDisplayCutoutForRotation(
1741 rotation).getDisplayCutout();
Tiger Huang7c610aa2018-10-27 00:01:01 +08001742 final int width = mDisplayPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1743 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001744 if (width < displayInfo.smallestNominalAppWidth) {
1745 displayInfo.smallestNominalAppWidth = width;
1746 }
1747 if (width > displayInfo.largestNominalAppWidth) {
1748 displayInfo.largestNominalAppWidth = width;
1749 }
Tiger Huang7c610aa2018-10-27 00:01:01 +08001750 final int height = mDisplayPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1751 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001752 if (height < displayInfo.smallestNominalAppHeight) {
1753 displayInfo.smallestNominalAppHeight = height;
1754 }
1755 if (height > displayInfo.largestNominalAppHeight) {
1756 displayInfo.largestNominalAppHeight = height;
1757 }
1758 }
1759
Riddle Hsua4d6fa22018-08-11 00:50:39 +08001760 /**
1761 * Apps that use the compact menu panel (as controlled by the panelMenuIsCompact
1762 * theme attribute) on devices that feature a physical options menu key attempt to position
1763 * their menu panel window along the edge of the screen nearest the physical menu key.
1764 * This lowers the travel distance between invoking the menu panel and selecting
1765 * a menu option.
1766 *
1767 * This method helps control where that menu is placed. Its current implementation makes
1768 * assumptions about the menu key and its relationship to the screen based on whether
1769 * the device's natural orientation is portrait (width < height) or landscape.
1770 *
1771 * The menu key is assumed to be located along the bottom edge of natural-portrait
1772 * devices and along the right edge of natural-landscape devices. If these assumptions
1773 * do not hold for the target device, this method should be changed to reflect that.
1774 *
1775 * @return A {@link Gravity} value for placing the options menu window.
1776 */
1777 int getPreferredOptionsPanelGravity() {
1778 final int rotation = getRotation();
1779 if (mInitialDisplayWidth < mInitialDisplayHeight) {
1780 // On devices with a natural orientation of portrait.
1781 switch (rotation) {
1782 default:
1783 case Surface.ROTATION_0:
1784 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1785 case Surface.ROTATION_90:
1786 return Gravity.RIGHT | Gravity.BOTTOM;
1787 case Surface.ROTATION_180:
1788 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1789 case Surface.ROTATION_270:
1790 return Gravity.START | Gravity.BOTTOM;
1791 }
1792 }
1793
1794 // On devices with a natural orientation of landscape.
1795 switch (rotation) {
1796 default:
1797 case Surface.ROTATION_0:
1798 return Gravity.RIGHT | Gravity.BOTTOM;
1799 case Surface.ROTATION_90:
1800 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1801 case Surface.ROTATION_180:
1802 return Gravity.START | Gravity.BOTTOM;
1803 case Surface.ROTATION_270:
1804 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1805 }
1806 }
1807
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001808 DockedStackDividerController getDockedDividerController() {
1809 return mDividerControllerLocked;
1810 }
1811
Winson Chung655332c2016-10-31 13:14:28 -07001812 PinnedStackController getPinnedStackController() {
1813 return mPinnedStackControllerLocked;
1814 }
1815
Jeff Browna506a6e2013-06-04 00:02:38 -07001816 /**
1817 * Returns true if the specified UID has access to this display.
1818 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001819 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001820 return mDisplay.hasAccess(uid);
1821 }
1822
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001823 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001824 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001825 }
1826
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001827 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001828 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001829 }
1830
Wale Ogunwale61911492017-10-11 08:50:50 -07001831 /**
1832 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1833 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001834 TaskStack getSplitScreenPrimaryStack() {
1835 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001836 return (stack != null && stack.isVisible()) ? stack : null;
1837 }
1838
Robert Carr9785cf32018-04-25 15:06:07 -07001839 boolean hasSplitScreenPrimaryStack() {
1840 return getSplitScreenPrimaryStack() != null;
1841 }
1842
Wale Ogunwale61911492017-10-11 08:50:50 -07001843 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001844 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001845 * not visible.
1846 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001847 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1848 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001849 }
1850
1851 TaskStack getPinnedStack() {
1852 return mTaskStackContainers.getPinnedStack();
1853 }
1854
1855 private boolean hasPinnedStack() {
1856 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001857 }
1858
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001859 /**
1860 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1861 * Null is no compatible stack on the display.
1862 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001863 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001864 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1865 }
1866
1867 /**
1868 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1869 * activity type. Null is no compatible stack on the display.
1870 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001871 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001872 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001873 }
1874
Bryce Lee48f4b572017-04-10 10:54:15 -07001875 @VisibleForTesting
Kazuki Takise148d00a2018-05-31 15:32:19 +09001876 WindowList<TaskStack> getStacks() {
1877 return mTaskStackContainers.mChildren;
1878 }
1879
1880 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001881 TaskStack getTopStack() {
1882 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001883 }
1884
Winson Chunge2d72172018-01-25 17:46:20 +00001885 ArrayList<Task> getVisibleTasks() {
1886 return mTaskStackContainers.getVisibleTasks();
1887 }
1888
Wale Ogunwale61911492017-10-11 08:50:50 -07001889 void onStackWindowingModeChanged(TaskStack stack) {
1890 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001891 }
1892
Andrii Kulian441e4492016-09-29 15:25:00 -07001893 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001894 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001895 super.onConfigurationChanged(newParentConfig);
Tiger Huang7c610aa2018-10-27 00:01:01 +08001896 if (mDisplayPolicy != null) {
1897 mDisplayPolicy.onConfigurationChanged();
1898 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001899
Evan Roskye747c3e2018-10-30 20:06:41 -07001900 // If there was no pinned stack, we still need to notify the controller of the display info
1901 // update as a result of the config change.
1902 if (mPinnedStackControllerLocked != null && !hasPinnedStack()) {
1903 mPinnedStackControllerLocked.onDisplayInfoChanged(getDisplayInfo());
1904 }
Evan Roskye747c3e2018-10-30 20:06:41 -07001905 }
1906
1907 /**
1908 * Updates the resources used by docked/pinned controllers. This needs to be called at the
1909 * beginning of a configuration update cascade since the metrics from these resources are used
1910 * for bounds calculations. Since ActivityDisplay initiates the configuration update, this
1911 * should be called from there instead of DisplayContent's onConfigurationChanged.
1912 */
1913 void preOnConfigurationChanged() {
Bryce Leef3c6a472017-11-14 14:53:06 -08001914 final DockedStackDividerController dividerController = getDockedDividerController();
1915
1916 if (dividerController != null) {
1917 getDockedDividerController().onConfigurationChanged();
1918 }
1919
1920 final PinnedStackController pinnedStackController = getPinnedStackController();
1921
1922 if (pinnedStackController != null) {
1923 getPinnedStackController().onConfigurationChanged();
1924 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001925 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001926
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001927 @Override
1928 boolean fillsParent() {
1929 return true;
1930 }
1931
1932 @Override
1933 boolean isVisible() {
1934 return true;
1935 }
1936
1937 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001938 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001939 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001940 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001941 }
1942
Robert Carr9785cf32018-04-25 15:06:07 -07001943 /**
1944 * In split-screen mode we process the IME containers above the docked divider
1945 * rather than directly above their target.
1946 */
1947 private boolean skipTraverseChild(WindowContainer child) {
lumarkff0ab692018-11-05 20:32:30 +08001948 if (child == mImeWindowsContainers && mInputMethodTarget != null
Robert Carr9785cf32018-04-25 15:06:07 -07001949 && !hasSplitScreenPrimaryStack()) {
1950 return true;
1951 }
1952 return false;
1953 }
1954
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001955 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001956 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1957 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1958 // target, or here in order if there isn't an IME target.
1959 if (traverseTopToBottom) {
1960 for (int i = mChildren.size() - 1; i >= 0; --i) {
1961 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001962 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001963 continue;
1964 }
Robert Carr9785cf32018-04-25 15:06:07 -07001965
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001966 if (child.forAllWindows(callback, traverseTopToBottom)) {
1967 return true;
1968 }
1969 }
1970 } else {
1971 final int count = mChildren.size();
1972 for (int i = 0; i < count; i++) {
1973 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001974 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001975 continue;
1976 }
Robert Carr9785cf32018-04-25 15:06:07 -07001977
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001978 if (child.forAllWindows(callback, traverseTopToBottom)) {
1979 return true;
1980 }
1981 }
1982 }
1983 return false;
1984 }
1985
1986 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1987 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1988 }
1989
Wale Ogunwale399c8692017-05-08 14:22:42 -07001990 @Override
1991 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001992 final WindowManagerPolicy policy = mService.mPolicy;
1993
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001994 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001995 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001996 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1997 + " is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001998 // If the display is frozen, some activities may be in the middle of restarting, and
1999 // thus have removed their old window. If the window has the flag to hide the lock
2000 // screen, then the lock screen can re-appear and inflict its own orientation on us.
2001 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08002002 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002003 } else if (policy.isKeyguardLocked()) {
2004 // Use the last orientation the while the display is frozen with the keyguard
2005 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
2006 // window. We don't want to check the show when locked window directly though as
2007 // things aren't stable while the display is frozen, for example the window could be
2008 // momentarily unavailable due to activity relaunch.
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07002009 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
2010 + " is frozen while keyguard locked, return " + mLastOrientation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08002011 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07002012 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002013 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002014 final int orientation = mAboveAppWindowsContainers.getOrientation();
2015 if (orientation != SCREEN_ORIENTATION_UNSET) {
2016 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002017 }
Wale Ogunwale51362492016-09-08 17:49:17 -07002018 }
2019
Wale Ogunwale399c8692017-05-08 14:22:42 -07002020 // Top system windows are not requesting an orientation. Start searching from apps.
2021 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07002022 }
2023
Craig Mautner46ac6fa2013-08-01 10:06:34 -07002024 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07002025 // Check if display metrics changed and update base values if needed.
2026 updateBaseDisplayMetricsIfNeeded();
2027
Craig Mautner722285e2012-09-07 13:55:58 -07002028 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07002029 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07002030
Garfield Tan2f145f22018-11-01 15:27:03 -07002031 onDisplayChanged(this);
Craig Mautner722285e2012-09-07 13:55:58 -07002032 }
2033
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002034 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002035 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
2036 if (displayManagerInternal != null) {
2037 // Bootstrap the default logical display from the display manager.
2038 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
2039 if (newDisplayInfo != null) {
2040 mDisplayInfo.copyFrom(newDisplayInfo);
2041 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002042 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002043
Andrii Kuliancd097992017-03-23 18:31:59 -07002044 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
2045 mDisplayInfo.logicalDensityDpi);
2046 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
2047 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
2048 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002049 mInitialDisplayCutout = mDisplayInfo.displayCutout;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002050 }
2051
Andrii Kuliancd097992017-03-23 18:31:59 -07002052 /**
2053 * If display metrics changed, overrides are not set and it's not just a rotation - update base
2054 * values.
2055 */
2056 private void updateBaseDisplayMetricsIfNeeded() {
2057 // Get real display metrics without overrides from WM.
2058 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
2059 final int orientation = mDisplayInfo.rotation;
2060 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2061 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
2062 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
2063 final int newDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002064 final DisplayCutout newCutout = mDisplayInfo.displayCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07002065
2066 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
2067 || mInitialDisplayHeight != newHeight
Adrian Roos1cf585052018-01-03 18:43:27 +01002068 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi
2069 || !Objects.equals(mInitialDisplayCutout, newCutout);
Andrii Kuliancd097992017-03-23 18:31:59 -07002070
2071 if (displayMetricsChanged) {
2072 // Check if display size or density is forced.
2073 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
2074 || mBaseDisplayHeight != mInitialDisplayHeight;
2075 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
2076
2077 // If there is an override set for base values - use it, otherwise use new values.
2078 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
2079 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
2080 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
2081
2082 // Real display metrics changed, so we should also update initial values.
2083 mInitialDisplayWidth = newWidth;
2084 mInitialDisplayHeight = newHeight;
2085 mInitialDisplayDensity = newDensity;
Adrian Roos1cf585052018-01-03 18:43:27 +01002086 mInitialDisplayCutout = newCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07002087 mService.reconfigureDisplayLocked(this);
2088 }
2089 }
2090
Bryce Lee27cec322017-03-21 09:41:37 -07002091 /** Sets the maximum width the screen resolution can be */
2092 void setMaxUiWidth(int width) {
2093 if (DEBUG_DISPLAY) {
2094 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
2095 }
2096
2097 mMaxUiWidth = width;
2098
2099 // Update existing metrics.
2100 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
2101 }
2102
2103 /** Update base (override) display metrics. */
2104 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
2105 mBaseDisplayWidth = baseWidth;
2106 mBaseDisplayHeight = baseHeight;
2107 mBaseDisplayDensity = baseDensity;
2108
2109 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
2110 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
2111 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
2112 mBaseDisplayWidth = mMaxUiWidth;
2113
2114 if (DEBUG_DISPLAY) {
2115 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
2116 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
2117 + " on display:" + getDisplayId());
2118 }
2119 }
2120
2121 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08002122
2123 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07002124 }
2125
Riddle Hsuf53da812018-08-15 22:00:27 +08002126 /**
2127 * Forces this display to use the specified density.
2128 *
2129 * @param density The density in DPI to use. If the value equals to initial density, the setting
2130 * will be cleared.
2131 * @param userId The target user to apply. Only meaningful when this is default display. If the
2132 * user id is {@link UserHandle#USER_CURRENT}, it means to apply current settings
2133 * so only need to configure display.
2134 */
2135 void setForcedDensity(int density, int userId) {
2136 final boolean clear = density == mInitialDisplayDensity;
2137 final boolean updateCurrent = userId == UserHandle.USER_CURRENT;
2138 if (mService.mCurrentUserId == userId || updateCurrent) {
2139 mBaseDisplayDensity = density;
2140 mService.reconfigureDisplayLocked(this);
2141 }
2142 if (updateCurrent) {
2143 // We are applying existing settings so no need to save it again.
2144 return;
2145 }
2146
2147 if (density == mInitialDisplayDensity) {
2148 density = 0;
2149 }
Chilun8753ad32018-10-09 15:56:45 +08002150 mService.mDisplayWindowSettings.setForcedDensity(this, density, userId);
Riddle Hsuf53da812018-08-15 22:00:27 +08002151 }
2152
2153 /** @param mode {@link #FORCE_SCALING_MODE_AUTO} or {@link #FORCE_SCALING_MODE_DISABLED}. */
2154 void setForcedScalingMode(@ForceScalingMode int mode) {
2155 if (mode != FORCE_SCALING_MODE_DISABLED) {
2156 mode = FORCE_SCALING_MODE_AUTO;
2157 }
2158
2159 mDisplayScalingDisabled = (mode != FORCE_SCALING_MODE_AUTO);
2160 Slog.i(TAG_WM, "Using display scaling mode: " + (mDisplayScalingDisabled ? "off" : "auto"));
2161 mService.reconfigureDisplayLocked(this);
2162
Chilun8753ad32018-10-09 15:56:45 +08002163 mService.mDisplayWindowSettings.setForcedScalingMode(this, mode);
Riddle Hsuf53da812018-08-15 22:00:27 +08002164 }
2165
2166 /** If the given width and height equal to initial size, the setting will be cleared. */
2167 void setForcedSize(int width, int height) {
2168 final boolean clear = mInitialDisplayWidth == width && mInitialDisplayHeight == height;
2169 if (!clear) {
2170 // Set some sort of reasonable bounds on the size of the display that we will try
2171 // to emulate.
2172 final int minSize = 200;
2173 final int maxScale = 2;
2174 width = Math.min(Math.max(width, minSize), mInitialDisplayWidth * maxScale);
2175 height = Math.min(Math.max(height, minSize), mInitialDisplayHeight * maxScale);
2176 }
2177
2178 Slog.i(TAG_WM, "Using new display size: " + width + "x" + height);
2179 updateBaseDisplayMetrics(width, height, mBaseDisplayDensity);
2180 mService.reconfigureDisplayLocked(this);
2181
2182 if (clear) {
2183 width = height = 0;
2184 }
Chilun8753ad32018-10-09 15:56:45 +08002185 mService.mDisplayWindowSettings.setForcedSize(this, width, height);
Riddle Hsuf53da812018-08-15 22:00:27 +08002186 }
2187
Wale Ogunwaledb506192017-12-08 10:57:32 -08002188 void getStableRect(Rect out) {
2189 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08002190 }
2191
Wale Ogunwale61911492017-10-11 08:50:50 -07002192 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002193 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
2194 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07002195
Wale Ogunwale61911492017-10-11 08:50:50 -07002196 final TaskStack stack = new TaskStack(mService, stackId, controller);
2197 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002198 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07002199 }
2200
Andrii Kulian51c1b672017-04-07 18:39:32 -07002201 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002202 final DisplayContent prevDc = stack.getDisplayContent();
2203 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07002204 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
2205 + " which is not currently attached to any display");
2206 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002207 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07002208 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
2209 + " to its current displayId=" + mDisplayId);
2210 }
2211
lumark588a3e82018-07-20 18:53:54 +08002212 // Clean up all pending transitions when stack reparent to another display.
2213 stack.forAllAppWindows(AppWindowToken::removeFromPendingTransition);
2214
Wale Ogunwale61911492017-10-11 08:50:50 -07002215 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07002216 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08002217 }
2218
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002219 @Override
2220 protected void addChild(DisplayChildWindowContainer child,
2221 Comparator<DisplayChildWindowContainer> comparator) {
2222 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2223 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002224
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002225 @Override
2226 protected void addChild(DisplayChildWindowContainer child, int index) {
2227 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2228 }
2229
2230 @Override
2231 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002232 // Only allow removal of direct children from this display if the display is in the process
2233 // of been removed.
2234 if (mRemovingDisplay) {
2235 super.removeChild(child);
2236 return;
2237 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002238 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002239 }
2240
Andrii Kuliand2765632016-12-12 22:26:34 -08002241 @Override
2242 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
2243 // Children of the display are statically ordered, so the real intention here is to perform
2244 // the operation on the display and not the static direct children.
2245 getParent().positionChildAt(position, this, includingParents);
2246 }
2247
Riddle Hsu57831b52018-07-27 00:31:48 +08002248 void positionStackAt(int position, TaskStack child, boolean includingParents) {
2249 mTaskStackContainers.positionChildAt(position, child, includingParents);
Winson Chung59a47ded2018-01-25 17:46:06 +00002250 layoutAndAssignWindowLayersIfNeeded();
2251 }
2252
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002253 /**
2254 * Used to obtain task ID when user taps on coordinate (x, y) in this display, and outside
2255 * current task in focus.
2256 *
2257 * This returns the task ID of the foremost task at (x, y) if the task is not home. Otherwise it
2258 * returns -1.
2259 *
2260 * @param x horizontal coordinate of the tap position
2261 * @param y vertical coordinate of the tap position
2262 * @return the task ID if a non-home task is found; -1 if not
2263 */
2264 int taskForTapOutside(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002265 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2266 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Garfield Tanf2e0a9e2018-10-16 09:42:02 -07002267 if (stack.isActivityTypeHome() && !stack.inMultiWindowMode()) {
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002268 // We skip not only home stack, but also everything behind home because user can't
Garfield Tanf2e0a9e2018-10-16 09:42:02 -07002269 // see them when home stack is isn't in multi-window mode.
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002270 break;
2271 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002272 final int taskId = stack.taskIdFromPoint(x, y);
2273 if (taskId != -1) {
2274 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07002275 }
2276 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002277 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07002278 }
2279
Chong Zhang8e89b312015-09-09 15:09:30 -07002280 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08002281 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07002282 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07002283 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07002284 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002285 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002286 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07002287 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2288 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002289 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002290 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002291 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002292
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002293 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
2294 if (mTmpTaskForResizePointSearchResult.searchDone) {
2295 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07002296 }
2297 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002298 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002299 }
2300
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002301 void updateTouchExcludeRegion() {
2302 final Task focusedTask = (mFocusedApp != null ? mFocusedApp.getTask() : null);
David Stevensee9e2772017-02-09 16:30:27 -08002303 if (focusedTask == null) {
2304 mTouchExcludeRegion.setEmpty();
2305 } else {
2306 mTouchExcludeRegion.set(mBaseDisplayRect);
2307 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
2308 mTmpRect2.setEmpty();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002309 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0;
2310 --stackNdx) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002311 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002312 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
2313 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08002314 }
2315 // If we removed the focused task above, add it back and only leave its
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002316 // outside touch area in the exclusion. TapDetector is not interested in
David Stevensee9e2772017-02-09 16:30:27 -08002317 // any touch inside the focused task itself.
2318 if (!mTmpRect2.isEmpty()) {
2319 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
2320 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08002321 }
lumark90120a82018-08-15 00:33:03 +08002322 if (mInputMethodWindow != null && mInputMethodWindow.isVisibleLw()) {
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002323 // If the input method is visible and the user is typing, we don't want these touch
2324 // events to be intercepted and used to change focus. This would likely cause a
2325 // disappearance of the input method.
lumark90120a82018-08-15 00:33:03 +08002326 mInputMethodWindow.getTouchableRegion(mTmpRegion);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002327 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002328 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002329 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002330 final WindowState win = mTapExcludedWindows.get(i);
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002331 win.getTouchableRegion(mTmpRegion);
2332 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
2333 }
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002334 for (int i = mTapExcludeProvidingWindows.size() - 1; i >= 0; i--) {
2335 final WindowState win = mTapExcludeProvidingWindows.valueAt(i);
2336 win.amendTapExcludeRegion(mTouchExcludeRegion);
2337 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08002338 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07002339 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002340 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07002341 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002342 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
2343 }
Craig Mautner1bef3892015-02-17 15:09:47 -08002344 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07002345 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08002346 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07002347 }
2348
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002349 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07002350 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002351 super.switchUser();
2352 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07002353 }
2354
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07002355 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002356 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2357 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07002358 }
2359 }
2360
Wale Ogunwale10124582016-09-15 20:25:50 -07002361 @Override
2362 void removeIfPossible() {
2363 if (isAnimating()) {
2364 mDeferredRemoval = true;
2365 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07002366 }
Wale Ogunwale10124582016-09-15 20:25:50 -07002367 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07002368 }
2369
Wale Ogunwale10124582016-09-15 20:25:50 -07002370 @Override
2371 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002372 mRemovingDisplay = true;
2373 try {
lumark588a3e82018-07-20 18:53:54 +08002374 // Clear all transitions & screen frozen states when removing display.
2375 mOpeningApps.clear();
2376 mClosingApps.clear();
2377 mUnknownAppVisibilityController.clear();
2378 mAppTransition.removeAppTransitionTimeoutCallbacks();
2379 handleAnimatingStoppedAndTransition();
2380 mService.stopFreezingDisplayLocked();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002381 super.removeImmediately();
2382 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Arthur Hungbe5ce212018-09-13 18:41:56 +08002383 if (mPointerEventDispatcher != null && mTapDetector != null) {
2384 unregisterPointerEventListener(mTapDetector);
2385 unregisterPointerEventListener(mService.mMousePositionTracker);
2386 mTapDetector = null;
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002387 }
Jorim Jaggia3844032018-01-03 15:54:43 +01002388 mService.mAnimator.removeDisplayLocked(mDisplayId);
Louis Chang207f9112018-07-30 08:58:54 +08002389 mWindowingLayer.release();
2390 mOverlayLayer.release();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002391 } finally {
Riddle Hsu4e611772018-10-31 18:58:28 +08002392 mDisplayReady = false;
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002393 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08002394 }
Tiger Huang7c610aa2018-10-27 00:01:01 +08002395 mDisplayPolicy.onDisplayRemoved();
Arthur Hung39134b22018-08-14 11:58:28 +08002396 mInputMonitor.onRemoved();
Riddle Hsuf53da812018-08-15 22:00:27 +08002397 mService.mWindowPlacerLocked.requestTraversal();
Craig Mautner95da1082014-02-24 17:54:35 -08002398 }
2399
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002400 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07002401 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002402 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002403 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
2404
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002405 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002406 removeImmediately();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002407 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08002408 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002409 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08002410 }
2411
Andrii Kulian0214ed92017-05-16 13:44:05 -07002412 /** @return 'true' if removal of this display content is deferred due to active animation. */
2413 boolean isRemovalDeferred() {
2414 return mDeferredRemoval;
2415 }
2416
Wale Ogunwale10124582016-09-15 20:25:50 -07002417 boolean animateForIme(float interpolatedValue, float animationTarget,
2418 float dividerAnimationTarget) {
2419 boolean updated = false;
2420
Wale Ogunwale61911492017-10-11 08:50:50 -07002421 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2422 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002423 if (stack == null || !stack.isAdjustedForIme()) {
2424 continue;
2425 }
2426
2427 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
2428 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2429 updated = true;
2430 } else {
2431 mDividerControllerLocked.mLastAnimationProgress =
2432 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
2433 mDividerControllerLocked.mLastDividerProgress =
2434 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
2435 updated |= stack.updateAdjustForIme(
2436 mDividerControllerLocked.mLastAnimationProgress,
2437 mDividerControllerLocked.mLastDividerProgress,
2438 false /* force */);
2439 }
2440 if (interpolatedValue >= 1f) {
2441 stack.endImeAdjustAnimation();
2442 }
2443 }
2444
2445 return updated;
2446 }
2447
2448 boolean clearImeAdjustAnimation() {
2449 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002450 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2451 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002452 if (stack != null && stack.isAdjustedForIme()) {
2453 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2454 changed = true;
2455 }
2456 }
2457 return changed;
2458 }
2459
2460 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002461 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2462 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002463 if (stack.isVisible() && stack.isAdjustedForIme()) {
2464 stack.beginImeAdjustAnimation();
2465 }
2466 }
2467 }
2468
2469 void adjustForImeIfNeeded() {
lumark90120a82018-08-15 00:33:03 +08002470 final WindowState imeWin = mInputMethodWindow;
Wale Ogunwale10124582016-09-15 20:25:50 -07002471 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2472 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002473 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002474 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2475 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2476 imeTargetStack.getDockSide() : DOCKED_INVALID;
2477 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2478 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2479 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002480 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002481 final boolean imeHeightChanged = imeVisible &&
2482 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2483
2484 // The divider could be adjusted for IME position, or be thinner than usual,
2485 // or both. There are three possible cases:
2486 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2487 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2488 // - If IME is not visible, divider is not moved and is normal width.
2489
2490 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002491 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2492 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002493 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002494 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2495 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002496 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2497 } else {
2498 stack.resetAdjustedForIme(false);
2499 }
2500 }
2501 mDividerControllerLocked.setAdjustedForIme(
2502 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2503 } else {
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 stack.resetAdjustedForIme(!dockVisible);
2507 }
2508 mDividerControllerLocked.setAdjustedForIme(
2509 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2510 }
Winson Chung655332c2016-10-31 13:14:28 -07002511 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002512 }
2513
2514 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002515 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2516 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002517 stack.prepareFreezingTaskBounds();
2518 }
2519 }
2520
Wale Ogunwale94744212015-09-21 19:01:47 -07002521 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002522 getBounds(mTmpRect, newRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002523 rotateBounds(mTmpRect, oldRotation, newRotation, bounds);
2524 }
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002525
Evan Rosky39b6f232018-10-30 18:35:41 -07002526 void rotateBounds(Rect parentBounds, int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002527 // Compute a transform matrix to undo the coordinate space transformation,
2528 // and present the window at the same physical position it previously occupied.
2529 final int deltaRotation = deltaRotation(newRotation, oldRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002530 createRotationMatrix(
2531 deltaRotation, parentBounds.width(), parentBounds.height(), mTmpMatrix);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002532
2533 mTmpRectF.set(bounds);
2534 mTmpMatrix.mapRect(mTmpRectF);
2535 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002536 }
2537
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002538 static int deltaRotation(int oldRotation, int newRotation) {
2539 int delta = newRotation - oldRotation;
2540 if (delta < 0) delta += 4;
2541 return delta;
2542 }
2543
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002544 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002545 Matrix outMatrix) {
2546 // For rotations without Z-ordering we don't need the target rectangle's position.
2547 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2548 displayHeight, outMatrix);
2549 }
2550
2551 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2552 float displayWidth, float displayHeight, Matrix outMatrix) {
2553 switch (rotation) {
2554 case ROTATION_0:
2555 outMatrix.reset();
2556 break;
2557 case ROTATION_270:
2558 outMatrix.setRotate(270, 0, 0);
2559 outMatrix.postTranslate(0, displayHeight);
2560 outMatrix.postTranslate(rectTop, 0);
2561 break;
2562 case ROTATION_180:
2563 outMatrix.reset();
2564 break;
2565 case ROTATION_90:
2566 outMatrix.setRotate(90, 0, 0);
2567 outMatrix.postTranslate(displayWidth, 0);
2568 outMatrix.postTranslate(-rectTop, rectLeft);
2569 break;
2570 }
2571 }
2572
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002573 @CallSuper
2574 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002575 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002576 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002577 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002578 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002579 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2580 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002581 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002582 }
2583 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2584 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002585 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2586 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002587 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002588 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002589 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2590 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002591 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002592 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002593 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2594 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002595 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002596 }
2597 proto.write(DPI, mBaseDisplayDensity);
2598 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002599 proto.write(ROTATION, mRotation);
2600 final ScreenRotationAnimation screenRotationAnimation =
2601 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2602 if (screenRotationAnimation != null) {
2603 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2604 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002605 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
lumark588a3e82018-07-20 18:53:54 +08002606 mAppTransition.writeToProto(proto, APP_TRANSITION);
Louis Chang7501e332018-08-20 13:08:39 +08002607 proto.write(SURFACE_SIZE, mSurfaceSize);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002608 if (mFocusedApp != null) {
2609 mFocusedApp.writeNameToProto(proto, FOCUSED_APP);
2610 }
Steven Timotiusaf03df62017-07-18 16:56:43 -07002611 proto.end(token);
2612 }
2613
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002614 @Override
2615 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2616 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002617 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2618 final String subPrefix = " " + prefix;
2619 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2620 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2621 pw.print("dpi");
2622 if (mInitialDisplayWidth != mBaseDisplayWidth
2623 || mInitialDisplayHeight != mBaseDisplayHeight
2624 || mInitialDisplayDensity != mBaseDisplayDensity) {
2625 pw.print(" base=");
2626 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2627 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2628 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002629 if (mDisplayScalingDisabled) {
2630 pw.println(" noscale");
2631 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002632 pw.print(" cur=");
2633 pw.print(mDisplayInfo.logicalWidth);
2634 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2635 pw.print(" app=");
2636 pw.print(mDisplayInfo.appWidth);
2637 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2638 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2639 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2640 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2641 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002642 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002643 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002644 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002645
Craig Mautnerdc548482014-02-05 13:35:24 -08002646 pw.println();
Adrian Roos5251b1d2018-03-23 18:57:43 +01002647 pw.print(prefix); pw.print("mLayoutSeq="); pw.println(mLayoutSeq);
Riddle Hsu654a6f92018-07-13 22:59:36 +08002648 pw.print(prefix);
2649 pw.print("mDeferredRotationPauseCount="); pw.println(mDeferredRotationPauseCount);
Adrian Roos5251b1d2018-03-23 18:57:43 +01002650
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002651 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
2652 if (mLastFocus != mCurrentFocus) {
2653 pw.print(" mLastFocus="); pw.println(mLastFocus);
2654 }
2655 if (mLosingFocus.size() > 0) {
2656 pw.println();
2657 pw.println(" Windows losing focus:");
2658 for (int i = mLosingFocus.size() - 1; i >= 0; i--) {
2659 final WindowState w = mLosingFocus.get(i);
2660 pw.print(" Losing #"); pw.print(i); pw.print(' ');
2661 pw.print(w);
2662 if (dumpAll) {
2663 pw.println(":");
2664 w.dump(pw, " ", true);
2665 } else {
2666 pw.println();
2667 }
2668 }
2669 }
2670 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
Tiger Huang7c610aa2018-10-27 00:01:01 +08002671 if (mLastStatusBarVisibility != 0) {
2672 pw.print(" mLastStatusBarVisibility=0x");
2673 pw.println(Integer.toHexString(mLastStatusBarVisibility));
2674 }
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002675
Adrian Roos5251b1d2018-03-23 18:57:43 +01002676 pw.println();
wilsonshihc32538e2018-11-07 17:27:34 +08002677 mWallpaperController.dump(pw, " ");
2678
2679 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002680 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002681 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2682 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002683 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002684 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002685
Craig Mautnerdc548482014-02-05 13:35:24 -08002686 pw.println();
2687 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002688 pw.println();
2689 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002690 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002691 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002692 pw.print(" Exiting #"); pw.print(i);
2693 pw.print(' '); pw.print(token);
2694 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002695 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002696 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002697 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002698
Jorim Jaggi31f71702016-05-04 16:43:04 -07002699 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002700
2701 // Dump stack references
2702 final TaskStack homeStack = getHomeStack();
2703 if (homeStack != null) {
2704 pw.println(prefix + "homeStack=" + homeStack.getName());
2705 }
2706 final TaskStack pinnedStack = getPinnedStack();
2707 if (pinnedStack != null) {
2708 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2709 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002710 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002711 if (splitScreenPrimaryStack != null) {
2712 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2713 }
2714
2715 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002716 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002717 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002718 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002719
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002720 pw.println();
2721 mDisplayFrames.dump(prefix, pw);
Arthur Hung95b38a92018-07-20 18:56:12 +08002722 pw.println();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08002723 mDisplayPolicy.dump(prefix, pw);
2724 pw.println();
Riddle Hsuad256a12018-07-18 16:11:30 +08002725 mDisplayRotation.dump(prefix, pw);
2726 pw.println();
Arthur Hung95b38a92018-07-20 18:56:12 +08002727 mInputMonitor.dump(pw, " ");
Craig Mautner59c00972012-07-30 12:10:24 -07002728 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002729
2730 @Override
2731 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002732 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002733 }
2734
2735 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002736 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002737 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002738
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002739 /** Returns true if the stack in the windowing mode is visible. */
2740 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002741 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002742 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002743 }
2744
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002745 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002746 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002747 final int x = (int) xf;
2748 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002749 final WindowState touchedWin = getWindow(w -> {
2750 final int flags = w.mAttrs.flags;
2751 if (!w.isVisibleLw()) {
2752 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002753 }
2754 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002755 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002756 }
2757
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002758 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002759 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002760 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002761 }
2762
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002763 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002764
2765 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002766 return mTmpRegion.contains(x, y) || touchFlags == 0;
2767 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002768
2769 return touchedWin;
2770 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002771
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002772 boolean canAddToastWindowForUid(int uid) {
2773 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002774 // Also if the app is focused adding more than one toast at
2775 // a time for better backwards compatibility.
2776 final WindowState focusedWindowForUid = getWindow(w ->
2777 w.mOwnerUid == uid && w.isFocused());
2778 if (focusedWindowForUid != null) {
2779 return true;
2780 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002781 final WindowState win = getWindow(w ->
2782 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2783 && !w.mWindowRemovalAllowed);
2784 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002785 }
2786
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002787 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002788 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2789 return;
2790 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002791
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002792 // Used to communicate the old focus to the callback method.
2793 mTmpWindow = oldFocus;
2794
2795 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002796 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002797
2798 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002799 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002800
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002801 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002802
2803 if (mTmpWindow == null) {
2804 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2805 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002806 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002807 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002808 }
2809
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002810
2811 /**
2812 * Update the focused window and make some adjustments if the focus has changed.
2813 *
2814 * @param mode Indicates the situation we are in. Possible modes are:
2815 * {@link WindowManagerService#UPDATE_FOCUS_NORMAL},
2816 * {@link WindowManagerService#UPDATE_FOCUS_PLACING_SURFACES},
2817 * {@link WindowManagerService#UPDATE_FOCUS_WILL_PLACE_SURFACES},
2818 * {@link WindowManagerService#UPDATE_FOCUS_REMOVING_FOCUS}
2819 * @param updateInputWindows Whether to sync the window information to the input module.
2820 * @return {@code true} if the focused window has changed.
2821 */
2822 boolean updateFocusedWindowLocked(int mode, boolean updateInputWindows, boolean focusFound) {
2823 final WindowState newFocus = findFocusedWindow();
2824 if (mCurrentFocus == newFocus) {
2825 return false;
2826 }
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002827 boolean imWindowChanged = false;
2828 // TODO (b/111080190): Multi-Session IME
2829 if (!focusFound) {
2830 final WindowState imWindow = mInputMethodWindow;
2831 if (imWindow != null) {
lumarkff0ab692018-11-05 20:32:30 +08002832 final WindowState prevTarget = mInputMethodTarget;
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002833
2834 final WindowState newTarget = computeImeTarget(true /* updateImeTarget*/);
2835 imWindowChanged = prevTarget != newTarget;
2836
2837 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS
2838 && mode != UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2839 assignWindowLayers(false /* setLayoutNeeded */);
2840 }
2841 }
2842 }
2843
2844 if (imWindowChanged) {
2845 mService.mWindowsChanged = true;
2846 setLayoutNeeded();
2847 }
2848
2849 if (DEBUG_FOCUS_LIGHT || mService.localLOGV) Slog.v(TAG_WM, "Changing focus from "
2850 + mCurrentFocus + " to " + newFocus + " displayId=" + getDisplayId()
2851 + " Callers=" + Debug.getCallers(4));
2852 final WindowState oldFocus = mCurrentFocus;
2853 mCurrentFocus = newFocus;
2854 mLosingFocus.remove(newFocus);
2855
2856 if (newFocus != null) {
2857 mWinAddedSinceNullFocus.clear();
2858 mWinRemovedSinceNullFocus.clear();
2859
2860 if (newFocus.canReceiveKeys()) {
2861 // Displaying a window implicitly causes dispatching to be unpaused.
2862 // This is to protect against bugs if someone pauses dispatching but
2863 // forgets to resume.
2864 newFocus.mToken.paused = false;
2865 }
2866 }
2867
Tiger Huang7c610aa2018-10-27 00:01:01 +08002868 int focusChanged = getDisplayPolicy().focusChangedLw(oldFocus, newFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002869
2870 if (imWindowChanged && oldFocus != mInputMethodWindow) {
2871 // Focus of the input method window changed. Perform layout if needed.
2872 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2873 performLayout(true /*initial*/, updateInputWindows);
2874 focusChanged &= ~FINISH_LAYOUT_REDO_LAYOUT;
2875 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2876 // Client will do the layout, but we need to assign layers
2877 // for handleNewWindowLocked() below.
2878 assignWindowLayers(false /* setLayoutNeeded */);
2879 }
2880 }
2881
2882 if ((focusChanged & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2883 // The change in focus caused us to need to do a layout. Okay.
2884 setLayoutNeeded();
2885 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2886 performLayout(true /*initial*/, updateInputWindows);
2887 } else if (mode == UPDATE_FOCUS_REMOVING_FOCUS) {
2888 mService.mRoot.performSurfacePlacement(false);
2889 }
2890 }
2891
2892 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
2893 // If we defer assigning layers, then the caller is responsible for doing this part.
2894 getInputMonitor().setInputFocusLw(newFocus, updateInputWindows);
2895 }
2896
2897 adjustForImeIfNeeded();
2898
2899 // We may need to schedule some toast windows to be removed. The toasts for an app that
2900 // does not have input focus are removed within a timeout to prevent apps to redress
2901 // other apps' UI.
2902 scheduleToastWindowsTimeoutIfNeededLocked(oldFocus, newFocus);
2903
2904 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2905 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
2906 }
2907 return true;
2908 }
2909
2910 /**
2911 * Set the new focused app to this display.
2912 *
2913 * @param newFocus the new focused AppWindowToken.
2914 * @return true if the focused app is changed.
2915 */
2916 boolean setFocusedApp(AppWindowToken newFocus) {
2917 if (newFocus != null) {
2918 final DisplayContent appDisplay = newFocus.getDisplayContent();
2919 if (appDisplay != this) {
2920 throw new IllegalStateException(newFocus + " is not on " + getName()
2921 + " but " + ((appDisplay != null) ? appDisplay.getName() : "none"));
2922 }
2923 }
2924 if (mFocusedApp == newFocus) {
2925 return false;
2926 }
2927 mFocusedApp = newFocus;
2928 getInputMonitor().setFocusedAppLw(newFocus);
2929 updateTouchExcludeRegion();
2930 return true;
2931 }
2932
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002933 /** Updates the layer assignment of windows on this display. */
2934 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002935 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07002936 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002937 if (setLayoutNeeded) {
2938 setLayoutNeeded();
2939 }
Robert Carrb1579c82017-09-05 14:54:47 -07002940
Robert Carrf59b8dd2017-10-02 18:58:36 -07002941 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002942 // the application of this transaction until the animation pass triggers
2943 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2944 // the hiding and showing of surfaces.
2945 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002946 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002947 }
2948
Wale Ogunwale1666e312016-12-16 11:27:18 -08002949 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2950 // moving containers or resizing them. Need to investigate the best way to have it automatically
2951 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002952 void layoutAndAssignWindowLayersIfNeeded() {
2953 mService.mWindowsChanged = true;
2954 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002955
2956 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2957 false /*updateInputWindows*/)) {
2958 assignWindowLayers(false /* setLayoutNeeded */);
2959 }
2960
Arthur Hung95b38a92018-07-20 18:56:12 +08002961 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002962 mService.mWindowPlacerLocked.performSurfacePlacement();
Arthur Hung95b38a92018-07-20 18:56:12 +08002963 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002964 }
2965
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002966 /** Returns true if a leaked surface was destroyed */
2967 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002968 // Used to indicate that a surface was leaked.
2969 mTmpWindow = null;
2970 forAllWindows(w -> {
2971 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002972 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002973 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002974 }
2975 if (!mService.mSessions.contains(wsa.mSession)) {
2976 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002977 + w + " surface=" + wsa.mSurfaceController
2978 + " token=" + w.mToken
2979 + " pid=" + w.mSession.mPid
2980 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002981 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002982 mService.mForceRemoves.add(w);
2983 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002984 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002985 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002986 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002987 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002988 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002989 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002990 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002991 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002992 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002993
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002994 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002995 }
2996
2997 /**
lumark90120a82018-08-15 00:33:03 +08002998 * Set input method window for the display.
2999 * @param win Set when window added or Null when destroyed.
3000 */
3001 void setInputMethodWindowLocked(WindowState win) {
3002 mInputMethodWindow = win;
lumark70865a82018-09-17 16:56:26 +08003003 // Update display configuration for IME process.
3004 if (mInputMethodWindow != null) {
3005 final int imePid = mInputMethodWindow.mSession.mPid;
3006 mService.mAtmInternal.onImeWindowSetOnDisplay(imePid,
3007 mInputMethodWindow.getDisplayId());
3008 }
lumark90120a82018-08-15 00:33:03 +08003009 computeImeTarget(true /* updateImeTarget */);
3010 }
3011
3012 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003013 * Determine and return the window that should be the IME target.
3014 * @param updateImeTarget If true the system IME target will be updated to match what we found.
3015 * @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 +00003016 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003017 WindowState computeImeTarget(boolean updateImeTarget) {
lumark90120a82018-08-15 00:33:03 +08003018 if (mInputMethodWindow == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003019 // There isn't an IME so there shouldn't be a target...That was easy!
3020 if (updateImeTarget) {
3021 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
lumarkff0ab692018-11-05 20:32:30 +08003022 + mInputMethodTarget + " to null since mInputMethodWindow is null");
3023 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003024 }
3025 return null;
3026 }
3027
lumarkff0ab692018-11-05 20:32:30 +08003028 final WindowState curTarget = mInputMethodTarget;
Chavi Weingarten3a748552018-05-14 17:32:42 +00003029 if (!canUpdateImeTarget()) {
3030 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Defer updating IME target");
3031 return curTarget;
3032 }
3033
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003034 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
3035 // same display. Or even when the current IME/target are not on the same screen as the next
3036 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08003037 mUpdateImeTarget = updateImeTarget;
3038 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003039
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003040
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003041 // Yet more tricksyness! If this window is a "starting" window, we do actually want
3042 // to be on top of it, but it is not -really- where input will go. So look down below
3043 // for a real window to target...
3044 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
3045 final AppWindowToken token = target.mAppToken;
3046 if (token != null) {
3047 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
3048 if (betterTarget != null) {
3049 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003050 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003051 }
3052 }
3053
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003054 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
lumarkff0ab692018-11-05 20:32:30 +08003055 "Proposed new IME target: " + target + " for display: " + getDisplayId());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003056
chaviw1c13ad32018-06-12 16:44:23 -07003057 // Now, a special case -- if the last target's window is in the process of exiting, but
3058 // not removed, and the new target is home, keep on the last target to avoid flicker.
3059 // Home is a special case since its above other stacks in the ordering list, but layed
3060 // out below the others.
3061 if (curTarget != null && !curTarget.mRemoved && curTarget.isDisplayedLw()
3062 && curTarget.isClosing() && (target == null || target.isActivityTypeHome())) {
chaviw4ad54912018-05-30 11:05:44 -07003063 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "New target is home while current target is "
Chavi Weingarten3a748552018-05-14 17:32:42 +00003064 + "closing, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003065 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003066 }
3067
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003068 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
3069 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003070
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003071 if (target == null) {
3072 if (updateImeTarget) {
3073 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
3074 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
3075 + Debug.getCallers(4) : ""));
lumarkff0ab692018-11-05 20:32:30 +08003076 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003077 }
3078
3079 return null;
3080 }
3081
3082 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003083 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
3084 if (token != null) {
3085
3086 // Now some fun for dealing with window animations that modify the Z order. We need
3087 // to look at all windows below the current target that are in this app, finding the
3088 // highest visible one in layering.
3089 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003090 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003091 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003092 }
3093
3094 if (highestTarget != null) {
lumark588a3e82018-07-20 18:53:54 +08003095 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, mAppTransition + " " + highestTarget
Jorim Jaggi35d328a2018-08-14 17:00:20 +02003096 + " animating=" + highestTarget.isAnimating());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003097
lumark588a3e82018-07-20 18:53:54 +08003098 if (mAppTransition.isTransitionSet()) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003099 // If we are currently setting up for an animation, hold everything until we
3100 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003101 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003102 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003103 }
3104 }
3105 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003106
3107 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
3108 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003109 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003110 }
3111
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003112 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003113 }
3114
lumarkff0ab692018-11-05 20:32:30 +08003115 /**
3116 * Calling {@link #computeImeTarget(boolean)} to update the input method target window in
3117 * the candidate app window token if needed.
3118 */
3119 void computeImeTargetIfNeeded(AppWindowToken candidate) {
3120 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == candidate) {
3121 computeImeTarget(true /* updateImeTarget */);
3122 }
3123 }
3124
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003125 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
lumarkff0ab692018-11-05 20:32:30 +08003126 if (target == mInputMethodTarget && mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003127 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003128 }
3129
lumarkff0ab692018-11-05 20:32:30 +08003130 mInputMethodTarget = target;
3131 mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003132 assignWindowLayers(false /* setLayoutNeeded */);
3133 }
3134
3135 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
3136 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3137 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
3138 }
3139
3140 // Used to indicate we have reached the first window in the range we are interested in.
3141 mTmpWindow = null;
3142
3143 // TODO: Figure-out a more efficient way to do this.
3144 final WindowState candidate = getWindow(w -> {
3145 if (w == top) {
3146 // Reached the first window in the range we are interested in.
3147 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003148 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003149 if (mTmpWindow == null) {
3150 return false;
3151 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003152
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003153 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3154 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003155 }
3156 // If we reached the bottom of the range of windows we are considering,
3157 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003158 if (w == bottom) {
3159 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003160 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003161 return false;
3162 });
3163
3164 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003165 }
3166
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003167 void setLayoutNeeded() {
3168 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
3169 mLayoutNeeded = true;
3170 }
3171
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003172 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003173 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
3174 mLayoutNeeded = false;
3175 }
3176
3177 boolean isLayoutNeeded() {
3178 return mLayoutNeeded;
3179 }
3180
Wale Ogunwale02319a62016-09-26 15:21:22 -07003181 void dumpTokens(PrintWriter pw, boolean dumpAll) {
3182 if (mTokenMap.isEmpty()) {
3183 return;
3184 }
3185 pw.println(" Display #" + mDisplayId);
3186 final Iterator<WindowToken> it = mTokenMap.values().iterator();
3187 while (it.hasNext()) {
3188 final WindowToken token = it.next();
3189 pw.print(" ");
3190 pw.print(token);
3191 if (dumpAll) {
3192 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003193 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07003194 } else {
3195 pw.println();
3196 }
3197 }
lumark588a3e82018-07-20 18:53:54 +08003198
3199 if (!mOpeningApps.isEmpty() || !mClosingApps.isEmpty()) {
3200 pw.println();
3201 if (mOpeningApps.size() > 0) {
3202 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
3203 }
3204 if (mClosingApps.size() > 0) {
3205 pw.print(" mClosingApps="); pw.println(mClosingApps);
3206 }
3207 }
3208
3209 mUnknownAppVisibilityController.dump(pw, " ");
Wale Ogunwale02319a62016-09-26 15:21:22 -07003210 }
3211
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003212 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003213 final int[] index = new int[1];
3214 forAllWindows(w -> {
3215 final WindowStateAnimator wAnim = w.mWinAnimator;
3216 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
3217 index[0] = index[0] + 1;
3218 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003219 }
3220
Jorim Jaggife762342016-10-13 14:33:27 +02003221 /**
3222 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
3223 */
3224 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
3225 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003226 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02003227 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
3228 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01003229 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02003230 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003231 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02003232 }
3233
Wale Ogunwale494009b82016-10-21 09:01:38 -07003234 boolean checkWaitingForWindows() {
3235
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003236 mHaveBootMsg = false;
3237 mHaveApp = false;
3238 mHaveWallpaper = false;
3239 mHaveKeyguard = true;
3240
3241 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003242 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
3243 return true;
3244 }
3245 if (w.isDrawnLw()) {
3246 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003247 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003248 } else if (w.mAttrs.type == TYPE_APPLICATION
3249 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003250 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003251 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003252 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003253 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003254 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003255 }
3256 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003257 return false;
3258 });
3259
3260 if (visibleWindow != null) {
3261 // We have a visible window.
3262 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003263 }
3264
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003265 // if the wallpaper service is disabled on the device, we're never going to have
3266 // wallpaper, don't bother waiting for it
3267 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
3268 com.android.internal.R.bool.config_enableWallpaperService)
Daichi Hironoc1432222018-03-29 13:06:07 +09003269 && mService.mContext.getResources().getBoolean(
3270 com.android.internal.R.bool.config_checkWallpaperAtBoot)
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003271 && !mService.mOnlyCore;
3272
Wale Ogunwale494009b82016-10-21 09:01:38 -07003273 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
3274 "******** booted=" + mService.mSystemBooted
3275 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003276 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
3277 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
3278 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003279
3280 // If we are turning on the screen to show the boot message, don't do it until the boot
3281 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003282 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003283 return true;
3284 }
3285
3286 // If we are turning on the screen after the boot is completed normally, don't do so until
3287 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003288 if (mService.mSystemBooted
3289 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003290 return true;
3291 }
3292
3293 return false;
3294 }
3295
Jorim Jaggi8f520872018-08-14 17:00:20 +02003296 void updateWindowsForAnimator() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003297 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003298 }
3299
Jorim Jaggi8f520872018-08-14 17:00:20 +02003300 /**
3301 * Updates the {@link TaskStack#setAnimationBackground} for all windows.
3302 */
3303 void updateBackgroundForAnimator() {
Jorim Jaggi7156b652016-10-25 08:31:58 -07003304 resetAnimationBackgroundAnimator();
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003305 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003306 }
3307
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003308 boolean isInputMethodClientFocus(int uid, int pid) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003309 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003310 if (imFocus == null) {
3311 return false;
3312 }
3313
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003314 if (DEBUG_INPUT_METHOD) {
3315 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003316 Slog.i(TAG_WM, "Current focus: " + mCurrentFocus + " displayId=" + mDisplayId);
3317 Slog.i(TAG_WM, "Last focus: " + mLastFocus + " displayId=" + mDisplayId);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003318 }
3319
Wale Ogunwale494009b82016-10-21 09:01:38 -07003320 if (DEBUG_INPUT_METHOD) {
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003321 Slog.i(TAG_WM, "IM target uid/pid: " + imFocus.mSession.mUid
3322 + "/" + imFocus.mSession.mPid);
3323 Slog.i(TAG_WM, "Requesting client uid/pid: " + uid + "/" + pid);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003324 }
3325
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003326 return imFocus.mSession.mUid == uid && imFocus.mSession.mPid == pid;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003327 }
3328
3329 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003330 final WindowState win = getWindow(
3331 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
3332 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003333 }
3334
Tiger Huang7c610aa2018-10-27 00:01:01 +08003335 void statusBarVisibilityChanged(int visibility) {
3336 mLastStatusBarVisibility = visibility;
3337 visibility = getDisplayPolicy().adjustSystemUiVisibilityLw(visibility);
3338 updateStatusBarVisibilityLocked(visibility);
3339 }
3340
3341 private boolean updateStatusBarVisibilityLocked(int visibility) {
3342 if (mLastDispatchedSystemUiVisibility == visibility) {
3343 return false;
3344 }
3345 final int globalDiff = (visibility ^ mLastDispatchedSystemUiVisibility)
3346 // We are only interested in differences of one of the
3347 // clearable flags...
3348 & View.SYSTEM_UI_CLEARABLE_FLAGS
3349 // ...if it has actually been cleared.
3350 & ~visibility;
3351
3352 mLastDispatchedSystemUiVisibility = visibility;
3353 if (isDefaultDisplay) {
3354 mService.mInputManager.setSystemUiVisibility(visibility);
3355 }
3356 updateSystemUiVisibility(visibility, globalDiff);
3357 return true;
3358 }
3359
Wale Ogunwale494009b82016-10-21 09:01:38 -07003360 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003361 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003362 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003363 final int curValue = w.mSystemUiVisibility;
3364 final int diff = (curValue ^ visibility) & globalDiff;
3365 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003366 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003367 w.mSeq++;
3368 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003369 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003370 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
3371 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07003372 visibility, newValue, diff);
3373 }
3374 } catch (RemoteException e) {
3375 // so sorry
3376 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003377 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003378 }
3379
Tiger Huang7c610aa2018-10-27 00:01:01 +08003380 void reevaluateStatusBarVisibility() {
3381 int visibility = getDisplayPolicy().adjustSystemUiVisibilityLw(mLastStatusBarVisibility);
3382 if (updateStatusBarVisibilityLocked(visibility)) {
3383 mService.mWindowPlacerLocked.requestTraversal();
3384 }
3385 }
3386
Wale Ogunwale494009b82016-10-21 09:01:38 -07003387 void onWindowFreezeTimeout() {
3388 Slog.w(TAG_WM, "Window freeze timeout expired.");
3389 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003390
3391 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07003392 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003393 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003394 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07003395 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003396 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
3397 - mService.mDisplayFreezeTime);
3398 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003399 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003400 mService.mWindowPlacerLocked.performSurfacePlacement();
3401 }
3402
3403 void waitForAllWindowsDrawn() {
3404 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003405 forAllWindows(w -> {
3406 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
3407 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
3408 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003409 // Force add to mResizingWindows.
chaviw9c81e632018-07-31 11:17:52 -07003410 w.resetLastContentInsets();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003411 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003412 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003413 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003414 }
3415
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003416 // TODO: Super crazy long method that should be broken down...
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003417 void applySurfaceChangesTransaction(boolean recoveringMemory) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003418 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
3419
3420 mTmpUpdateAllDrawn.clear();
3421
3422 int repeats = 0;
3423 do {
3424 repeats++;
3425 if (repeats > 6) {
3426 Slog.w(TAG, "Animation repeat aborted after too many iterations");
3427 clearLayoutNeeded();
3428 break;
3429 }
3430
3431 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
3432 pendingLayoutChanges);
3433
wilsonshihc32538e2018-11-07 17:27:34 +08003434 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
3435 mWallpaperController.adjustWallpaperWindows();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003436 }
3437
Riddle Hsu654a6f92018-07-13 22:59:36 +08003438 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003439 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
Riddle Hsu4e611772018-10-31 18:58:28 +08003440 if (updateOrientationFromAppTokens()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003441 setLayoutNeeded();
Riddle Hsu4e611772018-10-31 18:58:28 +08003442 sendNewConfiguration();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003443 }
3444 }
3445
3446 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
3447 setLayoutNeeded();
3448 }
3449
3450 // FIRST LOOP: Perform a layout, if needed.
3451 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
3452 performLayout(repeats == 1, false /* updateInputWindows */);
3453 } else {
3454 Slog.w(TAG, "Layout repeat skipped after too many iterations");
3455 }
3456
3457 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
3458 pendingLayoutChanges = 0;
3459
Tiger Huang7c610aa2018-10-27 00:01:01 +08003460 mDisplayPolicy.beginPostLayoutPolicyLw();
3461 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
3462 pendingLayoutChanges |= mDisplayPolicy.finishPostLayoutPolicyLw();
3463 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
3464 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003465 } while (pendingLayoutChanges != 0);
3466
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003467 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003468
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003469 mTmpRecoveringMemory = recoveringMemory;
3470 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01003471 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003472
Jorim Jaggif1292892018-09-10 11:58:13 +02003473 mLastHasContent = mTmpApplySurfaceChangesTransactionState.displayHasContent;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003474 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Jorim Jaggif1292892018-09-10 11:58:13 +02003475 mLastHasContent,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003476 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
3477 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003478 true /* inTraversal, must call performTraversalInTrans... below */);
3479
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003480 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
3481 if (wallpaperVisible != mLastWallpaperVisible) {
3482 mLastWallpaperVisible = wallpaperVisible;
3483 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
3484 }
3485
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003486 while (!mTmpUpdateAllDrawn.isEmpty()) {
3487 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
3488 // See if any windows have been drawn, so they (and others associated with them)
3489 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07003490 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003491 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003492 }
3493
Bryce Leef3c6a472017-11-14 14:53:06 -08003494 private void updateBounds() {
Evan Rosky39b6f232018-10-30 18:35:41 -07003495 calculateBounds(mDisplayInfo, mTmpBounds);
Bryce Leef3c6a472017-11-14 14:53:06 -08003496 setBounds(mTmpBounds);
3497 }
3498
3499 // Determines the current display bounds based on the current state
Evan Rosky39b6f232018-10-30 18:35:41 -07003500 private void calculateBounds(DisplayInfo displayInfo, Rect out) {
Bryce Leef3c6a472017-11-14 14:53:06 -08003501 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Evan Rosky39b6f232018-10-30 18:35:41 -07003502 final int rotation = displayInfo.rotation;
3503 boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Bryce Leef3c6a472017-11-14 14:53:06 -08003504 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
3505 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Evan Rosky39b6f232018-10-30 18:35:41 -07003506 int width = displayInfo.logicalWidth;
Bryce Leef3c6a472017-11-14 14:53:06 -08003507 int left = (physWidth - width) / 2;
Evan Rosky39b6f232018-10-30 18:35:41 -07003508 int height = displayInfo.logicalHeight;
Bryce Leef3c6a472017-11-14 14:53:06 -08003509 int top = (physHeight - height) / 2;
3510 out.set(left, top, left + width, top + height);
3511 }
3512
3513 @Override
3514 public void getBounds(Rect out) {
Evan Rosky39b6f232018-10-30 18:35:41 -07003515 calculateBounds(mDisplayInfo, out);
Bryce Leef3c6a472017-11-14 14:53:06 -08003516 }
3517
3518 private void getBounds(Rect out, int orientation) {
3519 getBounds(out);
3520
3521 // Rotate the Rect if needed.
3522 final int currentRotation = mDisplayInfo.rotation;
3523 final int rotationDelta = deltaRotation(currentRotation, orientation);
3524 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
3525 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
3526 mTmpRectF.set(out);
3527 mTmpMatrix.mapRect(mTmpRectF);
3528 mTmpRectF.round(out);
3529 }
3530 }
3531
Louis Chang7501e332018-08-20 13:08:39 +08003532 int getSurfaceSize() {
3533 return mSurfaceSize;
3534 }
3535
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003536 void performLayout(boolean initial, boolean updateInputWindows) {
3537 if (!isLayoutNeeded()) {
3538 return;
3539 }
3540 clearLayoutNeeded();
3541
3542 final int dw = mDisplayInfo.logicalWidth;
3543 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003544 if (DEBUG_LAYOUT) {
3545 Slog.v(TAG, "-------------------------------------");
3546 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
3547 }
3548
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01003549 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
3550 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00003551 // TODO: Not sure if we really need to set the rotation here since we are updating from the
3552 // display info above...
3553 mDisplayFrames.mRotation = mRotation;
Tiger Huang7c610aa2018-10-27 00:01:01 +08003554 mDisplayPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003555
Adrian Roos5251b1d2018-03-23 18:57:43 +01003556 int seq = mLayoutSeq + 1;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003557 if (seq < 0) seq = 0;
Adrian Roos5251b1d2018-03-23 18:57:43 +01003558 mLayoutSeq = seq;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003559
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003560 // Used to indicate that we have processed the dream window and all additional windows are
3561 // behind it.
3562 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003563 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003564
3565 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003566 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003567
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003568 // Used to indicate that we have processed the dream window and all additional attached
3569 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003570 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003571 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003572
3573 // Now perform layout of attached windows, which usually depend on the position of the
3574 // window they are attached to. XXX does not deal with windows that are attached to windows
3575 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003576 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003577
3578 // Window frames may have changed. Tell the input dispatcher about it.
Arthur Hung95b38a92018-07-20 18:56:12 +08003579 mInputMonitor.layoutInputConsumers(dw, dh);
3580 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003581 if (updateInputWindows) {
Arthur Hung95b38a92018-07-20 18:56:12 +08003582 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003583 }
3584
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003585 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
3586 }
3587
3588 /**
3589 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
3590 * In portrait mode, it grabs the full screenshot.
3591 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003592 * @param config of the output bitmap
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003593 */
chaviw0315a1a2018-03-05 15:28:35 -08003594 Bitmap screenshotDisplayLocked(Bitmap.Config config) {
3595 if (!mService.mPolicy.isScreenOn()) {
3596 if (DEBUG_SCREENSHOT) {
3597 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003598 }
chaviw0315a1a2018-03-05 15:28:35 -08003599 return null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003600 }
chaviwfbe47df2017-11-10 16:14:49 -08003601
chaviw0315a1a2018-03-05 15:28:35 -08003602 int dw = mDisplayInfo.logicalWidth;
3603 int dh = mDisplayInfo.logicalHeight;
chaviwfbe47df2017-11-10 16:14:49 -08003604
chaviw0315a1a2018-03-05 15:28:35 -08003605 if (dw <= 0 || dh <= 0) {
3606 return null;
3607 }
chaviwfbe47df2017-11-10 16:14:49 -08003608
chaviw0315a1a2018-03-05 15:28:35 -08003609 final Rect frame = new Rect(0, 0, dw, dh);
chaviwfbe47df2017-11-10 16:14:49 -08003610
chaviw0315a1a2018-03-05 15:28:35 -08003611 // The screenshot API does not apply the current screen rotation.
3612 int rot = mDisplay.getRotation();
chaviwfbe47df2017-11-10 16:14:49 -08003613
chaviw0315a1a2018-03-05 15:28:35 -08003614 if (rot == ROTATION_90 || rot == ROTATION_270) {
3615 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3616 }
chaviwfbe47df2017-11-10 16:14:49 -08003617
chaviw0315a1a2018-03-05 15:28:35 -08003618 // SurfaceFlinger is not aware of orientation, so convert our logical
3619 // crop to SurfaceFlinger's portrait orientation.
3620 convertCropForSurfaceFlinger(frame, rot, dw, dh);
3621
3622 final ScreenRotationAnimation screenRotationAnimation =
3623 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3624 final boolean inRotation = screenRotationAnimation != null &&
3625 screenRotationAnimation.isAnimating();
3626 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
3627
3628 // TODO(b/68392460): We should screenshot Task controls directly
3629 // but it's difficult at the moment as the Task doesn't have the
3630 // correct size set.
chaviw08520a02018-09-10 16:44:56 -07003631 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, inRotation, rot);
chaviw0315a1a2018-03-05 15:28:35 -08003632 if (bitmap == null) {
3633 Slog.w(TAG_WM, "Failed to take screenshot");
3634 return null;
3635 }
3636
3637 // Create a copy of the screenshot that is immutable and backed in ashmem.
3638 // This greatly reduces the overhead of passing the bitmap between processes.
3639 final Bitmap ret = bitmap.createAshmemBitmap(config);
3640 bitmap.recycle();
3641 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003642 }
3643
3644 // TODO: Can this use createRotationMatrix()?
3645 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3646 if (rot == Surface.ROTATION_90) {
3647 final int tmp = crop.top;
3648 crop.top = dw - crop.right;
3649 crop.right = crop.bottom;
3650 crop.bottom = dw - crop.left;
3651 crop.left = tmp;
3652 } else if (rot == Surface.ROTATION_180) {
3653 int tmp = crop.top;
3654 crop.top = dh - crop.bottom;
3655 crop.bottom = dh - tmp;
3656 tmp = crop.right;
3657 crop.right = dw - crop.left;
3658 crop.left = dw - tmp;
3659 } else if (rot == Surface.ROTATION_270) {
3660 final int tmp = crop.top;
3661 crop.top = crop.left;
3662 crop.left = dh - crop.bottom;
3663 crop.bottom = crop.right;
3664 crop.right = dh - tmp;
3665 }
3666 }
3667
3668 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003669 // Used to indicate the layout is needed.
3670 mTmpWindow = null;
3671
3672 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003673 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003674 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003675 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003676 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003677 w.setDisplayLayoutNeeded();
3678 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003679 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003680
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003681 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003682 mService.mWindowPlacerLocked.performSurfacePlacement();
3683 }
3684 }
3685
Wale Ogunwale1666e312016-12-16 11:27:18 -08003686 void setExitingTokensHasVisible(boolean hasVisible) {
3687 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3688 mExitingTokens.get(i).hasVisible = hasVisible;
3689 }
3690
3691 // Initialize state of exiting applications.
3692 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3693 }
3694
3695 void removeExistingTokensIfPossible() {
3696 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3697 final WindowToken token = mExitingTokens.get(i);
3698 if (!token.hasVisible) {
3699 mExitingTokens.remove(i);
3700 }
3701 }
3702
3703 // Time to remove any exiting applications?
3704 mTaskStackContainers.removeExistingAppTokensIfPossible();
3705 }
3706
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003707 @Override
3708 void onDescendantOverrideConfigurationChanged() {
3709 setLayoutNeeded();
3710 mService.requestTraversal();
3711 }
3712
David Stevens9440dc82017-03-16 19:00:20 -07003713 boolean okToDisplay() {
3714 if (mDisplayId == DEFAULT_DISPLAY) {
3715 return !mService.mDisplayFrozen
3716 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3717 }
3718 return mDisplayInfo.state == Display.STATE_ON;
3719 }
3720
3721 boolean okToAnimate() {
3722 return okToDisplay() &&
3723 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3724 }
3725
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003726 static final class TaskForResizePointSearchResult {
3727 boolean searchDone;
3728 Task taskForResize;
3729
3730 void reset() {
3731 searchDone = false;
3732 taskForResize = null;
3733 }
3734 }
Robert Carr3b716242016-08-16 16:02:21 -07003735
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003736 private static final class ApplySurfaceChangesTransactionState {
3737 boolean displayHasContent;
3738 boolean obscured;
3739 boolean syswin;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003740 float preferredRefreshRate;
3741 int preferredModeId;
3742
3743 void reset() {
3744 displayHasContent = false;
3745 obscured = false;
3746 syswin = false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003747 preferredRefreshRate = 0;
3748 preferredModeId = 0;
3749 }
3750 }
3751
3752 private static final class ScreenshotApplicationState {
3753 WindowState appWin;
3754 int maxLayer;
3755 int minLayer;
3756 boolean screenshotReady;
3757
3758 void reset(boolean screenshotReady) {
3759 appWin = null;
3760 maxLayer = 0;
3761 minLayer = 0;
3762 this.screenshotReady = screenshotReady;
3763 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3764 }
3765 }
3766
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003767 /**
3768 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3769 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3770 * homogeneous children type which is currently required by sub-classes of
3771 * {@link WindowContainer} class.
3772 */
3773 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3774
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003775 DisplayChildWindowContainer(WindowManagerService service) {
3776 super(service);
3777 }
3778
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003779 @Override
3780 boolean fillsParent() {
3781 return true;
3782 }
3783
3784 @Override
3785 boolean isVisible() {
3786 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003787 }
3788 }
3789
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003790 /**
3791 * Window container class that contains all containers on this display relating to Apps.
3792 * I.e Activities.
3793 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003794 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003795 /**
3796 * A control placed at the appropriate level for transitions to occur.
3797 */
chaviw23ee71c2017-12-18 11:29:41 -08003798 SurfaceControl mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02003799 SurfaceControl mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02003800 SurfaceControl mHomeAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003801
Robert Carrf7a7ca82017-12-06 13:17:07 -08003802 /**
3803 * Given that the split-screen divider does not have an AppWindowToken, it
3804 * will have to live inside of a "NonAppWindowContainer", in particular
3805 * {@link DisplayContent#mAboveAppWindowsContainers}. However, in visual Z order
3806 * it will need to be interleaved with some of our children, appearing on top of
3807 * both docked stacks but underneath any assistant stacks.
3808 *
3809 * To solve this problem we have this anchor control, which will always exist so
3810 * we can always assign it the correct value in our {@link #assignChildLayers}.
3811 * Likewise since it always exists, {@link AboveAppWindowContainers} can always
3812 * assign the divider a layer relative to it. This way we prevent linking lifecycle
3813 * events between the two containers.
3814 */
3815 SurfaceControl mSplitScreenDividerAnchor = null;
3816
Wale Ogunwale61911492017-10-11 08:50:50 -07003817 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3818 // through the list to find them.
3819 private TaskStack mHomeStack = null;
3820 private TaskStack mPinnedStack = null;
3821 private TaskStack mSplitScreenPrimaryStack = null;
3822
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003823 TaskStackContainers(WindowManagerService service) {
3824 super(service);
3825 }
3826
Wale Ogunwale61911492017-10-11 08:50:50 -07003827 /**
3828 * Returns the topmost stack on the display that is compatible with the input windowing mode
3829 * and activity type. Null is no compatible stack on the display.
3830 */
3831 TaskStack getStack(int windowingMode, int activityType) {
3832 if (activityType == ACTIVITY_TYPE_HOME) {
3833 return mHomeStack;
3834 }
3835 if (windowingMode == WINDOWING_MODE_PINNED) {
3836 return mPinnedStack;
3837 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3838 return mSplitScreenPrimaryStack;
3839 }
3840 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3841 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003842 if (activityType == ACTIVITY_TYPE_UNDEFINED
3843 && windowingMode == stack.getWindowingMode()) {
3844 // Passing in undefined type means we want to match the topmost stack with the
3845 // windowing mode.
3846 return stack;
3847 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003848 if (stack.isCompatible(windowingMode, activityType)) {
3849 return stack;
3850 }
3851 }
3852 return null;
3853 }
3854
3855 @VisibleForTesting
3856 TaskStack getTopStack() {
3857 return mTaskStackContainers.getChildCount() > 0
3858 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3859 }
3860
3861 TaskStack getHomeStack() {
3862 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3863 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3864 }
3865 return mHomeStack;
3866 }
3867
3868 TaskStack getPinnedStack() {
3869 return mPinnedStack;
3870 }
3871
Matthew Ng64e77cf2017-10-31 14:01:31 -07003872 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003873 return mSplitScreenPrimaryStack;
3874 }
3875
Winson Chunge2d72172018-01-25 17:46:20 +00003876 ArrayList<Task> getVisibleTasks() {
3877 final ArrayList<Task> visibleTasks = new ArrayList<>();
3878 forAllTasks(task -> {
3879 if (task.isVisible()) {
3880 visibleTasks.add(task);
3881 }
3882 });
3883 return visibleTasks;
3884 }
3885
Andrii Kulian839def92016-11-02 10:58:58 -07003886 /**
3887 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003888 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003889 */
3890 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003891 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003892 addChild(stack, onTop);
3893 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003894 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003895
Wale Ogunwale61911492017-10-11 08:50:50 -07003896 void onStackWindowingModeChanged(TaskStack stack) {
3897 removeStackReferenceIfNeeded(stack);
3898 addStackReferenceIfNeeded(stack);
3899 if (stack == mPinnedStack && getTopStack() != stack) {
3900 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3901 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3902 }
3903 }
3904
3905 private void addStackReferenceIfNeeded(TaskStack stack) {
3906 if (stack.isActivityTypeHome()) {
3907 if (mHomeStack != null) {
Louis Chang7d0037c2018-08-13 12:42:06 +08003908 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
Wale Ogunwale61911492017-10-11 08:50:50 -07003909 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
Louis Chang7d0037c2018-08-13 12:42:06 +08003910
Wale Ogunwale61911492017-10-11 08:50:50 -07003911 }
Louis Chang7d0037c2018-08-13 12:42:06 +08003912 mHomeStack = stack;
Wale Ogunwale61911492017-10-11 08:50:50 -07003913 }
3914 final int windowingMode = stack.getWindowingMode();
3915 if (windowingMode == WINDOWING_MODE_PINNED) {
3916 if (mPinnedStack != null) {
3917 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3918 + mPinnedStack + " already exist on display=" + this
3919 + " stack=" + stack);
3920 }
3921 mPinnedStack = stack;
3922 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3923 if (mSplitScreenPrimaryStack != null) {
3924 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3925 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3926 + " already exist on display=" + this + " stack=" + stack);
3927 }
3928 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003929 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003930 }
3931 }
3932
3933 private void removeStackReferenceIfNeeded(TaskStack stack) {
3934 if (stack == mHomeStack) {
3935 mHomeStack = null;
3936 } else if (stack == mPinnedStack) {
3937 mPinnedStack = null;
3938 } else if (stack == mSplitScreenPrimaryStack) {
3939 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003940 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3941 mService.setDockedStackCreateStateLocked(
3942 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3943 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003944 }
Andrii Kulian839def92016-11-02 10:58:58 -07003945 }
3946
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003947 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003948 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3949 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003950 addChild(stack, addIndex);
3951 setLayoutNeeded();
3952 }
3953
Wale Ogunwale61911492017-10-11 08:50:50 -07003954 @Override
3955 protected void removeChild(TaskStack stack) {
3956 super.removeChild(stack);
3957 removeStackReferenceIfNeeded(stack);
3958 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003959
3960 @Override
3961 boolean isOnTop() {
3962 // Considered always on top
3963 return true;
3964 }
3965
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003966 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003967 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003968 if (child.getWindowConfiguration().isAlwaysOnTop()
3969 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003970 // This stack is always-on-top, override the default behavior.
3971 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3972
3973 // Moving to its current position, as we must call super but we don't want to
3974 // perform any meaningful action.
3975 final int currentPosition = mChildren.indexOf(child);
3976 super.positionChildAt(currentPosition, child, false /* includingParents */);
3977 return;
3978 }
3979
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003980 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3981 super.positionChildAt(targetPosition, child, includingParents);
3982
Riddle Hsu2f6e1742018-08-23 22:44:36 +08003983 if (includingParents) {
3984 // We still want to move the display of this stack container to top because even the
3985 // target position is adjusted to non-top, the intention of the condition is to have
3986 // higher z-order to gain focus (e.g. moving a task of a fullscreen stack to front
3987 // in a non-top display which is using picture-in-picture mode).
3988 final int topChildPosition = getChildCount() - 1;
3989 if (targetPosition < topChildPosition && position >= topChildPosition) {
3990 getParent().positionChildAt(POSITION_TOP, this /* child */,
3991 true /* includingParents */);
3992 }
3993 }
3994
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003995 setLayoutNeeded();
3996 }
3997
3998 /**
3999 * When stack is added or repositioned, find a proper position for it.
4000 * This will make sure that pinned stack always stays on top.
4001 * @param requestedPosition Position requested by caller.
4002 * @param stack Stack to be added or positioned.
4003 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
4004 * @return The proper position for the stack.
4005 */
4006 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09004007 if (stack.inPinnedWindowingMode()) {
Kazuki Takisef85197b2018-06-18 18:18:36 +09004008 return POSITION_TOP;
Kazuki Takise148d00a2018-05-31 15:32:19 +09004009 }
4010
Kazuki Takisef85197b2018-06-18 18:18:36 +09004011 final int topChildPosition = mChildren.size() - 1;
4012 int belowAlwaysOnTopPosition = POSITION_BOTTOM;
4013 for (int i = topChildPosition; i >= 0; --i) {
4014 if (getStacks().get(i) != stack && !getStacks().get(i).isAlwaysOnTop()) {
4015 belowAlwaysOnTopPosition = i;
4016 break;
4017 }
4018 }
4019
4020 // The max possible position we can insert the stack at.
4021 int maxPosition = POSITION_TOP;
4022 // The min possible position we can insert the stack at.
4023 int minPosition = POSITION_BOTTOM;
4024
4025 if (stack.isAlwaysOnTop()) {
4026 if (hasPinnedStack()) {
4027 // Always-on-top stacks go below the pinned stack.
4028 maxPosition = getStacks().indexOf(mPinnedStack) - 1;
4029 }
4030 // Always-on-top stacks need to be above all other stacks.
4031 minPosition = belowAlwaysOnTopPosition !=
4032 POSITION_BOTTOM ? belowAlwaysOnTopPosition : topChildPosition;
4033 } else {
4034 // Other stacks need to be below the always-on-top stacks.
4035 maxPosition = belowAlwaysOnTopPosition !=
Arthur Hung928d8dc2018-07-18 15:38:05 +08004036 POSITION_BOTTOM ? belowAlwaysOnTopPosition : 0;
Kazuki Takisef85197b2018-06-18 18:18:36 +09004037 }
4038
4039 int targetPosition = requestedPosition;
4040 targetPosition = Math.min(targetPosition, maxPosition);
4041 targetPosition = Math.max(targetPosition, minPosition);
4042
4043 int prevPosition = getStacks().indexOf(stack);
4044 // The positions we calculated above (maxPosition, minPosition) do not take into
4045 // consideration the following edge cases.
4046 // 1) We need to adjust the position depending on the value "adding".
4047 // 2) When we are moving a stack to another position, we also need to adjust the
4048 // position depending on whether the stack is moving to a higher or lower position.
4049 if ((targetPosition != requestedPosition) &&
4050 (adding || targetPosition < prevPosition)) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09004051 targetPosition++;
4052 }
4053
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004054 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08004055 }
4056
4057 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004058 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
4059 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004060 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004061 if (super.forAllWindows(callback, traverseTopToBottom)) {
4062 return true;
4063 }
4064 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4065 return true;
4066 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004067 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004068 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4069 return true;
4070 }
4071 if (super.forAllWindows(callback, traverseTopToBottom)) {
4072 return true;
4073 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004074 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004075 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004076 }
4077
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004078 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004079 boolean traverseTopToBottom) {
4080 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
4081 // app tokens.
4082 // TODO: Investigate if we need to continue to do this or if we can just process them
4083 // in-order.
4084 if (traverseTopToBottom) {
4085 for (int i = mChildren.size() - 1; i >= 0; --i) {
4086 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4087 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004088 if (appTokens.get(j).forAllWindowsUnchecked(callback,
4089 traverseTopToBottom)) {
4090 return true;
4091 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004092 }
4093 }
4094 } else {
4095 final int count = mChildren.size();
4096 for (int i = 0; i < count; ++i) {
4097 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4098 final int appTokensCount = appTokens.size();
4099 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004100 if (appTokens.get(j).forAllWindowsUnchecked(callback,
4101 traverseTopToBottom)) {
4102 return true;
4103 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004104 }
4105 }
4106 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004107 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004108 }
4109
Wale Ogunwale1666e312016-12-16 11:27:18 -08004110 void setExitingTokensHasVisible(boolean hasVisible) {
4111 for (int i = mChildren.size() - 1; i >= 0; --i) {
4112 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4113 for (int j = appTokens.size() - 1; j >= 0; --j) {
4114 appTokens.get(j).hasVisible = hasVisible;
4115 }
4116 }
4117 }
4118
4119 void removeExistingAppTokensIfPossible() {
4120 for (int i = mChildren.size() - 1; i >= 0; --i) {
4121 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4122 for (int j = appTokens.size() - 1; j >= 0; --j) {
4123 final AppWindowToken token = appTokens.get(j);
lumark588a3e82018-07-20 18:53:54 +08004124 if (!token.hasVisible && !mClosingApps.contains(token)
Wale Ogunwale1666e312016-12-16 11:27:18 -08004125 && (!token.mIsExiting || token.isEmpty())) {
4126 // Make sure there is no animation running on this token, so any windows
4127 // associated with it will be removed as soon as their animations are
4128 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02004129 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004130 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
4131 "performLayout: App token exiting now removed" + token);
4132 token.removeIfPossible();
4133 }
4134 }
4135 }
4136 }
4137
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004138 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004139 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004140 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
4141 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004142 // Apps and their containers are not allowed to specify an orientation while the
Wale Ogunwaled0c225c2018-06-15 14:47:46 -07004143 // docked or freeform stack is visible...except for the home stack if the docked
4144 // stack is minimized and it actually set something and the bounds is different from
4145 // the display.
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004146 if (mHomeStack != null && mHomeStack.isVisible()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004147 && mDividerControllerLocked.isMinimizedDock()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004148 && !(mDividerControllerLocked.isHomeStackResizable()
4149 && mHomeStack.matchParentBounds())) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004150 final int orientation = mHomeStack.getOrientation();
4151 if (orientation != SCREEN_ORIENTATION_UNSET) {
4152 return orientation;
4153 }
4154 }
4155 return SCREEN_ORIENTATION_UNSPECIFIED;
4156 }
4157
4158 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07004159 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
4160 PackageManager.FEATURE_AUTOMOTIVE);
4161 if (isCar) {
4162 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
4163 // allow anything but the default orientation.
4164 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004165 "Forcing UNSPECIFIED orientation in car for display id=" + mDisplayId
4166 + ". Ignoring " + orientation);
Dean Harding3e5a1522017-10-06 09:19:01 -07004167 return SCREEN_ORIENTATION_UNSPECIFIED;
4168 }
4169
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004170 if (orientation != SCREEN_ORIENTATION_UNSET
4171 && orientation != SCREEN_ORIENTATION_BEHIND) {
4172 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004173 "App is requesting an orientation, return " + orientation
4174 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004175 return orientation;
4176 }
4177
4178 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004179 "No app is requesting an orientation, return " + mLastOrientation
4180 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004181 // The next app has not been requested to be visible, so we keep the current orientation
4182 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08004183 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004184 }
Robert Carrb1579c82017-09-05 14:54:47 -07004185
4186 @Override
4187 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004188 assignStackOrdering(t);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004189
Robert Carr2f8aa392018-01-31 14:46:51 -08004190 for (int i = 0; i < mChildren.size(); i++) {
4191 final TaskStack s = mChildren.get(i);
4192 s.assignChildLayers(t);
4193 }
4194 }
4195
4196 void assignStackOrdering(SurfaceControl.Transaction t) {
Jorim Jaggibe418292018-03-26 16:14:12 +02004197
Robert Carrf7a7ca82017-12-06 13:17:07 -08004198 final int HOME_STACK_STATE = 0;
4199 final int NORMAL_STACK_STATE = 1;
4200 final int ALWAYS_ON_TOP_STATE = 2;
4201
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004202 int layer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08004203 int layerForAnimationLayer = 0;
Jorim Jaggibe418292018-03-26 16:14:12 +02004204 int layerForBoostedAnimationLayer = 0;
Jorim Jaggi391790622018-04-18 15:30:44 +02004205 int layerForHomeAnimationLayer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08004206
Robert Carrf7a7ca82017-12-06 13:17:07 -08004207 for (int state = 0; state <= ALWAYS_ON_TOP_STATE; state++) {
4208 for (int i = 0; i < mChildren.size(); i++) {
4209 final TaskStack s = mChildren.get(i);
Robert Carr2f8aa392018-01-31 14:46:51 -08004210 if (state == HOME_STACK_STATE && !s.isActivityTypeHome()) {
4211 continue;
4212 } else if (state == NORMAL_STACK_STATE && (s.isActivityTypeHome()
4213 || s.isAlwaysOnTop())) {
4214 continue;
4215 } else if (state == ALWAYS_ON_TOP_STATE && !s.isAlwaysOnTop()) {
4216 continue;
4217 }
4218 s.assignLayer(t, layer++);
4219 if (s.inSplitScreenWindowingMode() && mSplitScreenDividerAnchor != null) {
4220 t.setLayer(mSplitScreenDividerAnchor, layer++);
4221 }
Winson Chungd41f71d2018-03-16 15:26:07 -07004222 if ((s.isTaskAnimating() || s.isAppAnimating())
4223 && state != ALWAYS_ON_TOP_STATE) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004224 // Ensure the animation layer ends up above the
4225 // highest animating stack and no higher.
4226 layerForAnimationLayer = layer++;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004227 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004228 if (state != ALWAYS_ON_TOP_STATE) {
4229 layerForBoostedAnimationLayer = layer++;
4230 }
Robert Carrf7a7ca82017-12-06 13:17:07 -08004231 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004232 if (state == HOME_STACK_STATE) {
4233 layerForHomeAnimationLayer = layer++;
4234 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004235 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004236 if (mAppAnimationLayer != null) {
4237 t.setLayer(mAppAnimationLayer, layerForAnimationLayer);
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004238 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004239 if (mBoostedAppAnimationLayer != null) {
4240 t.setLayer(mBoostedAppAnimationLayer, layerForBoostedAnimationLayer);
4241 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004242 if (mHomeAppAnimationLayer != null) {
4243 t.setLayer(mHomeAppAnimationLayer, layerForHomeAnimationLayer);
4244 }
Robert Carrb1579c82017-09-05 14:54:47 -07004245 }
4246
4247 @Override
Jorim Jaggi391790622018-04-18 15:30:44 +02004248 SurfaceControl getAppAnimationLayer(@AnimationLayer int animationLayer) {
4249 switch (animationLayer) {
4250 case ANIMATION_LAYER_BOOSTED:
4251 return mBoostedAppAnimationLayer;
4252 case ANIMATION_LAYER_HOME:
4253 return mHomeAppAnimationLayer;
4254 case ANIMATION_LAYER_STANDARD:
4255 default:
4256 return mAppAnimationLayer;
4257 }
chaviw23ee71c2017-12-18 11:29:41 -08004258 }
4259
Robert Carrf7a7ca82017-12-06 13:17:07 -08004260 SurfaceControl getSplitScreenDividerAnchor() {
4261 return mSplitScreenDividerAnchor;
4262 }
4263
chaviw23ee71c2017-12-18 11:29:41 -08004264 @Override
Robert Carrb1579c82017-09-05 14:54:47 -07004265 void onParentSet() {
4266 super.onParentSet();
4267 if (getParent() != null) {
chaviw23ee71c2017-12-18 11:29:41 -08004268 mAppAnimationLayer = makeChildSurface(null)
4269 .setName("animationLayer")
4270 .build();
Jorim Jaggibe418292018-03-26 16:14:12 +02004271 mBoostedAppAnimationLayer = makeChildSurface(null)
4272 .setName("boostedAnimationLayer")
4273 .build();
Jorim Jaggi391790622018-04-18 15:30:44 +02004274 mHomeAppAnimationLayer = makeChildSurface(null)
4275 .setName("homeAnimationLayer")
4276 .build();
Robert Carrf7a7ca82017-12-06 13:17:07 -08004277 mSplitScreenDividerAnchor = makeChildSurface(null)
4278 .setName("splitScreenDividerAnchor")
4279 .build();
4280 getPendingTransaction()
4281 .show(mAppAnimationLayer)
Jorim Jaggibe418292018-03-26 16:14:12 +02004282 .show(mBoostedAppAnimationLayer)
Jorim Jaggi391790622018-04-18 15:30:44 +02004283 .show(mHomeAppAnimationLayer)
Robert Carrf7a7ca82017-12-06 13:17:07 -08004284 .show(mSplitScreenDividerAnchor);
chaviw23ee71c2017-12-18 11:29:41 -08004285 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07004286 } else {
chaviw23ee71c2017-12-18 11:29:41 -08004287 mAppAnimationLayer.destroy();
4288 mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02004289 mBoostedAppAnimationLayer.destroy();
4290 mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02004291 mHomeAppAnimationLayer.destroy();
4292 mHomeAppAnimationLayer = null;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004293 mSplitScreenDividerAnchor.destroy();
4294 mSplitScreenDividerAnchor = null;
Robert Carrb1579c82017-09-05 14:54:47 -07004295 }
4296 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004297 }
4298
Robert Carree4d4b92017-11-22 12:21:46 -08004299 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004300 AboveAppWindowContainers(String name, WindowManagerService service) {
4301 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08004302 }
4303
Robert Carrb9506032018-02-13 13:54:00 -08004304 @Override
Vishnu Nair83537a72018-07-19 21:27:48 -07004305 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
4306 final SurfaceControl.Builder builder = super.makeChildSurface(child);
4307 if (child instanceof WindowToken && ((WindowToken) child).mRoundedCornerOverlay) {
4308 // To draw above the ColorFade layer during the screen off transition, the
4309 // rounded corner overlays need to be at the root of the surface hierarchy.
4310 // TODO: move the ColorLayer into the display overlay layer such that this is not
4311 // necessary anymore.
4312 builder.setParent(null);
4313 }
4314 return builder;
4315 }
4316
4317 @Override
Robert Carrb9506032018-02-13 13:54:00 -08004318 void assignChildLayers(SurfaceControl.Transaction t) {
4319 assignChildLayers(t, null /* imeContainer */);
4320 }
4321
Robert Carree4d4b92017-11-22 12:21:46 -08004322 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
4323 boolean needAssignIme = imeContainer != null
4324 && imeContainer.getSurfaceControl() != null;
4325 for (int j = 0; j < mChildren.size(); ++j) {
4326 final WindowToken wt = mChildren.get(j);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004327
4328 // See {@link mSplitScreenDividerAnchor}
4329 if (wt.windowType == TYPE_DOCK_DIVIDER) {
4330 wt.assignRelativeLayer(t, mTaskStackContainers.getSplitScreenDividerAnchor(), 1);
4331 continue;
4332 }
Vishnu Nair83537a72018-07-19 21:27:48 -07004333 if (wt.mRoundedCornerOverlay) {
4334 wt.assignLayer(t, WindowManagerPolicy.COLOR_FADE_LAYER + 1);
4335 continue;
4336 }
Robert Carree4d4b92017-11-22 12:21:46 -08004337 wt.assignLayer(t, j);
4338 wt.assignChildLayers(t);
4339
4340 int layer = mService.mPolicy.getWindowLayerFromTypeLw(
4341 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08004342
4343 if (needAssignIme && layer >= mService.mPolicy.getWindowLayerFromTypeLw(
4344 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004345 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08004346 needAssignIme = false;
4347 }
4348 }
4349 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004350 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08004351 }
4352 }
4353 }
4354
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004355 /**
4356 * Window container class that contains all containers on this display that are not related to
4357 * Apps. E.g. status bar.
4358 */
Robert Carree4d4b92017-11-22 12:21:46 -08004359 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07004360 /**
4361 * Compares two child window tokens returns -1 if the first is lesser than the second in
4362 * terms of z-order and 1 otherwise.
4363 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004364 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07004365 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004366 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
4367 token1.mOwnerCanManageAppTokens)
4368 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
4369 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004370
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004371 private final Predicate<WindowState> mGetOrientingWindow = w -> {
4372 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
4373 return false;
4374 }
4375 final int req = w.mAttrs.screenOrientation;
4376 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
4377 || req == SCREEN_ORIENTATION_UNSET) {
4378 return false;
4379 }
4380 return true;
4381 };
4382
Wale Ogunwale3a931692016-11-02 16:49:48 -07004383 private final String mName;
chaviwf29223e2018-01-31 13:23:51 -08004384 private final Dimmer mDimmer = new Dimmer(this);
4385 private final Rect mTmpDimBoundsRect = new Rect();
4386
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004387 NonAppWindowContainers(String name, WindowManagerService service) {
4388 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07004389 mName = name;
4390 }
4391
4392 void addChild(WindowToken token) {
4393 addChild(token, mWindowComparator);
4394 }
4395
4396 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004397 int getOrientation() {
4398 final WindowManagerPolicy policy = mService.mPolicy;
4399 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004400 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004401
4402 if (win != null) {
4403 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004404 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004405 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07004406 if (mService.mKeyguardGoingAway) {
4407 // Keyguard can't affect the orientation if it is going away...
4408 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
4409 return SCREEN_ORIENTATION_UNSET;
4410 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004411 }
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004412 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req
4413 + " for display id=" + mDisplayId);
Andrii Kulian8ee72852017-03-10 10:36:45 -08004414 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004415 }
4416
Andrii Kulian8ee72852017-03-10 10:36:45 -08004417 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004418
Jorim Jaggi1c530592018-04-06 15:11:47 +02004419 // Only allow force setting the orientation when all unknown visibilities have been
4420 // resolved, as otherwise we just may be starting another occluding activity.
4421 final boolean isUnoccluding =
lumark588a3e82018-07-20 18:53:54 +08004422 mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
4423 && mUnknownAppVisibilityController.allResolved();
Jorim Jaggi1c530592018-04-06 15:11:47 +02004424 if (policy.isKeyguardShowingAndNotOccluded() || isUnoccluding) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004425 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004426 }
4427
4428 return SCREEN_ORIENTATION_UNSET;
4429 }
4430
4431 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07004432 String getName() {
4433 return mName;
4434 }
chaviwf29223e2018-01-31 13:23:51 -08004435
4436 @Override
4437 Dimmer getDimmer() {
4438 return mDimmer;
4439 }
4440
4441 @Override
4442 void prepareSurfaces() {
4443 mDimmer.resetDimStates();
4444 super.prepareSurfaces();
4445 getBounds(mTmpDimBoundsRect);
4446
4447 if (mDimmer.updateDims(getPendingTransaction(), mTmpDimBoundsRect)) {
4448 scheduleAnimation();
4449 }
4450 }
Robert Carr3b716242016-08-16 16:02:21 -07004451 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01004452
Robert Carr9034d962018-01-04 18:27:42 -08004453 private class NonMagnifiableWindowContainers extends NonAppWindowContainers {
4454 NonMagnifiableWindowContainers(String name, WindowManagerService service) {
4455 super(name, service);
4456 }
4457
4458 @Override
4459 void applyMagnificationSpec(Transaction t, MagnificationSpec spec) {
4460 }
4461 };
4462
Robert Carrb1579c82017-09-05 14:54:47 -07004463 SurfaceControl.Builder makeSurface(SurfaceSession s) {
4464 return mService.makeSurfaceBuilder(s)
4465 .setParent(mWindowingLayer);
4466 }
4467
4468 @Override
4469 SurfaceSession getSession() {
4470 return mSession;
4471 }
4472
4473 @Override
4474 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07004475 SurfaceSession s = child != null ? child.getSession() : getSession();
4476 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrb1579c82017-09-05 14:54:47 -07004477 b.setSize(mSurfaceSize, mSurfaceSize);
Robert Carrf59b8dd2017-10-02 18:58:36 -07004478
4479 if (child == null) {
4480 return b;
4481 }
4482
Robert Carree4d4b92017-11-22 12:21:46 -08004483 return b.setName(child.getName())
4484 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07004485 }
4486
4487 /**
4488 * The makeSurface variants are for use by the window-container
4489 * hierarchy. makeOverlay here is a function for various non windowing
4490 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
4491 * and other potpourii.
4492 */
4493 SurfaceControl.Builder makeOverlay() {
4494 return mService.makeSurfaceBuilder(mSession)
4495 .setParent(mOverlayLayer);
4496 }
4497
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004498 /**
4499 * Reparents the given surface to mOverlayLayer.
4500 */
4501 void reparentToOverlay(Transaction transaction, SurfaceControl surface) {
4502 transaction.reparent(surface, mOverlayLayer.getHandle());
4503 }
4504
Robert Carrb1579c82017-09-05 14:54:47 -07004505 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carr24be9ab2018-04-30 17:54:53 -07004506 if (spec.scale != 1.0) {
4507 mMagnificationSpec = spec;
4508 } else {
4509 mMagnificationSpec = null;
4510 }
4511
Robert Carrf59b8dd2017-10-02 18:58:36 -07004512 applyMagnificationSpec(getPendingTransaction(), spec);
4513 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07004514 }
4515
Robert Carr24be9ab2018-04-30 17:54:53 -07004516 void reapplyMagnificationSpec() {
4517 if (mMagnificationSpec != null) {
4518 applyMagnificationSpec(getPendingTransaction(), mMagnificationSpec);
4519 }
4520 }
4521
Robert Carrb1579c82017-09-05 14:54:47 -07004522 @Override
4523 void onParentSet() {
4524 // Since we are the top of the SurfaceControl hierarchy here
4525 // we create the root surfaces explicitly rather than chaining
4526 // up as the default implementation in onParentSet does. So we
4527 // explicitly do NOT call super here.
4528 }
4529
4530 @Override
4531 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07004532
4533 // These are layers as children of "mWindowingLayer"
4534 mBelowAppWindowsContainers.assignLayer(t, 0);
4535 mTaskStackContainers.assignLayer(t, 1);
4536 mAboveAppWindowsContainers.assignLayer(t, 2);
4537
lumarkff0ab692018-11-05 20:32:30 +08004538 final WindowState imeTarget = mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08004539 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08004540
Robert Carree4d4b92017-11-22 12:21:46 -08004541 // In the case where we have an IME target that is not in split-screen
4542 // mode IME assignment is easy. We just need the IME to go directly above
4543 // the target. This way children of the target will naturally go above the IME
4544 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08004545 //
Robert Carree4d4b92017-11-22 12:21:46 -08004546 // In the case of split-screen windowing mode, we need to elevate the IME above the
4547 // docked divider while keeping the app itself below the docked divider, so instead
4548 // we use relative layering of the IME targets child windows, and place the
4549 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08004550 //
Robert Carr234b6332018-03-20 13:38:16 -07004551 // In the case the IME target is animating, the animation Z order may be different
4552 // than the WindowContainer Z order, so it's difficult to be sure we have the correct
4553 // IME target. In this case we just layer the IME over all transitions by placing it in the
4554 // above applications layer.
4555 //
Robert Carree4d4b92017-11-22 12:21:46 -08004556 // In the case where we have no IME target we assign it where it's base layer would
4557 // place it in the AboveAppWindowContainers.
Robert Carr234b6332018-03-20 13:38:16 -07004558 if (imeTarget != null && !(imeTarget.inSplitScreenWindowingMode()
4559 || imeTarget.mToken.isAppAnimating())
Robert Carree4d4b92017-11-22 12:21:46 -08004560 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004561 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07004562 // TODO: We need to use an extra level on the app surface to ensure
4563 // this is always above SurfaceView but always below attached window.
4564 1);
Robert Carree4d4b92017-11-22 12:21:46 -08004565 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07004566 }
4567
4568 // Above we have assigned layers to our children, now we ask them to assign
4569 // layers to their children.
4570 mBelowAppWindowsContainers.assignChildLayers(t);
4571 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08004572 mAboveAppWindowsContainers.assignChildLayers(t,
4573 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07004574 mImeWindowsContainers.assignChildLayers(t);
4575 }
4576
4577 /**
4578 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
4579 * that the IME target is one of the docked applications. We'd like the docked divider to be
4580 * above both of the applications, and we'd like the IME to be above the docked divider.
4581 * However we need child windows of the applications to be above the IME (Text drag handles).
4582 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
4583 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
Tiger Huang7c610aa2018-10-27 00:01:01 +08004584 * with {@link WindowState#assignLayer}
Robert Carrb1579c82017-09-05 14:54:47 -07004585 */
4586 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
Robert Carrbb5de662018-04-26 13:29:51 -07004587 child.assignRelativeLayer(t, mImeWindowsContainers.getSurfaceControl(), 1);
Robert Carrb1579c82017-09-05 14:54:47 -07004588 }
4589
4590 @Override
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004591 void prepareSurfaces() {
4592 final ScreenRotationAnimation screenRotationAnimation =
4593 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
4594 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
4595 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
4596 mPendingTransaction.setMatrix(mWindowingLayer,
4597 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
4598 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
4599 mPendingTransaction.setPosition(mWindowingLayer,
4600 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
4601 mPendingTransaction.setAlpha(mWindowingLayer,
4602 screenRotationAnimation.getEnterTransformation().getAlpha());
4603 }
Chavi Weingartenb736e322018-02-23 00:27:54 +00004604
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004605 super.prepareSurfaces();
4606 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004607
Jorim Jaggibe418292018-03-26 16:14:12 +02004608 void assignStackOrdering() {
4609 mTaskStackContainers.assignStackOrdering(getPendingTransaction());
Robert Carr2f8aa392018-01-31 14:46:51 -08004610 }
Chavi Weingarten3a748552018-05-14 17:32:42 +00004611
4612 /**
4613 * Increment the deferral count to determine whether to update the IME target.
4614 */
4615 void deferUpdateImeTarget() {
4616 mDeferUpdateImeTargetCount++;
4617 }
4618
4619 /**
4620 * Decrement the deferral count to determine whether to update the IME target. If the count
4621 * reaches 0, a new ime target will get computed.
4622 */
4623 void continueUpdateImeTarget() {
4624 if (mDeferUpdateImeTargetCount == 0) {
4625 return;
4626 }
4627
4628 mDeferUpdateImeTargetCount--;
4629 if (mDeferUpdateImeTargetCount == 0) {
4630 computeImeTarget(true /* updateImeTarget */);
4631 }
4632 }
4633
4634 /**
4635 * @return Whether a new IME target should be computed.
4636 */
4637 private boolean canUpdateImeTarget() {
4638 return mDeferUpdateImeTargetCount == 0;
4639 }
Arthur Hung95b38a92018-07-20 18:56:12 +08004640
4641 InputMonitor getInputMonitor() {
4642 return mInputMonitor;
4643 }
Jorim Jaggif1292892018-09-10 11:58:13 +02004644
4645 /**
4646 * @return Cached value whether we told display manager that we have content.
4647 */
4648 boolean getLastHasContent() {
4649 return mLastHasContent;
4650 }
Arthur Hungbe5ce212018-09-13 18:41:56 +08004651
4652 void registerPointerEventListener(@NonNull PointerEventListener listener) {
4653 if (mPointerEventDispatcher != null) {
4654 mPointerEventDispatcher.registerInputEventListener(listener);
4655 }
4656 }
4657
4658 void unregisterPointerEventListener(@NonNull PointerEventListener listener) {
4659 if (mPointerEventDispatcher != null) {
4660 mPointerEventDispatcher.unregisterInputEventListener(listener);
4661 }
4662 }
lumark588a3e82018-07-20 18:53:54 +08004663
4664 void prepareAppTransition(@WindowManager.TransitionType int transit,
4665 boolean alwaysKeepCurrent, @WindowManager.TransitionFlags int flags,
4666 boolean forceOverride) {
4667 final boolean prepared = mAppTransition.prepareAppTransitionLocked(
4668 transit, alwaysKeepCurrent, flags, forceOverride);
4669 if (prepared && okToAnimate()) {
4670 mSkipAppTransitionAnimation = false;
4671 }
4672 }
4673
4674 void executeAppTransition() {
4675 if (mAppTransition.isTransitionSet()) {
4676 if (DEBUG_APP_TRANSITIONS) {
4677 Slog.w(TAG_WM, "Execute app transition: " + mAppTransition + ", displayId: "
4678 + mDisplayId + " Callers=" + Debug.getCallers(5));
4679 }
4680 mAppTransition.setReady();
4681 mService.mWindowPlacerLocked.requestTraversal();
4682 }
4683 }
4684
4685 /**
4686 * Update pendingLayoutChanges after app transition has finished.
4687 */
4688 void handleAnimatingStoppedAndTransition() {
4689 int changes = 0;
4690
4691 mAppTransition.setIdle();
4692
4693 for (int i = mNoAnimationNotifyOnTransitionFinished.size() - 1; i >= 0; i--) {
4694 final IBinder token = mNoAnimationNotifyOnTransitionFinished.get(i);
4695 mAppTransition.notifyAppTransitionFinishedLocked(token);
4696 }
4697 mNoAnimationNotifyOnTransitionFinished.clear();
4698
4699 mWallpaperController.hideDeferredWallpapersIfNeeded();
4700
4701 onAppTransitionDone();
4702
4703 changes |= FINISH_LAYOUT_REDO_LAYOUT;
4704 if (DEBUG_WALLPAPER_LIGHT) {
4705 Slog.v(TAG_WM, "Wallpaper layer changed: assigning layers + relayout");
4706 }
4707 computeImeTarget(true /* updateImeTarget */);
wilsonshihc32538e2018-11-07 17:27:34 +08004708 mWallpaperMayChange = true;
lumark588a3e82018-07-20 18:53:54 +08004709 // Since the window list has been rebuilt, focus might have to be recomputed since the
4710 // actual order of windows might have changed again.
4711 mService.mFocusMayChange = true;
4712
4713 pendingLayoutChanges |= changes;
4714 }
Andrii Kulian15cfb422018-11-07 13:38:49 -08004715
4716 /**
4717 * @see Display#FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS
4718 */
4719 boolean supportsSystemDecorations() {
4720 // TODO(b/114338689): Read the setting from DisplaySettings.
4721 return mDisplay.supportsSystemDecorations()
4722 // TODO (b/111363427): Remove this and set the new FLAG_SHOULD_SHOW_LAUNCHER flag
4723 // (b/114338689) whenever vr 2d display id is set.
4724 || mDisplayId == mService.mVr2dDisplayId
4725 || mService.mForceDesktopModeOnExternalDisplays;
4726 }
Craig Mautner59c00972012-07-30 12:10:24 -07004727}