blob: 478340d85d017ea423cd2268785a387e9dc77fb7 [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
Vishnu Nairba183352018-11-21 11:16:49 -0800355 /**
356 * Used to gate application window layout until we have sent the complete configuration.
357 * TODO: There are still scenarios where we may be out of sync with the client. Ideally
358 * we want to replace this flag with a mechanism that will confirm the configuration
359 * applied by the client is the one expected by the system server.
360 */
361 boolean mWaitingForConfig;
362
Andrii Kulian839def92016-11-02 10:58:58 -0700363 // TODO(multi-display): remove some of the usages.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800364 @VisibleForTesting
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800365 boolean isDefaultDisplay;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800366
Andrii Kulianf0379de2018-03-14 16:24:07 -0700367 /**
368 * Flag indicating whether WindowManager should override info for this display in
369 * DisplayManager.
370 */
371 boolean mShouldOverrideDisplayConfiguration = true;
Craig Mautner39834192012-09-02 07:47:24 -0700372
Craig Mautnerdc548482014-02-05 13:35:24 -0800373 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700374 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800375
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700376 /** Detect user tapping outside of current focused task bounds .*/
377 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700378
Craig Mautner6601b7b2013-04-29 10:29:11 -0700379 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700380 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700381
Craig Mautner6601b7b2013-04-29 10:29:11 -0700382 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800383 private final Rect mTmpRect = new Rect();
384 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700385 private final RectF mTmpRectF = new RectF();
386 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800387 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700388
Issei Suzuki43190bd2018-08-20 17:28:41 +0200389
Bryce Leef3c6a472017-11-14 14:53:06 -0800390 /** Used for handing back size of display */
391 private final Rect mTmpBounds = new Rect();
392
Craig Mautner95da1082014-02-24 17:54:35 -0800393 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700394 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800395
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700396 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700397 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700398
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800399 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800400 /** A collection of windows that provide tap exclude regions inside of them. */
401 final ArraySet<WindowState> mTapExcludeProvidingWindows = new ArraySet<>();
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800402
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000403 private boolean mHaveBootMsg = false;
404 private boolean mHaveApp = false;
405 private boolean mHaveWallpaper = false;
406 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700407
408 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
409
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700410 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
411 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000412 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
413 new ApplySurfaceChangesTransactionState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700414
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700415 // True if this display is in the process of being removed. Used to determine if the removal of
416 // the display's direct children should be allowed.
417 private boolean mRemovingDisplay = false;
418
Bryce Leed1871262017-06-12 14:12:29 -0700419 // {@code false} if this display is in the processing of being created.
420 private boolean mDisplayReady = false;
421
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800422 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700423
wilsonshihc32538e2018-11-07 17:27:34 +0800424 boolean mWallpaperMayChange = false;
425
Robert Carrb1579c82017-09-05 14:54:47 -0700426 private final SurfaceSession mSession = new SurfaceSession();
427
428 /**
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800429 * Window that is currently interacting with the user. This window is responsible for receiving
430 * key events and pointer events from the user.
431 */
432 WindowState mCurrentFocus = null;
433
434 /**
435 * The last focused window that we've notified the client that the focus is changed.
436 */
437 WindowState mLastFocus = null;
438
439 /**
440 * Windows that have lost input focus and are waiting for the new focus window to be displayed
441 * before they are told about this.
442 */
443 ArrayList<WindowState> mLosingFocus = new ArrayList<>();
444
445 /**
446 * The foreground app of this display. Windows below this app cannot be the focused window. If
447 * the user taps on the area outside of the task of the focused app, we will notify AM about the
448 * new task the user wants to interact with.
449 */
450 AppWindowToken mFocusedApp = null;
451
452 /** Windows added since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
453 final ArrayList<WindowState> mWinAddedSinceNullFocus = new ArrayList<>();
454
455 /** Windows removed since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
456 final ArrayList<WindowState> mWinRemovedSinceNullFocus = new ArrayList<>();
457
458 /**
Robert Carrb1579c82017-09-05 14:54:47 -0700459 * We organize all top-level Surfaces in to the following layers.
460 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800461 * and omitted from Screen-Magnification, for example the strict mode flash or
462 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700463 * {@link #mWindowingLayer} contains everything else.
464 */
465 private SurfaceControl mOverlayLayer;
466
467 /**
468 * See {@link #mOverlayLayer}
469 */
470 private SurfaceControl mWindowingLayer;
471
472 /**
473 * Specifies the size of the surfaces in {@link #mOverlayLayer} and {@link #mWindowingLayer}.
474 * <p>
475 * For these surfaces currently we use a surface based on the larger of width or height so we
476 * don't have to resize when rotating the display.
477 */
478 private int mSurfaceSize;
479
Adrian Roos5251b1d2018-03-23 18:57:43 +0100480 /**
481 * Sequence number for the current layout pass.
482 */
483 int mLayoutSeq = 0;
484
Chavi Weingarten3a748552018-05-14 17:32:42 +0000485 /**
486 * Specifies the count to determine whether to defer updating the IME target until ready.
487 */
488 private int mDeferUpdateImeTargetCount;
489
Jorim Jaggi9af095b2017-12-12 17:18:57 +0100490 /** Temporary float array to retrieve 3x3 matrix values. */
491 private final float[] mTmpFloats = new float[9];
492
Robert Carr24be9ab2018-04-30 17:54:53 -0700493 private MagnificationSpec mMagnificationSpec;
494
Arthur Hung95b38a92018-07-20 18:56:12 +0800495 private InputMonitor mInputMonitor;
496
Jorim Jaggif1292892018-09-10 11:58:13 +0200497 /** Caches the value whether told display manager that we have content. */
498 private boolean mLastHasContent;
499
Issei Suzuki43190bd2018-08-20 17:28:41 +0200500 private DisplayRotationUtil mRotationUtil = new DisplayRotationUtil();
501
lumark90120a82018-08-15 00:33:03 +0800502 /**
503 * The input method window for this display.
504 */
505 WindowState mInputMethodWindow;
506
lumarkff0ab692018-11-05 20:32:30 +0800507 /**
508 * This just indicates the window the input method is on top of, not
509 * necessarily the window its input is going to.
510 */
511 WindowState mInputMethodTarget;
512
513 /** If true hold off on modifying the animation layer of mInputMethodTarget */
514 boolean mInputMethodTargetWaitingAnim;
515
Arthur Hungbe5ce212018-09-13 18:41:56 +0800516 private final PointerEventDispatcher mPointerEventDispatcher;
517
Tiger Huang7c610aa2018-10-27 00:01:01 +0800518 // Last systemUiVisibility we received from status bar.
519 private int mLastStatusBarVisibility = 0;
520 // Last systemUiVisibility we dispatched to windows.
521 private int mLastDispatchedSystemUiVisibility = 0;
522
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800523 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
524 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800525 final AppWindowToken atoken = w.mAppToken;
526 if (winAnimator.mDrawState == READY_TO_SHOW) {
Jorim Jaggiaf0d6d22018-06-08 15:25:35 +0200527 if (atoken == null || atoken.canShowWindows()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800528 if (w.performShowLocked()) {
529 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
530 if (DEBUG_LAYOUT_REPEATS) {
531 mService.mWindowPlacerLocked.debugLayoutRepeats(
532 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
533 }
534 }
535 }
536 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800537 };
538
539 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
540 final WindowStateAnimator winAnimator = w.mWinAnimator;
541 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
542 return;
543 }
544
Jorim Jaggi8f520872018-08-14 17:00:20 +0200545 // If this window is animating, ensure the animation background is set.
546 final AnimationAdapter anim = w.mAppToken != null
547 ? w.mAppToken.getAnimation()
548 : w.getAnimation();
549 if (anim != null) {
550 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800551 if (color != 0) {
552 final TaskStack stack = w.getStack();
553 if (stack != null) {
554 stack.setAnimationBackground(winAnimator, color);
555 }
556 }
557 }
558 };
559
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800560 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
561 final int lostFocusUid = mTmpWindow.mOwnerUid;
562 final Handler handler = mService.mH;
563 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
564 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
565 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
566 w.mAttrs.hideTimeoutMilliseconds);
567 }
568 }
569 };
570
571 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800572 final AppWindowToken focusedApp = mFocusedApp;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800573 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
574 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
575
576 if (!w.canReceiveKeys()) {
577 return false;
578 }
579
580 final AppWindowToken wtoken = w.mAppToken;
581
582 // If this window's application has been removed, just skip it.
583 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
584 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
585 + (wtoken.removed ? "removed" : "sendingToBottom"));
586 return false;
587 }
588
589 if (focusedApp == null) {
590 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
591 + " using new focus @ " + w);
592 mTmpWindow = w;
593 return true;
594 }
595
596 if (!focusedApp.windowsAreFocusable()) {
597 // Current focused app windows aren't focusable...
598 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
599 + " focusable using new focus @ " + w);
600 mTmpWindow = w;
601 return true;
602 }
603
604 // Descend through all of the app tokens and find the first that either matches
605 // win.mAppToken (return win) or mFocusedApp (return null).
606 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
607 if (focusedApp.compareTo(wtoken) > 0) {
608 // App stack below focused app stack. No focus for you!!!
609 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
610 "findFocusedWindow: Reached focused app=" + focusedApp);
611 mTmpWindow = null;
612 return true;
613 }
614 }
615
616 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
617 mTmpWindow = w;
618 return true;
619 };
620
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800621 private final Consumer<WindowState> mPerformLayout = w -> {
622 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
623 // wasting time and funky changes while a window is animating away.
624 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
625 || w.isGoneForLayoutLw();
626
627 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
628 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
629 + " mLayoutAttached=" + w.mLayoutAttached
630 + " screen changed=" + w.isConfigChanged());
631 final AppWindowToken atoken = w.mAppToken;
632 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200633 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800634 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
635 + " parentHidden=" + w.isParentWindowHidden());
636 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200637 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800638 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
639 + " parentHidden=" + w.isParentWindowHidden());
640 }
641
642 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
643 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
644 // since that means "perform layout as normal, just don't display").
645 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
646 || ((w.isConfigChanged() || w.setReportResizeHints())
647 && !w.isGoneForLayoutLw() &&
648 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
649 (w.mHasSurface && w.mAppToken != null &&
650 w.mAppToken.layoutConfigChanges)))) {
651 if (!w.mLayoutAttached) {
652 if (mTmpInitial) {
653 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700654 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800655 }
656 if (w.mAttrs.type == TYPE_DREAM) {
657 // Don't layout windows behind a dream, so that if it does stuff like hide
658 // the status bar we won't get a bad transition when it goes away.
659 mTmpWindow = w;
660 }
661 w.mLayoutNeeded = false;
662 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200663 final boolean firstLayout = !w.isLaidOut();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800664 getDisplayPolicy().layoutWindowLw(w, null, mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100665 w.mLayoutSeq = mLayoutSeq;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800666
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200667 // If this is the first layout, we need to initialize the last inset values as
668 // otherwise we'd immediately cause an unnecessary resize.
669 if (firstLayout) {
670 w.updateLastInsetValues();
671 }
672
Adrian Roos23df3a32018-03-15 15:41:13 +0100673 if (w.mAppToken != null) {
674 w.mAppToken.layoutLetterbox(w);
675 }
676
chaviw492139a2018-07-16 16:07:35 -0700677 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700678 + " mContainingFrame=" + w.getContainingFrame()
679 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800680 }
681 }
682 };
683
684 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
685 if (w.mLayoutAttached) {
686 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
687 + " mViewVisibility=" + w.mViewVisibility
688 + " mRelayoutCalled=" + w.mRelayoutCalled);
689 // If this view is GONE, then skip it -- keep the current frame, and let the caller
690 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
691 // windows, since that means "perform layout as normal, just don't display").
692 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
693 return;
694 }
695 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
696 || w.mLayoutNeeded) {
697 if (mTmpInitial) {
698 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700699 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800700 }
701 w.mLayoutNeeded = false;
702 w.prelayout();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800703 getDisplayPolicy().layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100704 w.mLayoutSeq = mLayoutSeq;
chaviw492139a2018-07-16 16:07:35 -0700705 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700706 + " mContainingFrame=" + w.getContainingFrame()
707 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800708 }
709 } else if (w.mAttrs.type == TYPE_DREAM) {
710 // Don't layout windows behind a dream, so that if it does stuff like hide the
711 // status bar we won't get a bad transition when it goes away.
712 mTmpWindow = mTmpWindow2;
713 }
714 };
715
716 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
717 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
718 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
719 return w.canBeImeTarget();
720 };
721
722 private final Consumer<WindowState> mApplyPostLayoutPolicy =
Tiger Huang7c610aa2018-10-27 00:01:01 +0800723 w -> getDisplayPolicy().applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
lumarkff0ab692018-11-05 20:32:30 +0800724 mInputMethodTarget);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800725
726 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
727 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
728 final boolean obscuredChanged = w.mObscured !=
729 mTmpApplySurfaceChangesTransactionState.obscured;
730 final RootWindowContainer root = mService.mRoot;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800731
732 // Update effect.
733 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
734 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
735 final boolean isDisplayed = w.isDisplayedLw();
736
737 if (isDisplayed && w.isObscuringDisplay()) {
738 // This window completely covers everything behind it, so we want to leave all
739 // of them as undimmed (for performance reasons).
740 root.mObscuringWindow = w;
741 mTmpApplySurfaceChangesTransactionState.obscured = true;
742 }
743
744 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
745 root.handleNotObscuredLocked(w,
746 mTmpApplySurfaceChangesTransactionState.obscured,
747 mTmpApplySurfaceChangesTransactionState.syswin);
748
749 if (w.mHasSurface && isDisplayed) {
750 final int type = w.mAttrs.type;
751 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
752 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
753 mTmpApplySurfaceChangesTransactionState.syswin = true;
754 }
755 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
756 && w.mAttrs.preferredRefreshRate != 0) {
757 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
758 = w.mAttrs.preferredRefreshRate;
759 }
760 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
761 && w.mAttrs.preferredDisplayModeId != 0) {
762 mTmpApplySurfaceChangesTransactionState.preferredModeId
763 = w.mAttrs.preferredDisplayModeId;
764 }
765 }
766 }
767
wilsonshihc32538e2018-11-07 17:27:34 +0800768 if (obscuredChanged && w.isVisibleLw() && mWallpaperController.isWallpaperTarget(w)) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800769 // This is the wallpaper target and its obscured state changed... make sure the
770 // current wallpaper's visibility has been updated accordingly.
771 mWallpaperController.updateWallpaperVisibility();
772 }
773
chaviw161ea3e2018-01-31 12:01:12 -0800774 w.handleWindowMovedIfNeeded();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800775
776 final WindowStateAnimator winAnimator = w.mWinAnimator;
777
778 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
chaviw1454b392018-08-06 09:54:04 -0700779 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800780
781 // Moved from updateWindowsAndWallpaperLocked().
782 if (w.mHasSurface) {
783 // Take care of the window being ready to display.
784 final boolean committed = winAnimator.commitFinishDrawingLocked();
785 if (isDefaultDisplay && committed) {
786 if (w.mAttrs.type == TYPE_DREAM) {
787 // HACK: When a dream is shown, it may at that point hide the lock screen.
788 // So we need to redo the layout to let the phone window manager make this
789 // happen.
790 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
791 if (DEBUG_LAYOUT_REPEATS) {
792 surfacePlacer.debugLayoutRepeats(
793 "dream and commitFinishDrawingLocked true",
794 pendingLayoutChanges);
795 }
796 }
797 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
798 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
799 "First draw done in potential wallpaper target " + w);
wilsonshihc32538e2018-11-07 17:27:34 +0800800 mWallpaperMayChange = true;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800801 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
802 if (DEBUG_LAYOUT_REPEATS) {
803 surfacePlacer.debugLayoutRepeats(
804 "wallpaper and commitFinishDrawingLocked true",
805 pendingLayoutChanges);
806 }
807 }
808 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800809 }
810
811 final AppWindowToken atoken = w.mAppToken;
812 if (atoken != null) {
Adrian Roos23df3a32018-03-15 15:41:13 +0100813 atoken.updateLetterboxSurface(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800814 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
815 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
816 mTmpUpdateAllDrawn.add(atoken);
817 }
818 }
819
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800820 if (!mLosingFocus.isEmpty() && w.isFocused() && w.isDisplayedLw()) {
821 mService.mH.obtainMessage(REPORT_LOSING_FOCUS, this).sendToTarget();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800822 }
823
824 w.updateResizingWindowIfNeeded();
825 };
826
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800827 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800828 * Create new {@link DisplayContent} instance, add itself to the root window container and
829 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700830 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800831 * @param service You know.
wilsonshihc32538e2018-11-07 17:27:34 +0800832 * @param controller The controller for the display container.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700833 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700834 DisplayContent(Display display, WindowManagerService service,
wilsonshihc32538e2018-11-07 17:27:34 +0800835 DisplayWindowController controller) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100836 super(service);
Bryce Leef19cbe22018-02-02 15:09:21 -0800837 setController(controller);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800838 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
839 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
840 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
841 + " new=" + display);
842 }
843
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700844 mDisplay = display;
845 mDisplayId = display.getDisplayId();
wilsonshihc32538e2018-11-07 17:27:34 +0800846 mWallpaperController = new WallpaperController(mService, this);
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700847 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700848 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700849 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100850 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo,
851 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700852 initializeDisplayBaseInfo();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700853
lumark588a3e82018-07-20 18:53:54 +0800854 mAppTransition = new AppTransition(service.mContext, service, this);
855 mAppTransition.registerListenerLocked(service.mActivityManagerAppTransitionNotifier);
856 mAppTransitionController = new AppTransitionController(service, this);
857 mUnknownAppVisibilityController = new UnknownAppVisibilityController(service, this);
858
859 AnimationHandler animationHandler = new AnimationHandler();
860 mBoundsAnimationController = new BoundsAnimationController(service.mContext,
861 mAppTransition, SurfaceAnimationThread.getHandler(), animationHandler);
862
Tiger Huang7c610aa2018-10-27 00:01:01 +0800863 if (mService.mInputManager != null) {
864 final InputChannel inputChannel = mService.mInputManager.monitorInput("Display "
865 + mDisplayId, mDisplayId);
866 mPointerEventDispatcher = inputChannel != null
867 ? new PointerEventDispatcher(inputChannel) : null;
868 } else {
869 mPointerEventDispatcher = null;
870 }
871 mDisplayPolicy = new DisplayPolicy(service, this);
872 mDisplayRotation = new DisplayRotation(service, this);
873 if (isDefaultDisplay) {
874 // The policy may be invoked right after here, so it requires the necessary default
875 // fields of this display content.
876 mService.mPolicy.setDefaultDisplay(this);
877 }
878 if (mService.mDisplayReady) {
879 mDisplayPolicy.onConfigurationChanged();
880 }
881 if (mService.mSystemReady) {
882 mDisplayPolicy.systemReady();
883 }
884 mDividerControllerLocked = new DockedStackDividerController(service, this);
885 mPinnedStackControllerLocked = new PinnedStackController(service, this);
886
Robert Carr74a66a22018-02-23 12:17:51 -0800887 // We use this as our arbitrary surface size for buffer-less parents
888 // that don't impose cropping on their children. It may need to be larger
889 // than the display size because fullscreen windows can be shifted offscreen
890 // due to surfaceInsets. 2 times the largest display dimension feels like an
891 // appropriately arbitrary number. Eventually we would like to give SurfaceFlinger
892 // layers the ability to match their parent sizes and be able to skip
893 // such arbitrary size settings.
894 mSurfaceSize = Math.max(mBaseDisplayHeight, mBaseDisplayWidth) * 2;
Robert Carrb1579c82017-09-05 14:54:47 -0700895
896 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession)
897 .setSize(mSurfaceSize, mSurfaceSize)
898 .setOpaque(true);
899 mWindowingLayer = b.setName("Display Root").build();
900 mOverlayLayer = b.setName("Display Overlays").build();
901
Robert Carrf59b8dd2017-10-02 18:58:36 -0700902 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700903 .setLayerStack(mWindowingLayer, mDisplayId)
904 .show(mWindowingLayer)
905 .setLayer(mOverlayLayer, 1)
906 .setLayerStack(mOverlayLayer, mDisplayId)
907 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700908 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700909
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700910 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700911 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700912 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700913 super.addChild(mAboveAppWindowsContainers, null);
914 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800915
916 // Add itself as a child to the root container.
917 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700918
919 // TODO(b/62541591): evaluate whether this is the best spot to declare the
920 // {@link DisplayContent} ready for use.
921 mDisplayReady = true;
Arthur Hung95b38a92018-07-20 18:56:12 +0800922
Riddle Hsuf53da812018-08-15 22:00:27 +0800923 mService.mAnimator.addDisplayLocked(mDisplayId);
Arthur Hung39134b22018-08-14 11:58:28 +0800924 mInputMonitor = new InputMonitor(service, mDisplayId);
Bryce Leed1871262017-06-12 14:12:29 -0700925 }
926
927 boolean isReady() {
928 // The display is ready when the system and the individual display are both ready.
929 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700930 }
931
932 int getDisplayId() {
933 return mDisplayId;
934 }
935
Wale Ogunwale02319a62016-09-26 15:21:22 -0700936 WindowToken getWindowToken(IBinder binder) {
937 return mTokenMap.get(binder);
938 }
939
940 AppWindowToken getAppWindowToken(IBinder binder) {
941 final WindowToken token = getWindowToken(binder);
942 if (token == null) {
943 return null;
944 }
945 return token.asAppWindowToken();
946 }
947
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700948 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700949 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
950 if (dc != null) {
951 // We currently don't support adding a window token to the display if the display
952 // already has the binder mapped to another token. If there is a use case for supporting
953 // this moving forward we will either need to merge the WindowTokens some how or have
954 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700955 throw new IllegalArgumentException("Can't map token=" + token + " to display="
956 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700957 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700958 if (binder == null) {
959 throw new IllegalArgumentException("Can't map token=" + token + " to display="
960 + getName() + " binder is null");
961 }
962 if (token == null) {
963 throw new IllegalArgumentException("Can't map null token to display="
964 + getName() + " binder=" + binder);
965 }
966
Wale Ogunwale02319a62016-09-26 15:21:22 -0700967 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700968
969 if (token.asAppWindowToken() == null) {
970 // Add non-app token to container hierarchy on the display. App tokens are added through
971 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700972 switch (token.windowType) {
973 case TYPE_WALLPAPER:
974 mBelowAppWindowsContainers.addChild(token);
975 break;
976 case TYPE_INPUT_METHOD:
977 case TYPE_INPUT_METHOD_DIALOG:
978 mImeWindowsContainers.addChild(token);
979 break;
980 default:
981 mAboveAppWindowsContainers.addChild(token);
982 break;
983 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700984 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700985 }
986
987 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700988 final WindowToken token = mTokenMap.remove(binder);
989 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800990 token.setExiting();
991 }
992 return token;
993 }
994
995 /** Changes the display the input window token is housed on to this one. */
996 void reParentWindowToken(WindowToken token) {
997 final DisplayContent prevDc = token.getDisplayContent();
998 if (prevDc == this) {
999 return;
1000 }
Riddle Hsu85bd04b2018-11-17 00:34:36 +08001001 if (prevDc != null) {
1002 if (prevDc.mTokenMap.remove(token.token) != null && token.asAppWindowToken() == null) {
1003 // Removed the token from the map, but made sure it's not an app token before
1004 // removing from parent.
1005 token.getParent().removeChild(token);
1006 }
1007 if (prevDc.mLastFocus == mCurrentFocus) {
1008 // The window has become the focus of this display, so it should not be notified
1009 // that it lost focus from the previous display.
1010 prevDc.mLastFocus = null;
1011 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001012 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -08001013
1014 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -07001015 }
1016
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001017 void removeAppToken(IBinder binder) {
1018 final WindowToken token = removeWindowToken(binder);
1019 if (token == null) {
1020 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
1021 return;
1022 }
1023
1024 final AppWindowToken appToken = token.asAppWindowToken();
1025
1026 if (appToken == null) {
1027 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
1028 return;
1029 }
1030
1031 appToken.onRemovedFromDisplay();
1032 }
1033
Riddle Hsuad256a12018-07-18 16:11:30 +08001034 @Override
Riddle Hsu86cb7de2018-08-13 23:29:58 +08001035 DisplayWindowController getController() {
1036 return (DisplayWindowController) super.getController();
1037 }
1038
1039 @Override
Riddle Hsuad256a12018-07-18 16:11:30 +08001040 public Display getDisplay() {
Craig Mautnerb47bbc32012-08-22 17:41:48 -07001041 return mDisplay;
1042 }
1043
Craig Mautner59c00972012-07-30 12:10:24 -07001044 DisplayInfo getDisplayInfo() {
1045 return mDisplayInfo;
1046 }
1047
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001048 DisplayMetrics getDisplayMetrics() {
1049 return mDisplayMetrics;
1050 }
1051
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001052 DisplayPolicy getDisplayPolicy() {
1053 return mDisplayPolicy;
1054 }
1055
Riddle Hsuad256a12018-07-18 16:11:30 +08001056 @Override
1057 public DisplayRotation getDisplayRotation() {
1058 return mDisplayRotation;
1059 }
1060
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001061 @VisibleForTesting
1062 void setDisplayRotation(DisplayRotation displayRotation) {
1063 mDisplayRotation = displayRotation;
1064 }
1065
Andrii Kulian8ee72852017-03-10 10:36:45 -08001066 int getRotation() {
1067 return mRotation;
1068 }
1069
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001070 @VisibleForTesting
Andrii Kulian8ee72852017-03-10 10:36:45 -08001071 void setRotation(int newRotation) {
1072 mRotation = newRotation;
Riddle Hsuad256a12018-07-18 16:11:30 +08001073 mDisplayRotation.setRotation(newRotation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08001074 }
1075
1076 int getLastOrientation() {
1077 return mLastOrientation;
1078 }
1079
Andrii Kulian8ee72852017-03-10 10:36:45 -08001080 boolean getAltOrientation() {
1081 return mAltOrientation;
1082 }
1083
Andrii Kulian8ee72852017-03-10 10:36:45 -08001084 int getLastWindowForcedOrientation() {
1085 return mLastWindowForcedOrientation;
1086 }
1087
Andrii Kulian06d07d62017-03-14 11:11:47 -07001088 /**
Riddle Hsu654a6f92018-07-13 22:59:36 +08001089 * Temporarily pauses rotation changes until resumed.
1090 *
1091 * This can be used to prevent rotation changes from occurring while the user is
1092 * performing certain operations, such as drag and drop.
1093 *
1094 * This call nests and must be matched by an equal number of calls to
1095 * {@link #resumeRotationLocked}.
1096 */
1097 void pauseRotationLocked() {
1098 mDeferredRotationPauseCount++;
1099 }
1100
1101 /**
1102 * Resumes normal rotation changes after being paused.
1103 */
1104 void resumeRotationLocked() {
1105 if (mDeferredRotationPauseCount <= 0) {
1106 return;
1107 }
1108
1109 mDeferredRotationPauseCount--;
1110 if (mDeferredRotationPauseCount == 0) {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001111 updateRotationAndSendNewConfigIfNeeded();
Riddle Hsu654a6f92018-07-13 22:59:36 +08001112 }
1113 }
1114
1115 /**
Riddle Hsuad256a12018-07-18 16:11:30 +08001116 * If this is true we have updated our desired orientation, but not yet changed the real
1117 * orientation our applied our screen rotation animation. For example, because a previous
1118 * screen rotation was in progress.
1119 *
1120 * @return {@code true} if the there is an ongoing rotation change.
1121 */
1122 boolean rotationNeedsUpdate() {
1123 final int lastOrientation = getLastOrientation();
1124 final int oldRotation = getRotation();
1125 final boolean oldAltOrientation = getAltOrientation();
1126
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001127 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Riddle Hsuad256a12018-07-18 16:11:30 +08001128 final boolean altOrientation = !mDisplayRotation.rotationHasCompatibleMetrics(
1129 lastOrientation, rotation);
1130 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1131 return false;
1132 }
1133 return true;
1134 }
1135
Riddle Hsu4e611772018-10-31 18:58:28 +08001136 /** Notify the configuration change of this display. */
1137 void sendNewConfiguration() {
1138 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, this).sendToTarget();
1139 }
1140
1141 /**
1142 * Determine the new desired orientation of this display.
1143 *
1144 * The orientation is computed from non-application windows first. If none of the
1145 * non-application windows specify orientation, the orientation is computed from application
1146 * tokens.
1147 *
1148 * @return {@code true} if the orientation is changed.
1149 */
1150 boolean updateOrientationFromAppTokens() {
1151 return updateOrientationFromAppTokens(false /* forceUpdate */);
1152 }
1153
1154 boolean updateOrientationFromAppTokens(boolean forceUpdate) {
1155 final int req = getOrientation();
1156 if (req != mLastOrientation || forceUpdate) {
1157 mLastOrientation = req;
1158 mDisplayRotation.setCurrentOrientation(req);
1159 return updateRotationUnchecked(forceUpdate);
1160 }
1161 return false;
1162 }
1163
Riddle Hsuad256a12018-07-18 16:11:30 +08001164 /**
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001165 * Update rotation of the display and send configuration if the rotation is changed.
1166 *
1167 * @return {@code true} if the rotation has been changed and the new config is sent.
1168 */
1169 boolean updateRotationAndSendNewConfigIfNeeded() {
1170 final boolean changed = updateRotationUnchecked(false /* forceUpdate */);
1171 if (changed) {
Riddle Hsu4e611772018-10-31 18:58:28 +08001172 sendNewConfiguration();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001173 }
1174 return changed;
1175 }
1176
1177 /**
Andrii Kulian06d07d62017-03-14 11:11:47 -07001178 * Update rotation of the display.
1179 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001180 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
1181 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001182 */
Robert Carrae606b42018-02-15 15:36:23 -08001183 boolean updateRotationUnchecked() {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001184 return updateRotationUnchecked(false /* forceUpdate */);
1185 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001186
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001187 /**
Evan Roskye747c3e2018-10-30 20:06:41 -07001188 * Update rotation of the DisplayContent with an option to force the update. This updates
1189 * the container's perception of rotation and, depending on the top activities, will freeze
1190 * the screen or start seamless rotation. The display itself gets rotated in
1191 * {@link #applyRotationLocked} during {@link WindowManagerService#sendNewConfiguration}.
1192 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001193 * @param forceUpdate Force the rotation update. Sometimes in WM we might skip updating
1194 * orientation because we're waiting for some rotation to finish or display
1195 * to unfreeze, which results in configuration of the previously visible
1196 * activity being applied to a newly visible one. Forcing the rotation
1197 * update allows to workaround this issue.
1198 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
Evan Roskye747c3e2018-10-30 20:06:41 -07001199 * {@link WindowManagerService#sendNewConfiguration(int)} TO COMPLETE THE ROTATION AND
1200 * UNFREEZE THE SCREEN.
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001201 */
1202 boolean updateRotationUnchecked(boolean forceUpdate) {
1203 ScreenRotationAnimation screenRotationAnimation;
1204 if (!forceUpdate) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001205 if (mDeferredRotationPauseCount > 0) {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001206 // Rotation updates have been paused temporarily. Defer the update until
1207 // updates have been resumed.
1208 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
1209 return false;
1210 }
1211
1212 screenRotationAnimation =
1213 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
1214 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
1215 // Rotation updates cannot be performed while the previous rotation change
1216 // animation is still in progress. Skip this update. We will try updating
1217 // again after the animation is finished and the display is unfrozen.
1218 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
1219 return false;
1220 }
1221 if (mService.mDisplayFrozen) {
1222 // Even if the screen rotation animation has finished (e.g. isAnimating
1223 // returns false), there is still some time where we haven't yet unfrozen
1224 // the display. We also need to abort rotation here.
1225 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
1226 "Deferring rotation, still finishing previous rotation");
1227 return false;
1228 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001229 }
1230
1231 if (!mService.mDisplayEnabled) {
1232 // No point choosing a rotation if the display is not enabled.
1233 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
1234 return false;
1235 }
1236
1237 final int oldRotation = mRotation;
1238 final int lastOrientation = mLastOrientation;
1239 final boolean oldAltOrientation = mAltOrientation;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001240 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001241 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Computed rotation=" + rotation + " for display id="
1242 + mDisplayId + " based on lastOrientation=" + lastOrientation
1243 + " and oldRotation=" + oldRotation);
Tiger Huang7c610aa2018-10-27 00:01:01 +08001244 boolean mayRotateSeamlessly = mDisplayPolicy.shouldRotateSeamlessly(mDisplayRotation,
Riddle Hsuad256a12018-07-18 16:11:30 +08001245 oldRotation, rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001246
Robert Carr0e007272017-10-02 13:00:55 -07001247 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001248 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001249 if (seamlessRotated != null && !forceUpdate) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001250 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
1251 // to complete (that is, waiting for windows to redraw). It's tempting to check
1252 // w.mSeamlessRotationCount but that could be incorrect in the case of
1253 // window-removal.
1254 return false;
1255 }
Robert Carr0e007272017-10-02 13:00:55 -07001256
1257 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001258 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -07001259 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -07001260 mayRotateSeamlessly = false;
1261 }
1262 for (int i = 0; i < mService.mSessions.size(); i++) {
1263 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
1264 mayRotateSeamlessly = false;
1265 break;
1266 }
1267 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001268 }
Robert Carr0e007272017-10-02 13:00:55 -07001269 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001270
1271 // TODO: Implement forced rotation changes.
1272 // Set mAltOrientation to indicate that the application is receiving
1273 // an orientation that has different metrics than it expected.
1274 // eg. Portrait instead of Landscape.
1275
Riddle Hsuad256a12018-07-18 16:11:30 +08001276 final boolean altOrientation = !mDisplayRotation.rotationHasCompatibleMetrics(
Andrii Kulian06d07d62017-03-14 11:11:47 -07001277 lastOrientation, rotation);
1278
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001279 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1280 + " selected orientation " + lastOrientation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001281 + ", got rotation " + rotation + " which has "
1282 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1283
1284 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1285 // No change.
1286 return false;
1287 }
1288
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001289 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1290 + " rotation changed to " + rotation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001291 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1292 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1293
1294 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
Vishnu Nairba183352018-11-21 11:16:49 -08001295 mWaitingForConfig = true;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001296 }
1297
Evan Roskye747c3e2018-10-30 20:06:41 -07001298 mRotation = rotation;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001299 mAltOrientation = altOrientation;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001300
1301 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
Riddle Hsu654a6f92018-07-13 22:59:36 +08001302 mService.mH.sendNewMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1303 this, WINDOW_FREEZE_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001304
1305 setLayoutNeeded();
1306 final int[] anim = new int[2];
Tiger Huang7c610aa2018-10-27 00:01:01 +08001307 mDisplayPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001308
1309 if (!rotateSeamlessly) {
Robert Carrae606b42018-02-15 15:36:23 -08001310 mService.startFreezingDisplayLocked(anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001311 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001312 } else {
1313 // The screen rotation animation uses a screenshot to freeze the screen
1314 // while windows resize underneath.
1315 // When we are rotating seamlessly, we allow the elements to transition
1316 // to their rotated state independently and without a freeze required.
Robert Carrefc75702018-02-16 11:46:16 -08001317 mService.startSeamlessRotation();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001318 }
1319
Evan Roskye747c3e2018-10-30 20:06:41 -07001320 return true;
1321 }
1322
1323 /**
1324 * Applies the rotation transaction. This must be called after {@link #updateRotationUnchecked}
1325 * (if it returned {@code true}) to actually finish the rotation.
1326 *
1327 * @param oldRotation the rotation we are coming from.
1328 * @param rotation the rotation to apply.
1329 */
1330 void applyRotationLocked(final int oldRotation, final int rotation) {
1331 mDisplayRotation.setRotation(rotation);
1332 final boolean rotateSeamlessly = mService.isRotatingSeamlessly();
1333 ScreenRotationAnimation screenRotationAnimation = rotateSeamlessly
1334 ? null : mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001335 // We need to update our screen size information to match the new rotation. If the rotation
1336 // has actually changed then this method will return true and, according to the comment at
1337 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1338 // By updating the Display info here it will be available to
1339 // #computeScreenConfiguration() later.
1340 updateDisplayAndOrientation(getConfiguration().uiMode);
1341
Robert Carrae606b42018-02-15 15:36:23 -08001342 // NOTE: We disable the rotation in the emulator because
1343 // it doesn't support hardware OpenGL emulation yet.
1344 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1345 && screenRotationAnimation.hasScreenshot()) {
1346 if (screenRotationAnimation.setRotation(getPendingTransaction(), rotation,
1347 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1348 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1349 mService.scheduleAnimationLocked();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001350 }
1351 }
1352
Vishnu Nair83537a72018-07-19 21:27:48 -07001353 forAllWindows(w -> {
Vishnu Nairddd80742018-08-21 14:12:46 -07001354 w.seamlesslyRotateIfAllowed(getPendingTransaction(), oldRotation, rotation,
1355 rotateSeamlessly);
Vishnu Nair83537a72018-07-19 21:27:48 -07001356 }, true /* traverseTopToBottom */);
1357
Robert Carrae606b42018-02-15 15:36:23 -08001358 mService.mDisplayManagerInternal.performTraversal(getPendingTransaction());
1359 scheduleAnimation();
1360
Andrii Kulian06d07d62017-03-14 11:11:47 -07001361 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001362 if (w.mHasSurface && !rotateSeamlessly) {
1363 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001364 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001365 mService.mRoot.mOrientationChangeComplete = false;
1366 w.mLastFreezeDuration = 0;
1367 }
1368 w.mReportOrientationChanged = true;
1369 }, true /* traverseTopToBottom */);
1370
1371 if (rotateSeamlessly) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001372 mService.mH.sendNewMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1373 this, SEAMLESS_ROTATION_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001374 }
1375
1376 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1377 final WindowManagerService.RotationWatcher rotationWatcher
1378 = mService.mRotationWatchers.get(i);
1379 if (rotationWatcher.mDisplayId == mDisplayId) {
1380 try {
1381 rotationWatcher.mWatcher.onRotationChanged(rotation);
1382 } catch (RemoteException e) {
1383 // Ignore
1384 }
1385 }
1386 }
1387
1388 // TODO (multi-display): Magnification is supported only for the default display.
1389 // Announce rotation only if we will not animate as we already have the
1390 // windows in final state. Otherwise, we make this call at the rotation end.
1391 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1392 && isDefaultDisplay) {
1393 mService.mAccessibilityController.onRotationChangedLocked(this);
1394 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001395 }
1396
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001397 void configureDisplayPolicy() {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001398 final int width = mBaseDisplayWidth;
1399 final int height = mBaseDisplayHeight;
1400 final int shortSize;
1401 final int longSize;
1402 if (width > height) {
1403 shortSize = height;
1404 longSize = width;
1405 } else {
1406 shortSize = width;
1407 longSize = height;
1408 }
1409
1410 final int shortSizeDp = shortSize * DisplayMetrics.DENSITY_DEFAULT / mBaseDisplayDensity;
1411 final int longSizeDp = longSize * DisplayMetrics.DENSITY_DEFAULT / mBaseDisplayDensity;
1412
1413 mDisplayRotation.configure(width, height, shortSizeDp, longSizeDp);
1414 mDisplayPolicy.configure(width, height, shortSizeDp);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001415
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001416 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
1417 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Arthur Hungbe5ce212018-09-13 18:41:56 +08001418
1419 // Tap Listeners are supported for:
1420 // 1. All physical displays (multi-display).
1421 // 2. VirtualDisplays on VR, AA (and everything else).
1422 if (mPointerEventDispatcher != null && mTapDetector == null) {
1423 if (DEBUG_DISPLAY) {
1424 Slog.d(TAG,
1425 "Registering PointerEventListener for DisplayId: " + mDisplayId);
1426 }
1427 mTapDetector = new TaskTapPointerEventListener(mService, this);
1428 registerPointerEventListener(mTapDetector);
1429 registerPointerEventListener(mService.mMousePositionTracker);
1430 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001431 }
1432
Andrii Kulian06d07d62017-03-14 11:11:47 -07001433 /**
1434 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1435 * changed.
1436 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1437 */
1438 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1439 // Use the effective "visual" dimensions based on current rotation
1440 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1441 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1442 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1443 int dw = realdw;
1444 int dh = realdh;
1445
1446 if (mAltOrientation) {
1447 if (realdw > realdh) {
1448 // Turn landscape into portrait.
1449 int maxw = (int)(realdh/1.3f);
1450 if (maxw < realdw) {
1451 dw = maxw;
1452 }
1453 } else {
1454 // Turn portrait into landscape.
1455 int maxh = (int)(realdw/1.3f);
1456 if (maxh < realdh) {
1457 dh = maxh;
1458 }
1459 }
1460 }
1461
1462 // Update application display metrics.
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001463 final WmDisplayCutout wmDisplayCutout = calculateDisplayCutoutForRotation(mRotation);
1464 final DisplayCutout displayCutout = wmDisplayCutout.getDisplayCutout();
1465
Tiger Huang7c610aa2018-10-27 00:01:01 +08001466 final int appWidth = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1467 displayCutout);
1468 final int appHeight = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1469 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001470 mDisplayInfo.rotation = mRotation;
1471 mDisplayInfo.logicalWidth = dw;
1472 mDisplayInfo.logicalHeight = dh;
1473 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1474 mDisplayInfo.appWidth = appWidth;
1475 mDisplayInfo.appHeight = appHeight;
1476 if (isDefaultDisplay) {
1477 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1478 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1479 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001480 mDisplayInfo.displayCutout = displayCutout.isEmpty() ? null : displayCutout;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001481 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1482 if (mDisplayScalingDisabled) {
1483 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1484 } else {
1485 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1486 }
1487
Andrii Kulianf0379de2018-03-14 16:24:07 -07001488 // We usually set the override info in DisplayManager so that we get consistent display
1489 // metrics values when displays are changing and don't send out new values until WM is aware
1490 // of them. However, we don't do this for displays that serve as containers for ActivityView
1491 // because we don't want letter-/pillar-boxing during resize.
1492 final DisplayInfo overrideDisplayInfo = mShouldOverrideDisplayConfiguration
1493 ? mDisplayInfo : null;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001494 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
Andrii Kulianf0379de2018-03-14 16:24:07 -07001495 overrideDisplayInfo);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001496
1497 mBaseDisplayRect.set(0, 0, dw, dh);
1498
1499 if (isDefaultDisplay) {
1500 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1501 mCompatDisplayMetrics);
1502 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001503
Andrii Kulian06d07d62017-03-14 11:11:47 -07001504 return mDisplayInfo;
1505 }
1506
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001507 WmDisplayCutout calculateDisplayCutoutForRotation(int rotation) {
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001508 return mDisplayCutoutCache.getOrCompute(mInitialDisplayCutout, rotation);
1509 }
1510
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001511 private WmDisplayCutout calculateDisplayCutoutForRotationUncached(
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001512 DisplayCutout cutout, int rotation) {
Adrian Roos24264212018-02-19 16:26:15 +01001513 if (cutout == null || cutout == DisplayCutout.NO_CUTOUT) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001514 return WmDisplayCutout.NO_CUTOUT;
Adrian Roos1cf585052018-01-03 18:43:27 +01001515 }
Adrian Roos11c25582018-02-19 18:06:36 +01001516 if (rotation == ROTATION_0) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001517 return WmDisplayCutout.computeSafeInsets(
1518 cutout, mInitialDisplayWidth, mInitialDisplayHeight);
Adrian Roos24264212018-02-19 16:26:15 +01001519 }
Adrian Roosbed538e2018-02-21 17:50:07 +01001520 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Issei Suzuki43190bd2018-08-20 17:28:41 +02001521 final Rect[] newBounds = mRotationUtil.getRotatedBounds(
1522 WmDisplayCutout.computeSafeInsets(
Jorim Jaggi60640512018-06-29 01:14:31 +02001523 cutout, mInitialDisplayWidth, mInitialDisplayHeight)
Issei Suzuki43190bd2018-08-20 17:28:41 +02001524 .getDisplayCutout().getBoundingRectsAll(),
1525 rotation, mInitialDisplayWidth, mInitialDisplayHeight);
1526 return WmDisplayCutout.computeSafeInsets(DisplayCutout.fromBounds(newBounds),
Adrian Roos24264212018-02-19 16:26:15 +01001527 rotated ? mInitialDisplayHeight : mInitialDisplayWidth,
1528 rotated ? mInitialDisplayWidth : mInitialDisplayHeight);
Adrian Roos1cf585052018-01-03 18:43:27 +01001529 }
1530
Andrii Kulian06d07d62017-03-14 11:11:47 -07001531 /**
1532 * Compute display configuration based on display properties and policy settings.
1533 * Do not call if mDisplayReady == false.
1534 */
1535 void computeScreenConfiguration(Configuration config) {
1536 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
Evan Roskye747c3e2018-10-30 20:06:41 -07001537 calculateBounds(displayInfo, mTmpBounds);
1538 config.windowConfiguration.setBounds(mTmpBounds);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001539
1540 final int dw = displayInfo.logicalWidth;
1541 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001542 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
Garfield Tane0846042018-07-26 13:42:04 -07001543 config.windowConfiguration.setWindowingMode(getWindowingMode());
Evan Roskye747c3e2018-10-30 20:06:41 -07001544 config.windowConfiguration.setRotation(displayInfo.rotation);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001545
Adrian Roos11c25582018-02-19 18:06:36 +01001546 final float density = mDisplayMetrics.density;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001547 config.screenWidthDp =
Tiger Huang7c610aa2018-10-27 00:01:01 +08001548 (int)(mDisplayPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1549 config.uiMode, displayInfo.displayCutout) / density);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001550 config.screenHeightDp =
Tiger Huang7c610aa2018-10-27 00:01:01 +08001551 (int)(mDisplayPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1552 config.uiMode, displayInfo.displayCutout) / density);
Bryce Lee7566d762017-03-30 09:34:15 -07001553
Tiger Huang7c610aa2018-10-27 00:01:01 +08001554 mDisplayPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh,
Adrian Roos11c25582018-02-19 18:06:36 +01001555 displayInfo.displayCutout, mTmpRect);
Bryce Lee7566d762017-03-30 09:34:15 -07001556 final int leftInset = mTmpRect.left;
1557 final int topInset = mTmpRect.top;
1558 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001559 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1560 leftInset + displayInfo.appWidth /* right */,
1561 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001562 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1563 || displayInfo.rotation == Surface.ROTATION_270);
1564
Tiger Huang7c610aa2018-10-27 00:01:01 +08001565 computeSizeRangesAndScreenLayout(displayInfo, rotated, config.uiMode, dw, dh, density,
1566 config);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001567
1568 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1569 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1570 ? Configuration.SCREENLAYOUT_ROUND_YES
1571 : Configuration.SCREENLAYOUT_ROUND_NO);
1572
1573 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1574 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1575 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001576 dh, displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001577 config.densityDpi = displayInfo.logicalDensityDpi;
1578
1579 config.colorMode =
Chia-I Wuf739bad2018-05-09 12:36:57 -07001580 ((displayInfo.isHdr() && mService.hasHdrSupport())
Andrii Kulian06d07d62017-03-14 11:11:47 -07001581 ? Configuration.COLOR_MODE_HDR_YES
1582 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001583 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001584 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1585 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1586
1587 // Update the configuration based on available input devices, lid switch,
1588 // and platform configuration.
1589 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1590 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1591 config.navigation = Configuration.NAVIGATION_NONAV;
1592
1593 int keyboardPresence = 0;
1594 int navigationPresence = 0;
1595 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1596 final int len = devices != null ? devices.length : 0;
1597 for (int i = 0; i < len; i++) {
1598 InputDevice device = devices[i];
1599 if (!device.isVirtual()) {
1600 final int sources = device.getSources();
1601 final int presenceFlag = device.isExternal() ?
1602 WindowManagerPolicy.PRESENCE_EXTERNAL :
1603 WindowManagerPolicy.PRESENCE_INTERNAL;
1604
1605 // TODO(multi-display): Configure on per-display basis.
1606 if (mService.mIsTouchDevice) {
1607 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1608 InputDevice.SOURCE_TOUCHSCREEN) {
1609 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1610 }
1611 } else {
1612 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1613 }
1614
1615 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1616 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1617 navigationPresence |= presenceFlag;
1618 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1619 && config.navigation == Configuration.NAVIGATION_NONAV) {
1620 config.navigation = Configuration.NAVIGATION_DPAD;
1621 navigationPresence |= presenceFlag;
1622 }
1623
1624 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1625 config.keyboard = Configuration.KEYBOARD_QWERTY;
1626 keyboardPresence |= presenceFlag;
1627 }
1628 }
1629 }
1630
1631 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1632 config.navigation = Configuration.NAVIGATION_DPAD;
1633 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1634 }
1635
1636 // Determine whether a hard keyboard is available and enabled.
1637 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1638 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1639 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1640 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1641 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1642 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1643 }
1644
Tiger Huang7c610aa2018-10-27 00:01:01 +08001645 mDisplayPolicy.updateConfigurationDependentBehaviors();
1646
Andrii Kulian06d07d62017-03-14 11:11:47 -07001647 // Let the policy update hidden states.
1648 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1649 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1650 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1651 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1652 }
1653
1654 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001655 DisplayCutout displayCutout) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001656 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1657 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1658 final int unrotDw, unrotDh;
1659 if (rotated) {
1660 unrotDw = dh;
1661 unrotDh = dw;
1662 } else {
1663 unrotDw = dw;
1664 unrotDh = dh;
1665 }
1666 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001667 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001668 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001669 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001670 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001671 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001672 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001673 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001674 return sw;
1675 }
1676
1677 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001678 DisplayMetrics dm, int dw, int dh, DisplayCutout displayCutout) {
1679 dm.noncompatWidthPixels = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1680 displayCutout);
1681 dm.noncompatHeightPixels = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode,
1682 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001683 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1684 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1685 if (curSize == 0 || size < curSize) {
1686 curSize = size;
1687 }
1688 return curSize;
1689 }
1690
Tiger Huang7c610aa2018-10-27 00:01:01 +08001691 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, boolean rotated,
1692 int uiMode, int dw, int dh, float density, Configuration outConfig) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001693
1694 // We need to determine the smallest width that will occur under normal
1695 // operation. To this, start with the base screen size and compute the
1696 // width under the different possible rotations. We need to un-rotate
1697 // the current screen dimensions before doing this.
1698 int unrotDw, unrotDh;
1699 if (rotated) {
1700 unrotDw = dh;
1701 unrotDh = dw;
1702 } else {
1703 unrotDw = dw;
1704 unrotDh = dh;
1705 }
1706 displayInfo.smallestNominalAppWidth = 1<<30;
1707 displayInfo.smallestNominalAppHeight = 1<<30;
1708 displayInfo.largestNominalAppWidth = 0;
1709 displayInfo.largestNominalAppHeight = 0;
Tiger Huang7c610aa2018-10-27 00:01:01 +08001710 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_0, uiMode, unrotDw, unrotDh);
1711 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_90, uiMode, unrotDh, unrotDw);
1712 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_180, uiMode, unrotDw, unrotDh);
1713 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_270, uiMode, unrotDh, unrotDw);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001714 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1715 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001716 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001717 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001718 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001719 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001720 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001721 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001722 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001723 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1724 outConfig.screenLayout = sl;
1725 }
1726
1727 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001728 int uiMode, DisplayCutout displayCutout) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001729 // Get the app screen size at this rotation.
Tiger Huang7c610aa2018-10-27 00:01:01 +08001730 int w = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayCutout);
1731 int h = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001732
1733 // Compute the screen layout size class for this rotation.
1734 int longSize = w;
1735 int shortSize = h;
1736 if (longSize < shortSize) {
1737 int tmp = longSize;
1738 longSize = shortSize;
1739 shortSize = tmp;
1740 }
1741 longSize = (int)(longSize/density);
1742 shortSize = (int)(shortSize/density);
1743 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1744 }
1745
Tiger Huang7c610aa2018-10-27 00:01:01 +08001746 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int rotation,
Andrii Kulian06d07d62017-03-14 11:11:47 -07001747 int uiMode, int dw, int dh) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001748 final DisplayCutout displayCutout = calculateDisplayCutoutForRotation(
1749 rotation).getDisplayCutout();
Tiger Huang7c610aa2018-10-27 00:01:01 +08001750 final int width = mDisplayPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1751 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001752 if (width < displayInfo.smallestNominalAppWidth) {
1753 displayInfo.smallestNominalAppWidth = width;
1754 }
1755 if (width > displayInfo.largestNominalAppWidth) {
1756 displayInfo.largestNominalAppWidth = width;
1757 }
Tiger Huang7c610aa2018-10-27 00:01:01 +08001758 final int height = mDisplayPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1759 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001760 if (height < displayInfo.smallestNominalAppHeight) {
1761 displayInfo.smallestNominalAppHeight = height;
1762 }
1763 if (height > displayInfo.largestNominalAppHeight) {
1764 displayInfo.largestNominalAppHeight = height;
1765 }
1766 }
1767
Riddle Hsua4d6fa22018-08-11 00:50:39 +08001768 /**
1769 * Apps that use the compact menu panel (as controlled by the panelMenuIsCompact
1770 * theme attribute) on devices that feature a physical options menu key attempt to position
1771 * their menu panel window along the edge of the screen nearest the physical menu key.
1772 * This lowers the travel distance between invoking the menu panel and selecting
1773 * a menu option.
1774 *
1775 * This method helps control where that menu is placed. Its current implementation makes
1776 * assumptions about the menu key and its relationship to the screen based on whether
1777 * the device's natural orientation is portrait (width < height) or landscape.
1778 *
1779 * The menu key is assumed to be located along the bottom edge of natural-portrait
1780 * devices and along the right edge of natural-landscape devices. If these assumptions
1781 * do not hold for the target device, this method should be changed to reflect that.
1782 *
1783 * @return A {@link Gravity} value for placing the options menu window.
1784 */
1785 int getPreferredOptionsPanelGravity() {
1786 final int rotation = getRotation();
1787 if (mInitialDisplayWidth < mInitialDisplayHeight) {
1788 // On devices with a natural orientation of portrait.
1789 switch (rotation) {
1790 default:
1791 case Surface.ROTATION_0:
1792 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1793 case Surface.ROTATION_90:
1794 return Gravity.RIGHT | Gravity.BOTTOM;
1795 case Surface.ROTATION_180:
1796 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1797 case Surface.ROTATION_270:
1798 return Gravity.START | Gravity.BOTTOM;
1799 }
1800 }
1801
1802 // On devices with a natural orientation of landscape.
1803 switch (rotation) {
1804 default:
1805 case Surface.ROTATION_0:
1806 return Gravity.RIGHT | Gravity.BOTTOM;
1807 case Surface.ROTATION_90:
1808 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1809 case Surface.ROTATION_180:
1810 return Gravity.START | Gravity.BOTTOM;
1811 case Surface.ROTATION_270:
1812 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1813 }
1814 }
1815
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001816 DockedStackDividerController getDockedDividerController() {
1817 return mDividerControllerLocked;
1818 }
1819
Winson Chung655332c2016-10-31 13:14:28 -07001820 PinnedStackController getPinnedStackController() {
1821 return mPinnedStackControllerLocked;
1822 }
1823
Jeff Browna506a6e2013-06-04 00:02:38 -07001824 /**
1825 * Returns true if the specified UID has access to this display.
1826 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001827 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001828 return mDisplay.hasAccess(uid);
1829 }
1830
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001831 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001832 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001833 }
1834
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001835 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001836 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001837 }
1838
Wale Ogunwale61911492017-10-11 08:50:50 -07001839 /**
1840 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1841 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001842 TaskStack getSplitScreenPrimaryStack() {
1843 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001844 return (stack != null && stack.isVisible()) ? stack : null;
1845 }
1846
Robert Carr9785cf32018-04-25 15:06:07 -07001847 boolean hasSplitScreenPrimaryStack() {
1848 return getSplitScreenPrimaryStack() != null;
1849 }
1850
Wale Ogunwale61911492017-10-11 08:50:50 -07001851 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001852 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001853 * not visible.
1854 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001855 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1856 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001857 }
1858
1859 TaskStack getPinnedStack() {
1860 return mTaskStackContainers.getPinnedStack();
1861 }
1862
1863 private boolean hasPinnedStack() {
1864 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001865 }
1866
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001867 /**
1868 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1869 * Null is no compatible stack on the display.
1870 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001871 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001872 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1873 }
1874
1875 /**
1876 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1877 * activity type. Null is no compatible stack on the display.
1878 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001879 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001880 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001881 }
1882
Bryce Lee48f4b572017-04-10 10:54:15 -07001883 @VisibleForTesting
Kazuki Takise148d00a2018-05-31 15:32:19 +09001884 WindowList<TaskStack> getStacks() {
1885 return mTaskStackContainers.mChildren;
1886 }
1887
1888 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001889 TaskStack getTopStack() {
1890 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001891 }
1892
Winson Chunge2d72172018-01-25 17:46:20 +00001893 ArrayList<Task> getVisibleTasks() {
1894 return mTaskStackContainers.getVisibleTasks();
1895 }
1896
Wale Ogunwale61911492017-10-11 08:50:50 -07001897 void onStackWindowingModeChanged(TaskStack stack) {
1898 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001899 }
1900
Andrii Kulian441e4492016-09-29 15:25:00 -07001901 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001902 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001903 super.onConfigurationChanged(newParentConfig);
Tiger Huang7c610aa2018-10-27 00:01:01 +08001904 if (mDisplayPolicy != null) {
1905 mDisplayPolicy.onConfigurationChanged();
1906 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001907
Evan Roskye747c3e2018-10-30 20:06:41 -07001908 // If there was no pinned stack, we still need to notify the controller of the display info
1909 // update as a result of the config change.
1910 if (mPinnedStackControllerLocked != null && !hasPinnedStack()) {
1911 mPinnedStackControllerLocked.onDisplayInfoChanged(getDisplayInfo());
1912 }
Evan Roskye747c3e2018-10-30 20:06:41 -07001913 }
1914
1915 /**
1916 * Updates the resources used by docked/pinned controllers. This needs to be called at the
1917 * beginning of a configuration update cascade since the metrics from these resources are used
1918 * for bounds calculations. Since ActivityDisplay initiates the configuration update, this
1919 * should be called from there instead of DisplayContent's onConfigurationChanged.
1920 */
1921 void preOnConfigurationChanged() {
Bryce Leef3c6a472017-11-14 14:53:06 -08001922 final DockedStackDividerController dividerController = getDockedDividerController();
1923
1924 if (dividerController != null) {
1925 getDockedDividerController().onConfigurationChanged();
1926 }
1927
1928 final PinnedStackController pinnedStackController = getPinnedStackController();
1929
1930 if (pinnedStackController != null) {
1931 getPinnedStackController().onConfigurationChanged();
1932 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001933 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001934
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001935 @Override
1936 boolean fillsParent() {
1937 return true;
1938 }
1939
1940 @Override
1941 boolean isVisible() {
1942 return true;
1943 }
1944
1945 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001946 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001947 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001948 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001949 }
1950
Robert Carr9785cf32018-04-25 15:06:07 -07001951 /**
1952 * In split-screen mode we process the IME containers above the docked divider
1953 * rather than directly above their target.
1954 */
1955 private boolean skipTraverseChild(WindowContainer child) {
lumarkff0ab692018-11-05 20:32:30 +08001956 if (child == mImeWindowsContainers && mInputMethodTarget != null
Robert Carr9785cf32018-04-25 15:06:07 -07001957 && !hasSplitScreenPrimaryStack()) {
1958 return true;
1959 }
1960 return false;
1961 }
1962
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001963 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001964 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1965 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1966 // target, or here in order if there isn't an IME target.
1967 if (traverseTopToBottom) {
1968 for (int i = mChildren.size() - 1; i >= 0; --i) {
1969 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001970 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001971 continue;
1972 }
Robert Carr9785cf32018-04-25 15:06:07 -07001973
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001974 if (child.forAllWindows(callback, traverseTopToBottom)) {
1975 return true;
1976 }
1977 }
1978 } else {
1979 final int count = mChildren.size();
1980 for (int i = 0; i < count; i++) {
1981 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001982 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001983 continue;
1984 }
Robert Carr9785cf32018-04-25 15:06:07 -07001985
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001986 if (child.forAllWindows(callback, traverseTopToBottom)) {
1987 return true;
1988 }
1989 }
1990 }
1991 return false;
1992 }
1993
1994 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1995 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1996 }
1997
Wale Ogunwale399c8692017-05-08 14:22:42 -07001998 @Override
1999 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002000 final WindowManagerPolicy policy = mService.mPolicy;
2001
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002002 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08002003 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07002004 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
2005 + " is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002006 // If the display is frozen, some activities may be in the middle of restarting, and
2007 // thus have removed their old window. If the window has the flag to hide the lock
2008 // screen, then the lock screen can re-appear and inflict its own orientation on us.
2009 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08002010 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002011 } else if (policy.isKeyguardLocked()) {
2012 // Use the last orientation the while the display is frozen with the keyguard
2013 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
2014 // window. We don't want to check the show when locked window directly though as
2015 // things aren't stable while the display is frozen, for example the window could be
2016 // momentarily unavailable due to activity relaunch.
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07002017 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
2018 + " is frozen while keyguard locked, return " + mLastOrientation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08002019 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07002020 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002021 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002022 final int orientation = mAboveAppWindowsContainers.getOrientation();
2023 if (orientation != SCREEN_ORIENTATION_UNSET) {
2024 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002025 }
Wale Ogunwale51362492016-09-08 17:49:17 -07002026 }
2027
Wale Ogunwale399c8692017-05-08 14:22:42 -07002028 // Top system windows are not requesting an orientation. Start searching from apps.
2029 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07002030 }
2031
Craig Mautner46ac6fa2013-08-01 10:06:34 -07002032 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07002033 // Check if display metrics changed and update base values if needed.
2034 updateBaseDisplayMetricsIfNeeded();
2035
Craig Mautner722285e2012-09-07 13:55:58 -07002036 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07002037 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07002038
Garfield Tan2f145f22018-11-01 15:27:03 -07002039 onDisplayChanged(this);
Craig Mautner722285e2012-09-07 13:55:58 -07002040 }
2041
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002042 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002043 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
2044 if (displayManagerInternal != null) {
2045 // Bootstrap the default logical display from the display manager.
2046 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
2047 if (newDisplayInfo != null) {
2048 mDisplayInfo.copyFrom(newDisplayInfo);
2049 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002050 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002051
Andrii Kuliancd097992017-03-23 18:31:59 -07002052 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
2053 mDisplayInfo.logicalDensityDpi);
2054 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
2055 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
2056 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002057 mInitialDisplayCutout = mDisplayInfo.displayCutout;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002058 }
2059
Andrii Kuliancd097992017-03-23 18:31:59 -07002060 /**
2061 * If display metrics changed, overrides are not set and it's not just a rotation - update base
2062 * values.
2063 */
2064 private void updateBaseDisplayMetricsIfNeeded() {
2065 // Get real display metrics without overrides from WM.
2066 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
2067 final int orientation = mDisplayInfo.rotation;
2068 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2069 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
2070 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
2071 final int newDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002072 final DisplayCutout newCutout = mDisplayInfo.displayCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07002073
2074 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
2075 || mInitialDisplayHeight != newHeight
Adrian Roos1cf585052018-01-03 18:43:27 +01002076 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi
2077 || !Objects.equals(mInitialDisplayCutout, newCutout);
Andrii Kuliancd097992017-03-23 18:31:59 -07002078
2079 if (displayMetricsChanged) {
2080 // Check if display size or density is forced.
2081 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
2082 || mBaseDisplayHeight != mInitialDisplayHeight;
2083 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
2084
2085 // If there is an override set for base values - use it, otherwise use new values.
2086 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
2087 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
2088 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
2089
2090 // Real display metrics changed, so we should also update initial values.
2091 mInitialDisplayWidth = newWidth;
2092 mInitialDisplayHeight = newHeight;
2093 mInitialDisplayDensity = newDensity;
Adrian Roos1cf585052018-01-03 18:43:27 +01002094 mInitialDisplayCutout = newCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07002095 mService.reconfigureDisplayLocked(this);
2096 }
2097 }
2098
Bryce Lee27cec322017-03-21 09:41:37 -07002099 /** Sets the maximum width the screen resolution can be */
2100 void setMaxUiWidth(int width) {
2101 if (DEBUG_DISPLAY) {
2102 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
2103 }
2104
2105 mMaxUiWidth = width;
2106
2107 // Update existing metrics.
2108 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
2109 }
2110
2111 /** Update base (override) display metrics. */
2112 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
2113 mBaseDisplayWidth = baseWidth;
2114 mBaseDisplayHeight = baseHeight;
2115 mBaseDisplayDensity = baseDensity;
2116
2117 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
2118 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
2119 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
2120 mBaseDisplayWidth = mMaxUiWidth;
2121
2122 if (DEBUG_DISPLAY) {
2123 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
2124 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
2125 + " on display:" + getDisplayId());
2126 }
2127 }
2128
2129 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08002130
2131 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07002132 }
2133
Riddle Hsuf53da812018-08-15 22:00:27 +08002134 /**
2135 * Forces this display to use the specified density.
2136 *
2137 * @param density The density in DPI to use. If the value equals to initial density, the setting
2138 * will be cleared.
2139 * @param userId The target user to apply. Only meaningful when this is default display. If the
2140 * user id is {@link UserHandle#USER_CURRENT}, it means to apply current settings
2141 * so only need to configure display.
2142 */
2143 void setForcedDensity(int density, int userId) {
2144 final boolean clear = density == mInitialDisplayDensity;
2145 final boolean updateCurrent = userId == UserHandle.USER_CURRENT;
2146 if (mService.mCurrentUserId == userId || updateCurrent) {
2147 mBaseDisplayDensity = density;
2148 mService.reconfigureDisplayLocked(this);
2149 }
2150 if (updateCurrent) {
2151 // We are applying existing settings so no need to save it again.
2152 return;
2153 }
2154
2155 if (density == mInitialDisplayDensity) {
2156 density = 0;
2157 }
Chilun8753ad32018-10-09 15:56:45 +08002158 mService.mDisplayWindowSettings.setForcedDensity(this, density, userId);
Riddle Hsuf53da812018-08-15 22:00:27 +08002159 }
2160
2161 /** @param mode {@link #FORCE_SCALING_MODE_AUTO} or {@link #FORCE_SCALING_MODE_DISABLED}. */
2162 void setForcedScalingMode(@ForceScalingMode int mode) {
2163 if (mode != FORCE_SCALING_MODE_DISABLED) {
2164 mode = FORCE_SCALING_MODE_AUTO;
2165 }
2166
2167 mDisplayScalingDisabled = (mode != FORCE_SCALING_MODE_AUTO);
2168 Slog.i(TAG_WM, "Using display scaling mode: " + (mDisplayScalingDisabled ? "off" : "auto"));
2169 mService.reconfigureDisplayLocked(this);
2170
Chilun8753ad32018-10-09 15:56:45 +08002171 mService.mDisplayWindowSettings.setForcedScalingMode(this, mode);
Riddle Hsuf53da812018-08-15 22:00:27 +08002172 }
2173
2174 /** If the given width and height equal to initial size, the setting will be cleared. */
2175 void setForcedSize(int width, int height) {
2176 final boolean clear = mInitialDisplayWidth == width && mInitialDisplayHeight == height;
2177 if (!clear) {
2178 // Set some sort of reasonable bounds on the size of the display that we will try
2179 // to emulate.
2180 final int minSize = 200;
2181 final int maxScale = 2;
2182 width = Math.min(Math.max(width, minSize), mInitialDisplayWidth * maxScale);
2183 height = Math.min(Math.max(height, minSize), mInitialDisplayHeight * maxScale);
2184 }
2185
2186 Slog.i(TAG_WM, "Using new display size: " + width + "x" + height);
2187 updateBaseDisplayMetrics(width, height, mBaseDisplayDensity);
2188 mService.reconfigureDisplayLocked(this);
2189
2190 if (clear) {
2191 width = height = 0;
2192 }
Chilun8753ad32018-10-09 15:56:45 +08002193 mService.mDisplayWindowSettings.setForcedSize(this, width, height);
Riddle Hsuf53da812018-08-15 22:00:27 +08002194 }
2195
Wale Ogunwaledb506192017-12-08 10:57:32 -08002196 void getStableRect(Rect out) {
2197 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08002198 }
2199
Wale Ogunwale61911492017-10-11 08:50:50 -07002200 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002201 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
2202 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07002203
Wale Ogunwale61911492017-10-11 08:50:50 -07002204 final TaskStack stack = new TaskStack(mService, stackId, controller);
2205 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002206 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07002207 }
2208
Andrii Kulian51c1b672017-04-07 18:39:32 -07002209 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002210 final DisplayContent prevDc = stack.getDisplayContent();
2211 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07002212 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
2213 + " which is not currently attached to any display");
2214 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002215 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07002216 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
2217 + " to its current displayId=" + mDisplayId);
2218 }
2219
lumark588a3e82018-07-20 18:53:54 +08002220 // Clean up all pending transitions when stack reparent to another display.
2221 stack.forAllAppWindows(AppWindowToken::removeFromPendingTransition);
2222
Wale Ogunwale61911492017-10-11 08:50:50 -07002223 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07002224 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08002225 }
2226
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002227 @Override
2228 protected void addChild(DisplayChildWindowContainer child,
2229 Comparator<DisplayChildWindowContainer> comparator) {
2230 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2231 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002232
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002233 @Override
2234 protected void addChild(DisplayChildWindowContainer child, int index) {
2235 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2236 }
2237
2238 @Override
2239 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002240 // Only allow removal of direct children from this display if the display is in the process
2241 // of been removed.
2242 if (mRemovingDisplay) {
2243 super.removeChild(child);
2244 return;
2245 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002246 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002247 }
2248
Andrii Kuliand2765632016-12-12 22:26:34 -08002249 @Override
2250 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
2251 // Children of the display are statically ordered, so the real intention here is to perform
2252 // the operation on the display and not the static direct children.
2253 getParent().positionChildAt(position, this, includingParents);
2254 }
2255
Riddle Hsu57831b52018-07-27 00:31:48 +08002256 void positionStackAt(int position, TaskStack child, boolean includingParents) {
2257 mTaskStackContainers.positionChildAt(position, child, includingParents);
Winson Chung59a47ded2018-01-25 17:46:06 +00002258 layoutAndAssignWindowLayersIfNeeded();
2259 }
2260
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002261 /**
2262 * Used to obtain task ID when user taps on coordinate (x, y) in this display, and outside
2263 * current task in focus.
2264 *
2265 * This returns the task ID of the foremost task at (x, y) if the task is not home. Otherwise it
2266 * returns -1.
2267 *
2268 * @param x horizontal coordinate of the tap position
2269 * @param y vertical coordinate of the tap position
2270 * @return the task ID if a non-home task is found; -1 if not
2271 */
2272 int taskForTapOutside(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002273 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2274 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Garfield Tanf2e0a9e2018-10-16 09:42:02 -07002275 if (stack.isActivityTypeHome() && !stack.inMultiWindowMode()) {
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002276 // We skip not only home stack, but also everything behind home because user can't
Garfield Tanf2e0a9e2018-10-16 09:42:02 -07002277 // see them when home stack is isn't in multi-window mode.
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002278 break;
2279 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002280 final int taskId = stack.taskIdFromPoint(x, y);
2281 if (taskId != -1) {
2282 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07002283 }
2284 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002285 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07002286 }
2287
Chong Zhang8e89b312015-09-09 15:09:30 -07002288 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08002289 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07002290 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07002291 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07002292 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002293 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002294 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07002295 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2296 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002297 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002298 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002299 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002300
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002301 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
2302 if (mTmpTaskForResizePointSearchResult.searchDone) {
2303 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07002304 }
2305 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002306 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002307 }
2308
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002309 void updateTouchExcludeRegion() {
2310 final Task focusedTask = (mFocusedApp != null ? mFocusedApp.getTask() : null);
David Stevensee9e2772017-02-09 16:30:27 -08002311 if (focusedTask == null) {
2312 mTouchExcludeRegion.setEmpty();
2313 } else {
2314 mTouchExcludeRegion.set(mBaseDisplayRect);
2315 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
2316 mTmpRect2.setEmpty();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002317 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0;
2318 --stackNdx) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002319 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002320 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
2321 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08002322 }
2323 // If we removed the focused task above, add it back and only leave its
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002324 // outside touch area in the exclusion. TapDetector is not interested in
David Stevensee9e2772017-02-09 16:30:27 -08002325 // any touch inside the focused task itself.
2326 if (!mTmpRect2.isEmpty()) {
2327 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
2328 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08002329 }
lumark90120a82018-08-15 00:33:03 +08002330 if (mInputMethodWindow != null && mInputMethodWindow.isVisibleLw()) {
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002331 // If the input method is visible and the user is typing, we don't want these touch
2332 // events to be intercepted and used to change focus. This would likely cause a
2333 // disappearance of the input method.
lumark90120a82018-08-15 00:33:03 +08002334 mInputMethodWindow.getTouchableRegion(mTmpRegion);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002335 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002336 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002337 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002338 final WindowState win = mTapExcludedWindows.get(i);
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002339 win.getTouchableRegion(mTmpRegion);
2340 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
2341 }
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002342 for (int i = mTapExcludeProvidingWindows.size() - 1; i >= 0; i--) {
2343 final WindowState win = mTapExcludeProvidingWindows.valueAt(i);
2344 win.amendTapExcludeRegion(mTouchExcludeRegion);
2345 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08002346 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07002347 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002348 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07002349 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002350 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
2351 }
Craig Mautner1bef3892015-02-17 15:09:47 -08002352 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07002353 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08002354 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07002355 }
2356
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002357 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07002358 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002359 super.switchUser();
2360 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07002361 }
2362
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07002363 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002364 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2365 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07002366 }
2367 }
2368
Wale Ogunwale10124582016-09-15 20:25:50 -07002369 @Override
2370 void removeIfPossible() {
2371 if (isAnimating()) {
2372 mDeferredRemoval = true;
2373 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07002374 }
Wale Ogunwale10124582016-09-15 20:25:50 -07002375 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07002376 }
2377
Wale Ogunwale10124582016-09-15 20:25:50 -07002378 @Override
2379 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002380 mRemovingDisplay = true;
2381 try {
lumark588a3e82018-07-20 18:53:54 +08002382 // Clear all transitions & screen frozen states when removing display.
2383 mOpeningApps.clear();
2384 mClosingApps.clear();
2385 mUnknownAppVisibilityController.clear();
2386 mAppTransition.removeAppTransitionTimeoutCallbacks();
2387 handleAnimatingStoppedAndTransition();
2388 mService.stopFreezingDisplayLocked();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002389 super.removeImmediately();
2390 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Arthur Hungbe5ce212018-09-13 18:41:56 +08002391 if (mPointerEventDispatcher != null && mTapDetector != null) {
2392 unregisterPointerEventListener(mTapDetector);
2393 unregisterPointerEventListener(mService.mMousePositionTracker);
2394 mTapDetector = null;
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002395 }
Jorim Jaggia3844032018-01-03 15:54:43 +01002396 mService.mAnimator.removeDisplayLocked(mDisplayId);
Louis Chang207f9112018-07-30 08:58:54 +08002397 mWindowingLayer.release();
2398 mOverlayLayer.release();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002399 } finally {
Riddle Hsu4e611772018-10-31 18:58:28 +08002400 mDisplayReady = false;
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002401 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08002402 }
Robert Carr679ccb02018-08-08 15:32:35 -07002403
Tiger Huang7c610aa2018-10-27 00:01:01 +08002404 mDisplayPolicy.onDisplayRemoved();
Riddle Hsuf53da812018-08-15 22:00:27 +08002405 mService.mWindowPlacerLocked.requestTraversal();
Craig Mautner95da1082014-02-24 17:54:35 -08002406 }
2407
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002408 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07002409 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002410 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002411 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
2412
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002413 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002414 removeImmediately();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002415 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08002416 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002417 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08002418 }
2419
Andrii Kulian0214ed92017-05-16 13:44:05 -07002420 /** @return 'true' if removal of this display content is deferred due to active animation. */
2421 boolean isRemovalDeferred() {
2422 return mDeferredRemoval;
2423 }
2424
Wale Ogunwale10124582016-09-15 20:25:50 -07002425 boolean animateForIme(float interpolatedValue, float animationTarget,
2426 float dividerAnimationTarget) {
2427 boolean updated = false;
2428
Wale Ogunwale61911492017-10-11 08:50:50 -07002429 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2430 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002431 if (stack == null || !stack.isAdjustedForIme()) {
2432 continue;
2433 }
2434
2435 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
2436 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2437 updated = true;
2438 } else {
2439 mDividerControllerLocked.mLastAnimationProgress =
2440 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
2441 mDividerControllerLocked.mLastDividerProgress =
2442 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
2443 updated |= stack.updateAdjustForIme(
2444 mDividerControllerLocked.mLastAnimationProgress,
2445 mDividerControllerLocked.mLastDividerProgress,
2446 false /* force */);
2447 }
2448 if (interpolatedValue >= 1f) {
2449 stack.endImeAdjustAnimation();
2450 }
2451 }
2452
2453 return updated;
2454 }
2455
2456 boolean clearImeAdjustAnimation() {
2457 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002458 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2459 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002460 if (stack != null && stack.isAdjustedForIme()) {
2461 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2462 changed = true;
2463 }
2464 }
2465 return changed;
2466 }
2467
2468 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002469 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2470 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002471 if (stack.isVisible() && stack.isAdjustedForIme()) {
2472 stack.beginImeAdjustAnimation();
2473 }
2474 }
2475 }
2476
2477 void adjustForImeIfNeeded() {
lumark90120a82018-08-15 00:33:03 +08002478 final WindowState imeWin = mInputMethodWindow;
Wale Ogunwale10124582016-09-15 20:25:50 -07002479 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2480 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002481 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002482 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2483 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2484 imeTargetStack.getDockSide() : DOCKED_INVALID;
2485 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2486 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2487 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002488 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002489 final boolean imeHeightChanged = imeVisible &&
2490 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2491
2492 // The divider could be adjusted for IME position, or be thinner than usual,
2493 // or both. There are three possible cases:
2494 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2495 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2496 // - If IME is not visible, divider is not moved and is normal width.
2497
2498 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002499 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2500 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002501 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002502 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2503 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002504 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2505 } else {
2506 stack.resetAdjustedForIme(false);
2507 }
2508 }
2509 mDividerControllerLocked.setAdjustedForIme(
2510 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2511 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002512 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2513 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002514 stack.resetAdjustedForIme(!dockVisible);
2515 }
2516 mDividerControllerLocked.setAdjustedForIme(
2517 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2518 }
Winson Chung655332c2016-10-31 13:14:28 -07002519 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002520 }
2521
2522 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002523 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2524 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002525 stack.prepareFreezingTaskBounds();
2526 }
2527 }
2528
Wale Ogunwale94744212015-09-21 19:01:47 -07002529 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002530 getBounds(mTmpRect, newRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002531 rotateBounds(mTmpRect, oldRotation, newRotation, bounds);
2532 }
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002533
Evan Rosky39b6f232018-10-30 18:35:41 -07002534 void rotateBounds(Rect parentBounds, int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002535 // Compute a transform matrix to undo the coordinate space transformation,
2536 // and present the window at the same physical position it previously occupied.
2537 final int deltaRotation = deltaRotation(newRotation, oldRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002538 createRotationMatrix(
2539 deltaRotation, parentBounds.width(), parentBounds.height(), mTmpMatrix);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002540
2541 mTmpRectF.set(bounds);
2542 mTmpMatrix.mapRect(mTmpRectF);
2543 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002544 }
2545
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002546 static int deltaRotation(int oldRotation, int newRotation) {
2547 int delta = newRotation - oldRotation;
2548 if (delta < 0) delta += 4;
2549 return delta;
2550 }
2551
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002552 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002553 Matrix outMatrix) {
2554 // For rotations without Z-ordering we don't need the target rectangle's position.
2555 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2556 displayHeight, outMatrix);
2557 }
2558
2559 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2560 float displayWidth, float displayHeight, Matrix outMatrix) {
2561 switch (rotation) {
2562 case ROTATION_0:
2563 outMatrix.reset();
2564 break;
2565 case ROTATION_270:
2566 outMatrix.setRotate(270, 0, 0);
2567 outMatrix.postTranslate(0, displayHeight);
2568 outMatrix.postTranslate(rectTop, 0);
2569 break;
2570 case ROTATION_180:
2571 outMatrix.reset();
2572 break;
2573 case ROTATION_90:
2574 outMatrix.setRotate(90, 0, 0);
2575 outMatrix.postTranslate(displayWidth, 0);
2576 outMatrix.postTranslate(-rectTop, rectLeft);
2577 break;
2578 }
2579 }
2580
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002581 @CallSuper
2582 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002583 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002584 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002585 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002586 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002587 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2588 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002589 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002590 }
2591 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2592 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002593 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2594 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002595 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002596 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002597 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2598 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002599 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002600 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002601 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2602 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002603 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002604 }
2605 proto.write(DPI, mBaseDisplayDensity);
2606 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002607 proto.write(ROTATION, mRotation);
2608 final ScreenRotationAnimation screenRotationAnimation =
2609 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2610 if (screenRotationAnimation != null) {
2611 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2612 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002613 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
lumark588a3e82018-07-20 18:53:54 +08002614 mAppTransition.writeToProto(proto, APP_TRANSITION);
Louis Chang7501e332018-08-20 13:08:39 +08002615 proto.write(SURFACE_SIZE, mSurfaceSize);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002616 if (mFocusedApp != null) {
2617 mFocusedApp.writeNameToProto(proto, FOCUSED_APP);
2618 }
Steven Timotiusaf03df62017-07-18 16:56:43 -07002619 proto.end(token);
2620 }
2621
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002622 @Override
2623 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2624 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002625 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2626 final String subPrefix = " " + prefix;
2627 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2628 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2629 pw.print("dpi");
2630 if (mInitialDisplayWidth != mBaseDisplayWidth
2631 || mInitialDisplayHeight != mBaseDisplayHeight
2632 || mInitialDisplayDensity != mBaseDisplayDensity) {
2633 pw.print(" base=");
2634 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2635 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2636 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002637 if (mDisplayScalingDisabled) {
2638 pw.println(" noscale");
2639 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002640 pw.print(" cur=");
2641 pw.print(mDisplayInfo.logicalWidth);
2642 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2643 pw.print(" app=");
2644 pw.print(mDisplayInfo.appWidth);
2645 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2646 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2647 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2648 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2649 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002650 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002651 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002652 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002653
Craig Mautnerdc548482014-02-05 13:35:24 -08002654 pw.println();
Adrian Roos5251b1d2018-03-23 18:57:43 +01002655 pw.print(prefix); pw.print("mLayoutSeq="); pw.println(mLayoutSeq);
Riddle Hsu654a6f92018-07-13 22:59:36 +08002656 pw.print(prefix);
2657 pw.print("mDeferredRotationPauseCount="); pw.println(mDeferredRotationPauseCount);
Adrian Roos5251b1d2018-03-23 18:57:43 +01002658
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002659 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
2660 if (mLastFocus != mCurrentFocus) {
2661 pw.print(" mLastFocus="); pw.println(mLastFocus);
2662 }
2663 if (mLosingFocus.size() > 0) {
2664 pw.println();
2665 pw.println(" Windows losing focus:");
2666 for (int i = mLosingFocus.size() - 1; i >= 0; i--) {
2667 final WindowState w = mLosingFocus.get(i);
2668 pw.print(" Losing #"); pw.print(i); pw.print(' ');
2669 pw.print(w);
2670 if (dumpAll) {
2671 pw.println(":");
2672 w.dump(pw, " ", true);
2673 } else {
2674 pw.println();
2675 }
2676 }
2677 }
2678 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
Tiger Huang7c610aa2018-10-27 00:01:01 +08002679 if (mLastStatusBarVisibility != 0) {
2680 pw.print(" mLastStatusBarVisibility=0x");
2681 pw.println(Integer.toHexString(mLastStatusBarVisibility));
2682 }
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002683
Adrian Roos5251b1d2018-03-23 18:57:43 +01002684 pw.println();
wilsonshihc32538e2018-11-07 17:27:34 +08002685 mWallpaperController.dump(pw, " ");
2686
2687 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002688 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002689 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2690 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002691 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002692 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002693
Craig Mautnerdc548482014-02-05 13:35:24 -08002694 pw.println();
2695 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002696 pw.println();
2697 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002698 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002699 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002700 pw.print(" Exiting #"); pw.print(i);
2701 pw.print(' '); pw.print(token);
2702 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002703 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002704 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002705 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002706
Jorim Jaggi31f71702016-05-04 16:43:04 -07002707 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002708
2709 // Dump stack references
2710 final TaskStack homeStack = getHomeStack();
2711 if (homeStack != null) {
2712 pw.println(prefix + "homeStack=" + homeStack.getName());
2713 }
2714 final TaskStack pinnedStack = getPinnedStack();
2715 if (pinnedStack != null) {
2716 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2717 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002718 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002719 if (splitScreenPrimaryStack != null) {
2720 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2721 }
2722
2723 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002724 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002725 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002726 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002727
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002728 pw.println();
2729 mDisplayFrames.dump(prefix, pw);
Arthur Hung95b38a92018-07-20 18:56:12 +08002730 pw.println();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08002731 mDisplayPolicy.dump(prefix, pw);
2732 pw.println();
Riddle Hsuad256a12018-07-18 16:11:30 +08002733 mDisplayRotation.dump(prefix, pw);
2734 pw.println();
Arthur Hung95b38a92018-07-20 18:56:12 +08002735 mInputMonitor.dump(pw, " ");
Craig Mautner59c00972012-07-30 12:10:24 -07002736 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002737
2738 @Override
2739 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002740 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002741 }
2742
2743 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002744 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002745 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002746
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002747 /** Returns true if the stack in the windowing mode is visible. */
2748 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002749 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002750 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002751 }
2752
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002753 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002754 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002755 final int x = (int) xf;
2756 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002757 final WindowState touchedWin = getWindow(w -> {
2758 final int flags = w.mAttrs.flags;
2759 if (!w.isVisibleLw()) {
2760 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002761 }
2762 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002763 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002764 }
2765
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002766 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002767 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002768 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002769 }
2770
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002771 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002772
2773 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002774 return mTmpRegion.contains(x, y) || touchFlags == 0;
2775 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002776
2777 return touchedWin;
2778 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002779
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002780 boolean canAddToastWindowForUid(int uid) {
2781 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002782 // Also if the app is focused adding more than one toast at
2783 // a time for better backwards compatibility.
2784 final WindowState focusedWindowForUid = getWindow(w ->
2785 w.mOwnerUid == uid && w.isFocused());
2786 if (focusedWindowForUid != null) {
2787 return true;
2788 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002789 final WindowState win = getWindow(w ->
2790 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2791 && !w.mWindowRemovalAllowed);
2792 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002793 }
2794
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002795 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002796 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2797 return;
2798 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002799
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002800 // Used to communicate the old focus to the callback method.
2801 mTmpWindow = oldFocus;
2802
2803 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002804 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002805
2806 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002807 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002808
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002809 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002810
2811 if (mTmpWindow == null) {
2812 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2813 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002814 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002815 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002816 }
2817
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002818
2819 /**
2820 * Update the focused window and make some adjustments if the focus has changed.
2821 *
2822 * @param mode Indicates the situation we are in. Possible modes are:
2823 * {@link WindowManagerService#UPDATE_FOCUS_NORMAL},
2824 * {@link WindowManagerService#UPDATE_FOCUS_PLACING_SURFACES},
2825 * {@link WindowManagerService#UPDATE_FOCUS_WILL_PLACE_SURFACES},
2826 * {@link WindowManagerService#UPDATE_FOCUS_REMOVING_FOCUS}
2827 * @param updateInputWindows Whether to sync the window information to the input module.
2828 * @return {@code true} if the focused window has changed.
2829 */
2830 boolean updateFocusedWindowLocked(int mode, boolean updateInputWindows, boolean focusFound) {
2831 final WindowState newFocus = findFocusedWindow();
2832 if (mCurrentFocus == newFocus) {
2833 return false;
2834 }
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002835 boolean imWindowChanged = false;
2836 // TODO (b/111080190): Multi-Session IME
2837 if (!focusFound) {
2838 final WindowState imWindow = mInputMethodWindow;
2839 if (imWindow != null) {
lumarkff0ab692018-11-05 20:32:30 +08002840 final WindowState prevTarget = mInputMethodTarget;
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002841
2842 final WindowState newTarget = computeImeTarget(true /* updateImeTarget*/);
2843 imWindowChanged = prevTarget != newTarget;
2844
2845 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS
2846 && mode != UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2847 assignWindowLayers(false /* setLayoutNeeded */);
2848 }
2849 }
2850 }
2851
2852 if (imWindowChanged) {
2853 mService.mWindowsChanged = true;
2854 setLayoutNeeded();
2855 }
2856
2857 if (DEBUG_FOCUS_LIGHT || mService.localLOGV) Slog.v(TAG_WM, "Changing focus from "
2858 + mCurrentFocus + " to " + newFocus + " displayId=" + getDisplayId()
2859 + " Callers=" + Debug.getCallers(4));
2860 final WindowState oldFocus = mCurrentFocus;
2861 mCurrentFocus = newFocus;
2862 mLosingFocus.remove(newFocus);
2863
2864 if (newFocus != null) {
2865 mWinAddedSinceNullFocus.clear();
2866 mWinRemovedSinceNullFocus.clear();
2867
2868 if (newFocus.canReceiveKeys()) {
2869 // Displaying a window implicitly causes dispatching to be unpaused.
2870 // This is to protect against bugs if someone pauses dispatching but
2871 // forgets to resume.
2872 newFocus.mToken.paused = false;
2873 }
2874 }
2875
Tiger Huang7c610aa2018-10-27 00:01:01 +08002876 int focusChanged = getDisplayPolicy().focusChangedLw(oldFocus, newFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002877
2878 if (imWindowChanged && oldFocus != mInputMethodWindow) {
2879 // Focus of the input method window changed. Perform layout if needed.
2880 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2881 performLayout(true /*initial*/, updateInputWindows);
2882 focusChanged &= ~FINISH_LAYOUT_REDO_LAYOUT;
2883 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2884 // Client will do the layout, but we need to assign layers
2885 // for handleNewWindowLocked() below.
2886 assignWindowLayers(false /* setLayoutNeeded */);
2887 }
2888 }
2889
2890 if ((focusChanged & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2891 // The change in focus caused us to need to do a layout. Okay.
2892 setLayoutNeeded();
2893 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2894 performLayout(true /*initial*/, updateInputWindows);
2895 } else if (mode == UPDATE_FOCUS_REMOVING_FOCUS) {
2896 mService.mRoot.performSurfacePlacement(false);
2897 }
2898 }
2899
2900 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
2901 // If we defer assigning layers, then the caller is responsible for doing this part.
2902 getInputMonitor().setInputFocusLw(newFocus, updateInputWindows);
2903 }
2904
2905 adjustForImeIfNeeded();
2906
2907 // We may need to schedule some toast windows to be removed. The toasts for an app that
2908 // does not have input focus are removed within a timeout to prevent apps to redress
2909 // other apps' UI.
2910 scheduleToastWindowsTimeoutIfNeededLocked(oldFocus, newFocus);
2911
2912 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2913 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
2914 }
2915 return true;
2916 }
2917
2918 /**
2919 * Set the new focused app to this display.
2920 *
2921 * @param newFocus the new focused AppWindowToken.
2922 * @return true if the focused app is changed.
2923 */
2924 boolean setFocusedApp(AppWindowToken newFocus) {
2925 if (newFocus != null) {
2926 final DisplayContent appDisplay = newFocus.getDisplayContent();
2927 if (appDisplay != this) {
2928 throw new IllegalStateException(newFocus + " is not on " + getName()
2929 + " but " + ((appDisplay != null) ? appDisplay.getName() : "none"));
2930 }
2931 }
2932 if (mFocusedApp == newFocus) {
2933 return false;
2934 }
2935 mFocusedApp = newFocus;
2936 getInputMonitor().setFocusedAppLw(newFocus);
2937 updateTouchExcludeRegion();
2938 return true;
2939 }
2940
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002941 /** Updates the layer assignment of windows on this display. */
2942 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002943 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07002944 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002945 if (setLayoutNeeded) {
2946 setLayoutNeeded();
2947 }
Robert Carrb1579c82017-09-05 14:54:47 -07002948
Robert Carrf59b8dd2017-10-02 18:58:36 -07002949 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002950 // the application of this transaction until the animation pass triggers
2951 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2952 // the hiding and showing of surfaces.
2953 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002954 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002955 }
2956
Wale Ogunwale1666e312016-12-16 11:27:18 -08002957 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2958 // moving containers or resizing them. Need to investigate the best way to have it automatically
2959 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002960 void layoutAndAssignWindowLayersIfNeeded() {
2961 mService.mWindowsChanged = true;
2962 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002963
2964 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2965 false /*updateInputWindows*/)) {
2966 assignWindowLayers(false /* setLayoutNeeded */);
2967 }
2968
Arthur Hung95b38a92018-07-20 18:56:12 +08002969 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002970 mService.mWindowPlacerLocked.performSurfacePlacement();
Arthur Hung95b38a92018-07-20 18:56:12 +08002971 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002972 }
2973
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002974 /** Returns true if a leaked surface was destroyed */
2975 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002976 // Used to indicate that a surface was leaked.
2977 mTmpWindow = null;
2978 forAllWindows(w -> {
2979 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002980 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002981 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002982 }
2983 if (!mService.mSessions.contains(wsa.mSession)) {
2984 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002985 + w + " surface=" + wsa.mSurfaceController
2986 + " token=" + w.mToken
2987 + " pid=" + w.mSession.mPid
2988 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002989 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002990 mService.mForceRemoves.add(w);
2991 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002992 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002993 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002994 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002995 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002996 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002997 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002998 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002999 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003000 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003001
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003002 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003003 }
3004
3005 /**
lumark90120a82018-08-15 00:33:03 +08003006 * Set input method window for the display.
3007 * @param win Set when window added or Null when destroyed.
3008 */
3009 void setInputMethodWindowLocked(WindowState win) {
3010 mInputMethodWindow = win;
lumark70865a82018-09-17 16:56:26 +08003011 // Update display configuration for IME process.
3012 if (mInputMethodWindow != null) {
3013 final int imePid = mInputMethodWindow.mSession.mPid;
3014 mService.mAtmInternal.onImeWindowSetOnDisplay(imePid,
3015 mInputMethodWindow.getDisplayId());
3016 }
lumark90120a82018-08-15 00:33:03 +08003017 computeImeTarget(true /* updateImeTarget */);
3018 }
3019
3020 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003021 * Determine and return the window that should be the IME target.
3022 * @param updateImeTarget If true the system IME target will be updated to match what we found.
3023 * @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 +00003024 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003025 WindowState computeImeTarget(boolean updateImeTarget) {
lumark90120a82018-08-15 00:33:03 +08003026 if (mInputMethodWindow == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003027 // There isn't an IME so there shouldn't be a target...That was easy!
3028 if (updateImeTarget) {
3029 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
lumarkff0ab692018-11-05 20:32:30 +08003030 + mInputMethodTarget + " to null since mInputMethodWindow is null");
3031 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003032 }
3033 return null;
3034 }
3035
lumarkff0ab692018-11-05 20:32:30 +08003036 final WindowState curTarget = mInputMethodTarget;
Chavi Weingarten3a748552018-05-14 17:32:42 +00003037 if (!canUpdateImeTarget()) {
3038 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Defer updating IME target");
3039 return curTarget;
3040 }
3041
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003042 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
3043 // same display. Or even when the current IME/target are not on the same screen as the next
3044 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08003045 mUpdateImeTarget = updateImeTarget;
3046 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003047
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003048
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003049 // Yet more tricksyness! If this window is a "starting" window, we do actually want
3050 // to be on top of it, but it is not -really- where input will go. So look down below
3051 // for a real window to target...
3052 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
3053 final AppWindowToken token = target.mAppToken;
3054 if (token != null) {
3055 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
3056 if (betterTarget != null) {
3057 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003058 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003059 }
3060 }
3061
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003062 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
lumarkff0ab692018-11-05 20:32:30 +08003063 "Proposed new IME target: " + target + " for display: " + getDisplayId());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003064
chaviw1c13ad32018-06-12 16:44:23 -07003065 // Now, a special case -- if the last target's window is in the process of exiting, but
3066 // not removed, and the new target is home, keep on the last target to avoid flicker.
3067 // Home is a special case since its above other stacks in the ordering list, but layed
3068 // out below the others.
3069 if (curTarget != null && !curTarget.mRemoved && curTarget.isDisplayedLw()
3070 && curTarget.isClosing() && (target == null || target.isActivityTypeHome())) {
chaviw4ad54912018-05-30 11:05:44 -07003071 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "New target is home while current target is "
Chavi Weingarten3a748552018-05-14 17:32:42 +00003072 + "closing, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003073 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003074 }
3075
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003076 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
3077 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003078
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003079 if (target == null) {
3080 if (updateImeTarget) {
3081 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
3082 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
3083 + Debug.getCallers(4) : ""));
lumarkff0ab692018-11-05 20:32:30 +08003084 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003085 }
3086
3087 return null;
3088 }
3089
3090 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003091 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
3092 if (token != null) {
3093
3094 // Now some fun for dealing with window animations that modify the Z order. We need
3095 // to look at all windows below the current target that are in this app, finding the
3096 // highest visible one in layering.
3097 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003098 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003099 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003100 }
3101
3102 if (highestTarget != null) {
lumark588a3e82018-07-20 18:53:54 +08003103 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, mAppTransition + " " + highestTarget
Jorim Jaggi35d328a2018-08-14 17:00:20 +02003104 + " animating=" + highestTarget.isAnimating());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003105
lumark588a3e82018-07-20 18:53:54 +08003106 if (mAppTransition.isTransitionSet()) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003107 // If we are currently setting up for an animation, hold everything until we
3108 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003109 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003110 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003111 }
3112 }
3113 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003114
3115 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
3116 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003117 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003118 }
3119
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003120 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003121 }
3122
lumarkff0ab692018-11-05 20:32:30 +08003123 /**
3124 * Calling {@link #computeImeTarget(boolean)} to update the input method target window in
3125 * the candidate app window token if needed.
3126 */
3127 void computeImeTargetIfNeeded(AppWindowToken candidate) {
3128 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == candidate) {
3129 computeImeTarget(true /* updateImeTarget */);
3130 }
3131 }
3132
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003133 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
lumarkff0ab692018-11-05 20:32:30 +08003134 if (target == mInputMethodTarget && mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003135 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003136 }
3137
lumarkff0ab692018-11-05 20:32:30 +08003138 mInputMethodTarget = target;
3139 mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003140 assignWindowLayers(false /* setLayoutNeeded */);
3141 }
3142
3143 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
3144 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3145 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
3146 }
3147
3148 // Used to indicate we have reached the first window in the range we are interested in.
3149 mTmpWindow = null;
3150
3151 // TODO: Figure-out a more efficient way to do this.
3152 final WindowState candidate = getWindow(w -> {
3153 if (w == top) {
3154 // Reached the first window in the range we are interested in.
3155 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003156 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003157 if (mTmpWindow == null) {
3158 return false;
3159 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003160
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003161 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3162 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003163 }
3164 // If we reached the bottom of the range of windows we are considering,
3165 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003166 if (w == bottom) {
3167 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003168 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003169 return false;
3170 });
3171
3172 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003173 }
3174
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003175 void setLayoutNeeded() {
3176 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
3177 mLayoutNeeded = true;
3178 }
3179
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003180 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003181 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
3182 mLayoutNeeded = false;
3183 }
3184
3185 boolean isLayoutNeeded() {
3186 return mLayoutNeeded;
3187 }
3188
Wale Ogunwale02319a62016-09-26 15:21:22 -07003189 void dumpTokens(PrintWriter pw, boolean dumpAll) {
3190 if (mTokenMap.isEmpty()) {
3191 return;
3192 }
3193 pw.println(" Display #" + mDisplayId);
3194 final Iterator<WindowToken> it = mTokenMap.values().iterator();
3195 while (it.hasNext()) {
3196 final WindowToken token = it.next();
3197 pw.print(" ");
3198 pw.print(token);
3199 if (dumpAll) {
3200 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003201 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07003202 } else {
3203 pw.println();
3204 }
3205 }
lumark588a3e82018-07-20 18:53:54 +08003206
3207 if (!mOpeningApps.isEmpty() || !mClosingApps.isEmpty()) {
3208 pw.println();
3209 if (mOpeningApps.size() > 0) {
3210 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
3211 }
3212 if (mClosingApps.size() > 0) {
3213 pw.print(" mClosingApps="); pw.println(mClosingApps);
3214 }
3215 }
3216
3217 mUnknownAppVisibilityController.dump(pw, " ");
Wale Ogunwale02319a62016-09-26 15:21:22 -07003218 }
3219
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003220 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003221 final int[] index = new int[1];
3222 forAllWindows(w -> {
3223 final WindowStateAnimator wAnim = w.mWinAnimator;
3224 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
3225 index[0] = index[0] + 1;
3226 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003227 }
3228
Jorim Jaggife762342016-10-13 14:33:27 +02003229 /**
3230 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
3231 */
3232 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
3233 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003234 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02003235 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
3236 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01003237 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02003238 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003239 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02003240 }
3241
Wale Ogunwale494009b82016-10-21 09:01:38 -07003242 boolean checkWaitingForWindows() {
3243
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003244 mHaveBootMsg = false;
3245 mHaveApp = false;
3246 mHaveWallpaper = false;
3247 mHaveKeyguard = true;
3248
3249 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003250 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
3251 return true;
3252 }
3253 if (w.isDrawnLw()) {
3254 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003255 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003256 } else if (w.mAttrs.type == TYPE_APPLICATION
3257 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003258 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003259 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003260 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003261 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003262 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003263 }
3264 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003265 return false;
3266 });
3267
3268 if (visibleWindow != null) {
3269 // We have a visible window.
3270 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003271 }
3272
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003273 // if the wallpaper service is disabled on the device, we're never going to have
3274 // wallpaper, don't bother waiting for it
3275 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
3276 com.android.internal.R.bool.config_enableWallpaperService)
Daichi Hironoc1432222018-03-29 13:06:07 +09003277 && mService.mContext.getResources().getBoolean(
3278 com.android.internal.R.bool.config_checkWallpaperAtBoot)
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003279 && !mService.mOnlyCore;
3280
Wale Ogunwale494009b82016-10-21 09:01:38 -07003281 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
3282 "******** booted=" + mService.mSystemBooted
3283 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003284 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
3285 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
3286 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003287
3288 // If we are turning on the screen to show the boot message, don't do it until the boot
3289 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003290 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003291 return true;
3292 }
3293
3294 // If we are turning on the screen after the boot is completed normally, don't do so until
3295 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003296 if (mService.mSystemBooted
3297 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003298 return true;
3299 }
3300
3301 return false;
3302 }
3303
Jorim Jaggi8f520872018-08-14 17:00:20 +02003304 void updateWindowsForAnimator() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003305 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003306 }
3307
Jorim Jaggi8f520872018-08-14 17:00:20 +02003308 /**
3309 * Updates the {@link TaskStack#setAnimationBackground} for all windows.
3310 */
3311 void updateBackgroundForAnimator() {
Jorim Jaggi7156b652016-10-25 08:31:58 -07003312 resetAnimationBackgroundAnimator();
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003313 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003314 }
3315
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003316 boolean isInputMethodClientFocus(int uid, int pid) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003317 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003318 if (imFocus == null) {
3319 return false;
3320 }
3321
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003322 if (DEBUG_INPUT_METHOD) {
3323 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003324 Slog.i(TAG_WM, "Current focus: " + mCurrentFocus + " displayId=" + mDisplayId);
3325 Slog.i(TAG_WM, "Last focus: " + mLastFocus + " displayId=" + mDisplayId);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003326 }
3327
Wale Ogunwale494009b82016-10-21 09:01:38 -07003328 if (DEBUG_INPUT_METHOD) {
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003329 Slog.i(TAG_WM, "IM target uid/pid: " + imFocus.mSession.mUid
3330 + "/" + imFocus.mSession.mPid);
3331 Slog.i(TAG_WM, "Requesting client uid/pid: " + uid + "/" + pid);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003332 }
3333
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003334 return imFocus.mSession.mUid == uid && imFocus.mSession.mPid == pid;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003335 }
3336
3337 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003338 final WindowState win = getWindow(
3339 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
3340 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003341 }
3342
Tiger Huang7c610aa2018-10-27 00:01:01 +08003343 void statusBarVisibilityChanged(int visibility) {
3344 mLastStatusBarVisibility = visibility;
3345 visibility = getDisplayPolicy().adjustSystemUiVisibilityLw(visibility);
3346 updateStatusBarVisibilityLocked(visibility);
3347 }
3348
3349 private boolean updateStatusBarVisibilityLocked(int visibility) {
3350 if (mLastDispatchedSystemUiVisibility == visibility) {
3351 return false;
3352 }
3353 final int globalDiff = (visibility ^ mLastDispatchedSystemUiVisibility)
3354 // We are only interested in differences of one of the
3355 // clearable flags...
3356 & View.SYSTEM_UI_CLEARABLE_FLAGS
3357 // ...if it has actually been cleared.
3358 & ~visibility;
3359
3360 mLastDispatchedSystemUiVisibility = visibility;
3361 if (isDefaultDisplay) {
3362 mService.mInputManager.setSystemUiVisibility(visibility);
3363 }
3364 updateSystemUiVisibility(visibility, globalDiff);
3365 return true;
3366 }
3367
Wale Ogunwale494009b82016-10-21 09:01:38 -07003368 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003369 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003370 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003371 final int curValue = w.mSystemUiVisibility;
3372 final int diff = (curValue ^ visibility) & globalDiff;
3373 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003374 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003375 w.mSeq++;
3376 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003377 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003378 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
3379 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07003380 visibility, newValue, diff);
3381 }
3382 } catch (RemoteException e) {
3383 // so sorry
3384 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003385 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003386 }
3387
Tiger Huang7c610aa2018-10-27 00:01:01 +08003388 void reevaluateStatusBarVisibility() {
3389 int visibility = getDisplayPolicy().adjustSystemUiVisibilityLw(mLastStatusBarVisibility);
3390 if (updateStatusBarVisibilityLocked(visibility)) {
3391 mService.mWindowPlacerLocked.requestTraversal();
3392 }
3393 }
3394
Wale Ogunwale494009b82016-10-21 09:01:38 -07003395 void onWindowFreezeTimeout() {
3396 Slog.w(TAG_WM, "Window freeze timeout expired.");
3397 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003398
3399 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07003400 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003401 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003402 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07003403 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003404 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
3405 - mService.mDisplayFreezeTime);
3406 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003407 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003408 mService.mWindowPlacerLocked.performSurfacePlacement();
3409 }
3410
3411 void waitForAllWindowsDrawn() {
3412 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003413 forAllWindows(w -> {
3414 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
3415 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
3416 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003417 // Force add to mResizingWindows.
chaviw9c81e632018-07-31 11:17:52 -07003418 w.resetLastContentInsets();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003419 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003420 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003421 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003422 }
3423
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003424 // TODO: Super crazy long method that should be broken down...
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003425 void applySurfaceChangesTransaction(boolean recoveringMemory) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003426 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
3427
3428 mTmpUpdateAllDrawn.clear();
3429
3430 int repeats = 0;
3431 do {
3432 repeats++;
3433 if (repeats > 6) {
3434 Slog.w(TAG, "Animation repeat aborted after too many iterations");
3435 clearLayoutNeeded();
3436 break;
3437 }
3438
3439 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
3440 pendingLayoutChanges);
3441
wilsonshihc32538e2018-11-07 17:27:34 +08003442 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
3443 mWallpaperController.adjustWallpaperWindows();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003444 }
3445
Riddle Hsu654a6f92018-07-13 22:59:36 +08003446 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003447 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
Riddle Hsu4e611772018-10-31 18:58:28 +08003448 if (updateOrientationFromAppTokens()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003449 setLayoutNeeded();
Riddle Hsu4e611772018-10-31 18:58:28 +08003450 sendNewConfiguration();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003451 }
3452 }
3453
3454 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
3455 setLayoutNeeded();
3456 }
3457
3458 // FIRST LOOP: Perform a layout, if needed.
3459 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
3460 performLayout(repeats == 1, false /* updateInputWindows */);
3461 } else {
3462 Slog.w(TAG, "Layout repeat skipped after too many iterations");
3463 }
3464
3465 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
3466 pendingLayoutChanges = 0;
3467
Tiger Huang7c610aa2018-10-27 00:01:01 +08003468 mDisplayPolicy.beginPostLayoutPolicyLw();
3469 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
3470 pendingLayoutChanges |= mDisplayPolicy.finishPostLayoutPolicyLw();
3471 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
3472 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003473 } while (pendingLayoutChanges != 0);
3474
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003475 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003476
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003477 mTmpRecoveringMemory = recoveringMemory;
3478 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01003479 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003480
Jorim Jaggif1292892018-09-10 11:58:13 +02003481 mLastHasContent = mTmpApplySurfaceChangesTransactionState.displayHasContent;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003482 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Jorim Jaggif1292892018-09-10 11:58:13 +02003483 mLastHasContent,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003484 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
3485 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003486 true /* inTraversal, must call performTraversalInTrans... below */);
3487
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003488 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
3489 if (wallpaperVisible != mLastWallpaperVisible) {
3490 mLastWallpaperVisible = wallpaperVisible;
3491 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
3492 }
3493
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003494 while (!mTmpUpdateAllDrawn.isEmpty()) {
3495 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
3496 // See if any windows have been drawn, so they (and others associated with them)
3497 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07003498 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003499 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003500 }
3501
Bryce Leef3c6a472017-11-14 14:53:06 -08003502 private void updateBounds() {
Evan Rosky39b6f232018-10-30 18:35:41 -07003503 calculateBounds(mDisplayInfo, mTmpBounds);
Bryce Leef3c6a472017-11-14 14:53:06 -08003504 setBounds(mTmpBounds);
3505 }
3506
3507 // Determines the current display bounds based on the current state
Evan Rosky39b6f232018-10-30 18:35:41 -07003508 private void calculateBounds(DisplayInfo displayInfo, Rect out) {
Bryce Leef3c6a472017-11-14 14:53:06 -08003509 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Evan Rosky39b6f232018-10-30 18:35:41 -07003510 final int rotation = displayInfo.rotation;
3511 boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Bryce Leef3c6a472017-11-14 14:53:06 -08003512 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
3513 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Evan Rosky39b6f232018-10-30 18:35:41 -07003514 int width = displayInfo.logicalWidth;
Bryce Leef3c6a472017-11-14 14:53:06 -08003515 int left = (physWidth - width) / 2;
Evan Rosky39b6f232018-10-30 18:35:41 -07003516 int height = displayInfo.logicalHeight;
Bryce Leef3c6a472017-11-14 14:53:06 -08003517 int top = (physHeight - height) / 2;
3518 out.set(left, top, left + width, top + height);
3519 }
3520
3521 @Override
3522 public void getBounds(Rect out) {
Evan Rosky39b6f232018-10-30 18:35:41 -07003523 calculateBounds(mDisplayInfo, out);
Bryce Leef3c6a472017-11-14 14:53:06 -08003524 }
3525
3526 private void getBounds(Rect out, int orientation) {
3527 getBounds(out);
3528
3529 // Rotate the Rect if needed.
3530 final int currentRotation = mDisplayInfo.rotation;
3531 final int rotationDelta = deltaRotation(currentRotation, orientation);
3532 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
3533 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
3534 mTmpRectF.set(out);
3535 mTmpMatrix.mapRect(mTmpRectF);
3536 mTmpRectF.round(out);
3537 }
3538 }
3539
Louis Chang7501e332018-08-20 13:08:39 +08003540 int getSurfaceSize() {
3541 return mSurfaceSize;
3542 }
3543
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003544 void performLayout(boolean initial, boolean updateInputWindows) {
3545 if (!isLayoutNeeded()) {
3546 return;
3547 }
3548 clearLayoutNeeded();
3549
3550 final int dw = mDisplayInfo.logicalWidth;
3551 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003552 if (DEBUG_LAYOUT) {
3553 Slog.v(TAG, "-------------------------------------");
3554 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
3555 }
3556
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01003557 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
3558 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00003559 // TODO: Not sure if we really need to set the rotation here since we are updating from the
3560 // display info above...
3561 mDisplayFrames.mRotation = mRotation;
Tiger Huang7c610aa2018-10-27 00:01:01 +08003562 mDisplayPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003563
Adrian Roos5251b1d2018-03-23 18:57:43 +01003564 int seq = mLayoutSeq + 1;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003565 if (seq < 0) seq = 0;
Adrian Roos5251b1d2018-03-23 18:57:43 +01003566 mLayoutSeq = seq;
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 windows are
3569 // behind it.
3570 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003571 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003572
3573 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003574 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003575
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003576 // Used to indicate that we have processed the dream window and all additional attached
3577 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003578 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003579 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003580
3581 // Now perform layout of attached windows, which usually depend on the position of the
3582 // window they are attached to. XXX does not deal with windows that are attached to windows
3583 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003584 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003585
3586 // Window frames may have changed. Tell the input dispatcher about it.
Arthur Hung95b38a92018-07-20 18:56:12 +08003587 mInputMonitor.layoutInputConsumers(dw, dh);
3588 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003589 if (updateInputWindows) {
Arthur Hung95b38a92018-07-20 18:56:12 +08003590 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003591 }
3592
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003593 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
3594 }
3595
3596 /**
3597 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
3598 * In portrait mode, it grabs the full screenshot.
3599 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003600 * @param config of the output bitmap
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003601 */
chaviw0315a1a2018-03-05 15:28:35 -08003602 Bitmap screenshotDisplayLocked(Bitmap.Config config) {
3603 if (!mService.mPolicy.isScreenOn()) {
3604 if (DEBUG_SCREENSHOT) {
3605 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003606 }
chaviw0315a1a2018-03-05 15:28:35 -08003607 return null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003608 }
chaviwfbe47df2017-11-10 16:14:49 -08003609
chaviw0315a1a2018-03-05 15:28:35 -08003610 int dw = mDisplayInfo.logicalWidth;
3611 int dh = mDisplayInfo.logicalHeight;
chaviwfbe47df2017-11-10 16:14:49 -08003612
chaviw0315a1a2018-03-05 15:28:35 -08003613 if (dw <= 0 || dh <= 0) {
3614 return null;
3615 }
chaviwfbe47df2017-11-10 16:14:49 -08003616
chaviw0315a1a2018-03-05 15:28:35 -08003617 final Rect frame = new Rect(0, 0, dw, dh);
chaviwfbe47df2017-11-10 16:14:49 -08003618
chaviw0315a1a2018-03-05 15:28:35 -08003619 // The screenshot API does not apply the current screen rotation.
3620 int rot = mDisplay.getRotation();
chaviwfbe47df2017-11-10 16:14:49 -08003621
chaviw0315a1a2018-03-05 15:28:35 -08003622 if (rot == ROTATION_90 || rot == ROTATION_270) {
3623 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3624 }
chaviwfbe47df2017-11-10 16:14:49 -08003625
chaviw0315a1a2018-03-05 15:28:35 -08003626 // SurfaceFlinger is not aware of orientation, so convert our logical
3627 // crop to SurfaceFlinger's portrait orientation.
3628 convertCropForSurfaceFlinger(frame, rot, dw, dh);
3629
3630 final ScreenRotationAnimation screenRotationAnimation =
3631 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3632 final boolean inRotation = screenRotationAnimation != null &&
3633 screenRotationAnimation.isAnimating();
3634 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
3635
3636 // TODO(b/68392460): We should screenshot Task controls directly
3637 // but it's difficult at the moment as the Task doesn't have the
3638 // correct size set.
chaviw08520a02018-09-10 16:44:56 -07003639 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, inRotation, rot);
chaviw0315a1a2018-03-05 15:28:35 -08003640 if (bitmap == null) {
3641 Slog.w(TAG_WM, "Failed to take screenshot");
3642 return null;
3643 }
3644
3645 // Create a copy of the screenshot that is immutable and backed in ashmem.
3646 // This greatly reduces the overhead of passing the bitmap between processes.
3647 final Bitmap ret = bitmap.createAshmemBitmap(config);
3648 bitmap.recycle();
3649 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003650 }
3651
3652 // TODO: Can this use createRotationMatrix()?
3653 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3654 if (rot == Surface.ROTATION_90) {
3655 final int tmp = crop.top;
3656 crop.top = dw - crop.right;
3657 crop.right = crop.bottom;
3658 crop.bottom = dw - crop.left;
3659 crop.left = tmp;
3660 } else if (rot == Surface.ROTATION_180) {
3661 int tmp = crop.top;
3662 crop.top = dh - crop.bottom;
3663 crop.bottom = dh - tmp;
3664 tmp = crop.right;
3665 crop.right = dw - crop.left;
3666 crop.left = dw - tmp;
3667 } else if (rot == Surface.ROTATION_270) {
3668 final int tmp = crop.top;
3669 crop.top = crop.left;
3670 crop.left = dh - crop.bottom;
3671 crop.bottom = crop.right;
3672 crop.right = dh - tmp;
3673 }
3674 }
3675
3676 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003677 // Used to indicate the layout is needed.
3678 mTmpWindow = null;
3679
3680 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003681 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003682 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003683 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003684 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003685 w.setDisplayLayoutNeeded();
Vishnu Nairba183352018-11-21 11:16:49 -08003686 w.finishSeamlessRotation(true /* timeout */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003687 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003688 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003689
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003690 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003691 mService.mWindowPlacerLocked.performSurfacePlacement();
3692 }
3693 }
3694
Wale Ogunwale1666e312016-12-16 11:27:18 -08003695 void setExitingTokensHasVisible(boolean hasVisible) {
3696 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3697 mExitingTokens.get(i).hasVisible = hasVisible;
3698 }
3699
3700 // Initialize state of exiting applications.
3701 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3702 }
3703
3704 void removeExistingTokensIfPossible() {
3705 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3706 final WindowToken token = mExitingTokens.get(i);
3707 if (!token.hasVisible) {
3708 mExitingTokens.remove(i);
3709 }
3710 }
3711
3712 // Time to remove any exiting applications?
3713 mTaskStackContainers.removeExistingAppTokensIfPossible();
3714 }
3715
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003716 @Override
3717 void onDescendantOverrideConfigurationChanged() {
3718 setLayoutNeeded();
3719 mService.requestTraversal();
3720 }
3721
David Stevens9440dc82017-03-16 19:00:20 -07003722 boolean okToDisplay() {
3723 if (mDisplayId == DEFAULT_DISPLAY) {
3724 return !mService.mDisplayFrozen
3725 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3726 }
3727 return mDisplayInfo.state == Display.STATE_ON;
3728 }
3729
3730 boolean okToAnimate() {
3731 return okToDisplay() &&
3732 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3733 }
3734
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003735 static final class TaskForResizePointSearchResult {
3736 boolean searchDone;
3737 Task taskForResize;
3738
3739 void reset() {
3740 searchDone = false;
3741 taskForResize = null;
3742 }
3743 }
Robert Carr3b716242016-08-16 16:02:21 -07003744
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003745 private static final class ApplySurfaceChangesTransactionState {
3746 boolean displayHasContent;
3747 boolean obscured;
3748 boolean syswin;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003749 float preferredRefreshRate;
3750 int preferredModeId;
3751
3752 void reset() {
3753 displayHasContent = false;
3754 obscured = false;
3755 syswin = false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003756 preferredRefreshRate = 0;
3757 preferredModeId = 0;
3758 }
3759 }
3760
3761 private static final class ScreenshotApplicationState {
3762 WindowState appWin;
3763 int maxLayer;
3764 int minLayer;
3765 boolean screenshotReady;
3766
3767 void reset(boolean screenshotReady) {
3768 appWin = null;
3769 maxLayer = 0;
3770 minLayer = 0;
3771 this.screenshotReady = screenshotReady;
3772 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3773 }
3774 }
3775
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003776 /**
3777 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3778 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3779 * homogeneous children type which is currently required by sub-classes of
3780 * {@link WindowContainer} class.
3781 */
3782 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3783
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003784 DisplayChildWindowContainer(WindowManagerService service) {
3785 super(service);
3786 }
3787
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003788 @Override
3789 boolean fillsParent() {
3790 return true;
3791 }
3792
3793 @Override
3794 boolean isVisible() {
3795 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003796 }
3797 }
3798
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003799 /**
3800 * Window container class that contains all containers on this display relating to Apps.
3801 * I.e Activities.
3802 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003803 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003804 /**
3805 * A control placed at the appropriate level for transitions to occur.
3806 */
chaviw23ee71c2017-12-18 11:29:41 -08003807 SurfaceControl mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02003808 SurfaceControl mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02003809 SurfaceControl mHomeAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003810
Robert Carrf7a7ca82017-12-06 13:17:07 -08003811 /**
3812 * Given that the split-screen divider does not have an AppWindowToken, it
3813 * will have to live inside of a "NonAppWindowContainer", in particular
3814 * {@link DisplayContent#mAboveAppWindowsContainers}. However, in visual Z order
3815 * it will need to be interleaved with some of our children, appearing on top of
3816 * both docked stacks but underneath any assistant stacks.
3817 *
3818 * To solve this problem we have this anchor control, which will always exist so
3819 * we can always assign it the correct value in our {@link #assignChildLayers}.
3820 * Likewise since it always exists, {@link AboveAppWindowContainers} can always
3821 * assign the divider a layer relative to it. This way we prevent linking lifecycle
3822 * events between the two containers.
3823 */
3824 SurfaceControl mSplitScreenDividerAnchor = null;
3825
Wale Ogunwale61911492017-10-11 08:50:50 -07003826 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3827 // through the list to find them.
3828 private TaskStack mHomeStack = null;
3829 private TaskStack mPinnedStack = null;
3830 private TaskStack mSplitScreenPrimaryStack = null;
3831
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003832 TaskStackContainers(WindowManagerService service) {
3833 super(service);
3834 }
3835
Wale Ogunwale61911492017-10-11 08:50:50 -07003836 /**
3837 * Returns the topmost stack on the display that is compatible with the input windowing mode
3838 * and activity type. Null is no compatible stack on the display.
3839 */
3840 TaskStack getStack(int windowingMode, int activityType) {
3841 if (activityType == ACTIVITY_TYPE_HOME) {
3842 return mHomeStack;
3843 }
3844 if (windowingMode == WINDOWING_MODE_PINNED) {
3845 return mPinnedStack;
3846 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3847 return mSplitScreenPrimaryStack;
3848 }
3849 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3850 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003851 if (activityType == ACTIVITY_TYPE_UNDEFINED
3852 && windowingMode == stack.getWindowingMode()) {
3853 // Passing in undefined type means we want to match the topmost stack with the
3854 // windowing mode.
3855 return stack;
3856 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003857 if (stack.isCompatible(windowingMode, activityType)) {
3858 return stack;
3859 }
3860 }
3861 return null;
3862 }
3863
3864 @VisibleForTesting
3865 TaskStack getTopStack() {
3866 return mTaskStackContainers.getChildCount() > 0
3867 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3868 }
3869
3870 TaskStack getHomeStack() {
3871 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3872 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3873 }
3874 return mHomeStack;
3875 }
3876
3877 TaskStack getPinnedStack() {
3878 return mPinnedStack;
3879 }
3880
Matthew Ng64e77cf2017-10-31 14:01:31 -07003881 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003882 return mSplitScreenPrimaryStack;
3883 }
3884
Winson Chunge2d72172018-01-25 17:46:20 +00003885 ArrayList<Task> getVisibleTasks() {
3886 final ArrayList<Task> visibleTasks = new ArrayList<>();
3887 forAllTasks(task -> {
3888 if (task.isVisible()) {
3889 visibleTasks.add(task);
3890 }
3891 });
3892 return visibleTasks;
3893 }
3894
Andrii Kulian839def92016-11-02 10:58:58 -07003895 /**
3896 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003897 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003898 */
3899 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003900 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003901 addChild(stack, onTop);
3902 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003903 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003904
Wale Ogunwale61911492017-10-11 08:50:50 -07003905 void onStackWindowingModeChanged(TaskStack stack) {
3906 removeStackReferenceIfNeeded(stack);
3907 addStackReferenceIfNeeded(stack);
3908 if (stack == mPinnedStack && getTopStack() != stack) {
3909 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3910 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3911 }
3912 }
3913
3914 private void addStackReferenceIfNeeded(TaskStack stack) {
3915 if (stack.isActivityTypeHome()) {
3916 if (mHomeStack != null) {
Louis Chang7d0037c2018-08-13 12:42:06 +08003917 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
Wale Ogunwale61911492017-10-11 08:50:50 -07003918 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
Louis Chang7d0037c2018-08-13 12:42:06 +08003919
Wale Ogunwale61911492017-10-11 08:50:50 -07003920 }
Louis Chang7d0037c2018-08-13 12:42:06 +08003921 mHomeStack = stack;
Wale Ogunwale61911492017-10-11 08:50:50 -07003922 }
3923 final int windowingMode = stack.getWindowingMode();
3924 if (windowingMode == WINDOWING_MODE_PINNED) {
3925 if (mPinnedStack != null) {
3926 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3927 + mPinnedStack + " already exist on display=" + this
3928 + " stack=" + stack);
3929 }
3930 mPinnedStack = stack;
3931 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3932 if (mSplitScreenPrimaryStack != null) {
3933 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3934 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3935 + " already exist on display=" + this + " stack=" + stack);
3936 }
3937 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003938 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003939 }
3940 }
3941
3942 private void removeStackReferenceIfNeeded(TaskStack stack) {
3943 if (stack == mHomeStack) {
3944 mHomeStack = null;
3945 } else if (stack == mPinnedStack) {
3946 mPinnedStack = null;
3947 } else if (stack == mSplitScreenPrimaryStack) {
3948 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003949 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3950 mService.setDockedStackCreateStateLocked(
3951 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3952 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003953 }
Andrii Kulian839def92016-11-02 10:58:58 -07003954 }
3955
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003956 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003957 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3958 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003959 addChild(stack, addIndex);
3960 setLayoutNeeded();
3961 }
3962
Wale Ogunwale61911492017-10-11 08:50:50 -07003963 @Override
3964 protected void removeChild(TaskStack stack) {
3965 super.removeChild(stack);
3966 removeStackReferenceIfNeeded(stack);
3967 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003968
3969 @Override
3970 boolean isOnTop() {
3971 // Considered always on top
3972 return true;
3973 }
3974
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003975 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003976 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003977 if (child.getWindowConfiguration().isAlwaysOnTop()
3978 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003979 // This stack is always-on-top, override the default behavior.
3980 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3981
3982 // Moving to its current position, as we must call super but we don't want to
3983 // perform any meaningful action.
3984 final int currentPosition = mChildren.indexOf(child);
3985 super.positionChildAt(currentPosition, child, false /* includingParents */);
3986 return;
3987 }
3988
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003989 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3990 super.positionChildAt(targetPosition, child, includingParents);
3991
Riddle Hsu2f6e1742018-08-23 22:44:36 +08003992 if (includingParents) {
3993 // We still want to move the display of this stack container to top because even the
3994 // target position is adjusted to non-top, the intention of the condition is to have
3995 // higher z-order to gain focus (e.g. moving a task of a fullscreen stack to front
3996 // in a non-top display which is using picture-in-picture mode).
3997 final int topChildPosition = getChildCount() - 1;
3998 if (targetPosition < topChildPosition && position >= topChildPosition) {
3999 getParent().positionChildAt(POSITION_TOP, this /* child */,
4000 true /* includingParents */);
4001 }
4002 }
4003
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004004 setLayoutNeeded();
4005 }
4006
4007 /**
4008 * When stack is added or repositioned, find a proper position for it.
4009 * This will make sure that pinned stack always stays on top.
4010 * @param requestedPosition Position requested by caller.
4011 * @param stack Stack to be added or positioned.
4012 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
4013 * @return The proper position for the stack.
4014 */
4015 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09004016 if (stack.inPinnedWindowingMode()) {
Kazuki Takisef85197b2018-06-18 18:18:36 +09004017 return POSITION_TOP;
Kazuki Takise148d00a2018-05-31 15:32:19 +09004018 }
4019
Kazuki Takisef85197b2018-06-18 18:18:36 +09004020 final int topChildPosition = mChildren.size() - 1;
4021 int belowAlwaysOnTopPosition = POSITION_BOTTOM;
4022 for (int i = topChildPosition; i >= 0; --i) {
4023 if (getStacks().get(i) != stack && !getStacks().get(i).isAlwaysOnTop()) {
4024 belowAlwaysOnTopPosition = i;
4025 break;
4026 }
4027 }
4028
4029 // The max possible position we can insert the stack at.
4030 int maxPosition = POSITION_TOP;
4031 // The min possible position we can insert the stack at.
4032 int minPosition = POSITION_BOTTOM;
4033
4034 if (stack.isAlwaysOnTop()) {
4035 if (hasPinnedStack()) {
4036 // Always-on-top stacks go below the pinned stack.
4037 maxPosition = getStacks().indexOf(mPinnedStack) - 1;
4038 }
4039 // Always-on-top stacks need to be above all other stacks.
4040 minPosition = belowAlwaysOnTopPosition !=
4041 POSITION_BOTTOM ? belowAlwaysOnTopPosition : topChildPosition;
4042 } else {
4043 // Other stacks need to be below the always-on-top stacks.
4044 maxPosition = belowAlwaysOnTopPosition !=
Arthur Hung928d8dc2018-07-18 15:38:05 +08004045 POSITION_BOTTOM ? belowAlwaysOnTopPosition : 0;
Kazuki Takisef85197b2018-06-18 18:18:36 +09004046 }
4047
4048 int targetPosition = requestedPosition;
4049 targetPosition = Math.min(targetPosition, maxPosition);
4050 targetPosition = Math.max(targetPosition, minPosition);
4051
4052 int prevPosition = getStacks().indexOf(stack);
4053 // The positions we calculated above (maxPosition, minPosition) do not take into
4054 // consideration the following edge cases.
4055 // 1) We need to adjust the position depending on the value "adding".
4056 // 2) When we are moving a stack to another position, we also need to adjust the
4057 // position depending on whether the stack is moving to a higher or lower position.
4058 if ((targetPosition != requestedPosition) &&
4059 (adding || targetPosition < prevPosition)) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09004060 targetPosition++;
4061 }
4062
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004063 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08004064 }
4065
4066 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004067 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
4068 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004069 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004070 if (super.forAllWindows(callback, traverseTopToBottom)) {
4071 return true;
4072 }
4073 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4074 return true;
4075 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004076 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004077 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4078 return true;
4079 }
4080 if (super.forAllWindows(callback, traverseTopToBottom)) {
4081 return true;
4082 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004083 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004084 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004085 }
4086
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004087 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004088 boolean traverseTopToBottom) {
4089 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
4090 // app tokens.
4091 // TODO: Investigate if we need to continue to do this or if we can just process them
4092 // in-order.
4093 if (traverseTopToBottom) {
4094 for (int i = mChildren.size() - 1; i >= 0; --i) {
4095 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4096 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004097 if (appTokens.get(j).forAllWindowsUnchecked(callback,
4098 traverseTopToBottom)) {
4099 return true;
4100 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004101 }
4102 }
4103 } else {
4104 final int count = mChildren.size();
4105 for (int i = 0; i < count; ++i) {
4106 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4107 final int appTokensCount = appTokens.size();
4108 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004109 if (appTokens.get(j).forAllWindowsUnchecked(callback,
4110 traverseTopToBottom)) {
4111 return true;
4112 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004113 }
4114 }
4115 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004116 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004117 }
4118
Wale Ogunwale1666e312016-12-16 11:27:18 -08004119 void setExitingTokensHasVisible(boolean hasVisible) {
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 appTokens.get(j).hasVisible = hasVisible;
4124 }
4125 }
4126 }
4127
4128 void removeExistingAppTokensIfPossible() {
4129 for (int i = mChildren.size() - 1; i >= 0; --i) {
4130 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4131 for (int j = appTokens.size() - 1; j >= 0; --j) {
4132 final AppWindowToken token = appTokens.get(j);
lumark588a3e82018-07-20 18:53:54 +08004133 if (!token.hasVisible && !mClosingApps.contains(token)
Wale Ogunwale1666e312016-12-16 11:27:18 -08004134 && (!token.mIsExiting || token.isEmpty())) {
4135 // Make sure there is no animation running on this token, so any windows
4136 // associated with it will be removed as soon as their animations are
4137 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02004138 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004139 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
4140 "performLayout: App token exiting now removed" + token);
4141 token.removeIfPossible();
4142 }
4143 }
4144 }
4145 }
4146
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004147 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004148 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004149 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
4150 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004151 // Apps and their containers are not allowed to specify an orientation while the
Wale Ogunwaled0c225c2018-06-15 14:47:46 -07004152 // docked or freeform stack is visible...except for the home stack if the docked
4153 // stack is minimized and it actually set something and the bounds is different from
4154 // the display.
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004155 if (mHomeStack != null && mHomeStack.isVisible()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004156 && mDividerControllerLocked.isMinimizedDock()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004157 && !(mDividerControllerLocked.isHomeStackResizable()
4158 && mHomeStack.matchParentBounds())) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004159 final int orientation = mHomeStack.getOrientation();
4160 if (orientation != SCREEN_ORIENTATION_UNSET) {
4161 return orientation;
4162 }
4163 }
4164 return SCREEN_ORIENTATION_UNSPECIFIED;
4165 }
4166
4167 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07004168 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
4169 PackageManager.FEATURE_AUTOMOTIVE);
4170 if (isCar) {
4171 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
4172 // allow anything but the default orientation.
4173 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004174 "Forcing UNSPECIFIED orientation in car for display id=" + mDisplayId
4175 + ". Ignoring " + orientation);
Dean Harding3e5a1522017-10-06 09:19:01 -07004176 return SCREEN_ORIENTATION_UNSPECIFIED;
4177 }
4178
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004179 if (orientation != SCREEN_ORIENTATION_UNSET
4180 && orientation != SCREEN_ORIENTATION_BEHIND) {
4181 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004182 "App is requesting an orientation, return " + orientation
4183 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004184 return orientation;
4185 }
4186
4187 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004188 "No app is requesting an orientation, return " + mLastOrientation
4189 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004190 // The next app has not been requested to be visible, so we keep the current orientation
4191 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08004192 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004193 }
Robert Carrb1579c82017-09-05 14:54:47 -07004194
4195 @Override
4196 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004197 assignStackOrdering(t);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004198
Robert Carr2f8aa392018-01-31 14:46:51 -08004199 for (int i = 0; i < mChildren.size(); i++) {
4200 final TaskStack s = mChildren.get(i);
4201 s.assignChildLayers(t);
4202 }
4203 }
4204
4205 void assignStackOrdering(SurfaceControl.Transaction t) {
Jorim Jaggibe418292018-03-26 16:14:12 +02004206
Robert Carrf7a7ca82017-12-06 13:17:07 -08004207 final int HOME_STACK_STATE = 0;
4208 final int NORMAL_STACK_STATE = 1;
4209 final int ALWAYS_ON_TOP_STATE = 2;
4210
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004211 int layer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08004212 int layerForAnimationLayer = 0;
Jorim Jaggibe418292018-03-26 16:14:12 +02004213 int layerForBoostedAnimationLayer = 0;
Jorim Jaggi391790622018-04-18 15:30:44 +02004214 int layerForHomeAnimationLayer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08004215
Robert Carrf7a7ca82017-12-06 13:17:07 -08004216 for (int state = 0; state <= ALWAYS_ON_TOP_STATE; state++) {
4217 for (int i = 0; i < mChildren.size(); i++) {
4218 final TaskStack s = mChildren.get(i);
Robert Carr2f8aa392018-01-31 14:46:51 -08004219 if (state == HOME_STACK_STATE && !s.isActivityTypeHome()) {
4220 continue;
4221 } else if (state == NORMAL_STACK_STATE && (s.isActivityTypeHome()
4222 || s.isAlwaysOnTop())) {
4223 continue;
4224 } else if (state == ALWAYS_ON_TOP_STATE && !s.isAlwaysOnTop()) {
4225 continue;
4226 }
4227 s.assignLayer(t, layer++);
4228 if (s.inSplitScreenWindowingMode() && mSplitScreenDividerAnchor != null) {
4229 t.setLayer(mSplitScreenDividerAnchor, layer++);
4230 }
Winson Chungd41f71d2018-03-16 15:26:07 -07004231 if ((s.isTaskAnimating() || s.isAppAnimating())
4232 && state != ALWAYS_ON_TOP_STATE) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004233 // Ensure the animation layer ends up above the
4234 // highest animating stack and no higher.
4235 layerForAnimationLayer = layer++;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004236 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004237 if (state != ALWAYS_ON_TOP_STATE) {
4238 layerForBoostedAnimationLayer = layer++;
4239 }
Robert Carrf7a7ca82017-12-06 13:17:07 -08004240 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004241 if (state == HOME_STACK_STATE) {
4242 layerForHomeAnimationLayer = layer++;
4243 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004244 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004245 if (mAppAnimationLayer != null) {
4246 t.setLayer(mAppAnimationLayer, layerForAnimationLayer);
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004247 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004248 if (mBoostedAppAnimationLayer != null) {
4249 t.setLayer(mBoostedAppAnimationLayer, layerForBoostedAnimationLayer);
4250 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004251 if (mHomeAppAnimationLayer != null) {
4252 t.setLayer(mHomeAppAnimationLayer, layerForHomeAnimationLayer);
4253 }
Robert Carrb1579c82017-09-05 14:54:47 -07004254 }
4255
4256 @Override
Jorim Jaggi391790622018-04-18 15:30:44 +02004257 SurfaceControl getAppAnimationLayer(@AnimationLayer int animationLayer) {
4258 switch (animationLayer) {
4259 case ANIMATION_LAYER_BOOSTED:
4260 return mBoostedAppAnimationLayer;
4261 case ANIMATION_LAYER_HOME:
4262 return mHomeAppAnimationLayer;
4263 case ANIMATION_LAYER_STANDARD:
4264 default:
4265 return mAppAnimationLayer;
4266 }
chaviw23ee71c2017-12-18 11:29:41 -08004267 }
4268
Robert Carrf7a7ca82017-12-06 13:17:07 -08004269 SurfaceControl getSplitScreenDividerAnchor() {
4270 return mSplitScreenDividerAnchor;
4271 }
4272
chaviw23ee71c2017-12-18 11:29:41 -08004273 @Override
Robert Carrb1579c82017-09-05 14:54:47 -07004274 void onParentSet() {
4275 super.onParentSet();
4276 if (getParent() != null) {
chaviw23ee71c2017-12-18 11:29:41 -08004277 mAppAnimationLayer = makeChildSurface(null)
4278 .setName("animationLayer")
4279 .build();
Jorim Jaggibe418292018-03-26 16:14:12 +02004280 mBoostedAppAnimationLayer = makeChildSurface(null)
4281 .setName("boostedAnimationLayer")
4282 .build();
Jorim Jaggi391790622018-04-18 15:30:44 +02004283 mHomeAppAnimationLayer = makeChildSurface(null)
4284 .setName("homeAnimationLayer")
4285 .build();
Robert Carrf7a7ca82017-12-06 13:17:07 -08004286 mSplitScreenDividerAnchor = makeChildSurface(null)
4287 .setName("splitScreenDividerAnchor")
4288 .build();
4289 getPendingTransaction()
4290 .show(mAppAnimationLayer)
Jorim Jaggibe418292018-03-26 16:14:12 +02004291 .show(mBoostedAppAnimationLayer)
Jorim Jaggi391790622018-04-18 15:30:44 +02004292 .show(mHomeAppAnimationLayer)
Robert Carrf7a7ca82017-12-06 13:17:07 -08004293 .show(mSplitScreenDividerAnchor);
chaviw23ee71c2017-12-18 11:29:41 -08004294 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07004295 } else {
chaviw23ee71c2017-12-18 11:29:41 -08004296 mAppAnimationLayer.destroy();
4297 mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02004298 mBoostedAppAnimationLayer.destroy();
4299 mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02004300 mHomeAppAnimationLayer.destroy();
4301 mHomeAppAnimationLayer = null;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004302 mSplitScreenDividerAnchor.destroy();
4303 mSplitScreenDividerAnchor = null;
Robert Carrb1579c82017-09-05 14:54:47 -07004304 }
4305 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004306 }
4307
Robert Carree4d4b92017-11-22 12:21:46 -08004308 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004309 AboveAppWindowContainers(String name, WindowManagerService service) {
4310 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08004311 }
4312
Robert Carrb9506032018-02-13 13:54:00 -08004313 @Override
Vishnu Nair83537a72018-07-19 21:27:48 -07004314 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
4315 final SurfaceControl.Builder builder = super.makeChildSurface(child);
4316 if (child instanceof WindowToken && ((WindowToken) child).mRoundedCornerOverlay) {
4317 // To draw above the ColorFade layer during the screen off transition, the
4318 // rounded corner overlays need to be at the root of the surface hierarchy.
4319 // TODO: move the ColorLayer into the display overlay layer such that this is not
4320 // necessary anymore.
4321 builder.setParent(null);
4322 }
4323 return builder;
4324 }
4325
4326 @Override
Robert Carrb9506032018-02-13 13:54:00 -08004327 void assignChildLayers(SurfaceControl.Transaction t) {
4328 assignChildLayers(t, null /* imeContainer */);
4329 }
4330
Robert Carree4d4b92017-11-22 12:21:46 -08004331 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
4332 boolean needAssignIme = imeContainer != null
4333 && imeContainer.getSurfaceControl() != null;
4334 for (int j = 0; j < mChildren.size(); ++j) {
4335 final WindowToken wt = mChildren.get(j);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004336
4337 // See {@link mSplitScreenDividerAnchor}
4338 if (wt.windowType == TYPE_DOCK_DIVIDER) {
4339 wt.assignRelativeLayer(t, mTaskStackContainers.getSplitScreenDividerAnchor(), 1);
4340 continue;
4341 }
Vishnu Nair83537a72018-07-19 21:27:48 -07004342 if (wt.mRoundedCornerOverlay) {
4343 wt.assignLayer(t, WindowManagerPolicy.COLOR_FADE_LAYER + 1);
4344 continue;
4345 }
Robert Carree4d4b92017-11-22 12:21:46 -08004346 wt.assignLayer(t, j);
4347 wt.assignChildLayers(t);
4348
4349 int layer = mService.mPolicy.getWindowLayerFromTypeLw(
4350 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08004351
4352 if (needAssignIme && layer >= mService.mPolicy.getWindowLayerFromTypeLw(
4353 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004354 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08004355 needAssignIme = false;
4356 }
4357 }
4358 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004359 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08004360 }
4361 }
4362 }
4363
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004364 /**
4365 * Window container class that contains all containers on this display that are not related to
4366 * Apps. E.g. status bar.
4367 */
Robert Carree4d4b92017-11-22 12:21:46 -08004368 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07004369 /**
4370 * Compares two child window tokens returns -1 if the first is lesser than the second in
4371 * terms of z-order and 1 otherwise.
4372 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004373 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07004374 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004375 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
4376 token1.mOwnerCanManageAppTokens)
4377 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
4378 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004379
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004380 private final Predicate<WindowState> mGetOrientingWindow = w -> {
4381 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
4382 return false;
4383 }
4384 final int req = w.mAttrs.screenOrientation;
4385 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
4386 || req == SCREEN_ORIENTATION_UNSET) {
4387 return false;
4388 }
4389 return true;
4390 };
4391
Wale Ogunwale3a931692016-11-02 16:49:48 -07004392 private final String mName;
chaviwf29223e2018-01-31 13:23:51 -08004393 private final Dimmer mDimmer = new Dimmer(this);
4394 private final Rect mTmpDimBoundsRect = new Rect();
4395
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004396 NonAppWindowContainers(String name, WindowManagerService service) {
4397 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07004398 mName = name;
4399 }
4400
4401 void addChild(WindowToken token) {
4402 addChild(token, mWindowComparator);
4403 }
4404
4405 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004406 int getOrientation() {
4407 final WindowManagerPolicy policy = mService.mPolicy;
4408 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004409 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004410
4411 if (win != null) {
4412 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004413 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004414 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07004415 if (mService.mKeyguardGoingAway) {
4416 // Keyguard can't affect the orientation if it is going away...
4417 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
4418 return SCREEN_ORIENTATION_UNSET;
4419 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004420 }
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004421 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req
4422 + " for display id=" + mDisplayId);
Andrii Kulian8ee72852017-03-10 10:36:45 -08004423 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004424 }
4425
Andrii Kulian8ee72852017-03-10 10:36:45 -08004426 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004427
Jorim Jaggi1c530592018-04-06 15:11:47 +02004428 // Only allow force setting the orientation when all unknown visibilities have been
4429 // resolved, as otherwise we just may be starting another occluding activity.
4430 final boolean isUnoccluding =
lumark588a3e82018-07-20 18:53:54 +08004431 mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
4432 && mUnknownAppVisibilityController.allResolved();
Jorim Jaggi1c530592018-04-06 15:11:47 +02004433 if (policy.isKeyguardShowingAndNotOccluded() || isUnoccluding) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004434 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004435 }
4436
4437 return SCREEN_ORIENTATION_UNSET;
4438 }
4439
4440 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07004441 String getName() {
4442 return mName;
4443 }
chaviwf29223e2018-01-31 13:23:51 -08004444
4445 @Override
4446 Dimmer getDimmer() {
4447 return mDimmer;
4448 }
4449
4450 @Override
4451 void prepareSurfaces() {
4452 mDimmer.resetDimStates();
4453 super.prepareSurfaces();
4454 getBounds(mTmpDimBoundsRect);
4455
4456 if (mDimmer.updateDims(getPendingTransaction(), mTmpDimBoundsRect)) {
4457 scheduleAnimation();
4458 }
4459 }
Robert Carr3b716242016-08-16 16:02:21 -07004460 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01004461
Robert Carr9034d962018-01-04 18:27:42 -08004462 private class NonMagnifiableWindowContainers extends NonAppWindowContainers {
4463 NonMagnifiableWindowContainers(String name, WindowManagerService service) {
4464 super(name, service);
4465 }
4466
4467 @Override
4468 void applyMagnificationSpec(Transaction t, MagnificationSpec spec) {
4469 }
4470 };
4471
Robert Carrb1579c82017-09-05 14:54:47 -07004472 SurfaceControl.Builder makeSurface(SurfaceSession s) {
4473 return mService.makeSurfaceBuilder(s)
4474 .setParent(mWindowingLayer);
4475 }
4476
4477 @Override
4478 SurfaceSession getSession() {
4479 return mSession;
4480 }
4481
4482 @Override
4483 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07004484 SurfaceSession s = child != null ? child.getSession() : getSession();
4485 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrb1579c82017-09-05 14:54:47 -07004486 b.setSize(mSurfaceSize, mSurfaceSize);
Robert Carrf59b8dd2017-10-02 18:58:36 -07004487
4488 if (child == null) {
4489 return b;
4490 }
4491
Robert Carree4d4b92017-11-22 12:21:46 -08004492 return b.setName(child.getName())
4493 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07004494 }
4495
4496 /**
4497 * The makeSurface variants are for use by the window-container
4498 * hierarchy. makeOverlay here is a function for various non windowing
4499 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
4500 * and other potpourii.
4501 */
4502 SurfaceControl.Builder makeOverlay() {
4503 return mService.makeSurfaceBuilder(mSession)
4504 .setParent(mOverlayLayer);
4505 }
4506
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004507 /**
4508 * Reparents the given surface to mOverlayLayer.
4509 */
4510 void reparentToOverlay(Transaction transaction, SurfaceControl surface) {
4511 transaction.reparent(surface, mOverlayLayer.getHandle());
4512 }
4513
Robert Carrb1579c82017-09-05 14:54:47 -07004514 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carr24be9ab2018-04-30 17:54:53 -07004515 if (spec.scale != 1.0) {
4516 mMagnificationSpec = spec;
4517 } else {
4518 mMagnificationSpec = null;
4519 }
4520
Robert Carrf59b8dd2017-10-02 18:58:36 -07004521 applyMagnificationSpec(getPendingTransaction(), spec);
4522 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07004523 }
4524
Robert Carr24be9ab2018-04-30 17:54:53 -07004525 void reapplyMagnificationSpec() {
4526 if (mMagnificationSpec != null) {
4527 applyMagnificationSpec(getPendingTransaction(), mMagnificationSpec);
4528 }
4529 }
4530
Robert Carrb1579c82017-09-05 14:54:47 -07004531 @Override
4532 void onParentSet() {
4533 // Since we are the top of the SurfaceControl hierarchy here
4534 // we create the root surfaces explicitly rather than chaining
4535 // up as the default implementation in onParentSet does. So we
4536 // explicitly do NOT call super here.
4537 }
4538
4539 @Override
4540 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07004541
4542 // These are layers as children of "mWindowingLayer"
4543 mBelowAppWindowsContainers.assignLayer(t, 0);
4544 mTaskStackContainers.assignLayer(t, 1);
4545 mAboveAppWindowsContainers.assignLayer(t, 2);
4546
lumarkff0ab692018-11-05 20:32:30 +08004547 final WindowState imeTarget = mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08004548 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08004549
Robert Carree4d4b92017-11-22 12:21:46 -08004550 // In the case where we have an IME target that is not in split-screen
4551 // mode IME assignment is easy. We just need the IME to go directly above
4552 // the target. This way children of the target will naturally go above the IME
4553 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08004554 //
Robert Carree4d4b92017-11-22 12:21:46 -08004555 // In the case of split-screen windowing mode, we need to elevate the IME above the
4556 // docked divider while keeping the app itself below the docked divider, so instead
4557 // we use relative layering of the IME targets child windows, and place the
4558 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08004559 //
Robert Carr234b6332018-03-20 13:38:16 -07004560 // In the case the IME target is animating, the animation Z order may be different
4561 // than the WindowContainer Z order, so it's difficult to be sure we have the correct
4562 // IME target. In this case we just layer the IME over all transitions by placing it in the
4563 // above applications layer.
4564 //
Robert Carree4d4b92017-11-22 12:21:46 -08004565 // In the case where we have no IME target we assign it where it's base layer would
4566 // place it in the AboveAppWindowContainers.
Robert Carr234b6332018-03-20 13:38:16 -07004567 if (imeTarget != null && !(imeTarget.inSplitScreenWindowingMode()
4568 || imeTarget.mToken.isAppAnimating())
Robert Carree4d4b92017-11-22 12:21:46 -08004569 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004570 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07004571 // TODO: We need to use an extra level on the app surface to ensure
4572 // this is always above SurfaceView but always below attached window.
4573 1);
Robert Carree4d4b92017-11-22 12:21:46 -08004574 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07004575 }
4576
4577 // Above we have assigned layers to our children, now we ask them to assign
4578 // layers to their children.
4579 mBelowAppWindowsContainers.assignChildLayers(t);
4580 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08004581 mAboveAppWindowsContainers.assignChildLayers(t,
4582 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07004583 mImeWindowsContainers.assignChildLayers(t);
4584 }
4585
4586 /**
4587 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
4588 * that the IME target is one of the docked applications. We'd like the docked divider to be
4589 * above both of the applications, and we'd like the IME to be above the docked divider.
4590 * However we need child windows of the applications to be above the IME (Text drag handles).
4591 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
4592 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
Tiger Huang7c610aa2018-10-27 00:01:01 +08004593 * with {@link WindowState#assignLayer}
Robert Carrb1579c82017-09-05 14:54:47 -07004594 */
4595 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
Robert Carrbb5de662018-04-26 13:29:51 -07004596 child.assignRelativeLayer(t, mImeWindowsContainers.getSurfaceControl(), 1);
Robert Carrb1579c82017-09-05 14:54:47 -07004597 }
4598
4599 @Override
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004600 void prepareSurfaces() {
4601 final ScreenRotationAnimation screenRotationAnimation =
4602 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
4603 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
4604 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
4605 mPendingTransaction.setMatrix(mWindowingLayer,
4606 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
4607 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
4608 mPendingTransaction.setPosition(mWindowingLayer,
4609 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
4610 mPendingTransaction.setAlpha(mWindowingLayer,
4611 screenRotationAnimation.getEnterTransformation().getAlpha());
4612 }
Chavi Weingartenb736e322018-02-23 00:27:54 +00004613
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004614 super.prepareSurfaces();
4615 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004616
Jorim Jaggibe418292018-03-26 16:14:12 +02004617 void assignStackOrdering() {
4618 mTaskStackContainers.assignStackOrdering(getPendingTransaction());
Robert Carr2f8aa392018-01-31 14:46:51 -08004619 }
Chavi Weingarten3a748552018-05-14 17:32:42 +00004620
4621 /**
4622 * Increment the deferral count to determine whether to update the IME target.
4623 */
4624 void deferUpdateImeTarget() {
4625 mDeferUpdateImeTargetCount++;
4626 }
4627
4628 /**
4629 * Decrement the deferral count to determine whether to update the IME target. If the count
4630 * reaches 0, a new ime target will get computed.
4631 */
4632 void continueUpdateImeTarget() {
4633 if (mDeferUpdateImeTargetCount == 0) {
4634 return;
4635 }
4636
4637 mDeferUpdateImeTargetCount--;
4638 if (mDeferUpdateImeTargetCount == 0) {
4639 computeImeTarget(true /* updateImeTarget */);
4640 }
4641 }
4642
4643 /**
4644 * @return Whether a new IME target should be computed.
4645 */
4646 private boolean canUpdateImeTarget() {
4647 return mDeferUpdateImeTargetCount == 0;
4648 }
Arthur Hung95b38a92018-07-20 18:56:12 +08004649
4650 InputMonitor getInputMonitor() {
4651 return mInputMonitor;
4652 }
Jorim Jaggif1292892018-09-10 11:58:13 +02004653
4654 /**
4655 * @return Cached value whether we told display manager that we have content.
4656 */
4657 boolean getLastHasContent() {
4658 return mLastHasContent;
4659 }
Arthur Hungbe5ce212018-09-13 18:41:56 +08004660
4661 void registerPointerEventListener(@NonNull PointerEventListener listener) {
4662 if (mPointerEventDispatcher != null) {
4663 mPointerEventDispatcher.registerInputEventListener(listener);
4664 }
4665 }
4666
4667 void unregisterPointerEventListener(@NonNull PointerEventListener listener) {
4668 if (mPointerEventDispatcher != null) {
4669 mPointerEventDispatcher.unregisterInputEventListener(listener);
4670 }
4671 }
lumark588a3e82018-07-20 18:53:54 +08004672
4673 void prepareAppTransition(@WindowManager.TransitionType int transit,
4674 boolean alwaysKeepCurrent, @WindowManager.TransitionFlags int flags,
4675 boolean forceOverride) {
4676 final boolean prepared = mAppTransition.prepareAppTransitionLocked(
4677 transit, alwaysKeepCurrent, flags, forceOverride);
4678 if (prepared && okToAnimate()) {
4679 mSkipAppTransitionAnimation = false;
4680 }
4681 }
4682
4683 void executeAppTransition() {
4684 if (mAppTransition.isTransitionSet()) {
4685 if (DEBUG_APP_TRANSITIONS) {
4686 Slog.w(TAG_WM, "Execute app transition: " + mAppTransition + ", displayId: "
4687 + mDisplayId + " Callers=" + Debug.getCallers(5));
4688 }
4689 mAppTransition.setReady();
4690 mService.mWindowPlacerLocked.requestTraversal();
4691 }
4692 }
4693
4694 /**
4695 * Update pendingLayoutChanges after app transition has finished.
4696 */
4697 void handleAnimatingStoppedAndTransition() {
4698 int changes = 0;
4699
4700 mAppTransition.setIdle();
4701
4702 for (int i = mNoAnimationNotifyOnTransitionFinished.size() - 1; i >= 0; i--) {
4703 final IBinder token = mNoAnimationNotifyOnTransitionFinished.get(i);
4704 mAppTransition.notifyAppTransitionFinishedLocked(token);
4705 }
4706 mNoAnimationNotifyOnTransitionFinished.clear();
4707
4708 mWallpaperController.hideDeferredWallpapersIfNeeded();
4709
4710 onAppTransitionDone();
4711
4712 changes |= FINISH_LAYOUT_REDO_LAYOUT;
4713 if (DEBUG_WALLPAPER_LIGHT) {
4714 Slog.v(TAG_WM, "Wallpaper layer changed: assigning layers + relayout");
4715 }
4716 computeImeTarget(true /* updateImeTarget */);
wilsonshihc32538e2018-11-07 17:27:34 +08004717 mWallpaperMayChange = true;
lumark588a3e82018-07-20 18:53:54 +08004718 // Since the window list has been rebuilt, focus might have to be recomputed since the
4719 // actual order of windows might have changed again.
4720 mService.mFocusMayChange = true;
4721
4722 pendingLayoutChanges |= changes;
4723 }
Andrii Kulian15cfb422018-11-07 13:38:49 -08004724
4725 /**
4726 * @see Display#FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS
4727 */
4728 boolean supportsSystemDecorations() {
4729 // TODO(b/114338689): Read the setting from DisplaySettings.
4730 return mDisplay.supportsSystemDecorations()
4731 // TODO (b/111363427): Remove this and set the new FLAG_SHOULD_SHOW_LAUNCHER flag
4732 // (b/114338689) whenever vr 2d display id is set.
4733 || mDisplayId == mService.mVr2dDisplayId
4734 || mService.mForceDesktopModeOnExternalDisplays;
4735 }
Craig Mautner59c00972012-07-30 12:10:24 -07004736}