blob: 4b939862904cf2a4678ac5f41b81620198d9ea3e [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;
lumark588a3e82018-07-20 18:53:54 +0800159import android.view.WindowManager;
Evan Rosky39b6f232018-10-30 18:35:41 -0700160import android.view.WindowManagerPolicyConstants.PointerEventListener;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700161
Bryce Lee48f4b572017-04-10 10:54:15 -0700162import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800163import com.android.internal.util.ToBooleanFunction;
Adrian Roose99bc052017-11-20 17:55:31 +0100164import com.android.server.policy.WindowManagerPolicy;
Issei Suzuki43190bd2018-08-20 17:28:41 +0200165import com.android.server.wm.utils.DisplayRotationUtil;
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100166import com.android.server.wm.utils.RotationCache;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100167import com.android.server.wm.utils.WmDisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700168
169import java.io.PrintWriter;
Riddle Hsuf53da812018-08-15 22:00:27 +0800170import java.lang.annotation.Retention;
171import java.lang.annotation.RetentionPolicy;
Craig Mautner59c00972012-07-30 12:10:24 -0700172import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700173import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700174import java.util.HashMap;
175import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700176import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700177import java.util.List;
Adrian Roos1cf585052018-01-03 18:43:27 +0100178import java.util.Objects;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800179import java.util.function.Consumer;
180import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700181
Craig Mautner59c00972012-07-30 12:10:24 -0700182/**
183 * Utility class for keeping track of the WindowStates and other pertinent contents of a
184 * particular Display.
185 *
186 * IMPORTANT: No method from this class should ever be used without holding
187 * WindowManagerService.mWindowMap.
188 */
Riddle Hsuad256a12018-07-18 16:11:30 +0800189class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer>
190 implements WindowManagerPolicy.DisplayContentInfo {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700191 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700192
Riddle Hsuf53da812018-08-15 22:00:27 +0800193 /** The default scaling mode that scales content automatically. */
194 static final int FORCE_SCALING_MODE_AUTO = 0;
195 /** For {@link #setForcedScalingMode} to apply flag {@link Display#FLAG_SCALING_DISABLED}. */
196 static final int FORCE_SCALING_MODE_DISABLED = 1;
197
198 @IntDef(prefix = { "FORCE_SCALING_MODE_" }, value = {
199 FORCE_SCALING_MODE_AUTO,
200 FORCE_SCALING_MODE_DISABLED
201 })
202 @Retention(RetentionPolicy.SOURCE)
203 @interface ForceScalingMode {}
204
Craig Mautner59c00972012-07-30 12:10:24 -0700205 /** Unique identifier of this stack. */
206 private final int mDisplayId;
207
Wale Ogunwale3a931692016-11-02 16:49:48 -0700208 /** The containers below are the only child containers the display can have. */
209 // Contains all window containers that are related to apps (Activities)
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100210 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers(mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700211 // Contains all non-app window containers that should be displayed above the app containers
212 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800213 private final AboveAppWindowContainers mAboveAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100214 new AboveAppWindowContainers("mAboveAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700215 // Contains all non-app window containers that should be displayed below the app containers
216 // (e.g. Wallpaper).
217 private final NonAppWindowContainers mBelowAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100218 new NonAppWindowContainers("mBelowAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700219 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
220 // 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 -0800221 // window containers together and move them in-sync if/when needed. We use a subclass of
222 // WindowContainer which is omitted from screen magnification, as the IME is never magnified.
223 private final NonMagnifiableWindowContainers mImeWindowsContainers =
224 new NonMagnifiableWindowContainers("mImeWindowsContainers", mService);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700225
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000226 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800227 private WindowState mTmpWindow2;
228 private WindowAnimator mTmpWindowAnimator;
229 private boolean mTmpRecoveringMemory;
230 private boolean mUpdateImeTarget;
231 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700232 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700233
lumark588a3e82018-07-20 18:53:54 +0800234 final AppTransition mAppTransition;
235 final AppTransitionController mAppTransitionController;
236 boolean mSkipAppTransitionAnimation = false;
237
238 final ArraySet<AppWindowToken> mOpeningApps = new ArraySet<>();
239 final ArraySet<AppWindowToken> mClosingApps = new ArraySet<>();
240 final UnknownAppVisibilityController mUnknownAppVisibilityController;
241 BoundsAnimationController mBoundsAnimationController;
242
243 /**
244 * List of clients without a transtiton animation that we notify once we are done
245 * transitioning since they won't be notified through the app window animator.
246 */
247 final List<IBinder> mNoAnimationNotifyOnTransitionFinished = new ArrayList<>();
248
Wale Ogunwale02319a62016-09-26 15:21:22 -0700249 // Mapping from a token IBinder to a WindowToken object on this display.
250 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
251
Andrii Kuliancd097992017-03-23 18:31:59 -0700252 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700253 int mInitialDisplayWidth = 0;
254 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700255 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700256
Adrian Roos1cf585052018-01-03 18:43:27 +0100257 DisplayCutout mInitialDisplayCutout;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100258 private final RotationCache<DisplayCutout, WmDisplayCutout> mDisplayCutoutCache
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100259 = new RotationCache<>(this::calculateDisplayCutoutForRotationUncached);
Adrian Roos1cf585052018-01-03 18:43:27 +0100260
Andrii Kuliancd097992017-03-23 18:31:59 -0700261 /**
262 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
263 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
264 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
265 */
Craig Mautner59c00972012-07-30 12:10:24 -0700266 int mBaseDisplayWidth = 0;
267 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700268 /**
269 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
270 * but can be set from Settings or via shell command "adb shell wm density".
271 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
272 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700273 int mBaseDisplayDensity = 0;
Riddle Hsuf53da812018-08-15 22:00:27 +0800274
275 /**
276 * Whether to disable display scaling. This can be set via shell command "adb shell wm scaling".
277 * @see WindowManagerService#setForcedDisplayScalingMode(int, int)
278 */
Jeff Brownd46747a2015-04-15 19:02:36 -0700279 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700280 private final DisplayInfo mDisplayInfo = new DisplayInfo();
281 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700282 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800283 private final DisplayPolicy mDisplayPolicy;
284 private DisplayRotation mDisplayRotation;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000285 DisplayFrames mDisplayFrames;
286
Andrii Kulian06d07d62017-03-14 11:11:47 -0700287 /**
288 * For default display it contains real metrics, empty for others.
289 * @see WindowManagerService#createWatermarkInTransaction()
290 */
291 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
292 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
293 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700294
Andrii Kulian06d07d62017-03-14 11:11:47 -0700295 /**
296 * Compat metrics computed based on {@link #mDisplayMetrics}.
297 * @see #updateDisplayAndOrientation(int)
298 */
299 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
300
301 /** The desired scaling factor for compatible apps. */
302 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700303
Andrii Kulian8ee72852017-03-10 10:36:45 -0800304 /**
305 * Current rotation of the display.
306 * Constants as per {@link android.view.Surface.Rotation}.
307 *
Robert Carrae606b42018-02-15 15:36:23 -0800308 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800309 */
310 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700311
Andrii Kulian8ee72852017-03-10 10:36:45 -0800312 /**
313 * Last applied orientation of the display.
314 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
315 *
Riddle Hsu4e611772018-10-31 18:58:28 +0800316 * @see #updateOrientationFromAppTokens()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800317 */
318 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700319
Andrii Kulian8ee72852017-03-10 10:36:45 -0800320 /**
321 * Flag indicating that the application is receiving an orientation that has different metrics
322 * than it expected. E.g. Portrait instead of Landscape.
323 *
Robert Carrae606b42018-02-15 15:36:23 -0800324 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800325 */
326 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700327
Andrii Kulian8ee72852017-03-10 10:36:45 -0800328 /**
329 * Orientation forced by some window. If there is no visible window that specifies orientation
330 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
331 *
332 * @see NonAppWindowContainers#getOrientation()
333 */
334 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700335
Andrii Kulian8ee72852017-03-10 10:36:45 -0800336 /**
337 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
338 * occluded.
339 *
340 * @see NonAppWindowContainers#getOrientation()
341 */
342 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
343
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700344 /**
345 * Keep track of wallpaper visibility to notify changes.
346 */
347 private boolean mLastWallpaperVisible = false;
348
Andrii Kulian06d07d62017-03-14 11:11:47 -0700349 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700350
Craig Mautner39834192012-09-02 07:47:24 -0700351 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700352 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700353 int pendingLayoutChanges;
Riddle Hsu654a6f92018-07-13 22:59:36 +0800354 int mDeferredRotationPauseCount;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800355
Andrii Kulian839def92016-11-02 10:58:58 -0700356 // TODO(multi-display): remove some of the usages.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800357 @VisibleForTesting
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800358 boolean isDefaultDisplay;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800359
Andrii Kulianf0379de2018-03-14 16:24:07 -0700360 /**
361 * Flag indicating whether WindowManager should override info for this display in
362 * DisplayManager.
363 */
364 boolean mShouldOverrideDisplayConfiguration = true;
Craig Mautner39834192012-09-02 07:47:24 -0700365
Craig Mautnerdc548482014-02-05 13:35:24 -0800366 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700367 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800368
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700369 /** Detect user tapping outside of current focused task bounds .*/
370 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700371
Craig Mautner6601b7b2013-04-29 10:29:11 -0700372 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700373 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700374
Craig Mautner6601b7b2013-04-29 10:29:11 -0700375 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800376 private final Rect mTmpRect = new Rect();
377 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700378 private final RectF mTmpRectF = new RectF();
379 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800380 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700381
Issei Suzuki43190bd2018-08-20 17:28:41 +0200382
Bryce Leef3c6a472017-11-14 14:53:06 -0800383 /** Used for handing back size of display */
384 private final Rect mTmpBounds = new Rect();
385
Craig Mautner95da1082014-02-24 17:54:35 -0800386 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700387 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800388
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700389 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700390 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700391
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800392 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800393 /** A collection of windows that provide tap exclude regions inside of them. */
394 final ArraySet<WindowState> mTapExcludeProvidingWindows = new ArraySet<>();
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800395
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000396 private boolean mHaveBootMsg = false;
397 private boolean mHaveApp = false;
398 private boolean mHaveWallpaper = false;
399 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700400
401 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
402
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700403 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
404 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000405 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
406 new ApplySurfaceChangesTransactionState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700407
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700408 // True if this display is in the process of being removed. Used to determine if the removal of
409 // the display's direct children should be allowed.
410 private boolean mRemovingDisplay = false;
411
Bryce Leed1871262017-06-12 14:12:29 -0700412 // {@code false} if this display is in the processing of being created.
413 private boolean mDisplayReady = false;
414
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800415 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700416
Robert Carrb1579c82017-09-05 14:54:47 -0700417 private final SurfaceSession mSession = new SurfaceSession();
418
419 /**
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800420 * Window that is currently interacting with the user. This window is responsible for receiving
421 * key events and pointer events from the user.
422 */
423 WindowState mCurrentFocus = null;
424
425 /**
426 * The last focused window that we've notified the client that the focus is changed.
427 */
428 WindowState mLastFocus = null;
429
430 /**
431 * Windows that have lost input focus and are waiting for the new focus window to be displayed
432 * before they are told about this.
433 */
434 ArrayList<WindowState> mLosingFocus = new ArrayList<>();
435
436 /**
437 * The foreground app of this display. Windows below this app cannot be the focused window. If
438 * the user taps on the area outside of the task of the focused app, we will notify AM about the
439 * new task the user wants to interact with.
440 */
441 AppWindowToken mFocusedApp = null;
442
443 /** Windows added since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
444 final ArrayList<WindowState> mWinAddedSinceNullFocus = new ArrayList<>();
445
446 /** Windows removed since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
447 final ArrayList<WindowState> mWinRemovedSinceNullFocus = new ArrayList<>();
448
449 /**
Robert Carrb1579c82017-09-05 14:54:47 -0700450 * We organize all top-level Surfaces in to the following layers.
451 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800452 * and omitted from Screen-Magnification, for example the strict mode flash or
453 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700454 * {@link #mWindowingLayer} contains everything else.
455 */
456 private SurfaceControl mOverlayLayer;
457
458 /**
459 * See {@link #mOverlayLayer}
460 */
461 private SurfaceControl mWindowingLayer;
462
463 /**
464 * Specifies the size of the surfaces in {@link #mOverlayLayer} and {@link #mWindowingLayer}.
465 * <p>
466 * For these surfaces currently we use a surface based on the larger of width or height so we
467 * don't have to resize when rotating the display.
468 */
469 private int mSurfaceSize;
470
Adrian Roos5251b1d2018-03-23 18:57:43 +0100471 /**
472 * Sequence number for the current layout pass.
473 */
474 int mLayoutSeq = 0;
475
Chavi Weingarten3a748552018-05-14 17:32:42 +0000476 /**
477 * Specifies the count to determine whether to defer updating the IME target until ready.
478 */
479 private int mDeferUpdateImeTargetCount;
480
Jorim Jaggi9af095b2017-12-12 17:18:57 +0100481 /** Temporary float array to retrieve 3x3 matrix values. */
482 private final float[] mTmpFloats = new float[9];
483
Robert Carr24be9ab2018-04-30 17:54:53 -0700484 private MagnificationSpec mMagnificationSpec;
485
Arthur Hung95b38a92018-07-20 18:56:12 +0800486 private InputMonitor mInputMonitor;
487
Jorim Jaggif1292892018-09-10 11:58:13 +0200488 /** Caches the value whether told display manager that we have content. */
489 private boolean mLastHasContent;
490
Issei Suzuki43190bd2018-08-20 17:28:41 +0200491 private DisplayRotationUtil mRotationUtil = new DisplayRotationUtil();
492
lumark90120a82018-08-15 00:33:03 +0800493 /**
494 * The input method window for this display.
495 */
496 WindowState mInputMethodWindow;
497
lumarkff0ab692018-11-05 20:32:30 +0800498 /**
499 * This just indicates the window the input method is on top of, not
500 * necessarily the window its input is going to.
501 */
502 WindowState mInputMethodTarget;
503
504 /** If true hold off on modifying the animation layer of mInputMethodTarget */
505 boolean mInputMethodTargetWaitingAnim;
506
Arthur Hungbe5ce212018-09-13 18:41:56 +0800507 private final PointerEventDispatcher mPointerEventDispatcher;
508
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800509 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
510 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800511 final AppWindowToken atoken = w.mAppToken;
512 if (winAnimator.mDrawState == READY_TO_SHOW) {
Jorim Jaggiaf0d6d22018-06-08 15:25:35 +0200513 if (atoken == null || atoken.canShowWindows()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800514 if (w.performShowLocked()) {
515 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
516 if (DEBUG_LAYOUT_REPEATS) {
517 mService.mWindowPlacerLocked.debugLayoutRepeats(
518 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
519 }
520 }
521 }
522 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800523 };
524
525 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
526 final WindowStateAnimator winAnimator = w.mWinAnimator;
527 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
528 return;
529 }
530
Jorim Jaggi8f520872018-08-14 17:00:20 +0200531 // If this window is animating, ensure the animation background is set.
532 final AnimationAdapter anim = w.mAppToken != null
533 ? w.mAppToken.getAnimation()
534 : w.getAnimation();
535 if (anim != null) {
536 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800537 if (color != 0) {
538 final TaskStack stack = w.getStack();
539 if (stack != null) {
540 stack.setAnimationBackground(winAnimator, color);
541 }
542 }
543 }
544 };
545
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800546 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
547 final int lostFocusUid = mTmpWindow.mOwnerUid;
548 final Handler handler = mService.mH;
549 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
550 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
551 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
552 w.mAttrs.hideTimeoutMilliseconds);
553 }
554 }
555 };
556
557 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800558 final AppWindowToken focusedApp = mFocusedApp;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800559 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
560 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
561
562 if (!w.canReceiveKeys()) {
563 return false;
564 }
565
566 final AppWindowToken wtoken = w.mAppToken;
567
568 // If this window's application has been removed, just skip it.
569 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
570 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
571 + (wtoken.removed ? "removed" : "sendingToBottom"));
572 return false;
573 }
574
575 if (focusedApp == null) {
576 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
577 + " using new focus @ " + w);
578 mTmpWindow = w;
579 return true;
580 }
581
582 if (!focusedApp.windowsAreFocusable()) {
583 // Current focused app windows aren't focusable...
584 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
585 + " focusable using new focus @ " + w);
586 mTmpWindow = w;
587 return true;
588 }
589
590 // Descend through all of the app tokens and find the first that either matches
591 // win.mAppToken (return win) or mFocusedApp (return null).
592 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
593 if (focusedApp.compareTo(wtoken) > 0) {
594 // App stack below focused app stack. No focus for you!!!
595 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
596 "findFocusedWindow: Reached focused app=" + focusedApp);
597 mTmpWindow = null;
598 return true;
599 }
600 }
601
602 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
603 mTmpWindow = w;
604 return true;
605 };
606
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800607 private final Consumer<WindowState> mPerformLayout = w -> {
608 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
609 // wasting time and funky changes while a window is animating away.
610 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
611 || w.isGoneForLayoutLw();
612
613 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
614 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
615 + " mLayoutAttached=" + w.mLayoutAttached
616 + " screen changed=" + w.isConfigChanged());
617 final AppWindowToken atoken = w.mAppToken;
618 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200619 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800620 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
621 + " parentHidden=" + w.isParentWindowHidden());
622 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200623 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800624 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
625 + " parentHidden=" + w.isParentWindowHidden());
626 }
627
628 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
629 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
630 // since that means "perform layout as normal, just don't display").
631 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
632 || ((w.isConfigChanged() || w.setReportResizeHints())
633 && !w.isGoneForLayoutLw() &&
634 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
635 (w.mHasSurface && w.mAppToken != null &&
636 w.mAppToken.layoutConfigChanges)))) {
637 if (!w.mLayoutAttached) {
638 if (mTmpInitial) {
639 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700640 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800641 }
642 if (w.mAttrs.type == TYPE_DREAM) {
643 // Don't layout windows behind a dream, so that if it does stuff like hide
644 // the status bar we won't get a bad transition when it goes away.
645 mTmpWindow = w;
646 }
647 w.mLayoutNeeded = false;
648 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200649 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000650 mService.mPolicy.layoutWindowLw(w, null, mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100651 w.mLayoutSeq = mLayoutSeq;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800652
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200653 // If this is the first layout, we need to initialize the last inset values as
654 // otherwise we'd immediately cause an unnecessary resize.
655 if (firstLayout) {
656 w.updateLastInsetValues();
657 }
658
Adrian Roos23df3a32018-03-15 15:41:13 +0100659 if (w.mAppToken != null) {
660 w.mAppToken.layoutLetterbox(w);
661 }
662
chaviw492139a2018-07-16 16:07:35 -0700663 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700664 + " mContainingFrame=" + w.getContainingFrame()
665 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800666 }
667 }
668 };
669
670 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
671 if (w.mLayoutAttached) {
672 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
673 + " mViewVisibility=" + w.mViewVisibility
674 + " mRelayoutCalled=" + w.mRelayoutCalled);
675 // If this view is GONE, then skip it -- keep the current frame, and let the caller
676 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
677 // windows, since that means "perform layout as normal, just don't display").
678 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
679 return;
680 }
681 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
682 || w.mLayoutNeeded) {
683 if (mTmpInitial) {
684 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700685 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800686 }
687 w.mLayoutNeeded = false;
688 w.prelayout();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000689 mService.mPolicy.layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100690 w.mLayoutSeq = mLayoutSeq;
chaviw492139a2018-07-16 16:07:35 -0700691 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700692 + " mContainingFrame=" + w.getContainingFrame()
693 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800694 }
695 } else if (w.mAttrs.type == TYPE_DREAM) {
696 // Don't layout windows behind a dream, so that if it does stuff like hide the
697 // status bar we won't get a bad transition when it goes away.
698 mTmpWindow = mTmpWindow2;
699 }
700 };
701
702 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
703 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
704 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
705 return w.canBeImeTarget();
706 };
707
708 private final Consumer<WindowState> mApplyPostLayoutPolicy =
709 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
lumarkff0ab692018-11-05 20:32:30 +0800710 mInputMethodTarget);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800711
712 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
713 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
714 final boolean obscuredChanged = w.mObscured !=
715 mTmpApplySurfaceChangesTransactionState.obscured;
716 final RootWindowContainer root = mService.mRoot;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800717
718 // Update effect.
719 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
720 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
721 final boolean isDisplayed = w.isDisplayedLw();
722
723 if (isDisplayed && w.isObscuringDisplay()) {
724 // This window completely covers everything behind it, so we want to leave all
725 // of them as undimmed (for performance reasons).
726 root.mObscuringWindow = w;
727 mTmpApplySurfaceChangesTransactionState.obscured = true;
728 }
729
730 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
731 root.handleNotObscuredLocked(w,
732 mTmpApplySurfaceChangesTransactionState.obscured,
733 mTmpApplySurfaceChangesTransactionState.syswin);
734
735 if (w.mHasSurface && isDisplayed) {
736 final int type = w.mAttrs.type;
737 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
738 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
739 mTmpApplySurfaceChangesTransactionState.syswin = true;
740 }
741 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
742 && w.mAttrs.preferredRefreshRate != 0) {
743 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
744 = w.mAttrs.preferredRefreshRate;
745 }
746 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
747 && w.mAttrs.preferredDisplayModeId != 0) {
748 mTmpApplySurfaceChangesTransactionState.preferredModeId
749 = w.mAttrs.preferredDisplayModeId;
750 }
751 }
752 }
753
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800754 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
755 && mWallpaperController.isWallpaperTarget(w)) {
756 // This is the wallpaper target and its obscured state changed... make sure the
757 // current wallpaper's visibility has been updated accordingly.
758 mWallpaperController.updateWallpaperVisibility();
759 }
760
chaviw161ea3e2018-01-31 12:01:12 -0800761 w.handleWindowMovedIfNeeded();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800762
763 final WindowStateAnimator winAnimator = w.mWinAnimator;
764
765 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
chaviw1454b392018-08-06 09:54:04 -0700766 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800767
768 // Moved from updateWindowsAndWallpaperLocked().
769 if (w.mHasSurface) {
770 // Take care of the window being ready to display.
771 final boolean committed = winAnimator.commitFinishDrawingLocked();
772 if (isDefaultDisplay && committed) {
773 if (w.mAttrs.type == TYPE_DREAM) {
774 // HACK: When a dream is shown, it may at that point hide the lock screen.
775 // So we need to redo the layout to let the phone window manager make this
776 // happen.
777 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
778 if (DEBUG_LAYOUT_REPEATS) {
779 surfacePlacer.debugLayoutRepeats(
780 "dream and commitFinishDrawingLocked true",
781 pendingLayoutChanges);
782 }
783 }
784 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
785 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
786 "First draw done in potential wallpaper target " + w);
787 root.mWallpaperMayChange = true;
788 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
789 if (DEBUG_LAYOUT_REPEATS) {
790 surfacePlacer.debugLayoutRepeats(
791 "wallpaper and commitFinishDrawingLocked true",
792 pendingLayoutChanges);
793 }
794 }
795 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800796 }
797
798 final AppWindowToken atoken = w.mAppToken;
799 if (atoken != null) {
Adrian Roos23df3a32018-03-15 15:41:13 +0100800 atoken.updateLetterboxSurface(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800801 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
802 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
803 mTmpUpdateAllDrawn.add(atoken);
804 }
805 }
806
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800807 if (!mLosingFocus.isEmpty() && w.isFocused() && w.isDisplayedLw()) {
808 mService.mH.obtainMessage(REPORT_LOSING_FOCUS, this).sendToTarget();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800809 }
810
811 w.updateResizingWindowIfNeeded();
812 };
813
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800814 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800815 * Create new {@link DisplayContent} instance, add itself to the root window container and
816 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700817 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800818 * @param service You know.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700819 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
820 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700821 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700822 DisplayContent(Display display, WindowManagerService service,
Bryce Leef19cbe22018-02-02 15:09:21 -0800823 WallpaperController wallpaperController, DisplayWindowController controller) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100824 super(service);
Bryce Leef19cbe22018-02-02 15:09:21 -0800825 setController(controller);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800826 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
827 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
828 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
829 + " new=" + display);
830 }
831
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700832 mDisplay = display;
833 mDisplayId = display.getDisplayId();
Wale Ogunwale0303c572016-10-20 10:16:29 -0700834 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700835 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700836 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700837 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100838 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo,
839 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700840 initializeDisplayBaseInfo();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800841 mDisplayPolicy = new DisplayPolicy(service);
842 mDisplayRotation = new DisplayRotation(service, this);
843 if (isDefaultDisplay) {
844 // The policy may be invoked right after here, so it requires the necessary default
845 // fields of this display content.
846 mService.mPolicy.setDefaultDisplay(this);
847 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800848 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700849 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700850
lumark588a3e82018-07-20 18:53:54 +0800851 mAppTransition = new AppTransition(service.mContext, service, this);
852 mAppTransition.registerListenerLocked(service.mActivityManagerAppTransitionNotifier);
853 mAppTransitionController = new AppTransitionController(service, this);
854 mUnknownAppVisibilityController = new UnknownAppVisibilityController(service, this);
855
856 AnimationHandler animationHandler = new AnimationHandler();
857 mBoundsAnimationController = new BoundsAnimationController(service.mContext,
858 mAppTransition, SurfaceAnimationThread.getHandler(), animationHandler);
859
Robert Carr74a66a22018-02-23 12:17:51 -0800860 // We use this as our arbitrary surface size for buffer-less parents
861 // that don't impose cropping on their children. It may need to be larger
862 // than the display size because fullscreen windows can be shifted offscreen
863 // due to surfaceInsets. 2 times the largest display dimension feels like an
864 // appropriately arbitrary number. Eventually we would like to give SurfaceFlinger
865 // layers the ability to match their parent sizes and be able to skip
866 // such arbitrary size settings.
867 mSurfaceSize = Math.max(mBaseDisplayHeight, mBaseDisplayWidth) * 2;
Robert Carrb1579c82017-09-05 14:54:47 -0700868
869 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession)
870 .setSize(mSurfaceSize, mSurfaceSize)
871 .setOpaque(true);
872 mWindowingLayer = b.setName("Display Root").build();
873 mOverlayLayer = b.setName("Display Overlays").build();
874
Robert Carrf59b8dd2017-10-02 18:58:36 -0700875 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700876 .setLayerStack(mWindowingLayer, mDisplayId)
877 .show(mWindowingLayer)
878 .setLayer(mOverlayLayer, 1)
879 .setLayerStack(mOverlayLayer, mDisplayId)
880 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700881 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700882
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700883 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700884 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700885 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700886 super.addChild(mAboveAppWindowsContainers, null);
887 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800888
889 // Add itself as a child to the root container.
890 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700891
892 // TODO(b/62541591): evaluate whether this is the best spot to declare the
893 // {@link DisplayContent} ready for use.
894 mDisplayReady = true;
Arthur Hung95b38a92018-07-20 18:56:12 +0800895
Riddle Hsuf53da812018-08-15 22:00:27 +0800896 mService.mAnimator.addDisplayLocked(mDisplayId);
Arthur Hung39134b22018-08-14 11:58:28 +0800897 mInputMonitor = new InputMonitor(service, mDisplayId);
Arthur Hungbe5ce212018-09-13 18:41:56 +0800898
899 if (mService.mInputManager != null) {
900 final InputChannel inputChannel = mService.mInputManager.monitorInput("Display "
901 + mDisplayId, mDisplayId);
902 mPointerEventDispatcher = inputChannel != null
903 ? new PointerEventDispatcher(inputChannel) : null;
904 } else {
905 mPointerEventDispatcher = null;
906 }
Bryce Leed1871262017-06-12 14:12:29 -0700907 }
908
909 boolean isReady() {
910 // The display is ready when the system and the individual display are both ready.
911 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700912 }
913
914 int getDisplayId() {
915 return mDisplayId;
916 }
917
Wale Ogunwale02319a62016-09-26 15:21:22 -0700918 WindowToken getWindowToken(IBinder binder) {
919 return mTokenMap.get(binder);
920 }
921
922 AppWindowToken getAppWindowToken(IBinder binder) {
923 final WindowToken token = getWindowToken(binder);
924 if (token == null) {
925 return null;
926 }
927 return token.asAppWindowToken();
928 }
929
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700930 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700931 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
932 if (dc != null) {
933 // We currently don't support adding a window token to the display if the display
934 // already has the binder mapped to another token. If there is a use case for supporting
935 // this moving forward we will either need to merge the WindowTokens some how or have
936 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700937 throw new IllegalArgumentException("Can't map token=" + token + " to display="
938 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700939 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700940 if (binder == null) {
941 throw new IllegalArgumentException("Can't map token=" + token + " to display="
942 + getName() + " binder is null");
943 }
944 if (token == null) {
945 throw new IllegalArgumentException("Can't map null token to display="
946 + getName() + " binder=" + binder);
947 }
948
Wale Ogunwale02319a62016-09-26 15:21:22 -0700949 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700950
951 if (token.asAppWindowToken() == null) {
952 // Add non-app token to container hierarchy on the display. App tokens are added through
953 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700954 switch (token.windowType) {
955 case TYPE_WALLPAPER:
956 mBelowAppWindowsContainers.addChild(token);
957 break;
958 case TYPE_INPUT_METHOD:
959 case TYPE_INPUT_METHOD_DIALOG:
960 mImeWindowsContainers.addChild(token);
961 break;
962 default:
963 mAboveAppWindowsContainers.addChild(token);
964 break;
965 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700966 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700967 }
968
969 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700970 final WindowToken token = mTokenMap.remove(binder);
971 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800972 token.setExiting();
973 }
974 return token;
975 }
976
977 /** Changes the display the input window token is housed on to this one. */
978 void reParentWindowToken(WindowToken token) {
979 final DisplayContent prevDc = token.getDisplayContent();
980 if (prevDc == this) {
981 return;
982 }
Riddle Hsu85bd04b2018-11-17 00:34:36 +0800983 if (prevDc != null) {
984 if (prevDc.mTokenMap.remove(token.token) != null && token.asAppWindowToken() == null) {
985 // Removed the token from the map, but made sure it's not an app token before
986 // removing from parent.
987 token.getParent().removeChild(token);
988 }
989 if (prevDc.mLastFocus == mCurrentFocus) {
990 // The window has become the focus of this display, so it should not be notified
991 // that it lost focus from the previous display.
992 prevDc.mLastFocus = null;
993 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700994 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800995
996 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700997 }
998
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700999 void removeAppToken(IBinder binder) {
1000 final WindowToken token = removeWindowToken(binder);
1001 if (token == null) {
1002 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
1003 return;
1004 }
1005
1006 final AppWindowToken appToken = token.asAppWindowToken();
1007
1008 if (appToken == null) {
1009 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
1010 return;
1011 }
1012
1013 appToken.onRemovedFromDisplay();
1014 }
1015
Riddle Hsuad256a12018-07-18 16:11:30 +08001016 @Override
Riddle Hsu86cb7de2018-08-13 23:29:58 +08001017 DisplayWindowController getController() {
1018 return (DisplayWindowController) super.getController();
1019 }
1020
1021 @Override
Riddle Hsuad256a12018-07-18 16:11:30 +08001022 public Display getDisplay() {
Craig Mautnerb47bbc32012-08-22 17:41:48 -07001023 return mDisplay;
1024 }
1025
Craig Mautner59c00972012-07-30 12:10:24 -07001026 DisplayInfo getDisplayInfo() {
1027 return mDisplayInfo;
1028 }
1029
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001030 DisplayMetrics getDisplayMetrics() {
1031 return mDisplayMetrics;
1032 }
1033
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001034 DisplayPolicy getDisplayPolicy() {
1035 return mDisplayPolicy;
1036 }
1037
Riddle Hsuad256a12018-07-18 16:11:30 +08001038 @Override
1039 public DisplayRotation getDisplayRotation() {
1040 return mDisplayRotation;
1041 }
1042
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001043 @VisibleForTesting
1044 void setDisplayRotation(DisplayRotation displayRotation) {
1045 mDisplayRotation = displayRotation;
1046 }
1047
Andrii Kulian8ee72852017-03-10 10:36:45 -08001048 int getRotation() {
1049 return mRotation;
1050 }
1051
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001052 @VisibleForTesting
Andrii Kulian8ee72852017-03-10 10:36:45 -08001053 void setRotation(int newRotation) {
1054 mRotation = newRotation;
Riddle Hsuad256a12018-07-18 16:11:30 +08001055 mDisplayRotation.setRotation(newRotation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08001056 }
1057
1058 int getLastOrientation() {
1059 return mLastOrientation;
1060 }
1061
Andrii Kulian8ee72852017-03-10 10:36:45 -08001062 boolean getAltOrientation() {
1063 return mAltOrientation;
1064 }
1065
Andrii Kulian8ee72852017-03-10 10:36:45 -08001066 int getLastWindowForcedOrientation() {
1067 return mLastWindowForcedOrientation;
1068 }
1069
Andrii Kulian06d07d62017-03-14 11:11:47 -07001070 /**
Riddle Hsu654a6f92018-07-13 22:59:36 +08001071 * Temporarily pauses rotation changes until resumed.
1072 *
1073 * This can be used to prevent rotation changes from occurring while the user is
1074 * performing certain operations, such as drag and drop.
1075 *
1076 * This call nests and must be matched by an equal number of calls to
1077 * {@link #resumeRotationLocked}.
1078 */
1079 void pauseRotationLocked() {
1080 mDeferredRotationPauseCount++;
1081 }
1082
1083 /**
1084 * Resumes normal rotation changes after being paused.
1085 */
1086 void resumeRotationLocked() {
1087 if (mDeferredRotationPauseCount <= 0) {
1088 return;
1089 }
1090
1091 mDeferredRotationPauseCount--;
1092 if (mDeferredRotationPauseCount == 0) {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001093 updateRotationAndSendNewConfigIfNeeded();
Riddle Hsu654a6f92018-07-13 22:59:36 +08001094 }
1095 }
1096
1097 /**
Riddle Hsuad256a12018-07-18 16:11:30 +08001098 * If this is true we have updated our desired orientation, but not yet changed the real
1099 * orientation our applied our screen rotation animation. For example, because a previous
1100 * screen rotation was in progress.
1101 *
1102 * @return {@code true} if the there is an ongoing rotation change.
1103 */
1104 boolean rotationNeedsUpdate() {
1105 final int lastOrientation = getLastOrientation();
1106 final int oldRotation = getRotation();
1107 final boolean oldAltOrientation = getAltOrientation();
1108
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001109 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Riddle Hsuad256a12018-07-18 16:11:30 +08001110 final boolean altOrientation = !mDisplayRotation.rotationHasCompatibleMetrics(
1111 lastOrientation, rotation);
1112 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1113 return false;
1114 }
1115 return true;
1116 }
1117
Riddle Hsu4e611772018-10-31 18:58:28 +08001118 /** Notify the configuration change of this display. */
1119 void sendNewConfiguration() {
1120 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, this).sendToTarget();
1121 }
1122
1123 /**
1124 * Determine the new desired orientation of this display.
1125 *
1126 * The orientation is computed from non-application windows first. If none of the
1127 * non-application windows specify orientation, the orientation is computed from application
1128 * tokens.
1129 *
1130 * @return {@code true} if the orientation is changed.
1131 */
1132 boolean updateOrientationFromAppTokens() {
1133 return updateOrientationFromAppTokens(false /* forceUpdate */);
1134 }
1135
1136 boolean updateOrientationFromAppTokens(boolean forceUpdate) {
1137 final int req = getOrientation();
1138 if (req != mLastOrientation || forceUpdate) {
1139 mLastOrientation = req;
1140 mDisplayRotation.setCurrentOrientation(req);
1141 return updateRotationUnchecked(forceUpdate);
1142 }
1143 return false;
1144 }
1145
Riddle Hsuad256a12018-07-18 16:11:30 +08001146 /**
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001147 * Update rotation of the display and send configuration if the rotation is changed.
1148 *
1149 * @return {@code true} if the rotation has been changed and the new config is sent.
1150 */
1151 boolean updateRotationAndSendNewConfigIfNeeded() {
1152 final boolean changed = updateRotationUnchecked(false /* forceUpdate */);
1153 if (changed) {
Riddle Hsu4e611772018-10-31 18:58:28 +08001154 sendNewConfiguration();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001155 }
1156 return changed;
1157 }
1158
1159 /**
Andrii Kulian06d07d62017-03-14 11:11:47 -07001160 * Update rotation of the display.
1161 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001162 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
1163 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001164 */
Robert Carrae606b42018-02-15 15:36:23 -08001165 boolean updateRotationUnchecked() {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001166 return updateRotationUnchecked(false /* forceUpdate */);
1167 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001168
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001169 /**
Evan Roskye747c3e2018-10-30 20:06:41 -07001170 * Update rotation of the DisplayContent with an option to force the update. This updates
1171 * the container's perception of rotation and, depending on the top activities, will freeze
1172 * the screen or start seamless rotation. The display itself gets rotated in
1173 * {@link #applyRotationLocked} during {@link WindowManagerService#sendNewConfiguration}.
1174 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001175 * @param forceUpdate Force the rotation update. Sometimes in WM we might skip updating
1176 * orientation because we're waiting for some rotation to finish or display
1177 * to unfreeze, which results in configuration of the previously visible
1178 * activity being applied to a newly visible one. Forcing the rotation
1179 * update allows to workaround this issue.
1180 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
Evan Roskye747c3e2018-10-30 20:06:41 -07001181 * {@link WindowManagerService#sendNewConfiguration(int)} TO COMPLETE THE ROTATION AND
1182 * UNFREEZE THE SCREEN.
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001183 */
1184 boolean updateRotationUnchecked(boolean forceUpdate) {
1185 ScreenRotationAnimation screenRotationAnimation;
1186 if (!forceUpdate) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001187 if (mDeferredRotationPauseCount > 0) {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001188 // Rotation updates have been paused temporarily. Defer the update until
1189 // updates have been resumed.
1190 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
1191 return false;
1192 }
1193
1194 screenRotationAnimation =
1195 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
1196 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
1197 // Rotation updates cannot be performed while the previous rotation change
1198 // animation is still in progress. Skip this update. We will try updating
1199 // again after the animation is finished and the display is unfrozen.
1200 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
1201 return false;
1202 }
1203 if (mService.mDisplayFrozen) {
1204 // Even if the screen rotation animation has finished (e.g. isAnimating
1205 // returns false), there is still some time where we haven't yet unfrozen
1206 // the display. We also need to abort rotation here.
1207 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
1208 "Deferring rotation, still finishing previous rotation");
1209 return false;
1210 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001211 }
1212
1213 if (!mService.mDisplayEnabled) {
1214 // No point choosing a rotation if the display is not enabled.
1215 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
1216 return false;
1217 }
1218
1219 final int oldRotation = mRotation;
1220 final int lastOrientation = mLastOrientation;
1221 final boolean oldAltOrientation = mAltOrientation;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001222 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001223 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Computed rotation=" + rotation + " for display id="
1224 + mDisplayId + " based on lastOrientation=" + lastOrientation
1225 + " and oldRotation=" + oldRotation);
Riddle Hsuad256a12018-07-18 16:11:30 +08001226 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(mDisplayRotation,
1227 oldRotation, rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001228
Robert Carr0e007272017-10-02 13:00:55 -07001229 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001230 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001231 if (seamlessRotated != null && !forceUpdate) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001232 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
1233 // to complete (that is, waiting for windows to redraw). It's tempting to check
1234 // w.mSeamlessRotationCount but that could be incorrect in the case of
1235 // window-removal.
1236 return false;
1237 }
Robert Carr0e007272017-10-02 13:00:55 -07001238
1239 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001240 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -07001241 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -07001242 mayRotateSeamlessly = false;
1243 }
1244 for (int i = 0; i < mService.mSessions.size(); i++) {
1245 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
1246 mayRotateSeamlessly = false;
1247 break;
1248 }
1249 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001250 }
Robert Carr0e007272017-10-02 13:00:55 -07001251 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001252
1253 // TODO: Implement forced rotation changes.
1254 // Set mAltOrientation to indicate that the application is receiving
1255 // an orientation that has different metrics than it expected.
1256 // eg. Portrait instead of Landscape.
1257
Riddle Hsuad256a12018-07-18 16:11:30 +08001258 final boolean altOrientation = !mDisplayRotation.rotationHasCompatibleMetrics(
Andrii Kulian06d07d62017-03-14 11:11:47 -07001259 lastOrientation, rotation);
1260
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001261 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1262 + " selected orientation " + lastOrientation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001263 + ", got rotation " + rotation + " which has "
1264 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1265
1266 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1267 // No change.
1268 return false;
1269 }
1270
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001271 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1272 + " rotation changed to " + rotation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001273 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1274 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1275
1276 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1277 mService.mWaitingForConfig = true;
1278 }
1279
Evan Roskye747c3e2018-10-30 20:06:41 -07001280 mRotation = rotation;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001281 mAltOrientation = altOrientation;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001282
1283 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
Riddle Hsu654a6f92018-07-13 22:59:36 +08001284 mService.mH.sendNewMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1285 this, WINDOW_FREEZE_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001286
1287 setLayoutNeeded();
1288 final int[] anim = new int[2];
Robert Carrf59b8dd2017-10-02 18:58:36 -07001289 mService.mPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001290
1291 if (!rotateSeamlessly) {
Robert Carrae606b42018-02-15 15:36:23 -08001292 mService.startFreezingDisplayLocked(anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001293 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001294 } else {
1295 // The screen rotation animation uses a screenshot to freeze the screen
1296 // while windows resize underneath.
1297 // When we are rotating seamlessly, we allow the elements to transition
1298 // to their rotated state independently and without a freeze required.
Robert Carrefc75702018-02-16 11:46:16 -08001299 mService.startSeamlessRotation();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001300 }
1301
Evan Roskye747c3e2018-10-30 20:06:41 -07001302 return true;
1303 }
1304
1305 /**
1306 * Applies the rotation transaction. This must be called after {@link #updateRotationUnchecked}
1307 * (if it returned {@code true}) to actually finish the rotation.
1308 *
1309 * @param oldRotation the rotation we are coming from.
1310 * @param rotation the rotation to apply.
1311 */
1312 void applyRotationLocked(final int oldRotation, final int rotation) {
1313 mDisplayRotation.setRotation(rotation);
1314 final boolean rotateSeamlessly = mService.isRotatingSeamlessly();
1315 ScreenRotationAnimation screenRotationAnimation = rotateSeamlessly
1316 ? null : mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001317 // We need to update our screen size information to match the new rotation. If the rotation
1318 // has actually changed then this method will return true and, according to the comment at
1319 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1320 // By updating the Display info here it will be available to
1321 // #computeScreenConfiguration() later.
1322 updateDisplayAndOrientation(getConfiguration().uiMode);
1323
Robert Carrae606b42018-02-15 15:36:23 -08001324 // NOTE: We disable the rotation in the emulator because
1325 // it doesn't support hardware OpenGL emulation yet.
1326 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1327 && screenRotationAnimation.hasScreenshot()) {
1328 if (screenRotationAnimation.setRotation(getPendingTransaction(), rotation,
1329 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1330 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1331 mService.scheduleAnimationLocked();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001332 }
1333 }
1334
Vishnu Nair83537a72018-07-19 21:27:48 -07001335 forAllWindows(w -> {
Vishnu Nairddd80742018-08-21 14:12:46 -07001336 w.seamlesslyRotateIfAllowed(getPendingTransaction(), oldRotation, rotation,
1337 rotateSeamlessly);
Vishnu Nair83537a72018-07-19 21:27:48 -07001338 }, true /* traverseTopToBottom */);
1339
Robert Carrae606b42018-02-15 15:36:23 -08001340 mService.mDisplayManagerInternal.performTraversal(getPendingTransaction());
1341 scheduleAnimation();
1342
Andrii Kulian06d07d62017-03-14 11:11:47 -07001343 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001344 if (w.mHasSurface && !rotateSeamlessly) {
1345 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001346 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001347 mService.mRoot.mOrientationChangeComplete = false;
1348 w.mLastFreezeDuration = 0;
1349 }
1350 w.mReportOrientationChanged = true;
1351 }, true /* traverseTopToBottom */);
1352
1353 if (rotateSeamlessly) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001354 mService.mH.sendNewMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1355 this, SEAMLESS_ROTATION_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001356 }
1357
1358 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1359 final WindowManagerService.RotationWatcher rotationWatcher
1360 = mService.mRotationWatchers.get(i);
1361 if (rotationWatcher.mDisplayId == mDisplayId) {
1362 try {
1363 rotationWatcher.mWatcher.onRotationChanged(rotation);
1364 } catch (RemoteException e) {
1365 // Ignore
1366 }
1367 }
1368 }
1369
1370 // TODO (multi-display): Magnification is supported only for the default display.
1371 // Announce rotation only if we will not animate as we already have the
1372 // windows in final state. Otherwise, we make this call at the rotation end.
1373 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1374 && isDefaultDisplay) {
1375 mService.mAccessibilityController.onRotationChangedLocked(this);
1376 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001377 }
1378
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001379 void configureDisplayPolicy() {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001380 final int width = mBaseDisplayWidth;
1381 final int height = mBaseDisplayHeight;
1382 final int shortSize;
1383 final int longSize;
1384 if (width > height) {
1385 shortSize = height;
1386 longSize = width;
1387 } else {
1388 shortSize = width;
1389 longSize = height;
1390 }
1391
1392 final int shortSizeDp = shortSize * DisplayMetrics.DENSITY_DEFAULT / mBaseDisplayDensity;
1393 final int longSizeDp = longSize * DisplayMetrics.DENSITY_DEFAULT / mBaseDisplayDensity;
1394
1395 mDisplayRotation.configure(width, height, shortSizeDp, longSizeDp);
1396 mDisplayPolicy.configure(width, height, shortSizeDp);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001397
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001398 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
1399 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Arthur Hungbe5ce212018-09-13 18:41:56 +08001400
1401 // Tap Listeners are supported for:
1402 // 1. All physical displays (multi-display).
1403 // 2. VirtualDisplays on VR, AA (and everything else).
1404 if (mPointerEventDispatcher != null && mTapDetector == null) {
1405 if (DEBUG_DISPLAY) {
1406 Slog.d(TAG,
1407 "Registering PointerEventListener for DisplayId: " + mDisplayId);
1408 }
1409 mTapDetector = new TaskTapPointerEventListener(mService, this);
1410 registerPointerEventListener(mTapDetector);
1411 registerPointerEventListener(mService.mMousePositionTracker);
1412 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001413 }
1414
Andrii Kulian06d07d62017-03-14 11:11:47 -07001415 /**
1416 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1417 * changed.
1418 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1419 */
1420 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1421 // Use the effective "visual" dimensions based on current rotation
1422 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1423 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1424 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1425 int dw = realdw;
1426 int dh = realdh;
1427
1428 if (mAltOrientation) {
1429 if (realdw > realdh) {
1430 // Turn landscape into portrait.
1431 int maxw = (int)(realdh/1.3f);
1432 if (maxw < realdw) {
1433 dw = maxw;
1434 }
1435 } else {
1436 // Turn portrait into landscape.
1437 int maxh = (int)(realdw/1.3f);
1438 if (maxh < realdh) {
1439 dh = maxh;
1440 }
1441 }
1442 }
1443
1444 // Update application display metrics.
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001445 final WmDisplayCutout wmDisplayCutout = calculateDisplayCutoutForRotation(mRotation);
1446 final DisplayCutout displayCutout = wmDisplayCutout.getDisplayCutout();
1447
Andrii Kulian06d07d62017-03-14 11:11:47 -07001448 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001449 mDisplayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001450 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001451 mDisplayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001452 mDisplayInfo.rotation = mRotation;
1453 mDisplayInfo.logicalWidth = dw;
1454 mDisplayInfo.logicalHeight = dh;
1455 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1456 mDisplayInfo.appWidth = appWidth;
1457 mDisplayInfo.appHeight = appHeight;
1458 if (isDefaultDisplay) {
1459 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1460 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1461 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001462 mDisplayInfo.displayCutout = displayCutout.isEmpty() ? null : displayCutout;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001463 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1464 if (mDisplayScalingDisabled) {
1465 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1466 } else {
1467 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1468 }
1469
Andrii Kulianf0379de2018-03-14 16:24:07 -07001470 // We usually set the override info in DisplayManager so that we get consistent display
1471 // metrics values when displays are changing and don't send out new values until WM is aware
1472 // of them. However, we don't do this for displays that serve as containers for ActivityView
1473 // because we don't want letter-/pillar-boxing during resize.
1474 final DisplayInfo overrideDisplayInfo = mShouldOverrideDisplayConfiguration
1475 ? mDisplayInfo : null;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001476 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
Andrii Kulianf0379de2018-03-14 16:24:07 -07001477 overrideDisplayInfo);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001478
1479 mBaseDisplayRect.set(0, 0, dw, dh);
1480
1481 if (isDefaultDisplay) {
1482 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1483 mCompatDisplayMetrics);
1484 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001485
Andrii Kulian06d07d62017-03-14 11:11:47 -07001486 return mDisplayInfo;
1487 }
1488
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001489 WmDisplayCutout calculateDisplayCutoutForRotation(int rotation) {
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001490 return mDisplayCutoutCache.getOrCompute(mInitialDisplayCutout, rotation);
1491 }
1492
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001493 private WmDisplayCutout calculateDisplayCutoutForRotationUncached(
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001494 DisplayCutout cutout, int rotation) {
Adrian Roos24264212018-02-19 16:26:15 +01001495 if (cutout == null || cutout == DisplayCutout.NO_CUTOUT) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001496 return WmDisplayCutout.NO_CUTOUT;
Adrian Roos1cf585052018-01-03 18:43:27 +01001497 }
Adrian Roos11c25582018-02-19 18:06:36 +01001498 if (rotation == ROTATION_0) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001499 return WmDisplayCutout.computeSafeInsets(
1500 cutout, mInitialDisplayWidth, mInitialDisplayHeight);
Adrian Roos24264212018-02-19 16:26:15 +01001501 }
Adrian Roosbed538e2018-02-21 17:50:07 +01001502 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Issei Suzuki43190bd2018-08-20 17:28:41 +02001503 final Rect[] newBounds = mRotationUtil.getRotatedBounds(
1504 WmDisplayCutout.computeSafeInsets(
Jorim Jaggi60640512018-06-29 01:14:31 +02001505 cutout, mInitialDisplayWidth, mInitialDisplayHeight)
Issei Suzuki43190bd2018-08-20 17:28:41 +02001506 .getDisplayCutout().getBoundingRectsAll(),
1507 rotation, mInitialDisplayWidth, mInitialDisplayHeight);
1508 return WmDisplayCutout.computeSafeInsets(DisplayCutout.fromBounds(newBounds),
Adrian Roos24264212018-02-19 16:26:15 +01001509 rotated ? mInitialDisplayHeight : mInitialDisplayWidth,
1510 rotated ? mInitialDisplayWidth : mInitialDisplayHeight);
Adrian Roos1cf585052018-01-03 18:43:27 +01001511 }
1512
Andrii Kulian06d07d62017-03-14 11:11:47 -07001513 /**
1514 * Compute display configuration based on display properties and policy settings.
1515 * Do not call if mDisplayReady == false.
1516 */
1517 void computeScreenConfiguration(Configuration config) {
1518 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
Evan Roskye747c3e2018-10-30 20:06:41 -07001519 calculateBounds(displayInfo, mTmpBounds);
1520 config.windowConfiguration.setBounds(mTmpBounds);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001521
1522 final int dw = displayInfo.logicalWidth;
1523 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001524 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
Garfield Tane0846042018-07-26 13:42:04 -07001525 config.windowConfiguration.setWindowingMode(getWindowingMode());
Evan Roskye747c3e2018-10-30 20:06:41 -07001526 config.windowConfiguration.setRotation(displayInfo.rotation);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001527
Adrian Roos11c25582018-02-19 18:06:36 +01001528 final float density = mDisplayMetrics.density;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001529 config.screenWidthDp =
1530 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001531 config.uiMode, mDisplayId, displayInfo.displayCutout) / density);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001532 config.screenHeightDp =
1533 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001534 config.uiMode, mDisplayId, displayInfo.displayCutout) / density);
Bryce Lee7566d762017-03-30 09:34:15 -07001535
Adrian Roos11c25582018-02-19 18:06:36 +01001536 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh,
1537 displayInfo.displayCutout, mTmpRect);
Bryce Lee7566d762017-03-30 09:34:15 -07001538 final int leftInset = mTmpRect.left;
1539 final int topInset = mTmpRect.top;
1540 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001541 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1542 leftInset + displayInfo.appWidth /* right */,
1543 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001544 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1545 || displayInfo.rotation == Surface.ROTATION_270);
1546
1547 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
Adrian Roos11c25582018-02-19 18:06:36 +01001548 density, config);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001549
1550 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1551 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1552 ? Configuration.SCREENLAYOUT_ROUND_YES
1553 : Configuration.SCREENLAYOUT_ROUND_NO);
1554
1555 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1556 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1557 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
Evan Rosky39b6f232018-10-30 18:35:41 -07001558 dh, displayInfo.displayCutout, mDisplayId);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001559 config.densityDpi = displayInfo.logicalDensityDpi;
1560
1561 config.colorMode =
Chia-I Wuf739bad2018-05-09 12:36:57 -07001562 ((displayInfo.isHdr() && mService.hasHdrSupport())
Andrii Kulian06d07d62017-03-14 11:11:47 -07001563 ? Configuration.COLOR_MODE_HDR_YES
1564 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001565 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001566 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1567 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1568
1569 // Update the configuration based on available input devices, lid switch,
1570 // and platform configuration.
1571 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1572 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1573 config.navigation = Configuration.NAVIGATION_NONAV;
1574
1575 int keyboardPresence = 0;
1576 int navigationPresence = 0;
1577 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1578 final int len = devices != null ? devices.length : 0;
1579 for (int i = 0; i < len; i++) {
1580 InputDevice device = devices[i];
1581 if (!device.isVirtual()) {
1582 final int sources = device.getSources();
1583 final int presenceFlag = device.isExternal() ?
1584 WindowManagerPolicy.PRESENCE_EXTERNAL :
1585 WindowManagerPolicy.PRESENCE_INTERNAL;
1586
1587 // TODO(multi-display): Configure on per-display basis.
1588 if (mService.mIsTouchDevice) {
1589 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1590 InputDevice.SOURCE_TOUCHSCREEN) {
1591 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1592 }
1593 } else {
1594 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1595 }
1596
1597 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1598 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1599 navigationPresence |= presenceFlag;
1600 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1601 && config.navigation == Configuration.NAVIGATION_NONAV) {
1602 config.navigation = Configuration.NAVIGATION_DPAD;
1603 navigationPresence |= presenceFlag;
1604 }
1605
1606 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1607 config.keyboard = Configuration.KEYBOARD_QWERTY;
1608 keyboardPresence |= presenceFlag;
1609 }
1610 }
1611 }
1612
1613 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1614 config.navigation = Configuration.NAVIGATION_DPAD;
1615 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1616 }
1617
1618 // Determine whether a hard keyboard is available and enabled.
1619 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1620 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1621 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1622 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1623 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1624 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1625 }
1626
1627 // Let the policy update hidden states.
1628 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1629 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1630 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1631 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1632 }
1633
1634 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
Evan Rosky39b6f232018-10-30 18:35:41 -07001635 DisplayCutout displayCutout, int displayId) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001636 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1637 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1638 final int unrotDw, unrotDh;
1639 if (rotated) {
1640 unrotDw = dh;
1641 unrotDh = dw;
1642 } else {
1643 unrotDw = dw;
1644 unrotDh = dh;
1645 }
1646 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
Evan Rosky39b6f232018-10-30 18:35:41 -07001647 displayCutout, displayId);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001648 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
Evan Rosky39b6f232018-10-30 18:35:41 -07001649 displayCutout, displayId);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001650 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
Evan Rosky39b6f232018-10-30 18:35:41 -07001651 displayCutout, displayId);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001652 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
Evan Rosky39b6f232018-10-30 18:35:41 -07001653 displayCutout, displayId);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001654 return sw;
1655 }
1656
1657 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
Evan Rosky39b6f232018-10-30 18:35:41 -07001658 DisplayMetrics dm, int dw, int dh, DisplayCutout displayCutout, int displayId) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001659 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
Evan Rosky39b6f232018-10-30 18:35:41 -07001660 displayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001661 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
Evan Rosky39b6f232018-10-30 18:35:41 -07001662 uiMode, displayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001663 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1664 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1665 if (curSize == 0 || size < curSize) {
1666 curSize = size;
1667 }
1668 return curSize;
1669 }
1670
1671 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1672 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1673
1674 // We need to determine the smallest width that will occur under normal
1675 // operation. To this, start with the base screen size and compute the
1676 // width under the different possible rotations. We need to un-rotate
1677 // the current screen dimensions before doing this.
1678 int unrotDw, unrotDh;
1679 if (rotated) {
1680 unrotDw = dh;
1681 unrotDh = dw;
1682 } else {
1683 unrotDw = dw;
1684 unrotDh = dh;
1685 }
1686 displayInfo.smallestNominalAppWidth = 1<<30;
1687 displayInfo.smallestNominalAppHeight = 1<<30;
1688 displayInfo.largestNominalAppWidth = 0;
1689 displayInfo.largestNominalAppHeight = 0;
1690 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1691 unrotDh);
1692 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1693 unrotDw);
1694 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1695 unrotDh);
1696 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1697 unrotDw);
1698 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1699 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
Evan Rosky39b6f232018-10-30 18:35:41 -07001700 displayInfo.displayCutout, displayId);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001701 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
Evan Rosky39b6f232018-10-30 18:35:41 -07001702 displayInfo.displayCutout, displayId);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001703 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
Evan Rosky39b6f232018-10-30 18:35:41 -07001704 displayInfo.displayCutout, displayId);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001705 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
Evan Rosky39b6f232018-10-30 18:35:41 -07001706 displayInfo.displayCutout, displayId);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001707 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1708 outConfig.screenLayout = sl;
1709 }
1710
1711 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
Evan Rosky39b6f232018-10-30 18:35:41 -07001712 int uiMode, DisplayCutout displayCutout, int displayId) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001713 // Get the app screen size at this rotation.
Adrian Roos11c25582018-02-19 18:06:36 +01001714 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId,
Evan Rosky39b6f232018-10-30 18:35:41 -07001715 displayCutout);
Adrian Roos11c25582018-02-19 18:06:36 +01001716 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId,
Evan Rosky39b6f232018-10-30 18:35:41 -07001717 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001718
1719 // Compute the screen layout size class for this rotation.
1720 int longSize = w;
1721 int shortSize = h;
1722 if (longSize < shortSize) {
1723 int tmp = longSize;
1724 longSize = shortSize;
1725 shortSize = tmp;
1726 }
1727 longSize = (int)(longSize/density);
1728 shortSize = (int)(shortSize/density);
1729 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1730 }
1731
1732 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1733 int uiMode, int dw, int dh) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001734 final DisplayCutout displayCutout = calculateDisplayCutoutForRotation(
1735 rotation).getDisplayCutout();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001736 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001737 displayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001738 if (width < displayInfo.smallestNominalAppWidth) {
1739 displayInfo.smallestNominalAppWidth = width;
1740 }
1741 if (width > displayInfo.largestNominalAppWidth) {
1742 displayInfo.largestNominalAppWidth = width;
1743 }
1744 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001745 displayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001746 if (height < displayInfo.smallestNominalAppHeight) {
1747 displayInfo.smallestNominalAppHeight = height;
1748 }
1749 if (height > displayInfo.largestNominalAppHeight) {
1750 displayInfo.largestNominalAppHeight = height;
1751 }
1752 }
1753
Riddle Hsua4d6fa22018-08-11 00:50:39 +08001754 /**
1755 * Apps that use the compact menu panel (as controlled by the panelMenuIsCompact
1756 * theme attribute) on devices that feature a physical options menu key attempt to position
1757 * their menu panel window along the edge of the screen nearest the physical menu key.
1758 * This lowers the travel distance between invoking the menu panel and selecting
1759 * a menu option.
1760 *
1761 * This method helps control where that menu is placed. Its current implementation makes
1762 * assumptions about the menu key and its relationship to the screen based on whether
1763 * the device's natural orientation is portrait (width < height) or landscape.
1764 *
1765 * The menu key is assumed to be located along the bottom edge of natural-portrait
1766 * devices and along the right edge of natural-landscape devices. If these assumptions
1767 * do not hold for the target device, this method should be changed to reflect that.
1768 *
1769 * @return A {@link Gravity} value for placing the options menu window.
1770 */
1771 int getPreferredOptionsPanelGravity() {
1772 final int rotation = getRotation();
1773 if (mInitialDisplayWidth < mInitialDisplayHeight) {
1774 // On devices with a natural orientation of portrait.
1775 switch (rotation) {
1776 default:
1777 case Surface.ROTATION_0:
1778 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1779 case Surface.ROTATION_90:
1780 return Gravity.RIGHT | Gravity.BOTTOM;
1781 case Surface.ROTATION_180:
1782 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1783 case Surface.ROTATION_270:
1784 return Gravity.START | Gravity.BOTTOM;
1785 }
1786 }
1787
1788 // On devices with a natural orientation of landscape.
1789 switch (rotation) {
1790 default:
1791 case Surface.ROTATION_0:
1792 return Gravity.RIGHT | Gravity.BOTTOM;
1793 case Surface.ROTATION_90:
1794 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1795 case Surface.ROTATION_180:
1796 return Gravity.START | Gravity.BOTTOM;
1797 case Surface.ROTATION_270:
1798 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1799 }
1800 }
1801
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001802 DockedStackDividerController getDockedDividerController() {
1803 return mDividerControllerLocked;
1804 }
1805
Winson Chung655332c2016-10-31 13:14:28 -07001806 PinnedStackController getPinnedStackController() {
1807 return mPinnedStackControllerLocked;
1808 }
1809
Jeff Browna506a6e2013-06-04 00:02:38 -07001810 /**
1811 * Returns true if the specified UID has access to this display.
1812 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001813 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001814 return mDisplay.hasAccess(uid);
1815 }
1816
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001817 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001818 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001819 }
1820
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001821 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001822 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001823 }
1824
Wale Ogunwale61911492017-10-11 08:50:50 -07001825 /**
1826 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1827 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001828 TaskStack getSplitScreenPrimaryStack() {
1829 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001830 return (stack != null && stack.isVisible()) ? stack : null;
1831 }
1832
Robert Carr9785cf32018-04-25 15:06:07 -07001833 boolean hasSplitScreenPrimaryStack() {
1834 return getSplitScreenPrimaryStack() != null;
1835 }
1836
Wale Ogunwale61911492017-10-11 08:50:50 -07001837 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001838 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001839 * not visible.
1840 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001841 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1842 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001843 }
1844
1845 TaskStack getPinnedStack() {
1846 return mTaskStackContainers.getPinnedStack();
1847 }
1848
1849 private boolean hasPinnedStack() {
1850 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001851 }
1852
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001853 /**
1854 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1855 * Null is no compatible stack on the display.
1856 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001857 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001858 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1859 }
1860
1861 /**
1862 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1863 * activity type. Null is no compatible stack on the display.
1864 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001865 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001866 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001867 }
1868
Bryce Lee48f4b572017-04-10 10:54:15 -07001869 @VisibleForTesting
Kazuki Takise148d00a2018-05-31 15:32:19 +09001870 WindowList<TaskStack> getStacks() {
1871 return mTaskStackContainers.mChildren;
1872 }
1873
1874 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001875 TaskStack getTopStack() {
1876 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001877 }
1878
Winson Chunge2d72172018-01-25 17:46:20 +00001879 ArrayList<Task> getVisibleTasks() {
1880 return mTaskStackContainers.getVisibleTasks();
1881 }
1882
Wale Ogunwale61911492017-10-11 08:50:50 -07001883 void onStackWindowingModeChanged(TaskStack stack) {
1884 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001885 }
1886
Andrii Kulian441e4492016-09-29 15:25:00 -07001887 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001888 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001889 super.onConfigurationChanged(newParentConfig);
1890
Evan Roskye747c3e2018-10-30 20:06:41 -07001891 // If there was no pinned stack, we still need to notify the controller of the display info
1892 // update as a result of the config change.
1893 if (mPinnedStackControllerLocked != null && !hasPinnedStack()) {
1894 mPinnedStackControllerLocked.onDisplayInfoChanged(getDisplayInfo());
1895 }
1896
Andrii Kulian3a507b52016-09-19 18:14:12 -07001897 // The display size information is heavily dependent on the resources in the current
1898 // configuration, so we need to reconfigure it every time the configuration changes.
Riddle Hsuf53da812018-08-15 22:00:27 +08001899 // See {@link #configureDisplayPolicy}...sigh...
Andrii Kulian3a507b52016-09-19 18:14:12 -07001900 mService.reconfigureDisplayLocked(this);
1901
Evan Roskye747c3e2018-10-30 20:06:41 -07001902 }
1903
1904 /**
1905 * Updates the resources used by docked/pinned controllers. This needs to be called at the
1906 * beginning of a configuration update cascade since the metrics from these resources are used
1907 * for bounds calculations. Since ActivityDisplay initiates the configuration update, this
1908 * should be called from there instead of DisplayContent's onConfigurationChanged.
1909 */
1910 void preOnConfigurationChanged() {
Bryce Leef3c6a472017-11-14 14:53:06 -08001911 final DockedStackDividerController dividerController = getDockedDividerController();
1912
1913 if (dividerController != null) {
1914 getDockedDividerController().onConfigurationChanged();
1915 }
1916
1917 final PinnedStackController pinnedStackController = getPinnedStackController();
1918
1919 if (pinnedStackController != null) {
1920 getPinnedStackController().onConfigurationChanged();
1921 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001922 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001923
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001924 @Override
1925 boolean fillsParent() {
1926 return true;
1927 }
1928
1929 @Override
1930 boolean isVisible() {
1931 return true;
1932 }
1933
1934 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001935 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001936 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001937 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001938 }
1939
Robert Carr9785cf32018-04-25 15:06:07 -07001940 /**
1941 * In split-screen mode we process the IME containers above the docked divider
1942 * rather than directly above their target.
1943 */
1944 private boolean skipTraverseChild(WindowContainer child) {
lumarkff0ab692018-11-05 20:32:30 +08001945 if (child == mImeWindowsContainers && mInputMethodTarget != null
Robert Carr9785cf32018-04-25 15:06:07 -07001946 && !hasSplitScreenPrimaryStack()) {
1947 return true;
1948 }
1949 return false;
1950 }
1951
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001952 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001953 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1954 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1955 // target, or here in order if there isn't an IME target.
1956 if (traverseTopToBottom) {
1957 for (int i = mChildren.size() - 1; i >= 0; --i) {
1958 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001959 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001960 continue;
1961 }
Robert Carr9785cf32018-04-25 15:06:07 -07001962
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001963 if (child.forAllWindows(callback, traverseTopToBottom)) {
1964 return true;
1965 }
1966 }
1967 } else {
1968 final int count = mChildren.size();
1969 for (int i = 0; i < count; i++) {
1970 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001971 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001972 continue;
1973 }
Robert Carr9785cf32018-04-25 15:06:07 -07001974
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001975 if (child.forAllWindows(callback, traverseTopToBottom)) {
1976 return true;
1977 }
1978 }
1979 }
1980 return false;
1981 }
1982
1983 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1984 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1985 }
1986
Wale Ogunwale399c8692017-05-08 14:22:42 -07001987 @Override
1988 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001989 final WindowManagerPolicy policy = mService.mPolicy;
1990
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001991 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001992 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001993 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1994 + " is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001995 // If the display is frozen, some activities may be in the middle of restarting, and
1996 // thus have removed their old window. If the window has the flag to hide the lock
1997 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1998 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001999 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002000 } else if (policy.isKeyguardLocked()) {
2001 // Use the last orientation the while the display is frozen with the keyguard
2002 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
2003 // window. We don't want to check the show when locked window directly though as
2004 // things aren't stable while the display is frozen, for example the window could be
2005 // momentarily unavailable due to activity relaunch.
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07002006 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
2007 + " is frozen while keyguard locked, return " + mLastOrientation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08002008 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07002009 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002010 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002011 final int orientation = mAboveAppWindowsContainers.getOrientation();
2012 if (orientation != SCREEN_ORIENTATION_UNSET) {
2013 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002014 }
Wale Ogunwale51362492016-09-08 17:49:17 -07002015 }
2016
Wale Ogunwale399c8692017-05-08 14:22:42 -07002017 // Top system windows are not requesting an orientation. Start searching from apps.
2018 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07002019 }
2020
Craig Mautner46ac6fa2013-08-01 10:06:34 -07002021 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07002022 // Check if display metrics changed and update base values if needed.
2023 updateBaseDisplayMetricsIfNeeded();
2024
Craig Mautner722285e2012-09-07 13:55:58 -07002025 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07002026 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07002027
Garfield Tan2f145f22018-11-01 15:27:03 -07002028 onDisplayChanged(this);
Craig Mautner722285e2012-09-07 13:55:58 -07002029 }
2030
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002031 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002032 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
2033 if (displayManagerInternal != null) {
2034 // Bootstrap the default logical display from the display manager.
2035 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
2036 if (newDisplayInfo != null) {
2037 mDisplayInfo.copyFrom(newDisplayInfo);
2038 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002039 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002040
Andrii Kuliancd097992017-03-23 18:31:59 -07002041 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
2042 mDisplayInfo.logicalDensityDpi);
2043 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
2044 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
2045 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002046 mInitialDisplayCutout = mDisplayInfo.displayCutout;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002047 }
2048
Andrii Kuliancd097992017-03-23 18:31:59 -07002049 /**
2050 * If display metrics changed, overrides are not set and it's not just a rotation - update base
2051 * values.
2052 */
2053 private void updateBaseDisplayMetricsIfNeeded() {
2054 // Get real display metrics without overrides from WM.
2055 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
2056 final int orientation = mDisplayInfo.rotation;
2057 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2058 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
2059 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
2060 final int newDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002061 final DisplayCutout newCutout = mDisplayInfo.displayCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07002062
2063 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
2064 || mInitialDisplayHeight != newHeight
Adrian Roos1cf585052018-01-03 18:43:27 +01002065 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi
2066 || !Objects.equals(mInitialDisplayCutout, newCutout);
Andrii Kuliancd097992017-03-23 18:31:59 -07002067
2068 if (displayMetricsChanged) {
2069 // Check if display size or density is forced.
2070 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
2071 || mBaseDisplayHeight != mInitialDisplayHeight;
2072 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
2073
2074 // If there is an override set for base values - use it, otherwise use new values.
2075 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
2076 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
2077 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
2078
2079 // Real display metrics changed, so we should also update initial values.
2080 mInitialDisplayWidth = newWidth;
2081 mInitialDisplayHeight = newHeight;
2082 mInitialDisplayDensity = newDensity;
Adrian Roos1cf585052018-01-03 18:43:27 +01002083 mInitialDisplayCutout = newCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07002084 mService.reconfigureDisplayLocked(this);
2085 }
2086 }
2087
Bryce Lee27cec322017-03-21 09:41:37 -07002088 /** Sets the maximum width the screen resolution can be */
2089 void setMaxUiWidth(int width) {
2090 if (DEBUG_DISPLAY) {
2091 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
2092 }
2093
2094 mMaxUiWidth = width;
2095
2096 // Update existing metrics.
2097 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
2098 }
2099
2100 /** Update base (override) display metrics. */
2101 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
2102 mBaseDisplayWidth = baseWidth;
2103 mBaseDisplayHeight = baseHeight;
2104 mBaseDisplayDensity = baseDensity;
2105
2106 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
2107 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
2108 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
2109 mBaseDisplayWidth = mMaxUiWidth;
2110
2111 if (DEBUG_DISPLAY) {
2112 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
2113 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
2114 + " on display:" + getDisplayId());
2115 }
2116 }
2117
2118 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08002119
2120 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07002121 }
2122
Riddle Hsuf53da812018-08-15 22:00:27 +08002123 /**
2124 * Forces this display to use the specified density.
2125 *
2126 * @param density The density in DPI to use. If the value equals to initial density, the setting
2127 * will be cleared.
2128 * @param userId The target user to apply. Only meaningful when this is default display. If the
2129 * user id is {@link UserHandle#USER_CURRENT}, it means to apply current settings
2130 * so only need to configure display.
2131 */
2132 void setForcedDensity(int density, int userId) {
2133 final boolean clear = density == mInitialDisplayDensity;
2134 final boolean updateCurrent = userId == UserHandle.USER_CURRENT;
2135 if (mService.mCurrentUserId == userId || updateCurrent) {
2136 mBaseDisplayDensity = density;
2137 mService.reconfigureDisplayLocked(this);
2138 }
2139 if (updateCurrent) {
2140 // We are applying existing settings so no need to save it again.
2141 return;
2142 }
2143
2144 if (density == mInitialDisplayDensity) {
2145 density = 0;
2146 }
Chilun8753ad32018-10-09 15:56:45 +08002147 mService.mDisplayWindowSettings.setForcedDensity(this, density, userId);
Riddle Hsuf53da812018-08-15 22:00:27 +08002148 }
2149
2150 /** @param mode {@link #FORCE_SCALING_MODE_AUTO} or {@link #FORCE_SCALING_MODE_DISABLED}. */
2151 void setForcedScalingMode(@ForceScalingMode int mode) {
2152 if (mode != FORCE_SCALING_MODE_DISABLED) {
2153 mode = FORCE_SCALING_MODE_AUTO;
2154 }
2155
2156 mDisplayScalingDisabled = (mode != FORCE_SCALING_MODE_AUTO);
2157 Slog.i(TAG_WM, "Using display scaling mode: " + (mDisplayScalingDisabled ? "off" : "auto"));
2158 mService.reconfigureDisplayLocked(this);
2159
Chilun8753ad32018-10-09 15:56:45 +08002160 mService.mDisplayWindowSettings.setForcedScalingMode(this, mode);
Riddle Hsuf53da812018-08-15 22:00:27 +08002161 }
2162
2163 /** If the given width and height equal to initial size, the setting will be cleared. */
2164 void setForcedSize(int width, int height) {
2165 final boolean clear = mInitialDisplayWidth == width && mInitialDisplayHeight == height;
2166 if (!clear) {
2167 // Set some sort of reasonable bounds on the size of the display that we will try
2168 // to emulate.
2169 final int minSize = 200;
2170 final int maxScale = 2;
2171 width = Math.min(Math.max(width, minSize), mInitialDisplayWidth * maxScale);
2172 height = Math.min(Math.max(height, minSize), mInitialDisplayHeight * maxScale);
2173 }
2174
2175 Slog.i(TAG_WM, "Using new display size: " + width + "x" + height);
2176 updateBaseDisplayMetrics(width, height, mBaseDisplayDensity);
2177 mService.reconfigureDisplayLocked(this);
2178
2179 if (clear) {
2180 width = height = 0;
2181 }
Chilun8753ad32018-10-09 15:56:45 +08002182 mService.mDisplayWindowSettings.setForcedSize(this, width, height);
Riddle Hsuf53da812018-08-15 22:00:27 +08002183 }
2184
Wale Ogunwaledb506192017-12-08 10:57:32 -08002185 void getStableRect(Rect out) {
2186 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08002187 }
2188
Wale Ogunwale61911492017-10-11 08:50:50 -07002189 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002190 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
2191 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07002192
Wale Ogunwale61911492017-10-11 08:50:50 -07002193 final TaskStack stack = new TaskStack(mService, stackId, controller);
2194 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002195 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07002196 }
2197
Andrii Kulian51c1b672017-04-07 18:39:32 -07002198 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002199 final DisplayContent prevDc = stack.getDisplayContent();
2200 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07002201 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
2202 + " which is not currently attached to any display");
2203 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002204 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07002205 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
2206 + " to its current displayId=" + mDisplayId);
2207 }
2208
lumark588a3e82018-07-20 18:53:54 +08002209 // Clean up all pending transitions when stack reparent to another display.
2210 stack.forAllAppWindows(AppWindowToken::removeFromPendingTransition);
2211
Wale Ogunwale61911492017-10-11 08:50:50 -07002212 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07002213 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08002214 }
2215
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002216 @Override
2217 protected void addChild(DisplayChildWindowContainer child,
2218 Comparator<DisplayChildWindowContainer> comparator) {
2219 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2220 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002221
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002222 @Override
2223 protected void addChild(DisplayChildWindowContainer child, int index) {
2224 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2225 }
2226
2227 @Override
2228 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002229 // Only allow removal of direct children from this display if the display is in the process
2230 // of been removed.
2231 if (mRemovingDisplay) {
2232 super.removeChild(child);
2233 return;
2234 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002235 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002236 }
2237
Andrii Kuliand2765632016-12-12 22:26:34 -08002238 @Override
2239 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
2240 // Children of the display are statically ordered, so the real intention here is to perform
2241 // the operation on the display and not the static direct children.
2242 getParent().positionChildAt(position, this, includingParents);
2243 }
2244
Riddle Hsu57831b52018-07-27 00:31:48 +08002245 void positionStackAt(int position, TaskStack child, boolean includingParents) {
2246 mTaskStackContainers.positionChildAt(position, child, includingParents);
Winson Chung59a47ded2018-01-25 17:46:06 +00002247 layoutAndAssignWindowLayersIfNeeded();
2248 }
2249
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002250 /**
2251 * Used to obtain task ID when user taps on coordinate (x, y) in this display, and outside
2252 * current task in focus.
2253 *
2254 * This returns the task ID of the foremost task at (x, y) if the task is not home. Otherwise it
2255 * returns -1.
2256 *
2257 * @param x horizontal coordinate of the tap position
2258 * @param y vertical coordinate of the tap position
2259 * @return the task ID if a non-home task is found; -1 if not
2260 */
2261 int taskForTapOutside(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002262 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2263 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Garfield Tanf2e0a9e2018-10-16 09:42:02 -07002264 if (stack.isActivityTypeHome() && !stack.inMultiWindowMode()) {
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002265 // We skip not only home stack, but also everything behind home because user can't
Garfield Tanf2e0a9e2018-10-16 09:42:02 -07002266 // see them when home stack is isn't in multi-window mode.
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002267 break;
2268 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002269 final int taskId = stack.taskIdFromPoint(x, y);
2270 if (taskId != -1) {
2271 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07002272 }
2273 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002274 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07002275 }
2276
Chong Zhang8e89b312015-09-09 15:09:30 -07002277 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08002278 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07002279 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07002280 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07002281 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002282 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002283 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07002284 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2285 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002286 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002287 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002288 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002289
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002290 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
2291 if (mTmpTaskForResizePointSearchResult.searchDone) {
2292 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07002293 }
2294 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002295 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002296 }
2297
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002298 void updateTouchExcludeRegion() {
2299 final Task focusedTask = (mFocusedApp != null ? mFocusedApp.getTask() : null);
David Stevensee9e2772017-02-09 16:30:27 -08002300 if (focusedTask == null) {
2301 mTouchExcludeRegion.setEmpty();
2302 } else {
2303 mTouchExcludeRegion.set(mBaseDisplayRect);
2304 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
2305 mTmpRect2.setEmpty();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002306 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0;
2307 --stackNdx) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002308 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002309 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
2310 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08002311 }
2312 // If we removed the focused task above, add it back and only leave its
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002313 // outside touch area in the exclusion. TapDetector is not interested in
David Stevensee9e2772017-02-09 16:30:27 -08002314 // any touch inside the focused task itself.
2315 if (!mTmpRect2.isEmpty()) {
2316 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
2317 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08002318 }
lumark90120a82018-08-15 00:33:03 +08002319 if (mInputMethodWindow != null && mInputMethodWindow.isVisibleLw()) {
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002320 // If the input method is visible and the user is typing, we don't want these touch
2321 // events to be intercepted and used to change focus. This would likely cause a
2322 // disappearance of the input method.
lumark90120a82018-08-15 00:33:03 +08002323 mInputMethodWindow.getTouchableRegion(mTmpRegion);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002324 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002325 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002326 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002327 final WindowState win = mTapExcludedWindows.get(i);
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002328 win.getTouchableRegion(mTmpRegion);
2329 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
2330 }
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002331 for (int i = mTapExcludeProvidingWindows.size() - 1; i >= 0; i--) {
2332 final WindowState win = mTapExcludeProvidingWindows.valueAt(i);
2333 win.amendTapExcludeRegion(mTouchExcludeRegion);
2334 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08002335 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07002336 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002337 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07002338 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002339 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
2340 }
Craig Mautner1bef3892015-02-17 15:09:47 -08002341 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07002342 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08002343 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07002344 }
2345
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002346 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07002347 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002348 super.switchUser();
2349 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07002350 }
2351
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07002352 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002353 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2354 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07002355 }
2356 }
2357
Wale Ogunwale10124582016-09-15 20:25:50 -07002358 @Override
2359 void removeIfPossible() {
2360 if (isAnimating()) {
2361 mDeferredRemoval = true;
2362 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07002363 }
Wale Ogunwale10124582016-09-15 20:25:50 -07002364 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07002365 }
2366
Wale Ogunwale10124582016-09-15 20:25:50 -07002367 @Override
2368 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002369 mRemovingDisplay = true;
2370 try {
lumark588a3e82018-07-20 18:53:54 +08002371 // Clear all transitions & screen frozen states when removing display.
2372 mOpeningApps.clear();
2373 mClosingApps.clear();
2374 mUnknownAppVisibilityController.clear();
2375 mAppTransition.removeAppTransitionTimeoutCallbacks();
2376 handleAnimatingStoppedAndTransition();
2377 mService.stopFreezingDisplayLocked();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002378 super.removeImmediately();
2379 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Arthur Hungbe5ce212018-09-13 18:41:56 +08002380 if (mPointerEventDispatcher != null && mTapDetector != null) {
2381 unregisterPointerEventListener(mTapDetector);
2382 unregisterPointerEventListener(mService.mMousePositionTracker);
2383 mTapDetector = null;
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002384 }
Jorim Jaggia3844032018-01-03 15:54:43 +01002385 mService.mAnimator.removeDisplayLocked(mDisplayId);
Louis Chang207f9112018-07-30 08:58:54 +08002386 mWindowingLayer.release();
2387 mOverlayLayer.release();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002388 } finally {
Riddle Hsu4e611772018-10-31 18:58:28 +08002389 mDisplayReady = false;
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002390 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08002391 }
Bryce Leef19cbe22018-02-02 15:09:21 -08002392
Arthur Hung39134b22018-08-14 11:58:28 +08002393 mInputMonitor.onRemoved();
Riddle Hsuf53da812018-08-15 22:00:27 +08002394 mService.mWindowPlacerLocked.requestTraversal();
Craig Mautner95da1082014-02-24 17:54:35 -08002395 }
2396
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002397 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07002398 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002399 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002400 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
2401
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002402 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002403 removeImmediately();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002404 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08002405 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002406 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08002407 }
2408
Andrii Kulian0214ed92017-05-16 13:44:05 -07002409 /** @return 'true' if removal of this display content is deferred due to active animation. */
2410 boolean isRemovalDeferred() {
2411 return mDeferredRemoval;
2412 }
2413
Wale Ogunwale10124582016-09-15 20:25:50 -07002414 boolean animateForIme(float interpolatedValue, float animationTarget,
2415 float dividerAnimationTarget) {
2416 boolean updated = false;
2417
Wale Ogunwale61911492017-10-11 08:50:50 -07002418 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2419 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002420 if (stack == null || !stack.isAdjustedForIme()) {
2421 continue;
2422 }
2423
2424 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
2425 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2426 updated = true;
2427 } else {
2428 mDividerControllerLocked.mLastAnimationProgress =
2429 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
2430 mDividerControllerLocked.mLastDividerProgress =
2431 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
2432 updated |= stack.updateAdjustForIme(
2433 mDividerControllerLocked.mLastAnimationProgress,
2434 mDividerControllerLocked.mLastDividerProgress,
2435 false /* force */);
2436 }
2437 if (interpolatedValue >= 1f) {
2438 stack.endImeAdjustAnimation();
2439 }
2440 }
2441
2442 return updated;
2443 }
2444
2445 boolean clearImeAdjustAnimation() {
2446 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002447 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2448 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002449 if (stack != null && stack.isAdjustedForIme()) {
2450 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2451 changed = true;
2452 }
2453 }
2454 return changed;
2455 }
2456
2457 void beginImeAdjustAnimation() {
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.isVisible() && stack.isAdjustedForIme()) {
2461 stack.beginImeAdjustAnimation();
2462 }
2463 }
2464 }
2465
2466 void adjustForImeIfNeeded() {
lumark90120a82018-08-15 00:33:03 +08002467 final WindowState imeWin = mInputMethodWindow;
Wale Ogunwale10124582016-09-15 20:25:50 -07002468 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2469 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002470 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002471 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2472 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2473 imeTargetStack.getDockSide() : DOCKED_INVALID;
2474 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2475 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2476 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002477 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002478 final boolean imeHeightChanged = imeVisible &&
2479 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2480
2481 // The divider could be adjusted for IME position, or be thinner than usual,
2482 // or both. There are three possible cases:
2483 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2484 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2485 // - If IME is not visible, divider is not moved and is normal width.
2486
2487 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002488 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2489 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002490 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002491 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2492 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002493 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2494 } else {
2495 stack.resetAdjustedForIme(false);
2496 }
2497 }
2498 mDividerControllerLocked.setAdjustedForIme(
2499 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2500 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002501 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2502 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002503 stack.resetAdjustedForIme(!dockVisible);
2504 }
2505 mDividerControllerLocked.setAdjustedForIme(
2506 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2507 }
Winson Chung655332c2016-10-31 13:14:28 -07002508 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002509 }
2510
2511 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002512 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2513 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002514 stack.prepareFreezingTaskBounds();
2515 }
2516 }
2517
Wale Ogunwale94744212015-09-21 19:01:47 -07002518 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002519 getBounds(mTmpRect, newRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002520 rotateBounds(mTmpRect, oldRotation, newRotation, bounds);
2521 }
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002522
Evan Rosky39b6f232018-10-30 18:35:41 -07002523 void rotateBounds(Rect parentBounds, int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002524 // Compute a transform matrix to undo the coordinate space transformation,
2525 // and present the window at the same physical position it previously occupied.
2526 final int deltaRotation = deltaRotation(newRotation, oldRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002527 createRotationMatrix(
2528 deltaRotation, parentBounds.width(), parentBounds.height(), mTmpMatrix);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002529
2530 mTmpRectF.set(bounds);
2531 mTmpMatrix.mapRect(mTmpRectF);
2532 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002533 }
2534
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002535 static int deltaRotation(int oldRotation, int newRotation) {
2536 int delta = newRotation - oldRotation;
2537 if (delta < 0) delta += 4;
2538 return delta;
2539 }
2540
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002541 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002542 Matrix outMatrix) {
2543 // For rotations without Z-ordering we don't need the target rectangle's position.
2544 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2545 displayHeight, outMatrix);
2546 }
2547
2548 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2549 float displayWidth, float displayHeight, Matrix outMatrix) {
2550 switch (rotation) {
2551 case ROTATION_0:
2552 outMatrix.reset();
2553 break;
2554 case ROTATION_270:
2555 outMatrix.setRotate(270, 0, 0);
2556 outMatrix.postTranslate(0, displayHeight);
2557 outMatrix.postTranslate(rectTop, 0);
2558 break;
2559 case ROTATION_180:
2560 outMatrix.reset();
2561 break;
2562 case ROTATION_90:
2563 outMatrix.setRotate(90, 0, 0);
2564 outMatrix.postTranslate(displayWidth, 0);
2565 outMatrix.postTranslate(-rectTop, rectLeft);
2566 break;
2567 }
2568 }
2569
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002570 @CallSuper
2571 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002572 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002573 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002574 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002575 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002576 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2577 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002578 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002579 }
2580 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2581 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002582 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2583 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002584 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002585 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002586 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2587 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002588 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002589 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002590 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2591 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002592 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002593 }
2594 proto.write(DPI, mBaseDisplayDensity);
2595 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002596 proto.write(ROTATION, mRotation);
2597 final ScreenRotationAnimation screenRotationAnimation =
2598 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2599 if (screenRotationAnimation != null) {
2600 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2601 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002602 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
lumark588a3e82018-07-20 18:53:54 +08002603 mAppTransition.writeToProto(proto, APP_TRANSITION);
Louis Chang7501e332018-08-20 13:08:39 +08002604 proto.write(SURFACE_SIZE, mSurfaceSize);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002605 if (mFocusedApp != null) {
2606 mFocusedApp.writeNameToProto(proto, FOCUSED_APP);
2607 }
Steven Timotiusaf03df62017-07-18 16:56:43 -07002608 proto.end(token);
2609 }
2610
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002611 @Override
2612 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2613 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002614 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2615 final String subPrefix = " " + prefix;
2616 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2617 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2618 pw.print("dpi");
2619 if (mInitialDisplayWidth != mBaseDisplayWidth
2620 || mInitialDisplayHeight != mBaseDisplayHeight
2621 || mInitialDisplayDensity != mBaseDisplayDensity) {
2622 pw.print(" base=");
2623 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2624 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2625 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002626 if (mDisplayScalingDisabled) {
2627 pw.println(" noscale");
2628 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002629 pw.print(" cur=");
2630 pw.print(mDisplayInfo.logicalWidth);
2631 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2632 pw.print(" app=");
2633 pw.print(mDisplayInfo.appWidth);
2634 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2635 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2636 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2637 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2638 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002639 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002640 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002641 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002642
Craig Mautnerdc548482014-02-05 13:35:24 -08002643 pw.println();
Adrian Roos5251b1d2018-03-23 18:57:43 +01002644 pw.print(prefix); pw.print("mLayoutSeq="); pw.println(mLayoutSeq);
Riddle Hsu654a6f92018-07-13 22:59:36 +08002645 pw.print(prefix);
2646 pw.print("mDeferredRotationPauseCount="); pw.println(mDeferredRotationPauseCount);
Adrian Roos5251b1d2018-03-23 18:57:43 +01002647
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002648 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
2649 if (mLastFocus != mCurrentFocus) {
2650 pw.print(" mLastFocus="); pw.println(mLastFocus);
2651 }
2652 if (mLosingFocus.size() > 0) {
2653 pw.println();
2654 pw.println(" Windows losing focus:");
2655 for (int i = mLosingFocus.size() - 1; i >= 0; i--) {
2656 final WindowState w = mLosingFocus.get(i);
2657 pw.print(" Losing #"); pw.print(i); pw.print(' ');
2658 pw.print(w);
2659 if (dumpAll) {
2660 pw.println(":");
2661 w.dump(pw, " ", true);
2662 } else {
2663 pw.println();
2664 }
2665 }
2666 }
2667 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
2668
Adrian Roos5251b1d2018-03-23 18:57:43 +01002669 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002670 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002671 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2672 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002673 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002674 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002675
Craig Mautnerdc548482014-02-05 13:35:24 -08002676 pw.println();
2677 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002678 pw.println();
2679 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002680 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002681 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002682 pw.print(" Exiting #"); pw.print(i);
2683 pw.print(' '); pw.print(token);
2684 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002685 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002686 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002687 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002688
Jorim Jaggi31f71702016-05-04 16:43:04 -07002689 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002690
2691 // Dump stack references
2692 final TaskStack homeStack = getHomeStack();
2693 if (homeStack != null) {
2694 pw.println(prefix + "homeStack=" + homeStack.getName());
2695 }
2696 final TaskStack pinnedStack = getPinnedStack();
2697 if (pinnedStack != null) {
2698 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2699 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002700 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002701 if (splitScreenPrimaryStack != null) {
2702 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2703 }
2704
2705 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002706 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002707 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002708 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002709
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002710 pw.println();
2711 mDisplayFrames.dump(prefix, pw);
Arthur Hung95b38a92018-07-20 18:56:12 +08002712 pw.println();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08002713 mDisplayPolicy.dump(prefix, pw);
2714 pw.println();
Riddle Hsuad256a12018-07-18 16:11:30 +08002715 mDisplayRotation.dump(prefix, pw);
2716 pw.println();
Arthur Hung95b38a92018-07-20 18:56:12 +08002717 mInputMonitor.dump(pw, " ");
Craig Mautner59c00972012-07-30 12:10:24 -07002718 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002719
2720 @Override
2721 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002722 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002723 }
2724
2725 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002726 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002727 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002728
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002729 /** Returns true if the stack in the windowing mode is visible. */
2730 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002731 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002732 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002733 }
2734
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002735 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002736 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002737 final int x = (int) xf;
2738 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002739 final WindowState touchedWin = getWindow(w -> {
2740 final int flags = w.mAttrs.flags;
2741 if (!w.isVisibleLw()) {
2742 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002743 }
2744 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002745 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002746 }
2747
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002748 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002749 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002750 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002751 }
2752
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002753 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002754
2755 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002756 return mTmpRegion.contains(x, y) || touchFlags == 0;
2757 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002758
2759 return touchedWin;
2760 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002761
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002762 boolean canAddToastWindowForUid(int uid) {
2763 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002764 // Also if the app is focused adding more than one toast at
2765 // a time for better backwards compatibility.
2766 final WindowState focusedWindowForUid = getWindow(w ->
2767 w.mOwnerUid == uid && w.isFocused());
2768 if (focusedWindowForUid != null) {
2769 return true;
2770 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002771 final WindowState win = getWindow(w ->
2772 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2773 && !w.mWindowRemovalAllowed);
2774 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002775 }
2776
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002777 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002778 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2779 return;
2780 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002781
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002782 // Used to communicate the old focus to the callback method.
2783 mTmpWindow = oldFocus;
2784
2785 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002786 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002787
2788 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002789 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002790
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002791 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002792
2793 if (mTmpWindow == null) {
2794 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2795 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002796 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002797 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002798 }
2799
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002800
2801 /**
2802 * Update the focused window and make some adjustments if the focus has changed.
2803 *
2804 * @param mode Indicates the situation we are in. Possible modes are:
2805 * {@link WindowManagerService#UPDATE_FOCUS_NORMAL},
2806 * {@link WindowManagerService#UPDATE_FOCUS_PLACING_SURFACES},
2807 * {@link WindowManagerService#UPDATE_FOCUS_WILL_PLACE_SURFACES},
2808 * {@link WindowManagerService#UPDATE_FOCUS_REMOVING_FOCUS}
2809 * @param updateInputWindows Whether to sync the window information to the input module.
2810 * @return {@code true} if the focused window has changed.
2811 */
2812 boolean updateFocusedWindowLocked(int mode, boolean updateInputWindows, boolean focusFound) {
2813 final WindowState newFocus = findFocusedWindow();
2814 if (mCurrentFocus == newFocus) {
2815 return false;
2816 }
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002817 boolean imWindowChanged = false;
2818 // TODO (b/111080190): Multi-Session IME
2819 if (!focusFound) {
2820 final WindowState imWindow = mInputMethodWindow;
2821 if (imWindow != null) {
lumarkff0ab692018-11-05 20:32:30 +08002822 final WindowState prevTarget = mInputMethodTarget;
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002823
2824 final WindowState newTarget = computeImeTarget(true /* updateImeTarget*/);
2825 imWindowChanged = prevTarget != newTarget;
2826
2827 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS
2828 && mode != UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2829 assignWindowLayers(false /* setLayoutNeeded */);
2830 }
2831 }
2832 }
2833
2834 if (imWindowChanged) {
2835 mService.mWindowsChanged = true;
2836 setLayoutNeeded();
2837 }
2838
2839 if (DEBUG_FOCUS_LIGHT || mService.localLOGV) Slog.v(TAG_WM, "Changing focus from "
2840 + mCurrentFocus + " to " + newFocus + " displayId=" + getDisplayId()
2841 + " Callers=" + Debug.getCallers(4));
2842 final WindowState oldFocus = mCurrentFocus;
2843 mCurrentFocus = newFocus;
2844 mLosingFocus.remove(newFocus);
2845
2846 if (newFocus != null) {
2847 mWinAddedSinceNullFocus.clear();
2848 mWinRemovedSinceNullFocus.clear();
2849
2850 if (newFocus.canReceiveKeys()) {
2851 // Displaying a window implicitly causes dispatching to be unpaused.
2852 // This is to protect against bugs if someone pauses dispatching but
2853 // forgets to resume.
2854 newFocus.mToken.paused = false;
2855 }
2856 }
2857
2858 // System UI is only shown on the default display.
2859 int focusChanged = isDefaultDisplay
2860 ? mService.mPolicy.focusChangedLw(oldFocus, newFocus) : 0;
2861
2862 if (imWindowChanged && oldFocus != mInputMethodWindow) {
2863 // Focus of the input method window changed. Perform layout if needed.
2864 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2865 performLayout(true /*initial*/, updateInputWindows);
2866 focusChanged &= ~FINISH_LAYOUT_REDO_LAYOUT;
2867 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2868 // Client will do the layout, but we need to assign layers
2869 // for handleNewWindowLocked() below.
2870 assignWindowLayers(false /* setLayoutNeeded */);
2871 }
2872 }
2873
2874 if ((focusChanged & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2875 // The change in focus caused us to need to do a layout. Okay.
2876 setLayoutNeeded();
2877 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2878 performLayout(true /*initial*/, updateInputWindows);
2879 } else if (mode == UPDATE_FOCUS_REMOVING_FOCUS) {
2880 mService.mRoot.performSurfacePlacement(false);
2881 }
2882 }
2883
2884 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
2885 // If we defer assigning layers, then the caller is responsible for doing this part.
2886 getInputMonitor().setInputFocusLw(newFocus, updateInputWindows);
2887 }
2888
2889 adjustForImeIfNeeded();
2890
2891 // We may need to schedule some toast windows to be removed. The toasts for an app that
2892 // does not have input focus are removed within a timeout to prevent apps to redress
2893 // other apps' UI.
2894 scheduleToastWindowsTimeoutIfNeededLocked(oldFocus, newFocus);
2895
2896 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2897 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
2898 }
2899 return true;
2900 }
2901
2902 /**
2903 * Set the new focused app to this display.
2904 *
2905 * @param newFocus the new focused AppWindowToken.
2906 * @return true if the focused app is changed.
2907 */
2908 boolean setFocusedApp(AppWindowToken newFocus) {
2909 if (newFocus != null) {
2910 final DisplayContent appDisplay = newFocus.getDisplayContent();
2911 if (appDisplay != this) {
2912 throw new IllegalStateException(newFocus + " is not on " + getName()
2913 + " but " + ((appDisplay != null) ? appDisplay.getName() : "none"));
2914 }
2915 }
2916 if (mFocusedApp == newFocus) {
2917 return false;
2918 }
2919 mFocusedApp = newFocus;
2920 getInputMonitor().setFocusedAppLw(newFocus);
2921 updateTouchExcludeRegion();
2922 return true;
2923 }
2924
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002925 /** Updates the layer assignment of windows on this display. */
2926 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002927 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07002928 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002929 if (setLayoutNeeded) {
2930 setLayoutNeeded();
2931 }
Robert Carrb1579c82017-09-05 14:54:47 -07002932
Robert Carrf59b8dd2017-10-02 18:58:36 -07002933 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002934 // the application of this transaction until the animation pass triggers
2935 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2936 // the hiding and showing of surfaces.
2937 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002938 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002939 }
2940
Wale Ogunwale1666e312016-12-16 11:27:18 -08002941 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2942 // moving containers or resizing them. Need to investigate the best way to have it automatically
2943 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002944 void layoutAndAssignWindowLayersIfNeeded() {
2945 mService.mWindowsChanged = true;
2946 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002947
2948 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2949 false /*updateInputWindows*/)) {
2950 assignWindowLayers(false /* setLayoutNeeded */);
2951 }
2952
Arthur Hung95b38a92018-07-20 18:56:12 +08002953 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002954 mService.mWindowPlacerLocked.performSurfacePlacement();
Arthur Hung95b38a92018-07-20 18:56:12 +08002955 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002956 }
2957
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002958 /** Returns true if a leaked surface was destroyed */
2959 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002960 // Used to indicate that a surface was leaked.
2961 mTmpWindow = null;
2962 forAllWindows(w -> {
2963 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002964 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002965 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002966 }
2967 if (!mService.mSessions.contains(wsa.mSession)) {
2968 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002969 + w + " surface=" + wsa.mSurfaceController
2970 + " token=" + w.mToken
2971 + " pid=" + w.mSession.mPid
2972 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002973 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002974 mService.mForceRemoves.add(w);
2975 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002976 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002977 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002978 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002979 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002980 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002981 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002982 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002983 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002984 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002985
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002986 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002987 }
2988
2989 /**
lumark90120a82018-08-15 00:33:03 +08002990 * Set input method window for the display.
2991 * @param win Set when window added or Null when destroyed.
2992 */
2993 void setInputMethodWindowLocked(WindowState win) {
2994 mInputMethodWindow = win;
lumark70865a82018-09-17 16:56:26 +08002995 // Update display configuration for IME process.
2996 if (mInputMethodWindow != null) {
2997 final int imePid = mInputMethodWindow.mSession.mPid;
2998 mService.mAtmInternal.onImeWindowSetOnDisplay(imePid,
2999 mInputMethodWindow.getDisplayId());
3000 }
lumark90120a82018-08-15 00:33:03 +08003001 computeImeTarget(true /* updateImeTarget */);
3002 }
3003
3004 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003005 * Determine and return the window that should be the IME target.
3006 * @param updateImeTarget If true the system IME target will be updated to match what we found.
3007 * @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 +00003008 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003009 WindowState computeImeTarget(boolean updateImeTarget) {
lumark90120a82018-08-15 00:33:03 +08003010 if (mInputMethodWindow == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003011 // There isn't an IME so there shouldn't be a target...That was easy!
3012 if (updateImeTarget) {
3013 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
lumarkff0ab692018-11-05 20:32:30 +08003014 + mInputMethodTarget + " to null since mInputMethodWindow is null");
3015 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003016 }
3017 return null;
3018 }
3019
lumarkff0ab692018-11-05 20:32:30 +08003020 final WindowState curTarget = mInputMethodTarget;
Chavi Weingarten3a748552018-05-14 17:32:42 +00003021 if (!canUpdateImeTarget()) {
3022 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Defer updating IME target");
3023 return curTarget;
3024 }
3025
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003026 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
3027 // same display. Or even when the current IME/target are not on the same screen as the next
3028 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08003029 mUpdateImeTarget = updateImeTarget;
3030 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003031
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003032
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003033 // Yet more tricksyness! If this window is a "starting" window, we do actually want
3034 // to be on top of it, but it is not -really- where input will go. So look down below
3035 // for a real window to target...
3036 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
3037 final AppWindowToken token = target.mAppToken;
3038 if (token != null) {
3039 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
3040 if (betterTarget != null) {
3041 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003042 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003043 }
3044 }
3045
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003046 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
lumarkff0ab692018-11-05 20:32:30 +08003047 "Proposed new IME target: " + target + " for display: " + getDisplayId());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003048
chaviw1c13ad32018-06-12 16:44:23 -07003049 // Now, a special case -- if the last target's window is in the process of exiting, but
3050 // not removed, and the new target is home, keep on the last target to avoid flicker.
3051 // Home is a special case since its above other stacks in the ordering list, but layed
3052 // out below the others.
3053 if (curTarget != null && !curTarget.mRemoved && curTarget.isDisplayedLw()
3054 && curTarget.isClosing() && (target == null || target.isActivityTypeHome())) {
chaviw4ad54912018-05-30 11:05:44 -07003055 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "New target is home while current target is "
Chavi Weingarten3a748552018-05-14 17:32:42 +00003056 + "closing, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003057 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003058 }
3059
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003060 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
3061 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003062
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003063 if (target == null) {
3064 if (updateImeTarget) {
3065 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
3066 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
3067 + Debug.getCallers(4) : ""));
lumarkff0ab692018-11-05 20:32:30 +08003068 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003069 }
3070
3071 return null;
3072 }
3073
3074 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003075 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
3076 if (token != null) {
3077
3078 // Now some fun for dealing with window animations that modify the Z order. We need
3079 // to look at all windows below the current target that are in this app, finding the
3080 // highest visible one in layering.
3081 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003082 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003083 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003084 }
3085
3086 if (highestTarget != null) {
lumark588a3e82018-07-20 18:53:54 +08003087 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, mAppTransition + " " + highestTarget
Jorim Jaggi35d328a2018-08-14 17:00:20 +02003088 + " animating=" + highestTarget.isAnimating());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003089
lumark588a3e82018-07-20 18:53:54 +08003090 if (mAppTransition.isTransitionSet()) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003091 // If we are currently setting up for an animation, hold everything until we
3092 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003093 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003094 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003095 }
3096 }
3097 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003098
3099 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
3100 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003101 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003102 }
3103
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003104 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003105 }
3106
lumarkff0ab692018-11-05 20:32:30 +08003107 /**
3108 * Calling {@link #computeImeTarget(boolean)} to update the input method target window in
3109 * the candidate app window token if needed.
3110 */
3111 void computeImeTargetIfNeeded(AppWindowToken candidate) {
3112 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == candidate) {
3113 computeImeTarget(true /* updateImeTarget */);
3114 }
3115 }
3116
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003117 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
lumarkff0ab692018-11-05 20:32:30 +08003118 if (target == mInputMethodTarget && mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003119 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003120 }
3121
lumarkff0ab692018-11-05 20:32:30 +08003122 mInputMethodTarget = target;
3123 mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003124 assignWindowLayers(false /* setLayoutNeeded */);
3125 }
3126
3127 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
3128 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3129 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
3130 }
3131
3132 // Used to indicate we have reached the first window in the range we are interested in.
3133 mTmpWindow = null;
3134
3135 // TODO: Figure-out a more efficient way to do this.
3136 final WindowState candidate = getWindow(w -> {
3137 if (w == top) {
3138 // Reached the first window in the range we are interested in.
3139 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003140 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003141 if (mTmpWindow == null) {
3142 return false;
3143 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003144
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003145 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3146 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003147 }
3148 // If we reached the bottom of the range of windows we are considering,
3149 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003150 if (w == bottom) {
3151 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003152 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003153 return false;
3154 });
3155
3156 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003157 }
3158
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003159 void setLayoutNeeded() {
3160 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
3161 mLayoutNeeded = true;
3162 }
3163
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003164 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003165 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
3166 mLayoutNeeded = false;
3167 }
3168
3169 boolean isLayoutNeeded() {
3170 return mLayoutNeeded;
3171 }
3172
Wale Ogunwale02319a62016-09-26 15:21:22 -07003173 void dumpTokens(PrintWriter pw, boolean dumpAll) {
3174 if (mTokenMap.isEmpty()) {
3175 return;
3176 }
3177 pw.println(" Display #" + mDisplayId);
3178 final Iterator<WindowToken> it = mTokenMap.values().iterator();
3179 while (it.hasNext()) {
3180 final WindowToken token = it.next();
3181 pw.print(" ");
3182 pw.print(token);
3183 if (dumpAll) {
3184 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003185 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07003186 } else {
3187 pw.println();
3188 }
3189 }
lumark588a3e82018-07-20 18:53:54 +08003190
3191 if (!mOpeningApps.isEmpty() || !mClosingApps.isEmpty()) {
3192 pw.println();
3193 if (mOpeningApps.size() > 0) {
3194 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
3195 }
3196 if (mClosingApps.size() > 0) {
3197 pw.print(" mClosingApps="); pw.println(mClosingApps);
3198 }
3199 }
3200
3201 mUnknownAppVisibilityController.dump(pw, " ");
Wale Ogunwale02319a62016-09-26 15:21:22 -07003202 }
3203
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003204 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003205 final int[] index = new int[1];
3206 forAllWindows(w -> {
3207 final WindowStateAnimator wAnim = w.mWinAnimator;
3208 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
3209 index[0] = index[0] + 1;
3210 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003211 }
3212
Jorim Jaggife762342016-10-13 14:33:27 +02003213 /**
3214 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
3215 */
3216 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
3217 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003218 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02003219 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
3220 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01003221 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02003222 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003223 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02003224 }
3225
Wale Ogunwale494009b82016-10-21 09:01:38 -07003226 boolean checkWaitingForWindows() {
3227
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003228 mHaveBootMsg = false;
3229 mHaveApp = false;
3230 mHaveWallpaper = false;
3231 mHaveKeyguard = true;
3232
3233 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003234 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
3235 return true;
3236 }
3237 if (w.isDrawnLw()) {
3238 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003239 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003240 } else if (w.mAttrs.type == TYPE_APPLICATION
3241 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003242 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003243 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003244 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003245 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003246 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003247 }
3248 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003249 return false;
3250 });
3251
3252 if (visibleWindow != null) {
3253 // We have a visible window.
3254 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003255 }
3256
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003257 // if the wallpaper service is disabled on the device, we're never going to have
3258 // wallpaper, don't bother waiting for it
3259 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
3260 com.android.internal.R.bool.config_enableWallpaperService)
Daichi Hironoc1432222018-03-29 13:06:07 +09003261 && mService.mContext.getResources().getBoolean(
3262 com.android.internal.R.bool.config_checkWallpaperAtBoot)
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003263 && !mService.mOnlyCore;
3264
Wale Ogunwale494009b82016-10-21 09:01:38 -07003265 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
3266 "******** booted=" + mService.mSystemBooted
3267 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003268 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
3269 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
3270 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003271
3272 // If we are turning on the screen to show the boot message, don't do it until the boot
3273 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003274 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003275 return true;
3276 }
3277
3278 // If we are turning on the screen after the boot is completed normally, don't do so until
3279 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003280 if (mService.mSystemBooted
3281 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003282 return true;
3283 }
3284
3285 return false;
3286 }
3287
Jorim Jaggi8f520872018-08-14 17:00:20 +02003288 void updateWindowsForAnimator() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003289 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003290 }
3291
Jorim Jaggi8f520872018-08-14 17:00:20 +02003292 /**
3293 * Updates the {@link TaskStack#setAnimationBackground} for all windows.
3294 */
3295 void updateBackgroundForAnimator() {
Jorim Jaggi7156b652016-10-25 08:31:58 -07003296 resetAnimationBackgroundAnimator();
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003297 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003298 }
3299
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003300 boolean isInputMethodClientFocus(int uid, int pid) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003301 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003302 if (imFocus == null) {
3303 return false;
3304 }
3305
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003306 if (DEBUG_INPUT_METHOD) {
3307 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003308 Slog.i(TAG_WM, "Current focus: " + mCurrentFocus + " displayId=" + mDisplayId);
3309 Slog.i(TAG_WM, "Last focus: " + mLastFocus + " displayId=" + mDisplayId);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003310 }
3311
Wale Ogunwale494009b82016-10-21 09:01:38 -07003312 if (DEBUG_INPUT_METHOD) {
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003313 Slog.i(TAG_WM, "IM target uid/pid: " + imFocus.mSession.mUid
3314 + "/" + imFocus.mSession.mPid);
3315 Slog.i(TAG_WM, "Requesting client uid/pid: " + uid + "/" + pid);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003316 }
3317
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003318 return imFocus.mSession.mUid == uid && imFocus.mSession.mPid == pid;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003319 }
3320
3321 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003322 final WindowState win = getWindow(
3323 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
3324 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003325 }
3326
3327 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003328 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003329 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003330 final int curValue = w.mSystemUiVisibility;
3331 final int diff = (curValue ^ visibility) & globalDiff;
3332 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003333 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003334 w.mSeq++;
3335 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003336 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003337 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
3338 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07003339 visibility, newValue, diff);
3340 }
3341 } catch (RemoteException e) {
3342 // so sorry
3343 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003344 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003345 }
3346
3347 void onWindowFreezeTimeout() {
3348 Slog.w(TAG_WM, "Window freeze timeout expired.");
3349 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003350
3351 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07003352 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003353 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003354 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07003355 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003356 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
3357 - mService.mDisplayFreezeTime);
3358 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003359 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003360 mService.mWindowPlacerLocked.performSurfacePlacement();
3361 }
3362
3363 void waitForAllWindowsDrawn() {
3364 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003365 forAllWindows(w -> {
3366 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
3367 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
3368 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003369 // Force add to mResizingWindows.
chaviw9c81e632018-07-31 11:17:52 -07003370 w.resetLastContentInsets();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003371 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003372 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003373 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003374 }
3375
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003376 // TODO: Super crazy long method that should be broken down...
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003377 void applySurfaceChangesTransaction(boolean recoveringMemory) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003378
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003379 final int dw = mDisplayInfo.logicalWidth;
3380 final int dh = mDisplayInfo.logicalHeight;
3381 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
3382
3383 mTmpUpdateAllDrawn.clear();
3384
3385 int repeats = 0;
3386 do {
3387 repeats++;
3388 if (repeats > 6) {
3389 Slog.w(TAG, "Animation repeat aborted after too many iterations");
3390 clearLayoutNeeded();
3391 break;
3392 }
3393
3394 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
3395 pendingLayoutChanges);
3396
Andrii Kulian839def92016-11-02 10:58:58 -07003397 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
3398 // the wallpaper window jumping across displays.
3399 // Remove check for default display when there will be support for multiple wallpaper
3400 // targets (on different displays).
3401 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003402 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003403 }
3404
Riddle Hsu654a6f92018-07-13 22:59:36 +08003405 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003406 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
Riddle Hsu4e611772018-10-31 18:58:28 +08003407 if (updateOrientationFromAppTokens()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003408 setLayoutNeeded();
Riddle Hsu4e611772018-10-31 18:58:28 +08003409 sendNewConfiguration();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003410 }
3411 }
3412
3413 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
3414 setLayoutNeeded();
3415 }
3416
3417 // FIRST LOOP: Perform a layout, if needed.
3418 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
3419 performLayout(repeats == 1, false /* updateInputWindows */);
3420 } else {
3421 Slog.w(TAG, "Layout repeat skipped after too many iterations");
3422 }
3423
3424 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
3425 pendingLayoutChanges = 0;
3426
3427 if (isDefaultDisplay) {
3428 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003429 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003430 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
3431 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
3432 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
3433 }
3434 } while (pendingLayoutChanges != 0);
3435
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003436 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003437
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003438 mTmpRecoveringMemory = recoveringMemory;
3439 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01003440 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003441
Jorim Jaggif1292892018-09-10 11:58:13 +02003442 mLastHasContent = mTmpApplySurfaceChangesTransactionState.displayHasContent;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003443 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Jorim Jaggif1292892018-09-10 11:58:13 +02003444 mLastHasContent,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003445 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
3446 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003447 true /* inTraversal, must call performTraversalInTrans... below */);
3448
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003449 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
3450 if (wallpaperVisible != mLastWallpaperVisible) {
3451 mLastWallpaperVisible = wallpaperVisible;
3452 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
3453 }
3454
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003455 while (!mTmpUpdateAllDrawn.isEmpty()) {
3456 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
3457 // See if any windows have been drawn, so they (and others associated with them)
3458 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07003459 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003460 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003461 }
3462
Bryce Leef3c6a472017-11-14 14:53:06 -08003463 private void updateBounds() {
Evan Rosky39b6f232018-10-30 18:35:41 -07003464 calculateBounds(mDisplayInfo, mTmpBounds);
Bryce Leef3c6a472017-11-14 14:53:06 -08003465 setBounds(mTmpBounds);
3466 }
3467
3468 // Determines the current display bounds based on the current state
Evan Rosky39b6f232018-10-30 18:35:41 -07003469 private void calculateBounds(DisplayInfo displayInfo, Rect out) {
Bryce Leef3c6a472017-11-14 14:53:06 -08003470 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Evan Rosky39b6f232018-10-30 18:35:41 -07003471 final int rotation = displayInfo.rotation;
3472 boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Bryce Leef3c6a472017-11-14 14:53:06 -08003473 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
3474 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Evan Rosky39b6f232018-10-30 18:35:41 -07003475 int width = displayInfo.logicalWidth;
Bryce Leef3c6a472017-11-14 14:53:06 -08003476 int left = (physWidth - width) / 2;
Evan Rosky39b6f232018-10-30 18:35:41 -07003477 int height = displayInfo.logicalHeight;
Bryce Leef3c6a472017-11-14 14:53:06 -08003478 int top = (physHeight - height) / 2;
3479 out.set(left, top, left + width, top + height);
3480 }
3481
3482 @Override
3483 public void getBounds(Rect out) {
Evan Rosky39b6f232018-10-30 18:35:41 -07003484 calculateBounds(mDisplayInfo, out);
Bryce Leef3c6a472017-11-14 14:53:06 -08003485 }
3486
3487 private void getBounds(Rect out, int orientation) {
3488 getBounds(out);
3489
3490 // Rotate the Rect if needed.
3491 final int currentRotation = mDisplayInfo.rotation;
3492 final int rotationDelta = deltaRotation(currentRotation, orientation);
3493 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
3494 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
3495 mTmpRectF.set(out);
3496 mTmpMatrix.mapRect(mTmpRectF);
3497 mTmpRectF.round(out);
3498 }
3499 }
3500
Louis Chang7501e332018-08-20 13:08:39 +08003501 int getSurfaceSize() {
3502 return mSurfaceSize;
3503 }
3504
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003505 void performLayout(boolean initial, boolean updateInputWindows) {
3506 if (!isLayoutNeeded()) {
3507 return;
3508 }
3509 clearLayoutNeeded();
3510
3511 final int dw = mDisplayInfo.logicalWidth;
3512 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003513 if (DEBUG_LAYOUT) {
3514 Slog.v(TAG, "-------------------------------------");
3515 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
3516 }
3517
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01003518 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
3519 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00003520 // TODO: Not sure if we really need to set the rotation here since we are updating from the
3521 // display info above...
3522 mDisplayFrames.mRotation = mRotation;
3523 mService.mPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003524 if (isDefaultDisplay) {
3525 // Not needed on non-default displays.
3526 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
3527 mService.mScreenRect.set(0, 0, dw, dh);
3528 }
3529
Adrian Roos5251b1d2018-03-23 18:57:43 +01003530 int seq = mLayoutSeq + 1;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003531 if (seq < 0) seq = 0;
Adrian Roos5251b1d2018-03-23 18:57:43 +01003532 mLayoutSeq = seq;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003533
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003534 // Used to indicate that we have processed the dream window and all additional windows are
3535 // behind it.
3536 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003537 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003538
3539 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003540 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003541
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003542 // Used to indicate that we have processed the dream window and all additional attached
3543 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003544 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003545 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003546
3547 // Now perform layout of attached windows, which usually depend on the position of the
3548 // window they are attached to. XXX does not deal with windows that are attached to windows
3549 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003550 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003551
3552 // Window frames may have changed. Tell the input dispatcher about it.
Arthur Hung95b38a92018-07-20 18:56:12 +08003553 mInputMonitor.layoutInputConsumers(dw, dh);
3554 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003555 if (updateInputWindows) {
Arthur Hung95b38a92018-07-20 18:56:12 +08003556 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003557 }
3558
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003559 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
3560 }
3561
3562 /**
3563 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
3564 * In portrait mode, it grabs the full screenshot.
3565 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003566 * @param config of the output bitmap
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003567 */
chaviw0315a1a2018-03-05 15:28:35 -08003568 Bitmap screenshotDisplayLocked(Bitmap.Config config) {
3569 if (!mService.mPolicy.isScreenOn()) {
3570 if (DEBUG_SCREENSHOT) {
3571 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003572 }
chaviw0315a1a2018-03-05 15:28:35 -08003573 return null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003574 }
chaviwfbe47df2017-11-10 16:14:49 -08003575
chaviw0315a1a2018-03-05 15:28:35 -08003576 int dw = mDisplayInfo.logicalWidth;
3577 int dh = mDisplayInfo.logicalHeight;
chaviwfbe47df2017-11-10 16:14:49 -08003578
chaviw0315a1a2018-03-05 15:28:35 -08003579 if (dw <= 0 || dh <= 0) {
3580 return null;
3581 }
chaviwfbe47df2017-11-10 16:14:49 -08003582
chaviw0315a1a2018-03-05 15:28:35 -08003583 final Rect frame = new Rect(0, 0, dw, dh);
chaviwfbe47df2017-11-10 16:14:49 -08003584
chaviw0315a1a2018-03-05 15:28:35 -08003585 // The screenshot API does not apply the current screen rotation.
3586 int rot = mDisplay.getRotation();
chaviwfbe47df2017-11-10 16:14:49 -08003587
chaviw0315a1a2018-03-05 15:28:35 -08003588 if (rot == ROTATION_90 || rot == ROTATION_270) {
3589 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3590 }
chaviwfbe47df2017-11-10 16:14:49 -08003591
chaviw0315a1a2018-03-05 15:28:35 -08003592 // SurfaceFlinger is not aware of orientation, so convert our logical
3593 // crop to SurfaceFlinger's portrait orientation.
3594 convertCropForSurfaceFlinger(frame, rot, dw, dh);
3595
3596 final ScreenRotationAnimation screenRotationAnimation =
3597 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3598 final boolean inRotation = screenRotationAnimation != null &&
3599 screenRotationAnimation.isAnimating();
3600 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
3601
3602 // TODO(b/68392460): We should screenshot Task controls directly
3603 // but it's difficult at the moment as the Task doesn't have the
3604 // correct size set.
chaviw08520a02018-09-10 16:44:56 -07003605 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, inRotation, rot);
chaviw0315a1a2018-03-05 15:28:35 -08003606 if (bitmap == null) {
3607 Slog.w(TAG_WM, "Failed to take screenshot");
3608 return null;
3609 }
3610
3611 // Create a copy of the screenshot that is immutable and backed in ashmem.
3612 // This greatly reduces the overhead of passing the bitmap between processes.
3613 final Bitmap ret = bitmap.createAshmemBitmap(config);
3614 bitmap.recycle();
3615 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003616 }
3617
3618 // TODO: Can this use createRotationMatrix()?
3619 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3620 if (rot == Surface.ROTATION_90) {
3621 final int tmp = crop.top;
3622 crop.top = dw - crop.right;
3623 crop.right = crop.bottom;
3624 crop.bottom = dw - crop.left;
3625 crop.left = tmp;
3626 } else if (rot == Surface.ROTATION_180) {
3627 int tmp = crop.top;
3628 crop.top = dh - crop.bottom;
3629 crop.bottom = dh - tmp;
3630 tmp = crop.right;
3631 crop.right = dw - crop.left;
3632 crop.left = dw - tmp;
3633 } else if (rot == Surface.ROTATION_270) {
3634 final int tmp = crop.top;
3635 crop.top = crop.left;
3636 crop.left = dh - crop.bottom;
3637 crop.bottom = crop.right;
3638 crop.right = dh - tmp;
3639 }
3640 }
3641
3642 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003643 // Used to indicate the layout is needed.
3644 mTmpWindow = null;
3645
3646 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003647 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003648 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003649 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003650 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003651 w.setDisplayLayoutNeeded();
3652 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003653 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003654
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003655 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003656 mService.mWindowPlacerLocked.performSurfacePlacement();
3657 }
3658 }
3659
Wale Ogunwale1666e312016-12-16 11:27:18 -08003660 void setExitingTokensHasVisible(boolean hasVisible) {
3661 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3662 mExitingTokens.get(i).hasVisible = hasVisible;
3663 }
3664
3665 // Initialize state of exiting applications.
3666 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3667 }
3668
3669 void removeExistingTokensIfPossible() {
3670 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3671 final WindowToken token = mExitingTokens.get(i);
3672 if (!token.hasVisible) {
3673 mExitingTokens.remove(i);
3674 }
3675 }
3676
3677 // Time to remove any exiting applications?
3678 mTaskStackContainers.removeExistingAppTokensIfPossible();
3679 }
3680
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003681 @Override
3682 void onDescendantOverrideConfigurationChanged() {
3683 setLayoutNeeded();
3684 mService.requestTraversal();
3685 }
3686
David Stevens9440dc82017-03-16 19:00:20 -07003687 boolean okToDisplay() {
3688 if (mDisplayId == DEFAULT_DISPLAY) {
3689 return !mService.mDisplayFrozen
3690 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3691 }
3692 return mDisplayInfo.state == Display.STATE_ON;
3693 }
3694
3695 boolean okToAnimate() {
3696 return okToDisplay() &&
3697 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3698 }
3699
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003700 static final class TaskForResizePointSearchResult {
3701 boolean searchDone;
3702 Task taskForResize;
3703
3704 void reset() {
3705 searchDone = false;
3706 taskForResize = null;
3707 }
3708 }
Robert Carr3b716242016-08-16 16:02:21 -07003709
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003710 private static final class ApplySurfaceChangesTransactionState {
3711 boolean displayHasContent;
3712 boolean obscured;
3713 boolean syswin;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003714 float preferredRefreshRate;
3715 int preferredModeId;
3716
3717 void reset() {
3718 displayHasContent = false;
3719 obscured = false;
3720 syswin = false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003721 preferredRefreshRate = 0;
3722 preferredModeId = 0;
3723 }
3724 }
3725
3726 private static final class ScreenshotApplicationState {
3727 WindowState appWin;
3728 int maxLayer;
3729 int minLayer;
3730 boolean screenshotReady;
3731
3732 void reset(boolean screenshotReady) {
3733 appWin = null;
3734 maxLayer = 0;
3735 minLayer = 0;
3736 this.screenshotReady = screenshotReady;
3737 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3738 }
3739 }
3740
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003741 /**
3742 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3743 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3744 * homogeneous children type which is currently required by sub-classes of
3745 * {@link WindowContainer} class.
3746 */
3747 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3748
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003749 DisplayChildWindowContainer(WindowManagerService service) {
3750 super(service);
3751 }
3752
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003753 @Override
3754 boolean fillsParent() {
3755 return true;
3756 }
3757
3758 @Override
3759 boolean isVisible() {
3760 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003761 }
3762 }
3763
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003764 /**
3765 * Window container class that contains all containers on this display relating to Apps.
3766 * I.e Activities.
3767 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003768 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003769 /**
3770 * A control placed at the appropriate level for transitions to occur.
3771 */
chaviw23ee71c2017-12-18 11:29:41 -08003772 SurfaceControl mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02003773 SurfaceControl mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02003774 SurfaceControl mHomeAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003775
Robert Carrf7a7ca82017-12-06 13:17:07 -08003776 /**
3777 * Given that the split-screen divider does not have an AppWindowToken, it
3778 * will have to live inside of a "NonAppWindowContainer", in particular
3779 * {@link DisplayContent#mAboveAppWindowsContainers}. However, in visual Z order
3780 * it will need to be interleaved with some of our children, appearing on top of
3781 * both docked stacks but underneath any assistant stacks.
3782 *
3783 * To solve this problem we have this anchor control, which will always exist so
3784 * we can always assign it the correct value in our {@link #assignChildLayers}.
3785 * Likewise since it always exists, {@link AboveAppWindowContainers} can always
3786 * assign the divider a layer relative to it. This way we prevent linking lifecycle
3787 * events between the two containers.
3788 */
3789 SurfaceControl mSplitScreenDividerAnchor = null;
3790
Wale Ogunwale61911492017-10-11 08:50:50 -07003791 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3792 // through the list to find them.
3793 private TaskStack mHomeStack = null;
3794 private TaskStack mPinnedStack = null;
3795 private TaskStack mSplitScreenPrimaryStack = null;
3796
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003797 TaskStackContainers(WindowManagerService service) {
3798 super(service);
3799 }
3800
Wale Ogunwale61911492017-10-11 08:50:50 -07003801 /**
3802 * Returns the topmost stack on the display that is compatible with the input windowing mode
3803 * and activity type. Null is no compatible stack on the display.
3804 */
3805 TaskStack getStack(int windowingMode, int activityType) {
3806 if (activityType == ACTIVITY_TYPE_HOME) {
3807 return mHomeStack;
3808 }
3809 if (windowingMode == WINDOWING_MODE_PINNED) {
3810 return mPinnedStack;
3811 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3812 return mSplitScreenPrimaryStack;
3813 }
3814 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3815 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003816 if (activityType == ACTIVITY_TYPE_UNDEFINED
3817 && windowingMode == stack.getWindowingMode()) {
3818 // Passing in undefined type means we want to match the topmost stack with the
3819 // windowing mode.
3820 return stack;
3821 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003822 if (stack.isCompatible(windowingMode, activityType)) {
3823 return stack;
3824 }
3825 }
3826 return null;
3827 }
3828
3829 @VisibleForTesting
3830 TaskStack getTopStack() {
3831 return mTaskStackContainers.getChildCount() > 0
3832 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3833 }
3834
3835 TaskStack getHomeStack() {
3836 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3837 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3838 }
3839 return mHomeStack;
3840 }
3841
3842 TaskStack getPinnedStack() {
3843 return mPinnedStack;
3844 }
3845
Matthew Ng64e77cf2017-10-31 14:01:31 -07003846 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003847 return mSplitScreenPrimaryStack;
3848 }
3849
Winson Chunge2d72172018-01-25 17:46:20 +00003850 ArrayList<Task> getVisibleTasks() {
3851 final ArrayList<Task> visibleTasks = new ArrayList<>();
3852 forAllTasks(task -> {
3853 if (task.isVisible()) {
3854 visibleTasks.add(task);
3855 }
3856 });
3857 return visibleTasks;
3858 }
3859
Andrii Kulian839def92016-11-02 10:58:58 -07003860 /**
3861 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003862 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003863 */
3864 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003865 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003866 addChild(stack, onTop);
3867 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003868 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003869
Wale Ogunwale61911492017-10-11 08:50:50 -07003870 void onStackWindowingModeChanged(TaskStack stack) {
3871 removeStackReferenceIfNeeded(stack);
3872 addStackReferenceIfNeeded(stack);
3873 if (stack == mPinnedStack && getTopStack() != stack) {
3874 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3875 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3876 }
3877 }
3878
3879 private void addStackReferenceIfNeeded(TaskStack stack) {
3880 if (stack.isActivityTypeHome()) {
3881 if (mHomeStack != null) {
Louis Chang7d0037c2018-08-13 12:42:06 +08003882 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
Wale Ogunwale61911492017-10-11 08:50:50 -07003883 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
Louis Chang7d0037c2018-08-13 12:42:06 +08003884
Wale Ogunwale61911492017-10-11 08:50:50 -07003885 }
Louis Chang7d0037c2018-08-13 12:42:06 +08003886 mHomeStack = stack;
Wale Ogunwale61911492017-10-11 08:50:50 -07003887 }
3888 final int windowingMode = stack.getWindowingMode();
3889 if (windowingMode == WINDOWING_MODE_PINNED) {
3890 if (mPinnedStack != null) {
3891 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3892 + mPinnedStack + " already exist on display=" + this
3893 + " stack=" + stack);
3894 }
3895 mPinnedStack = stack;
3896 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3897 if (mSplitScreenPrimaryStack != null) {
3898 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3899 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3900 + " already exist on display=" + this + " stack=" + stack);
3901 }
3902 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003903 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003904 }
3905 }
3906
3907 private void removeStackReferenceIfNeeded(TaskStack stack) {
3908 if (stack == mHomeStack) {
3909 mHomeStack = null;
3910 } else if (stack == mPinnedStack) {
3911 mPinnedStack = null;
3912 } else if (stack == mSplitScreenPrimaryStack) {
3913 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003914 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3915 mService.setDockedStackCreateStateLocked(
3916 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3917 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003918 }
Andrii Kulian839def92016-11-02 10:58:58 -07003919 }
3920
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003921 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003922 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3923 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003924 addChild(stack, addIndex);
3925 setLayoutNeeded();
3926 }
3927
Wale Ogunwale61911492017-10-11 08:50:50 -07003928 @Override
3929 protected void removeChild(TaskStack stack) {
3930 super.removeChild(stack);
3931 removeStackReferenceIfNeeded(stack);
3932 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003933
3934 @Override
3935 boolean isOnTop() {
3936 // Considered always on top
3937 return true;
3938 }
3939
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003940 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003941 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003942 if (child.getWindowConfiguration().isAlwaysOnTop()
3943 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003944 // This stack is always-on-top, override the default behavior.
3945 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3946
3947 // Moving to its current position, as we must call super but we don't want to
3948 // perform any meaningful action.
3949 final int currentPosition = mChildren.indexOf(child);
3950 super.positionChildAt(currentPosition, child, false /* includingParents */);
3951 return;
3952 }
3953
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003954 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3955 super.positionChildAt(targetPosition, child, includingParents);
3956
Riddle Hsu2f6e1742018-08-23 22:44:36 +08003957 if (includingParents) {
3958 // We still want to move the display of this stack container to top because even the
3959 // target position is adjusted to non-top, the intention of the condition is to have
3960 // higher z-order to gain focus (e.g. moving a task of a fullscreen stack to front
3961 // in a non-top display which is using picture-in-picture mode).
3962 final int topChildPosition = getChildCount() - 1;
3963 if (targetPosition < topChildPosition && position >= topChildPosition) {
3964 getParent().positionChildAt(POSITION_TOP, this /* child */,
3965 true /* includingParents */);
3966 }
3967 }
3968
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003969 setLayoutNeeded();
3970 }
3971
3972 /**
3973 * When stack is added or repositioned, find a proper position for it.
3974 * This will make sure that pinned stack always stays on top.
3975 * @param requestedPosition Position requested by caller.
3976 * @param stack Stack to be added or positioned.
3977 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3978 * @return The proper position for the stack.
3979 */
3980 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09003981 if (stack.inPinnedWindowingMode()) {
Kazuki Takisef85197b2018-06-18 18:18:36 +09003982 return POSITION_TOP;
Kazuki Takise148d00a2018-05-31 15:32:19 +09003983 }
3984
Kazuki Takisef85197b2018-06-18 18:18:36 +09003985 final int topChildPosition = mChildren.size() - 1;
3986 int belowAlwaysOnTopPosition = POSITION_BOTTOM;
3987 for (int i = topChildPosition; i >= 0; --i) {
3988 if (getStacks().get(i) != stack && !getStacks().get(i).isAlwaysOnTop()) {
3989 belowAlwaysOnTopPosition = i;
3990 break;
3991 }
3992 }
3993
3994 // The max possible position we can insert the stack at.
3995 int maxPosition = POSITION_TOP;
3996 // The min possible position we can insert the stack at.
3997 int minPosition = POSITION_BOTTOM;
3998
3999 if (stack.isAlwaysOnTop()) {
4000 if (hasPinnedStack()) {
4001 // Always-on-top stacks go below the pinned stack.
4002 maxPosition = getStacks().indexOf(mPinnedStack) - 1;
4003 }
4004 // Always-on-top stacks need to be above all other stacks.
4005 minPosition = belowAlwaysOnTopPosition !=
4006 POSITION_BOTTOM ? belowAlwaysOnTopPosition : topChildPosition;
4007 } else {
4008 // Other stacks need to be below the always-on-top stacks.
4009 maxPosition = belowAlwaysOnTopPosition !=
Arthur Hung928d8dc2018-07-18 15:38:05 +08004010 POSITION_BOTTOM ? belowAlwaysOnTopPosition : 0;
Kazuki Takisef85197b2018-06-18 18:18:36 +09004011 }
4012
4013 int targetPosition = requestedPosition;
4014 targetPosition = Math.min(targetPosition, maxPosition);
4015 targetPosition = Math.max(targetPosition, minPosition);
4016
4017 int prevPosition = getStacks().indexOf(stack);
4018 // The positions we calculated above (maxPosition, minPosition) do not take into
4019 // consideration the following edge cases.
4020 // 1) We need to adjust the position depending on the value "adding".
4021 // 2) When we are moving a stack to another position, we also need to adjust the
4022 // position depending on whether the stack is moving to a higher or lower position.
4023 if ((targetPosition != requestedPosition) &&
4024 (adding || targetPosition < prevPosition)) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09004025 targetPosition++;
4026 }
4027
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004028 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08004029 }
4030
4031 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004032 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
4033 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004034 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004035 if (super.forAllWindows(callback, traverseTopToBottom)) {
4036 return true;
4037 }
4038 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4039 return true;
4040 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004041 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004042 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4043 return true;
4044 }
4045 if (super.forAllWindows(callback, traverseTopToBottom)) {
4046 return true;
4047 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004048 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004049 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004050 }
4051
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004052 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004053 boolean traverseTopToBottom) {
4054 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
4055 // app tokens.
4056 // TODO: Investigate if we need to continue to do this or if we can just process them
4057 // in-order.
4058 if (traverseTopToBottom) {
4059 for (int i = mChildren.size() - 1; i >= 0; --i) {
4060 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4061 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004062 if (appTokens.get(j).forAllWindowsUnchecked(callback,
4063 traverseTopToBottom)) {
4064 return true;
4065 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004066 }
4067 }
4068 } else {
4069 final int count = mChildren.size();
4070 for (int i = 0; i < count; ++i) {
4071 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4072 final int appTokensCount = appTokens.size();
4073 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004074 if (appTokens.get(j).forAllWindowsUnchecked(callback,
4075 traverseTopToBottom)) {
4076 return true;
4077 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004078 }
4079 }
4080 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004081 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004082 }
4083
Wale Ogunwale1666e312016-12-16 11:27:18 -08004084 void setExitingTokensHasVisible(boolean hasVisible) {
4085 for (int i = mChildren.size() - 1; i >= 0; --i) {
4086 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4087 for (int j = appTokens.size() - 1; j >= 0; --j) {
4088 appTokens.get(j).hasVisible = hasVisible;
4089 }
4090 }
4091 }
4092
4093 void removeExistingAppTokensIfPossible() {
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) {
4097 final AppWindowToken token = appTokens.get(j);
lumark588a3e82018-07-20 18:53:54 +08004098 if (!token.hasVisible && !mClosingApps.contains(token)
Wale Ogunwale1666e312016-12-16 11:27:18 -08004099 && (!token.mIsExiting || token.isEmpty())) {
4100 // Make sure there is no animation running on this token, so any windows
4101 // associated with it will be removed as soon as their animations are
4102 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02004103 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004104 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
4105 "performLayout: App token exiting now removed" + token);
4106 token.removeIfPossible();
4107 }
4108 }
4109 }
4110 }
4111
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004112 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004113 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004114 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
4115 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004116 // Apps and their containers are not allowed to specify an orientation while the
Wale Ogunwaled0c225c2018-06-15 14:47:46 -07004117 // docked or freeform stack is visible...except for the home stack if the docked
4118 // stack is minimized and it actually set something and the bounds is different from
4119 // the display.
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004120 if (mHomeStack != null && mHomeStack.isVisible()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004121 && mDividerControllerLocked.isMinimizedDock()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004122 && !(mDividerControllerLocked.isHomeStackResizable()
4123 && mHomeStack.matchParentBounds())) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004124 final int orientation = mHomeStack.getOrientation();
4125 if (orientation != SCREEN_ORIENTATION_UNSET) {
4126 return orientation;
4127 }
4128 }
4129 return SCREEN_ORIENTATION_UNSPECIFIED;
4130 }
4131
4132 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07004133 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
4134 PackageManager.FEATURE_AUTOMOTIVE);
4135 if (isCar) {
4136 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
4137 // allow anything but the default orientation.
4138 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004139 "Forcing UNSPECIFIED orientation in car for display id=" + mDisplayId
4140 + ". Ignoring " + orientation);
Dean Harding3e5a1522017-10-06 09:19:01 -07004141 return SCREEN_ORIENTATION_UNSPECIFIED;
4142 }
4143
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004144 if (orientation != SCREEN_ORIENTATION_UNSET
4145 && orientation != SCREEN_ORIENTATION_BEHIND) {
4146 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004147 "App is requesting an orientation, return " + orientation
4148 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004149 return orientation;
4150 }
4151
4152 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004153 "No app is requesting an orientation, return " + mLastOrientation
4154 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004155 // The next app has not been requested to be visible, so we keep the current orientation
4156 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08004157 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004158 }
Robert Carrb1579c82017-09-05 14:54:47 -07004159
4160 @Override
4161 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004162 assignStackOrdering(t);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004163
Robert Carr2f8aa392018-01-31 14:46:51 -08004164 for (int i = 0; i < mChildren.size(); i++) {
4165 final TaskStack s = mChildren.get(i);
4166 s.assignChildLayers(t);
4167 }
4168 }
4169
4170 void assignStackOrdering(SurfaceControl.Transaction t) {
Jorim Jaggibe418292018-03-26 16:14:12 +02004171
Robert Carrf7a7ca82017-12-06 13:17:07 -08004172 final int HOME_STACK_STATE = 0;
4173 final int NORMAL_STACK_STATE = 1;
4174 final int ALWAYS_ON_TOP_STATE = 2;
4175
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004176 int layer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08004177 int layerForAnimationLayer = 0;
Jorim Jaggibe418292018-03-26 16:14:12 +02004178 int layerForBoostedAnimationLayer = 0;
Jorim Jaggi391790622018-04-18 15:30:44 +02004179 int layerForHomeAnimationLayer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08004180
Robert Carrf7a7ca82017-12-06 13:17:07 -08004181 for (int state = 0; state <= ALWAYS_ON_TOP_STATE; state++) {
4182 for (int i = 0; i < mChildren.size(); i++) {
4183 final TaskStack s = mChildren.get(i);
Robert Carr2f8aa392018-01-31 14:46:51 -08004184 if (state == HOME_STACK_STATE && !s.isActivityTypeHome()) {
4185 continue;
4186 } else if (state == NORMAL_STACK_STATE && (s.isActivityTypeHome()
4187 || s.isAlwaysOnTop())) {
4188 continue;
4189 } else if (state == ALWAYS_ON_TOP_STATE && !s.isAlwaysOnTop()) {
4190 continue;
4191 }
4192 s.assignLayer(t, layer++);
4193 if (s.inSplitScreenWindowingMode() && mSplitScreenDividerAnchor != null) {
4194 t.setLayer(mSplitScreenDividerAnchor, layer++);
4195 }
Winson Chungd41f71d2018-03-16 15:26:07 -07004196 if ((s.isTaskAnimating() || s.isAppAnimating())
4197 && state != ALWAYS_ON_TOP_STATE) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004198 // Ensure the animation layer ends up above the
4199 // highest animating stack and no higher.
4200 layerForAnimationLayer = layer++;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004201 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004202 if (state != ALWAYS_ON_TOP_STATE) {
4203 layerForBoostedAnimationLayer = layer++;
4204 }
Robert Carrf7a7ca82017-12-06 13:17:07 -08004205 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004206 if (state == HOME_STACK_STATE) {
4207 layerForHomeAnimationLayer = layer++;
4208 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004209 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004210 if (mAppAnimationLayer != null) {
4211 t.setLayer(mAppAnimationLayer, layerForAnimationLayer);
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004212 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004213 if (mBoostedAppAnimationLayer != null) {
4214 t.setLayer(mBoostedAppAnimationLayer, layerForBoostedAnimationLayer);
4215 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004216 if (mHomeAppAnimationLayer != null) {
4217 t.setLayer(mHomeAppAnimationLayer, layerForHomeAnimationLayer);
4218 }
Robert Carrb1579c82017-09-05 14:54:47 -07004219 }
4220
4221 @Override
Jorim Jaggi391790622018-04-18 15:30:44 +02004222 SurfaceControl getAppAnimationLayer(@AnimationLayer int animationLayer) {
4223 switch (animationLayer) {
4224 case ANIMATION_LAYER_BOOSTED:
4225 return mBoostedAppAnimationLayer;
4226 case ANIMATION_LAYER_HOME:
4227 return mHomeAppAnimationLayer;
4228 case ANIMATION_LAYER_STANDARD:
4229 default:
4230 return mAppAnimationLayer;
4231 }
chaviw23ee71c2017-12-18 11:29:41 -08004232 }
4233
Robert Carrf7a7ca82017-12-06 13:17:07 -08004234 SurfaceControl getSplitScreenDividerAnchor() {
4235 return mSplitScreenDividerAnchor;
4236 }
4237
chaviw23ee71c2017-12-18 11:29:41 -08004238 @Override
Robert Carrb1579c82017-09-05 14:54:47 -07004239 void onParentSet() {
4240 super.onParentSet();
4241 if (getParent() != null) {
chaviw23ee71c2017-12-18 11:29:41 -08004242 mAppAnimationLayer = makeChildSurface(null)
4243 .setName("animationLayer")
4244 .build();
Jorim Jaggibe418292018-03-26 16:14:12 +02004245 mBoostedAppAnimationLayer = makeChildSurface(null)
4246 .setName("boostedAnimationLayer")
4247 .build();
Jorim Jaggi391790622018-04-18 15:30:44 +02004248 mHomeAppAnimationLayer = makeChildSurface(null)
4249 .setName("homeAnimationLayer")
4250 .build();
Robert Carrf7a7ca82017-12-06 13:17:07 -08004251 mSplitScreenDividerAnchor = makeChildSurface(null)
4252 .setName("splitScreenDividerAnchor")
4253 .build();
4254 getPendingTransaction()
4255 .show(mAppAnimationLayer)
Jorim Jaggibe418292018-03-26 16:14:12 +02004256 .show(mBoostedAppAnimationLayer)
Jorim Jaggi391790622018-04-18 15:30:44 +02004257 .show(mHomeAppAnimationLayer)
Robert Carrf7a7ca82017-12-06 13:17:07 -08004258 .show(mSplitScreenDividerAnchor);
chaviw23ee71c2017-12-18 11:29:41 -08004259 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07004260 } else {
chaviw23ee71c2017-12-18 11:29:41 -08004261 mAppAnimationLayer.destroy();
4262 mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02004263 mBoostedAppAnimationLayer.destroy();
4264 mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02004265 mHomeAppAnimationLayer.destroy();
4266 mHomeAppAnimationLayer = null;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004267 mSplitScreenDividerAnchor.destroy();
4268 mSplitScreenDividerAnchor = null;
Robert Carrb1579c82017-09-05 14:54:47 -07004269 }
4270 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004271 }
4272
Robert Carree4d4b92017-11-22 12:21:46 -08004273 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004274 AboveAppWindowContainers(String name, WindowManagerService service) {
4275 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08004276 }
4277
Robert Carrb9506032018-02-13 13:54:00 -08004278 @Override
Vishnu Nair83537a72018-07-19 21:27:48 -07004279 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
4280 final SurfaceControl.Builder builder = super.makeChildSurface(child);
4281 if (child instanceof WindowToken && ((WindowToken) child).mRoundedCornerOverlay) {
4282 // To draw above the ColorFade layer during the screen off transition, the
4283 // rounded corner overlays need to be at the root of the surface hierarchy.
4284 // TODO: move the ColorLayer into the display overlay layer such that this is not
4285 // necessary anymore.
4286 builder.setParent(null);
4287 }
4288 return builder;
4289 }
4290
4291 @Override
Robert Carrb9506032018-02-13 13:54:00 -08004292 void assignChildLayers(SurfaceControl.Transaction t) {
4293 assignChildLayers(t, null /* imeContainer */);
4294 }
4295
Robert Carree4d4b92017-11-22 12:21:46 -08004296 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
4297 boolean needAssignIme = imeContainer != null
4298 && imeContainer.getSurfaceControl() != null;
4299 for (int j = 0; j < mChildren.size(); ++j) {
4300 final WindowToken wt = mChildren.get(j);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004301
4302 // See {@link mSplitScreenDividerAnchor}
4303 if (wt.windowType == TYPE_DOCK_DIVIDER) {
4304 wt.assignRelativeLayer(t, mTaskStackContainers.getSplitScreenDividerAnchor(), 1);
4305 continue;
4306 }
Vishnu Nair83537a72018-07-19 21:27:48 -07004307 if (wt.mRoundedCornerOverlay) {
4308 wt.assignLayer(t, WindowManagerPolicy.COLOR_FADE_LAYER + 1);
4309 continue;
4310 }
Robert Carree4d4b92017-11-22 12:21:46 -08004311 wt.assignLayer(t, j);
4312 wt.assignChildLayers(t);
4313
4314 int layer = mService.mPolicy.getWindowLayerFromTypeLw(
4315 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08004316
4317 if (needAssignIme && layer >= mService.mPolicy.getWindowLayerFromTypeLw(
4318 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004319 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08004320 needAssignIme = false;
4321 }
4322 }
4323 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004324 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08004325 }
4326 }
4327 }
4328
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004329 /**
4330 * Window container class that contains all containers on this display that are not related to
4331 * Apps. E.g. status bar.
4332 */
Robert Carree4d4b92017-11-22 12:21:46 -08004333 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07004334 /**
4335 * Compares two child window tokens returns -1 if the first is lesser than the second in
4336 * terms of z-order and 1 otherwise.
4337 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004338 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07004339 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004340 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
4341 token1.mOwnerCanManageAppTokens)
4342 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
4343 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004344
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004345 private final Predicate<WindowState> mGetOrientingWindow = w -> {
4346 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
4347 return false;
4348 }
4349 final int req = w.mAttrs.screenOrientation;
4350 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
4351 || req == SCREEN_ORIENTATION_UNSET) {
4352 return false;
4353 }
4354 return true;
4355 };
4356
Wale Ogunwale3a931692016-11-02 16:49:48 -07004357 private final String mName;
chaviwf29223e2018-01-31 13:23:51 -08004358 private final Dimmer mDimmer = new Dimmer(this);
4359 private final Rect mTmpDimBoundsRect = new Rect();
4360
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004361 NonAppWindowContainers(String name, WindowManagerService service) {
4362 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07004363 mName = name;
4364 }
4365
4366 void addChild(WindowToken token) {
4367 addChild(token, mWindowComparator);
4368 }
4369
4370 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004371 int getOrientation() {
4372 final WindowManagerPolicy policy = mService.mPolicy;
4373 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004374 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004375
4376 if (win != null) {
4377 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004378 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004379 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07004380 if (mService.mKeyguardGoingAway) {
4381 // Keyguard can't affect the orientation if it is going away...
4382 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
4383 return SCREEN_ORIENTATION_UNSET;
4384 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004385 }
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004386 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req
4387 + " for display id=" + mDisplayId);
Andrii Kulian8ee72852017-03-10 10:36:45 -08004388 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004389 }
4390
Andrii Kulian8ee72852017-03-10 10:36:45 -08004391 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004392
Jorim Jaggi1c530592018-04-06 15:11:47 +02004393 // Only allow force setting the orientation when all unknown visibilities have been
4394 // resolved, as otherwise we just may be starting another occluding activity.
4395 final boolean isUnoccluding =
lumark588a3e82018-07-20 18:53:54 +08004396 mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
4397 && mUnknownAppVisibilityController.allResolved();
Jorim Jaggi1c530592018-04-06 15:11:47 +02004398 if (policy.isKeyguardShowingAndNotOccluded() || isUnoccluding) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004399 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004400 }
4401
4402 return SCREEN_ORIENTATION_UNSET;
4403 }
4404
4405 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07004406 String getName() {
4407 return mName;
4408 }
chaviwf29223e2018-01-31 13:23:51 -08004409
4410 @Override
4411 Dimmer getDimmer() {
4412 return mDimmer;
4413 }
4414
4415 @Override
4416 void prepareSurfaces() {
4417 mDimmer.resetDimStates();
4418 super.prepareSurfaces();
4419 getBounds(mTmpDimBoundsRect);
4420
4421 if (mDimmer.updateDims(getPendingTransaction(), mTmpDimBoundsRect)) {
4422 scheduleAnimation();
4423 }
4424 }
Robert Carr3b716242016-08-16 16:02:21 -07004425 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01004426
Robert Carr9034d962018-01-04 18:27:42 -08004427 private class NonMagnifiableWindowContainers extends NonAppWindowContainers {
4428 NonMagnifiableWindowContainers(String name, WindowManagerService service) {
4429 super(name, service);
4430 }
4431
4432 @Override
4433 void applyMagnificationSpec(Transaction t, MagnificationSpec spec) {
4434 }
4435 };
4436
Robert Carrb1579c82017-09-05 14:54:47 -07004437 SurfaceControl.Builder makeSurface(SurfaceSession s) {
4438 return mService.makeSurfaceBuilder(s)
4439 .setParent(mWindowingLayer);
4440 }
4441
4442 @Override
4443 SurfaceSession getSession() {
4444 return mSession;
4445 }
4446
4447 @Override
4448 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07004449 SurfaceSession s = child != null ? child.getSession() : getSession();
4450 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrb1579c82017-09-05 14:54:47 -07004451 b.setSize(mSurfaceSize, mSurfaceSize);
Robert Carrf59b8dd2017-10-02 18:58:36 -07004452
4453 if (child == null) {
4454 return b;
4455 }
4456
Robert Carree4d4b92017-11-22 12:21:46 -08004457 return b.setName(child.getName())
4458 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07004459 }
4460
4461 /**
4462 * The makeSurface variants are for use by the window-container
4463 * hierarchy. makeOverlay here is a function for various non windowing
4464 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
4465 * and other potpourii.
4466 */
4467 SurfaceControl.Builder makeOverlay() {
4468 return mService.makeSurfaceBuilder(mSession)
4469 .setParent(mOverlayLayer);
4470 }
4471
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004472 /**
4473 * Reparents the given surface to mOverlayLayer.
4474 */
4475 void reparentToOverlay(Transaction transaction, SurfaceControl surface) {
4476 transaction.reparent(surface, mOverlayLayer.getHandle());
4477 }
4478
Robert Carrb1579c82017-09-05 14:54:47 -07004479 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carr24be9ab2018-04-30 17:54:53 -07004480 if (spec.scale != 1.0) {
4481 mMagnificationSpec = spec;
4482 } else {
4483 mMagnificationSpec = null;
4484 }
4485
Robert Carrf59b8dd2017-10-02 18:58:36 -07004486 applyMagnificationSpec(getPendingTransaction(), spec);
4487 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07004488 }
4489
Robert Carr24be9ab2018-04-30 17:54:53 -07004490 void reapplyMagnificationSpec() {
4491 if (mMagnificationSpec != null) {
4492 applyMagnificationSpec(getPendingTransaction(), mMagnificationSpec);
4493 }
4494 }
4495
Robert Carrb1579c82017-09-05 14:54:47 -07004496 @Override
4497 void onParentSet() {
4498 // Since we are the top of the SurfaceControl hierarchy here
4499 // we create the root surfaces explicitly rather than chaining
4500 // up as the default implementation in onParentSet does. So we
4501 // explicitly do NOT call super here.
4502 }
4503
4504 @Override
4505 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07004506
4507 // These are layers as children of "mWindowingLayer"
4508 mBelowAppWindowsContainers.assignLayer(t, 0);
4509 mTaskStackContainers.assignLayer(t, 1);
4510 mAboveAppWindowsContainers.assignLayer(t, 2);
4511
lumarkff0ab692018-11-05 20:32:30 +08004512 final WindowState imeTarget = mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08004513 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08004514
Robert Carree4d4b92017-11-22 12:21:46 -08004515 // In the case where we have an IME target that is not in split-screen
4516 // mode IME assignment is easy. We just need the IME to go directly above
4517 // the target. This way children of the target will naturally go above the IME
4518 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08004519 //
Robert Carree4d4b92017-11-22 12:21:46 -08004520 // In the case of split-screen windowing mode, we need to elevate the IME above the
4521 // docked divider while keeping the app itself below the docked divider, so instead
4522 // we use relative layering of the IME targets child windows, and place the
4523 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08004524 //
Robert Carr234b6332018-03-20 13:38:16 -07004525 // In the case the IME target is animating, the animation Z order may be different
4526 // than the WindowContainer Z order, so it's difficult to be sure we have the correct
4527 // IME target. In this case we just layer the IME over all transitions by placing it in the
4528 // above applications layer.
4529 //
Robert Carree4d4b92017-11-22 12:21:46 -08004530 // In the case where we have no IME target we assign it where it's base layer would
4531 // place it in the AboveAppWindowContainers.
Robert Carr234b6332018-03-20 13:38:16 -07004532 if (imeTarget != null && !(imeTarget.inSplitScreenWindowingMode()
4533 || imeTarget.mToken.isAppAnimating())
Robert Carree4d4b92017-11-22 12:21:46 -08004534 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004535 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07004536 // TODO: We need to use an extra level on the app surface to ensure
4537 // this is always above SurfaceView but always below attached window.
4538 1);
Robert Carree4d4b92017-11-22 12:21:46 -08004539 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07004540 }
4541
4542 // Above we have assigned layers to our children, now we ask them to assign
4543 // layers to their children.
4544 mBelowAppWindowsContainers.assignChildLayers(t);
4545 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08004546 mAboveAppWindowsContainers.assignChildLayers(t,
4547 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07004548 mImeWindowsContainers.assignChildLayers(t);
4549 }
4550
4551 /**
4552 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
4553 * that the IME target is one of the docked applications. We'd like the docked divider to be
4554 * above both of the applications, and we'd like the IME to be above the docked divider.
4555 * However we need child windows of the applications to be above the IME (Text drag handles).
4556 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
4557 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
4558 * with {@link #WindowState#assignLayer}
4559 */
4560 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
Robert Carrbb5de662018-04-26 13:29:51 -07004561 child.assignRelativeLayer(t, mImeWindowsContainers.getSurfaceControl(), 1);
Robert Carrb1579c82017-09-05 14:54:47 -07004562 }
4563
4564 @Override
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004565 void prepareSurfaces() {
4566 final ScreenRotationAnimation screenRotationAnimation =
4567 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
4568 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
4569 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
4570 mPendingTransaction.setMatrix(mWindowingLayer,
4571 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
4572 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
4573 mPendingTransaction.setPosition(mWindowingLayer,
4574 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
4575 mPendingTransaction.setAlpha(mWindowingLayer,
4576 screenRotationAnimation.getEnterTransformation().getAlpha());
4577 }
Chavi Weingartenb736e322018-02-23 00:27:54 +00004578
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004579 super.prepareSurfaces();
4580 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004581
Jorim Jaggibe418292018-03-26 16:14:12 +02004582 void assignStackOrdering() {
4583 mTaskStackContainers.assignStackOrdering(getPendingTransaction());
Robert Carr2f8aa392018-01-31 14:46:51 -08004584 }
Chavi Weingarten3a748552018-05-14 17:32:42 +00004585
4586 /**
4587 * Increment the deferral count to determine whether to update the IME target.
4588 */
4589 void deferUpdateImeTarget() {
4590 mDeferUpdateImeTargetCount++;
4591 }
4592
4593 /**
4594 * Decrement the deferral count to determine whether to update the IME target. If the count
4595 * reaches 0, a new ime target will get computed.
4596 */
4597 void continueUpdateImeTarget() {
4598 if (mDeferUpdateImeTargetCount == 0) {
4599 return;
4600 }
4601
4602 mDeferUpdateImeTargetCount--;
4603 if (mDeferUpdateImeTargetCount == 0) {
4604 computeImeTarget(true /* updateImeTarget */);
4605 }
4606 }
4607
4608 /**
4609 * @return Whether a new IME target should be computed.
4610 */
4611 private boolean canUpdateImeTarget() {
4612 return mDeferUpdateImeTargetCount == 0;
4613 }
Arthur Hung95b38a92018-07-20 18:56:12 +08004614
4615 InputMonitor getInputMonitor() {
4616 return mInputMonitor;
4617 }
Jorim Jaggif1292892018-09-10 11:58:13 +02004618
4619 /**
4620 * @return Cached value whether we told display manager that we have content.
4621 */
4622 boolean getLastHasContent() {
4623 return mLastHasContent;
4624 }
Arthur Hungbe5ce212018-09-13 18:41:56 +08004625
4626 void registerPointerEventListener(@NonNull PointerEventListener listener) {
4627 if (mPointerEventDispatcher != null) {
4628 mPointerEventDispatcher.registerInputEventListener(listener);
4629 }
4630 }
4631
4632 void unregisterPointerEventListener(@NonNull PointerEventListener listener) {
4633 if (mPointerEventDispatcher != null) {
4634 mPointerEventDispatcher.unregisterInputEventListener(listener);
4635 }
4636 }
lumark588a3e82018-07-20 18:53:54 +08004637
4638 void prepareAppTransition(@WindowManager.TransitionType int transit,
4639 boolean alwaysKeepCurrent, @WindowManager.TransitionFlags int flags,
4640 boolean forceOverride) {
4641 final boolean prepared = mAppTransition.prepareAppTransitionLocked(
4642 transit, alwaysKeepCurrent, flags, forceOverride);
4643 if (prepared && okToAnimate()) {
4644 mSkipAppTransitionAnimation = false;
4645 }
4646 }
4647
4648 void executeAppTransition() {
4649 if (mAppTransition.isTransitionSet()) {
4650 if (DEBUG_APP_TRANSITIONS) {
4651 Slog.w(TAG_WM, "Execute app transition: " + mAppTransition + ", displayId: "
4652 + mDisplayId + " Callers=" + Debug.getCallers(5));
4653 }
4654 mAppTransition.setReady();
4655 mService.mWindowPlacerLocked.requestTraversal();
4656 }
4657 }
4658
4659 /**
4660 * Update pendingLayoutChanges after app transition has finished.
4661 */
4662 void handleAnimatingStoppedAndTransition() {
4663 int changes = 0;
4664
4665 mAppTransition.setIdle();
4666
4667 for (int i = mNoAnimationNotifyOnTransitionFinished.size() - 1; i >= 0; i--) {
4668 final IBinder token = mNoAnimationNotifyOnTransitionFinished.get(i);
4669 mAppTransition.notifyAppTransitionFinishedLocked(token);
4670 }
4671 mNoAnimationNotifyOnTransitionFinished.clear();
4672
4673 mWallpaperController.hideDeferredWallpapersIfNeeded();
4674
4675 onAppTransitionDone();
4676
4677 changes |= FINISH_LAYOUT_REDO_LAYOUT;
4678 if (DEBUG_WALLPAPER_LIGHT) {
4679 Slog.v(TAG_WM, "Wallpaper layer changed: assigning layers + relayout");
4680 }
4681 computeImeTarget(true /* updateImeTarget */);
4682 mService.mRoot.mWallpaperMayChange = true;
4683 // Since the window list has been rebuilt, focus might have to be recomputed since the
4684 // actual order of windows might have changed again.
4685 mService.mFocusMayChange = true;
4686
4687 pendingLayoutChanges |= changes;
4688 }
Andrii Kulian15cfb422018-11-07 13:38:49 -08004689
4690 /**
4691 * @see Display#FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS
4692 */
4693 boolean supportsSystemDecorations() {
4694 // TODO(b/114338689): Read the setting from DisplaySettings.
4695 return mDisplay.supportsSystemDecorations()
4696 // TODO (b/111363427): Remove this and set the new FLAG_SHOULD_SHOW_LAUNCHER flag
4697 // (b/114338689) whenever vr 2d display id is set.
4698 || mDisplayId == mService.mVr2dDisplayId
4699 || mService.mForceDesktopModeOnExternalDisplays;
4700 }
Craig Mautner59c00972012-07-30 12:10:24 -07004701}