blob: 2f7956e8653182d7eee274b07f3f40a9df0308b7 [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
wilsonshihc32538e2018-11-07 17:27:34 +0800417 boolean mWallpaperMayChange = false;
418
Robert Carrb1579c82017-09-05 14:54:47 -0700419 private final SurfaceSession mSession = new SurfaceSession();
420
421 /**
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800422 * Window that is currently interacting with the user. This window is responsible for receiving
423 * key events and pointer events from the user.
424 */
425 WindowState mCurrentFocus = null;
426
427 /**
428 * The last focused window that we've notified the client that the focus is changed.
429 */
430 WindowState mLastFocus = null;
431
432 /**
433 * Windows that have lost input focus and are waiting for the new focus window to be displayed
434 * before they are told about this.
435 */
436 ArrayList<WindowState> mLosingFocus = new ArrayList<>();
437
438 /**
439 * The foreground app of this display. Windows below this app cannot be the focused window. If
440 * the user taps on the area outside of the task of the focused app, we will notify AM about the
441 * new task the user wants to interact with.
442 */
443 AppWindowToken mFocusedApp = null;
444
445 /** Windows added since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
446 final ArrayList<WindowState> mWinAddedSinceNullFocus = new ArrayList<>();
447
448 /** Windows removed since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
449 final ArrayList<WindowState> mWinRemovedSinceNullFocus = new ArrayList<>();
450
451 /**
Robert Carrb1579c82017-09-05 14:54:47 -0700452 * We organize all top-level Surfaces in to the following layers.
453 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800454 * and omitted from Screen-Magnification, for example the strict mode flash or
455 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700456 * {@link #mWindowingLayer} contains everything else.
457 */
458 private SurfaceControl mOverlayLayer;
459
460 /**
461 * See {@link #mOverlayLayer}
462 */
463 private SurfaceControl mWindowingLayer;
464
465 /**
466 * Specifies the size of the surfaces in {@link #mOverlayLayer} and {@link #mWindowingLayer}.
467 * <p>
468 * For these surfaces currently we use a surface based on the larger of width or height so we
469 * don't have to resize when rotating the display.
470 */
471 private int mSurfaceSize;
472
Adrian Roos5251b1d2018-03-23 18:57:43 +0100473 /**
474 * Sequence number for the current layout pass.
475 */
476 int mLayoutSeq = 0;
477
Chavi Weingarten3a748552018-05-14 17:32:42 +0000478 /**
479 * Specifies the count to determine whether to defer updating the IME target until ready.
480 */
481 private int mDeferUpdateImeTargetCount;
482
Jorim Jaggi9af095b2017-12-12 17:18:57 +0100483 /** Temporary float array to retrieve 3x3 matrix values. */
484 private final float[] mTmpFloats = new float[9];
485
Robert Carr24be9ab2018-04-30 17:54:53 -0700486 private MagnificationSpec mMagnificationSpec;
487
Arthur Hung95b38a92018-07-20 18:56:12 +0800488 private InputMonitor mInputMonitor;
489
Jorim Jaggif1292892018-09-10 11:58:13 +0200490 /** Caches the value whether told display manager that we have content. */
491 private boolean mLastHasContent;
492
Issei Suzuki43190bd2018-08-20 17:28:41 +0200493 private DisplayRotationUtil mRotationUtil = new DisplayRotationUtil();
494
lumark90120a82018-08-15 00:33:03 +0800495 /**
496 * The input method window for this display.
497 */
498 WindowState mInputMethodWindow;
499
lumarkff0ab692018-11-05 20:32:30 +0800500 /**
501 * This just indicates the window the input method is on top of, not
502 * necessarily the window its input is going to.
503 */
504 WindowState mInputMethodTarget;
505
506 /** If true hold off on modifying the animation layer of mInputMethodTarget */
507 boolean mInputMethodTargetWaitingAnim;
508
Arthur Hungbe5ce212018-09-13 18:41:56 +0800509 private final PointerEventDispatcher mPointerEventDispatcher;
510
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800511 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
512 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800513 final AppWindowToken atoken = w.mAppToken;
514 if (winAnimator.mDrawState == READY_TO_SHOW) {
Jorim Jaggiaf0d6d22018-06-08 15:25:35 +0200515 if (atoken == null || atoken.canShowWindows()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800516 if (w.performShowLocked()) {
517 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
518 if (DEBUG_LAYOUT_REPEATS) {
519 mService.mWindowPlacerLocked.debugLayoutRepeats(
520 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
521 }
522 }
523 }
524 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800525 };
526
527 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
528 final WindowStateAnimator winAnimator = w.mWinAnimator;
529 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
530 return;
531 }
532
Jorim Jaggi8f520872018-08-14 17:00:20 +0200533 // If this window is animating, ensure the animation background is set.
534 final AnimationAdapter anim = w.mAppToken != null
535 ? w.mAppToken.getAnimation()
536 : w.getAnimation();
537 if (anim != null) {
538 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800539 if (color != 0) {
540 final TaskStack stack = w.getStack();
541 if (stack != null) {
542 stack.setAnimationBackground(winAnimator, color);
543 }
544 }
545 }
546 };
547
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800548 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
549 final int lostFocusUid = mTmpWindow.mOwnerUid;
550 final Handler handler = mService.mH;
551 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
552 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
553 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
554 w.mAttrs.hideTimeoutMilliseconds);
555 }
556 }
557 };
558
559 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800560 final AppWindowToken focusedApp = mFocusedApp;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800561 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
562 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
563
564 if (!w.canReceiveKeys()) {
565 return false;
566 }
567
568 final AppWindowToken wtoken = w.mAppToken;
569
570 // If this window's application has been removed, just skip it.
571 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
572 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
573 + (wtoken.removed ? "removed" : "sendingToBottom"));
574 return false;
575 }
576
577 if (focusedApp == null) {
578 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
579 + " using new focus @ " + w);
580 mTmpWindow = w;
581 return true;
582 }
583
584 if (!focusedApp.windowsAreFocusable()) {
585 // Current focused app windows aren't focusable...
586 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
587 + " focusable using new focus @ " + w);
588 mTmpWindow = w;
589 return true;
590 }
591
592 // Descend through all of the app tokens and find the first that either matches
593 // win.mAppToken (return win) or mFocusedApp (return null).
594 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
595 if (focusedApp.compareTo(wtoken) > 0) {
596 // App stack below focused app stack. No focus for you!!!
597 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
598 "findFocusedWindow: Reached focused app=" + focusedApp);
599 mTmpWindow = null;
600 return true;
601 }
602 }
603
604 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
605 mTmpWindow = w;
606 return true;
607 };
608
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800609 private final Consumer<WindowState> mPerformLayout = w -> {
610 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
611 // wasting time and funky changes while a window is animating away.
612 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
613 || w.isGoneForLayoutLw();
614
615 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
616 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
617 + " mLayoutAttached=" + w.mLayoutAttached
618 + " screen changed=" + w.isConfigChanged());
619 final AppWindowToken atoken = w.mAppToken;
620 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200621 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800622 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
623 + " parentHidden=" + w.isParentWindowHidden());
624 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200625 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800626 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
627 + " parentHidden=" + w.isParentWindowHidden());
628 }
629
630 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
631 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
632 // since that means "perform layout as normal, just don't display").
633 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
634 || ((w.isConfigChanged() || w.setReportResizeHints())
635 && !w.isGoneForLayoutLw() &&
636 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
637 (w.mHasSurface && w.mAppToken != null &&
638 w.mAppToken.layoutConfigChanges)))) {
639 if (!w.mLayoutAttached) {
640 if (mTmpInitial) {
641 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700642 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800643 }
644 if (w.mAttrs.type == TYPE_DREAM) {
645 // Don't layout windows behind a dream, so that if it does stuff like hide
646 // the status bar we won't get a bad transition when it goes away.
647 mTmpWindow = w;
648 }
649 w.mLayoutNeeded = false;
650 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200651 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000652 mService.mPolicy.layoutWindowLw(w, null, mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100653 w.mLayoutSeq = mLayoutSeq;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800654
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200655 // If this is the first layout, we need to initialize the last inset values as
656 // otherwise we'd immediately cause an unnecessary resize.
657 if (firstLayout) {
658 w.updateLastInsetValues();
659 }
660
Adrian Roos23df3a32018-03-15 15:41:13 +0100661 if (w.mAppToken != null) {
662 w.mAppToken.layoutLetterbox(w);
663 }
664
chaviw492139a2018-07-16 16:07:35 -0700665 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700666 + " mContainingFrame=" + w.getContainingFrame()
667 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800668 }
669 }
670 };
671
672 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
673 if (w.mLayoutAttached) {
674 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
675 + " mViewVisibility=" + w.mViewVisibility
676 + " mRelayoutCalled=" + w.mRelayoutCalled);
677 // If this view is GONE, then skip it -- keep the current frame, and let the caller
678 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
679 // windows, since that means "perform layout as normal, just don't display").
680 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
681 return;
682 }
683 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
684 || w.mLayoutNeeded) {
685 if (mTmpInitial) {
686 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700687 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800688 }
689 w.mLayoutNeeded = false;
690 w.prelayout();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000691 mService.mPolicy.layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100692 w.mLayoutSeq = mLayoutSeq;
chaviw492139a2018-07-16 16:07:35 -0700693 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700694 + " mContainingFrame=" + w.getContainingFrame()
695 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800696 }
697 } else if (w.mAttrs.type == TYPE_DREAM) {
698 // Don't layout windows behind a dream, so that if it does stuff like hide the
699 // status bar we won't get a bad transition when it goes away.
700 mTmpWindow = mTmpWindow2;
701 }
702 };
703
704 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
705 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
706 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
707 return w.canBeImeTarget();
708 };
709
710 private final Consumer<WindowState> mApplyPostLayoutPolicy =
711 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
lumarkff0ab692018-11-05 20:32:30 +0800712 mInputMethodTarget);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800713
714 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
715 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
716 final boolean obscuredChanged = w.mObscured !=
717 mTmpApplySurfaceChangesTransactionState.obscured;
718 final RootWindowContainer root = mService.mRoot;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800719
720 // Update effect.
721 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
722 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
723 final boolean isDisplayed = w.isDisplayedLw();
724
725 if (isDisplayed && w.isObscuringDisplay()) {
726 // This window completely covers everything behind it, so we want to leave all
727 // of them as undimmed (for performance reasons).
728 root.mObscuringWindow = w;
729 mTmpApplySurfaceChangesTransactionState.obscured = true;
730 }
731
732 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
733 root.handleNotObscuredLocked(w,
734 mTmpApplySurfaceChangesTransactionState.obscured,
735 mTmpApplySurfaceChangesTransactionState.syswin);
736
737 if (w.mHasSurface && isDisplayed) {
738 final int type = w.mAttrs.type;
739 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
740 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
741 mTmpApplySurfaceChangesTransactionState.syswin = true;
742 }
743 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
744 && w.mAttrs.preferredRefreshRate != 0) {
745 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
746 = w.mAttrs.preferredRefreshRate;
747 }
748 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
749 && w.mAttrs.preferredDisplayModeId != 0) {
750 mTmpApplySurfaceChangesTransactionState.preferredModeId
751 = w.mAttrs.preferredDisplayModeId;
752 }
753 }
754 }
755
wilsonshihc32538e2018-11-07 17:27:34 +0800756 if (obscuredChanged && w.isVisibleLw() && mWallpaperController.isWallpaperTarget(w)) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800757 // This is the wallpaper target and its obscured state changed... make sure the
758 // current wallpaper's visibility has been updated accordingly.
759 mWallpaperController.updateWallpaperVisibility();
760 }
761
chaviw161ea3e2018-01-31 12:01:12 -0800762 w.handleWindowMovedIfNeeded();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800763
764 final WindowStateAnimator winAnimator = w.mWinAnimator;
765
766 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
chaviw1454b392018-08-06 09:54:04 -0700767 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800768
769 // Moved from updateWindowsAndWallpaperLocked().
770 if (w.mHasSurface) {
771 // Take care of the window being ready to display.
772 final boolean committed = winAnimator.commitFinishDrawingLocked();
773 if (isDefaultDisplay && committed) {
774 if (w.mAttrs.type == TYPE_DREAM) {
775 // HACK: When a dream is shown, it may at that point hide the lock screen.
776 // So we need to redo the layout to let the phone window manager make this
777 // happen.
778 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
779 if (DEBUG_LAYOUT_REPEATS) {
780 surfacePlacer.debugLayoutRepeats(
781 "dream and commitFinishDrawingLocked true",
782 pendingLayoutChanges);
783 }
784 }
785 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
786 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
787 "First draw done in potential wallpaper target " + w);
wilsonshihc32538e2018-11-07 17:27:34 +0800788 mWallpaperMayChange = true;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800789 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
790 if (DEBUG_LAYOUT_REPEATS) {
791 surfacePlacer.debugLayoutRepeats(
792 "wallpaper and commitFinishDrawingLocked true",
793 pendingLayoutChanges);
794 }
795 }
796 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800797 }
798
799 final AppWindowToken atoken = w.mAppToken;
800 if (atoken != null) {
Adrian Roos23df3a32018-03-15 15:41:13 +0100801 atoken.updateLetterboxSurface(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800802 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
803 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
804 mTmpUpdateAllDrawn.add(atoken);
805 }
806 }
807
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800808 if (!mLosingFocus.isEmpty() && w.isFocused() && w.isDisplayedLw()) {
809 mService.mH.obtainMessage(REPORT_LOSING_FOCUS, this).sendToTarget();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800810 }
811
812 w.updateResizingWindowIfNeeded();
813 };
814
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800815 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800816 * Create new {@link DisplayContent} instance, add itself to the root window container and
817 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700818 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800819 * @param service You know.
wilsonshihc32538e2018-11-07 17:27:34 +0800820 * @param controller The controller for the display container.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700821 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700822 DisplayContent(Display display, WindowManagerService service,
wilsonshihc32538e2018-11-07 17:27:34 +0800823 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();
wilsonshihc32538e2018-11-07 17:27:34 +0800834 mWallpaperController = new WallpaperController(mService, this);
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 }
Evan Roskye747c3e2018-10-30 20:06:41 -07001896 }
1897
1898 /**
1899 * Updates the resources used by docked/pinned controllers. This needs to be called at the
1900 * beginning of a configuration update cascade since the metrics from these resources are used
1901 * for bounds calculations. Since ActivityDisplay initiates the configuration update, this
1902 * should be called from there instead of DisplayContent's onConfigurationChanged.
1903 */
1904 void preOnConfigurationChanged() {
Bryce Leef3c6a472017-11-14 14:53:06 -08001905 final DockedStackDividerController dividerController = getDockedDividerController();
1906
1907 if (dividerController != null) {
1908 getDockedDividerController().onConfigurationChanged();
1909 }
1910
1911 final PinnedStackController pinnedStackController = getPinnedStackController();
1912
1913 if (pinnedStackController != null) {
1914 getPinnedStackController().onConfigurationChanged();
1915 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001916 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001917
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001918 @Override
1919 boolean fillsParent() {
1920 return true;
1921 }
1922
1923 @Override
1924 boolean isVisible() {
1925 return true;
1926 }
1927
1928 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001929 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001930 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001931 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001932 }
1933
Robert Carr9785cf32018-04-25 15:06:07 -07001934 /**
1935 * In split-screen mode we process the IME containers above the docked divider
1936 * rather than directly above their target.
1937 */
1938 private boolean skipTraverseChild(WindowContainer child) {
lumarkff0ab692018-11-05 20:32:30 +08001939 if (child == mImeWindowsContainers && mInputMethodTarget != null
Robert Carr9785cf32018-04-25 15:06:07 -07001940 && !hasSplitScreenPrimaryStack()) {
1941 return true;
1942 }
1943 return false;
1944 }
1945
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001946 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001947 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1948 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1949 // target, or here in order if there isn't an IME target.
1950 if (traverseTopToBottom) {
1951 for (int i = mChildren.size() - 1; i >= 0; --i) {
1952 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001953 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001954 continue;
1955 }
Robert Carr9785cf32018-04-25 15:06:07 -07001956
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001957 if (child.forAllWindows(callback, traverseTopToBottom)) {
1958 return true;
1959 }
1960 }
1961 } else {
1962 final int count = mChildren.size();
1963 for (int i = 0; i < count; i++) {
1964 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001965 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001966 continue;
1967 }
Robert Carr9785cf32018-04-25 15:06:07 -07001968
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001969 if (child.forAllWindows(callback, traverseTopToBottom)) {
1970 return true;
1971 }
1972 }
1973 }
1974 return false;
1975 }
1976
1977 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1978 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1979 }
1980
Wale Ogunwale399c8692017-05-08 14:22:42 -07001981 @Override
1982 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001983 final WindowManagerPolicy policy = mService.mPolicy;
1984
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001985 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001986 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001987 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1988 + " is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001989 // If the display is frozen, some activities may be in the middle of restarting, and
1990 // thus have removed their old window. If the window has the flag to hide the lock
1991 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1992 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001993 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001994 } else if (policy.isKeyguardLocked()) {
1995 // Use the last orientation the while the display is frozen with the keyguard
1996 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1997 // window. We don't want to check the show when locked window directly though as
1998 // things aren't stable while the display is frozen, for example the window could be
1999 // momentarily unavailable due to activity relaunch.
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07002000 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
2001 + " is frozen while keyguard locked, return " + mLastOrientation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08002002 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07002003 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002004 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002005 final int orientation = mAboveAppWindowsContainers.getOrientation();
2006 if (orientation != SCREEN_ORIENTATION_UNSET) {
2007 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002008 }
Wale Ogunwale51362492016-09-08 17:49:17 -07002009 }
2010
Wale Ogunwale399c8692017-05-08 14:22:42 -07002011 // Top system windows are not requesting an orientation. Start searching from apps.
2012 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07002013 }
2014
Craig Mautner46ac6fa2013-08-01 10:06:34 -07002015 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07002016 // Check if display metrics changed and update base values if needed.
2017 updateBaseDisplayMetricsIfNeeded();
2018
Craig Mautner722285e2012-09-07 13:55:58 -07002019 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07002020 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07002021
Garfield Tan2f145f22018-11-01 15:27:03 -07002022 onDisplayChanged(this);
Craig Mautner722285e2012-09-07 13:55:58 -07002023 }
2024
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002025 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002026 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
2027 if (displayManagerInternal != null) {
2028 // Bootstrap the default logical display from the display manager.
2029 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
2030 if (newDisplayInfo != null) {
2031 mDisplayInfo.copyFrom(newDisplayInfo);
2032 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002033 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002034
Andrii Kuliancd097992017-03-23 18:31:59 -07002035 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
2036 mDisplayInfo.logicalDensityDpi);
2037 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
2038 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
2039 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002040 mInitialDisplayCutout = mDisplayInfo.displayCutout;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002041 }
2042
Andrii Kuliancd097992017-03-23 18:31:59 -07002043 /**
2044 * If display metrics changed, overrides are not set and it's not just a rotation - update base
2045 * values.
2046 */
2047 private void updateBaseDisplayMetricsIfNeeded() {
2048 // Get real display metrics without overrides from WM.
2049 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
2050 final int orientation = mDisplayInfo.rotation;
2051 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2052 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
2053 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
2054 final int newDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002055 final DisplayCutout newCutout = mDisplayInfo.displayCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07002056
2057 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
2058 || mInitialDisplayHeight != newHeight
Adrian Roos1cf585052018-01-03 18:43:27 +01002059 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi
2060 || !Objects.equals(mInitialDisplayCutout, newCutout);
Andrii Kuliancd097992017-03-23 18:31:59 -07002061
2062 if (displayMetricsChanged) {
2063 // Check if display size or density is forced.
2064 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
2065 || mBaseDisplayHeight != mInitialDisplayHeight;
2066 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
2067
2068 // If there is an override set for base values - use it, otherwise use new values.
2069 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
2070 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
2071 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
2072
2073 // Real display metrics changed, so we should also update initial values.
2074 mInitialDisplayWidth = newWidth;
2075 mInitialDisplayHeight = newHeight;
2076 mInitialDisplayDensity = newDensity;
Adrian Roos1cf585052018-01-03 18:43:27 +01002077 mInitialDisplayCutout = newCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07002078 mService.reconfigureDisplayLocked(this);
2079 }
2080 }
2081
Bryce Lee27cec322017-03-21 09:41:37 -07002082 /** Sets the maximum width the screen resolution can be */
2083 void setMaxUiWidth(int width) {
2084 if (DEBUG_DISPLAY) {
2085 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
2086 }
2087
2088 mMaxUiWidth = width;
2089
2090 // Update existing metrics.
2091 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
2092 }
2093
2094 /** Update base (override) display metrics. */
2095 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
2096 mBaseDisplayWidth = baseWidth;
2097 mBaseDisplayHeight = baseHeight;
2098 mBaseDisplayDensity = baseDensity;
2099
2100 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
2101 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
2102 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
2103 mBaseDisplayWidth = mMaxUiWidth;
2104
2105 if (DEBUG_DISPLAY) {
2106 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
2107 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
2108 + " on display:" + getDisplayId());
2109 }
2110 }
2111
2112 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08002113
2114 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07002115 }
2116
Riddle Hsuf53da812018-08-15 22:00:27 +08002117 /**
2118 * Forces this display to use the specified density.
2119 *
2120 * @param density The density in DPI to use. If the value equals to initial density, the setting
2121 * will be cleared.
2122 * @param userId The target user to apply. Only meaningful when this is default display. If the
2123 * user id is {@link UserHandle#USER_CURRENT}, it means to apply current settings
2124 * so only need to configure display.
2125 */
2126 void setForcedDensity(int density, int userId) {
2127 final boolean clear = density == mInitialDisplayDensity;
2128 final boolean updateCurrent = userId == UserHandle.USER_CURRENT;
2129 if (mService.mCurrentUserId == userId || updateCurrent) {
2130 mBaseDisplayDensity = density;
2131 mService.reconfigureDisplayLocked(this);
2132 }
2133 if (updateCurrent) {
2134 // We are applying existing settings so no need to save it again.
2135 return;
2136 }
2137
2138 if (density == mInitialDisplayDensity) {
2139 density = 0;
2140 }
Chilun8753ad32018-10-09 15:56:45 +08002141 mService.mDisplayWindowSettings.setForcedDensity(this, density, userId);
Riddle Hsuf53da812018-08-15 22:00:27 +08002142 }
2143
2144 /** @param mode {@link #FORCE_SCALING_MODE_AUTO} or {@link #FORCE_SCALING_MODE_DISABLED}. */
2145 void setForcedScalingMode(@ForceScalingMode int mode) {
2146 if (mode != FORCE_SCALING_MODE_DISABLED) {
2147 mode = FORCE_SCALING_MODE_AUTO;
2148 }
2149
2150 mDisplayScalingDisabled = (mode != FORCE_SCALING_MODE_AUTO);
2151 Slog.i(TAG_WM, "Using display scaling mode: " + (mDisplayScalingDisabled ? "off" : "auto"));
2152 mService.reconfigureDisplayLocked(this);
2153
Chilun8753ad32018-10-09 15:56:45 +08002154 mService.mDisplayWindowSettings.setForcedScalingMode(this, mode);
Riddle Hsuf53da812018-08-15 22:00:27 +08002155 }
2156
2157 /** If the given width and height equal to initial size, the setting will be cleared. */
2158 void setForcedSize(int width, int height) {
2159 final boolean clear = mInitialDisplayWidth == width && mInitialDisplayHeight == height;
2160 if (!clear) {
2161 // Set some sort of reasonable bounds on the size of the display that we will try
2162 // to emulate.
2163 final int minSize = 200;
2164 final int maxScale = 2;
2165 width = Math.min(Math.max(width, minSize), mInitialDisplayWidth * maxScale);
2166 height = Math.min(Math.max(height, minSize), mInitialDisplayHeight * maxScale);
2167 }
2168
2169 Slog.i(TAG_WM, "Using new display size: " + width + "x" + height);
2170 updateBaseDisplayMetrics(width, height, mBaseDisplayDensity);
2171 mService.reconfigureDisplayLocked(this);
2172
2173 if (clear) {
2174 width = height = 0;
2175 }
Chilun8753ad32018-10-09 15:56:45 +08002176 mService.mDisplayWindowSettings.setForcedSize(this, width, height);
Riddle Hsuf53da812018-08-15 22:00:27 +08002177 }
2178
Wale Ogunwaledb506192017-12-08 10:57:32 -08002179 void getStableRect(Rect out) {
2180 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08002181 }
2182
Wale Ogunwale61911492017-10-11 08:50:50 -07002183 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002184 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
2185 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07002186
Wale Ogunwale61911492017-10-11 08:50:50 -07002187 final TaskStack stack = new TaskStack(mService, stackId, controller);
2188 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002189 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07002190 }
2191
Andrii Kulian51c1b672017-04-07 18:39:32 -07002192 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002193 final DisplayContent prevDc = stack.getDisplayContent();
2194 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07002195 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
2196 + " which is not currently attached to any display");
2197 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002198 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07002199 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
2200 + " to its current displayId=" + mDisplayId);
2201 }
2202
lumark588a3e82018-07-20 18:53:54 +08002203 // Clean up all pending transitions when stack reparent to another display.
2204 stack.forAllAppWindows(AppWindowToken::removeFromPendingTransition);
2205
Wale Ogunwale61911492017-10-11 08:50:50 -07002206 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07002207 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08002208 }
2209
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002210 @Override
2211 protected void addChild(DisplayChildWindowContainer child,
2212 Comparator<DisplayChildWindowContainer> comparator) {
2213 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2214 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002215
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002216 @Override
2217 protected void addChild(DisplayChildWindowContainer child, int index) {
2218 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2219 }
2220
2221 @Override
2222 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002223 // Only allow removal of direct children from this display if the display is in the process
2224 // of been removed.
2225 if (mRemovingDisplay) {
2226 super.removeChild(child);
2227 return;
2228 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002229 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002230 }
2231
Andrii Kuliand2765632016-12-12 22:26:34 -08002232 @Override
2233 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
2234 // Children of the display are statically ordered, so the real intention here is to perform
2235 // the operation on the display and not the static direct children.
2236 getParent().positionChildAt(position, this, includingParents);
2237 }
2238
Riddle Hsu57831b52018-07-27 00:31:48 +08002239 void positionStackAt(int position, TaskStack child, boolean includingParents) {
2240 mTaskStackContainers.positionChildAt(position, child, includingParents);
Winson Chung59a47ded2018-01-25 17:46:06 +00002241 layoutAndAssignWindowLayersIfNeeded();
2242 }
2243
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002244 /**
2245 * Used to obtain task ID when user taps on coordinate (x, y) in this display, and outside
2246 * current task in focus.
2247 *
2248 * This returns the task ID of the foremost task at (x, y) if the task is not home. Otherwise it
2249 * returns -1.
2250 *
2251 * @param x horizontal coordinate of the tap position
2252 * @param y vertical coordinate of the tap position
2253 * @return the task ID if a non-home task is found; -1 if not
2254 */
2255 int taskForTapOutside(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002256 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2257 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Garfield Tanf2e0a9e2018-10-16 09:42:02 -07002258 if (stack.isActivityTypeHome() && !stack.inMultiWindowMode()) {
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002259 // We skip not only home stack, but also everything behind home because user can't
Garfield Tanf2e0a9e2018-10-16 09:42:02 -07002260 // see them when home stack is isn't in multi-window mode.
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002261 break;
2262 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002263 final int taskId = stack.taskIdFromPoint(x, y);
2264 if (taskId != -1) {
2265 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07002266 }
2267 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002268 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07002269 }
2270
Chong Zhang8e89b312015-09-09 15:09:30 -07002271 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08002272 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07002273 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07002274 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07002275 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002276 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002277 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07002278 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2279 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002280 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002281 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002282 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002283
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002284 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
2285 if (mTmpTaskForResizePointSearchResult.searchDone) {
2286 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07002287 }
2288 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002289 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002290 }
2291
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002292 void updateTouchExcludeRegion() {
2293 final Task focusedTask = (mFocusedApp != null ? mFocusedApp.getTask() : null);
David Stevensee9e2772017-02-09 16:30:27 -08002294 if (focusedTask == null) {
2295 mTouchExcludeRegion.setEmpty();
2296 } else {
2297 mTouchExcludeRegion.set(mBaseDisplayRect);
2298 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
2299 mTmpRect2.setEmpty();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002300 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0;
2301 --stackNdx) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002302 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002303 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
2304 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08002305 }
2306 // If we removed the focused task above, add it back and only leave its
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002307 // outside touch area in the exclusion. TapDetector is not interested in
David Stevensee9e2772017-02-09 16:30:27 -08002308 // any touch inside the focused task itself.
2309 if (!mTmpRect2.isEmpty()) {
2310 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
2311 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08002312 }
lumark90120a82018-08-15 00:33:03 +08002313 if (mInputMethodWindow != null && mInputMethodWindow.isVisibleLw()) {
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002314 // If the input method is visible and the user is typing, we don't want these touch
2315 // events to be intercepted and used to change focus. This would likely cause a
2316 // disappearance of the input method.
lumark90120a82018-08-15 00:33:03 +08002317 mInputMethodWindow.getTouchableRegion(mTmpRegion);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002318 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002319 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002320 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002321 final WindowState win = mTapExcludedWindows.get(i);
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002322 win.getTouchableRegion(mTmpRegion);
2323 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
2324 }
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002325 for (int i = mTapExcludeProvidingWindows.size() - 1; i >= 0; i--) {
2326 final WindowState win = mTapExcludeProvidingWindows.valueAt(i);
2327 win.amendTapExcludeRegion(mTouchExcludeRegion);
2328 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08002329 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07002330 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002331 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07002332 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002333 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
2334 }
Craig Mautner1bef3892015-02-17 15:09:47 -08002335 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07002336 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08002337 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07002338 }
2339
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002340 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07002341 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002342 super.switchUser();
2343 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07002344 }
2345
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07002346 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002347 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2348 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07002349 }
2350 }
2351
Wale Ogunwale10124582016-09-15 20:25:50 -07002352 @Override
2353 void removeIfPossible() {
2354 if (isAnimating()) {
2355 mDeferredRemoval = true;
2356 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07002357 }
Wale Ogunwale10124582016-09-15 20:25:50 -07002358 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07002359 }
2360
Wale Ogunwale10124582016-09-15 20:25:50 -07002361 @Override
2362 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002363 mRemovingDisplay = true;
2364 try {
lumark588a3e82018-07-20 18:53:54 +08002365 // Clear all transitions & screen frozen states when removing display.
2366 mOpeningApps.clear();
2367 mClosingApps.clear();
2368 mUnknownAppVisibilityController.clear();
2369 mAppTransition.removeAppTransitionTimeoutCallbacks();
2370 handleAnimatingStoppedAndTransition();
2371 mService.stopFreezingDisplayLocked();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002372 super.removeImmediately();
2373 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Arthur Hungbe5ce212018-09-13 18:41:56 +08002374 if (mPointerEventDispatcher != null && mTapDetector != null) {
2375 unregisterPointerEventListener(mTapDetector);
2376 unregisterPointerEventListener(mService.mMousePositionTracker);
2377 mTapDetector = null;
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002378 }
Jorim Jaggia3844032018-01-03 15:54:43 +01002379 mService.mAnimator.removeDisplayLocked(mDisplayId);
Louis Chang207f9112018-07-30 08:58:54 +08002380 mWindowingLayer.release();
2381 mOverlayLayer.release();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002382 } finally {
Riddle Hsu4e611772018-10-31 18:58:28 +08002383 mDisplayReady = false;
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002384 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08002385 }
Bryce Leef19cbe22018-02-02 15:09:21 -08002386
Arthur Hung39134b22018-08-14 11:58:28 +08002387 mInputMonitor.onRemoved();
Riddle Hsuf53da812018-08-15 22:00:27 +08002388 mService.mWindowPlacerLocked.requestTraversal();
Craig Mautner95da1082014-02-24 17:54:35 -08002389 }
2390
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002391 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07002392 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002393 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002394 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
2395
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002396 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002397 removeImmediately();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002398 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08002399 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002400 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08002401 }
2402
Andrii Kulian0214ed92017-05-16 13:44:05 -07002403 /** @return 'true' if removal of this display content is deferred due to active animation. */
2404 boolean isRemovalDeferred() {
2405 return mDeferredRemoval;
2406 }
2407
Wale Ogunwale10124582016-09-15 20:25:50 -07002408 boolean animateForIme(float interpolatedValue, float animationTarget,
2409 float dividerAnimationTarget) {
2410 boolean updated = false;
2411
Wale Ogunwale61911492017-10-11 08:50:50 -07002412 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2413 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002414 if (stack == null || !stack.isAdjustedForIme()) {
2415 continue;
2416 }
2417
2418 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
2419 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2420 updated = true;
2421 } else {
2422 mDividerControllerLocked.mLastAnimationProgress =
2423 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
2424 mDividerControllerLocked.mLastDividerProgress =
2425 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
2426 updated |= stack.updateAdjustForIme(
2427 mDividerControllerLocked.mLastAnimationProgress,
2428 mDividerControllerLocked.mLastDividerProgress,
2429 false /* force */);
2430 }
2431 if (interpolatedValue >= 1f) {
2432 stack.endImeAdjustAnimation();
2433 }
2434 }
2435
2436 return updated;
2437 }
2438
2439 boolean clearImeAdjustAnimation() {
2440 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002441 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2442 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002443 if (stack != null && stack.isAdjustedForIme()) {
2444 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2445 changed = true;
2446 }
2447 }
2448 return changed;
2449 }
2450
2451 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002452 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2453 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002454 if (stack.isVisible() && stack.isAdjustedForIme()) {
2455 stack.beginImeAdjustAnimation();
2456 }
2457 }
2458 }
2459
2460 void adjustForImeIfNeeded() {
lumark90120a82018-08-15 00:33:03 +08002461 final WindowState imeWin = mInputMethodWindow;
Wale Ogunwale10124582016-09-15 20:25:50 -07002462 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2463 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002464 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002465 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2466 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2467 imeTargetStack.getDockSide() : DOCKED_INVALID;
2468 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2469 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2470 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002471 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002472 final boolean imeHeightChanged = imeVisible &&
2473 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2474
2475 // The divider could be adjusted for IME position, or be thinner than usual,
2476 // or both. There are three possible cases:
2477 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2478 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2479 // - If IME is not visible, divider is not moved and is normal width.
2480
2481 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002482 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2483 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002484 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002485 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2486 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002487 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2488 } else {
2489 stack.resetAdjustedForIme(false);
2490 }
2491 }
2492 mDividerControllerLocked.setAdjustedForIme(
2493 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2494 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002495 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2496 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002497 stack.resetAdjustedForIme(!dockVisible);
2498 }
2499 mDividerControllerLocked.setAdjustedForIme(
2500 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2501 }
Winson Chung655332c2016-10-31 13:14:28 -07002502 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002503 }
2504
2505 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002506 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2507 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002508 stack.prepareFreezingTaskBounds();
2509 }
2510 }
2511
Wale Ogunwale94744212015-09-21 19:01:47 -07002512 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002513 getBounds(mTmpRect, newRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002514 rotateBounds(mTmpRect, oldRotation, newRotation, bounds);
2515 }
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002516
Evan Rosky39b6f232018-10-30 18:35:41 -07002517 void rotateBounds(Rect parentBounds, int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002518 // Compute a transform matrix to undo the coordinate space transformation,
2519 // and present the window at the same physical position it previously occupied.
2520 final int deltaRotation = deltaRotation(newRotation, oldRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002521 createRotationMatrix(
2522 deltaRotation, parentBounds.width(), parentBounds.height(), mTmpMatrix);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002523
2524 mTmpRectF.set(bounds);
2525 mTmpMatrix.mapRect(mTmpRectF);
2526 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002527 }
2528
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002529 static int deltaRotation(int oldRotation, int newRotation) {
2530 int delta = newRotation - oldRotation;
2531 if (delta < 0) delta += 4;
2532 return delta;
2533 }
2534
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002535 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002536 Matrix outMatrix) {
2537 // For rotations without Z-ordering we don't need the target rectangle's position.
2538 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2539 displayHeight, outMatrix);
2540 }
2541
2542 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2543 float displayWidth, float displayHeight, Matrix outMatrix) {
2544 switch (rotation) {
2545 case ROTATION_0:
2546 outMatrix.reset();
2547 break;
2548 case ROTATION_270:
2549 outMatrix.setRotate(270, 0, 0);
2550 outMatrix.postTranslate(0, displayHeight);
2551 outMatrix.postTranslate(rectTop, 0);
2552 break;
2553 case ROTATION_180:
2554 outMatrix.reset();
2555 break;
2556 case ROTATION_90:
2557 outMatrix.setRotate(90, 0, 0);
2558 outMatrix.postTranslate(displayWidth, 0);
2559 outMatrix.postTranslate(-rectTop, rectLeft);
2560 break;
2561 }
2562 }
2563
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002564 @CallSuper
2565 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002566 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002567 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002568 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002569 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002570 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2571 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002572 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002573 }
2574 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2575 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002576 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2577 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002578 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002579 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002580 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2581 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002582 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002583 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002584 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2585 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002586 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002587 }
2588 proto.write(DPI, mBaseDisplayDensity);
2589 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002590 proto.write(ROTATION, mRotation);
2591 final ScreenRotationAnimation screenRotationAnimation =
2592 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2593 if (screenRotationAnimation != null) {
2594 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2595 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002596 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
lumark588a3e82018-07-20 18:53:54 +08002597 mAppTransition.writeToProto(proto, APP_TRANSITION);
Louis Chang7501e332018-08-20 13:08:39 +08002598 proto.write(SURFACE_SIZE, mSurfaceSize);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002599 if (mFocusedApp != null) {
2600 mFocusedApp.writeNameToProto(proto, FOCUSED_APP);
2601 }
Steven Timotiusaf03df62017-07-18 16:56:43 -07002602 proto.end(token);
2603 }
2604
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002605 @Override
2606 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2607 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002608 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2609 final String subPrefix = " " + prefix;
2610 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2611 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2612 pw.print("dpi");
2613 if (mInitialDisplayWidth != mBaseDisplayWidth
2614 || mInitialDisplayHeight != mBaseDisplayHeight
2615 || mInitialDisplayDensity != mBaseDisplayDensity) {
2616 pw.print(" base=");
2617 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2618 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2619 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002620 if (mDisplayScalingDisabled) {
2621 pw.println(" noscale");
2622 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002623 pw.print(" cur=");
2624 pw.print(mDisplayInfo.logicalWidth);
2625 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2626 pw.print(" app=");
2627 pw.print(mDisplayInfo.appWidth);
2628 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2629 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2630 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2631 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2632 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002633 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002634 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002635 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002636
Craig Mautnerdc548482014-02-05 13:35:24 -08002637 pw.println();
Adrian Roos5251b1d2018-03-23 18:57:43 +01002638 pw.print(prefix); pw.print("mLayoutSeq="); pw.println(mLayoutSeq);
Riddle Hsu654a6f92018-07-13 22:59:36 +08002639 pw.print(prefix);
2640 pw.print("mDeferredRotationPauseCount="); pw.println(mDeferredRotationPauseCount);
Adrian Roos5251b1d2018-03-23 18:57:43 +01002641
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002642 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
2643 if (mLastFocus != mCurrentFocus) {
2644 pw.print(" mLastFocus="); pw.println(mLastFocus);
2645 }
2646 if (mLosingFocus.size() > 0) {
2647 pw.println();
2648 pw.println(" Windows losing focus:");
2649 for (int i = mLosingFocus.size() - 1; i >= 0; i--) {
2650 final WindowState w = mLosingFocus.get(i);
2651 pw.print(" Losing #"); pw.print(i); pw.print(' ');
2652 pw.print(w);
2653 if (dumpAll) {
2654 pw.println(":");
2655 w.dump(pw, " ", true);
2656 } else {
2657 pw.println();
2658 }
2659 }
2660 }
2661 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
2662
Adrian Roos5251b1d2018-03-23 18:57:43 +01002663 pw.println();
wilsonshihc32538e2018-11-07 17:27:34 +08002664 mWallpaperController.dump(pw, " ");
2665
2666 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002667 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002668 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2669 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002670 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002671 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002672
Craig Mautnerdc548482014-02-05 13:35:24 -08002673 pw.println();
2674 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002675 pw.println();
2676 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002677 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002678 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002679 pw.print(" Exiting #"); pw.print(i);
2680 pw.print(' '); pw.print(token);
2681 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002682 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002683 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002684 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002685
Jorim Jaggi31f71702016-05-04 16:43:04 -07002686 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002687
2688 // Dump stack references
2689 final TaskStack homeStack = getHomeStack();
2690 if (homeStack != null) {
2691 pw.println(prefix + "homeStack=" + homeStack.getName());
2692 }
2693 final TaskStack pinnedStack = getPinnedStack();
2694 if (pinnedStack != null) {
2695 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2696 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002697 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002698 if (splitScreenPrimaryStack != null) {
2699 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2700 }
2701
2702 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002703 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002704 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002705 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002706
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002707 pw.println();
2708 mDisplayFrames.dump(prefix, pw);
Arthur Hung95b38a92018-07-20 18:56:12 +08002709 pw.println();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08002710 mDisplayPolicy.dump(prefix, pw);
2711 pw.println();
Riddle Hsuad256a12018-07-18 16:11:30 +08002712 mDisplayRotation.dump(prefix, pw);
2713 pw.println();
Arthur Hung95b38a92018-07-20 18:56:12 +08002714 mInputMonitor.dump(pw, " ");
Craig Mautner59c00972012-07-30 12:10:24 -07002715 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002716
2717 @Override
2718 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002719 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002720 }
2721
2722 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002723 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002724 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002725
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002726 /** Returns true if the stack in the windowing mode is visible. */
2727 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002728 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002729 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002730 }
2731
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002732 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002733 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002734 final int x = (int) xf;
2735 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002736 final WindowState touchedWin = getWindow(w -> {
2737 final int flags = w.mAttrs.flags;
2738 if (!w.isVisibleLw()) {
2739 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002740 }
2741 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002742 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002743 }
2744
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002745 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002746 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002747 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002748 }
2749
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002750 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002751
2752 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002753 return mTmpRegion.contains(x, y) || touchFlags == 0;
2754 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002755
2756 return touchedWin;
2757 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002758
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002759 boolean canAddToastWindowForUid(int uid) {
2760 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002761 // Also if the app is focused adding more than one toast at
2762 // a time for better backwards compatibility.
2763 final WindowState focusedWindowForUid = getWindow(w ->
2764 w.mOwnerUid == uid && w.isFocused());
2765 if (focusedWindowForUid != null) {
2766 return true;
2767 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002768 final WindowState win = getWindow(w ->
2769 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2770 && !w.mWindowRemovalAllowed);
2771 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002772 }
2773
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002774 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002775 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2776 return;
2777 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002778
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002779 // Used to communicate the old focus to the callback method.
2780 mTmpWindow = oldFocus;
2781
2782 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002783 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002784
2785 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002786 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002787
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002788 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002789
2790 if (mTmpWindow == null) {
2791 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2792 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002793 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002794 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002795 }
2796
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002797
2798 /**
2799 * Update the focused window and make some adjustments if the focus has changed.
2800 *
2801 * @param mode Indicates the situation we are in. Possible modes are:
2802 * {@link WindowManagerService#UPDATE_FOCUS_NORMAL},
2803 * {@link WindowManagerService#UPDATE_FOCUS_PLACING_SURFACES},
2804 * {@link WindowManagerService#UPDATE_FOCUS_WILL_PLACE_SURFACES},
2805 * {@link WindowManagerService#UPDATE_FOCUS_REMOVING_FOCUS}
2806 * @param updateInputWindows Whether to sync the window information to the input module.
2807 * @return {@code true} if the focused window has changed.
2808 */
2809 boolean updateFocusedWindowLocked(int mode, boolean updateInputWindows, boolean focusFound) {
2810 final WindowState newFocus = findFocusedWindow();
2811 if (mCurrentFocus == newFocus) {
2812 return false;
2813 }
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002814 boolean imWindowChanged = false;
2815 // TODO (b/111080190): Multi-Session IME
2816 if (!focusFound) {
2817 final WindowState imWindow = mInputMethodWindow;
2818 if (imWindow != null) {
lumarkff0ab692018-11-05 20:32:30 +08002819 final WindowState prevTarget = mInputMethodTarget;
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002820
2821 final WindowState newTarget = computeImeTarget(true /* updateImeTarget*/);
2822 imWindowChanged = prevTarget != newTarget;
2823
2824 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS
2825 && mode != UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2826 assignWindowLayers(false /* setLayoutNeeded */);
2827 }
2828 }
2829 }
2830
2831 if (imWindowChanged) {
2832 mService.mWindowsChanged = true;
2833 setLayoutNeeded();
2834 }
2835
2836 if (DEBUG_FOCUS_LIGHT || mService.localLOGV) Slog.v(TAG_WM, "Changing focus from "
2837 + mCurrentFocus + " to " + newFocus + " displayId=" + getDisplayId()
2838 + " Callers=" + Debug.getCallers(4));
2839 final WindowState oldFocus = mCurrentFocus;
2840 mCurrentFocus = newFocus;
2841 mLosingFocus.remove(newFocus);
2842
2843 if (newFocus != null) {
2844 mWinAddedSinceNullFocus.clear();
2845 mWinRemovedSinceNullFocus.clear();
2846
2847 if (newFocus.canReceiveKeys()) {
2848 // Displaying a window implicitly causes dispatching to be unpaused.
2849 // This is to protect against bugs if someone pauses dispatching but
2850 // forgets to resume.
2851 newFocus.mToken.paused = false;
2852 }
2853 }
2854
2855 // System UI is only shown on the default display.
2856 int focusChanged = isDefaultDisplay
2857 ? mService.mPolicy.focusChangedLw(oldFocus, newFocus) : 0;
2858
2859 if (imWindowChanged && oldFocus != mInputMethodWindow) {
2860 // Focus of the input method window changed. Perform layout if needed.
2861 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2862 performLayout(true /*initial*/, updateInputWindows);
2863 focusChanged &= ~FINISH_LAYOUT_REDO_LAYOUT;
2864 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2865 // Client will do the layout, but we need to assign layers
2866 // for handleNewWindowLocked() below.
2867 assignWindowLayers(false /* setLayoutNeeded */);
2868 }
2869 }
2870
2871 if ((focusChanged & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2872 // The change in focus caused us to need to do a layout. Okay.
2873 setLayoutNeeded();
2874 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2875 performLayout(true /*initial*/, updateInputWindows);
2876 } else if (mode == UPDATE_FOCUS_REMOVING_FOCUS) {
2877 mService.mRoot.performSurfacePlacement(false);
2878 }
2879 }
2880
2881 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
2882 // If we defer assigning layers, then the caller is responsible for doing this part.
2883 getInputMonitor().setInputFocusLw(newFocus, updateInputWindows);
2884 }
2885
2886 adjustForImeIfNeeded();
2887
2888 // We may need to schedule some toast windows to be removed. The toasts for an app that
2889 // does not have input focus are removed within a timeout to prevent apps to redress
2890 // other apps' UI.
2891 scheduleToastWindowsTimeoutIfNeededLocked(oldFocus, newFocus);
2892
2893 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2894 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
2895 }
2896 return true;
2897 }
2898
2899 /**
2900 * Set the new focused app to this display.
2901 *
2902 * @param newFocus the new focused AppWindowToken.
2903 * @return true if the focused app is changed.
2904 */
2905 boolean setFocusedApp(AppWindowToken newFocus) {
2906 if (newFocus != null) {
2907 final DisplayContent appDisplay = newFocus.getDisplayContent();
2908 if (appDisplay != this) {
2909 throw new IllegalStateException(newFocus + " is not on " + getName()
2910 + " but " + ((appDisplay != null) ? appDisplay.getName() : "none"));
2911 }
2912 }
2913 if (mFocusedApp == newFocus) {
2914 return false;
2915 }
2916 mFocusedApp = newFocus;
2917 getInputMonitor().setFocusedAppLw(newFocus);
2918 updateTouchExcludeRegion();
2919 return true;
2920 }
2921
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002922 /** Updates the layer assignment of windows on this display. */
2923 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002924 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07002925 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002926 if (setLayoutNeeded) {
2927 setLayoutNeeded();
2928 }
Robert Carrb1579c82017-09-05 14:54:47 -07002929
Robert Carrf59b8dd2017-10-02 18:58:36 -07002930 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002931 // the application of this transaction until the animation pass triggers
2932 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2933 // the hiding and showing of surfaces.
2934 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002935 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002936 }
2937
Wale Ogunwale1666e312016-12-16 11:27:18 -08002938 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2939 // moving containers or resizing them. Need to investigate the best way to have it automatically
2940 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002941 void layoutAndAssignWindowLayersIfNeeded() {
2942 mService.mWindowsChanged = true;
2943 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002944
2945 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2946 false /*updateInputWindows*/)) {
2947 assignWindowLayers(false /* setLayoutNeeded */);
2948 }
2949
Arthur Hung95b38a92018-07-20 18:56:12 +08002950 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002951 mService.mWindowPlacerLocked.performSurfacePlacement();
Arthur Hung95b38a92018-07-20 18:56:12 +08002952 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002953 }
2954
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002955 /** Returns true if a leaked surface was destroyed */
2956 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002957 // Used to indicate that a surface was leaked.
2958 mTmpWindow = null;
2959 forAllWindows(w -> {
2960 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002961 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002962 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002963 }
2964 if (!mService.mSessions.contains(wsa.mSession)) {
2965 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002966 + w + " surface=" + wsa.mSurfaceController
2967 + " token=" + w.mToken
2968 + " pid=" + w.mSession.mPid
2969 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002970 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002971 mService.mForceRemoves.add(w);
2972 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002973 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002974 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002975 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002976 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002977 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002978 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002979 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002980 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002981 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002982
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002983 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002984 }
2985
2986 /**
lumark90120a82018-08-15 00:33:03 +08002987 * Set input method window for the display.
2988 * @param win Set when window added or Null when destroyed.
2989 */
2990 void setInputMethodWindowLocked(WindowState win) {
2991 mInputMethodWindow = win;
lumark70865a82018-09-17 16:56:26 +08002992 // Update display configuration for IME process.
2993 if (mInputMethodWindow != null) {
2994 final int imePid = mInputMethodWindow.mSession.mPid;
2995 mService.mAtmInternal.onImeWindowSetOnDisplay(imePid,
2996 mInputMethodWindow.getDisplayId());
2997 }
lumark90120a82018-08-15 00:33:03 +08002998 computeImeTarget(true /* updateImeTarget */);
2999 }
3000
3001 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003002 * Determine and return the window that should be the IME target.
3003 * @param updateImeTarget If true the system IME target will be updated to match what we found.
3004 * @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 +00003005 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003006 WindowState computeImeTarget(boolean updateImeTarget) {
lumark90120a82018-08-15 00:33:03 +08003007 if (mInputMethodWindow == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003008 // There isn't an IME so there shouldn't be a target...That was easy!
3009 if (updateImeTarget) {
3010 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
lumarkff0ab692018-11-05 20:32:30 +08003011 + mInputMethodTarget + " to null since mInputMethodWindow is null");
3012 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003013 }
3014 return null;
3015 }
3016
lumarkff0ab692018-11-05 20:32:30 +08003017 final WindowState curTarget = mInputMethodTarget;
Chavi Weingarten3a748552018-05-14 17:32:42 +00003018 if (!canUpdateImeTarget()) {
3019 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Defer updating IME target");
3020 return curTarget;
3021 }
3022
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003023 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
3024 // same display. Or even when the current IME/target are not on the same screen as the next
3025 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08003026 mUpdateImeTarget = updateImeTarget;
3027 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003028
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003029
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003030 // Yet more tricksyness! If this window is a "starting" window, we do actually want
3031 // to be on top of it, but it is not -really- where input will go. So look down below
3032 // for a real window to target...
3033 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
3034 final AppWindowToken token = target.mAppToken;
3035 if (token != null) {
3036 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
3037 if (betterTarget != null) {
3038 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003039 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003040 }
3041 }
3042
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003043 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
lumarkff0ab692018-11-05 20:32:30 +08003044 "Proposed new IME target: " + target + " for display: " + getDisplayId());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003045
chaviw1c13ad32018-06-12 16:44:23 -07003046 // Now, a special case -- if the last target's window is in the process of exiting, but
3047 // not removed, and the new target is home, keep on the last target to avoid flicker.
3048 // Home is a special case since its above other stacks in the ordering list, but layed
3049 // out below the others.
3050 if (curTarget != null && !curTarget.mRemoved && curTarget.isDisplayedLw()
3051 && curTarget.isClosing() && (target == null || target.isActivityTypeHome())) {
chaviw4ad54912018-05-30 11:05:44 -07003052 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "New target is home while current target is "
Chavi Weingarten3a748552018-05-14 17:32:42 +00003053 + "closing, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003054 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003055 }
3056
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003057 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
3058 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003059
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003060 if (target == null) {
3061 if (updateImeTarget) {
3062 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
3063 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
3064 + Debug.getCallers(4) : ""));
lumarkff0ab692018-11-05 20:32:30 +08003065 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003066 }
3067
3068 return null;
3069 }
3070
3071 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003072 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
3073 if (token != null) {
3074
3075 // Now some fun for dealing with window animations that modify the Z order. We need
3076 // to look at all windows below the current target that are in this app, finding the
3077 // highest visible one in layering.
3078 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003079 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003080 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003081 }
3082
3083 if (highestTarget != null) {
lumark588a3e82018-07-20 18:53:54 +08003084 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, mAppTransition + " " + highestTarget
Jorim Jaggi35d328a2018-08-14 17:00:20 +02003085 + " animating=" + highestTarget.isAnimating());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003086
lumark588a3e82018-07-20 18:53:54 +08003087 if (mAppTransition.isTransitionSet()) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003088 // If we are currently setting up for an animation, hold everything until we
3089 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003090 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003091 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003092 }
3093 }
3094 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003095
3096 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
3097 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003098 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003099 }
3100
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003101 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003102 }
3103
lumarkff0ab692018-11-05 20:32:30 +08003104 /**
3105 * Calling {@link #computeImeTarget(boolean)} to update the input method target window in
3106 * the candidate app window token if needed.
3107 */
3108 void computeImeTargetIfNeeded(AppWindowToken candidate) {
3109 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == candidate) {
3110 computeImeTarget(true /* updateImeTarget */);
3111 }
3112 }
3113
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003114 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
lumarkff0ab692018-11-05 20:32:30 +08003115 if (target == mInputMethodTarget && mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003116 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003117 }
3118
lumarkff0ab692018-11-05 20:32:30 +08003119 mInputMethodTarget = target;
3120 mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003121 assignWindowLayers(false /* setLayoutNeeded */);
3122 }
3123
3124 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
3125 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3126 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
3127 }
3128
3129 // Used to indicate we have reached the first window in the range we are interested in.
3130 mTmpWindow = null;
3131
3132 // TODO: Figure-out a more efficient way to do this.
3133 final WindowState candidate = getWindow(w -> {
3134 if (w == top) {
3135 // Reached the first window in the range we are interested in.
3136 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003137 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003138 if (mTmpWindow == null) {
3139 return false;
3140 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003141
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003142 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3143 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003144 }
3145 // If we reached the bottom of the range of windows we are considering,
3146 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003147 if (w == bottom) {
3148 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003149 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003150 return false;
3151 });
3152
3153 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003154 }
3155
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003156 void setLayoutNeeded() {
3157 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
3158 mLayoutNeeded = true;
3159 }
3160
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003161 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003162 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
3163 mLayoutNeeded = false;
3164 }
3165
3166 boolean isLayoutNeeded() {
3167 return mLayoutNeeded;
3168 }
3169
Wale Ogunwale02319a62016-09-26 15:21:22 -07003170 void dumpTokens(PrintWriter pw, boolean dumpAll) {
3171 if (mTokenMap.isEmpty()) {
3172 return;
3173 }
3174 pw.println(" Display #" + mDisplayId);
3175 final Iterator<WindowToken> it = mTokenMap.values().iterator();
3176 while (it.hasNext()) {
3177 final WindowToken token = it.next();
3178 pw.print(" ");
3179 pw.print(token);
3180 if (dumpAll) {
3181 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003182 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07003183 } else {
3184 pw.println();
3185 }
3186 }
lumark588a3e82018-07-20 18:53:54 +08003187
3188 if (!mOpeningApps.isEmpty() || !mClosingApps.isEmpty()) {
3189 pw.println();
3190 if (mOpeningApps.size() > 0) {
3191 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
3192 }
3193 if (mClosingApps.size() > 0) {
3194 pw.print(" mClosingApps="); pw.println(mClosingApps);
3195 }
3196 }
3197
3198 mUnknownAppVisibilityController.dump(pw, " ");
Wale Ogunwale02319a62016-09-26 15:21:22 -07003199 }
3200
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003201 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003202 final int[] index = new int[1];
3203 forAllWindows(w -> {
3204 final WindowStateAnimator wAnim = w.mWinAnimator;
3205 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
3206 index[0] = index[0] + 1;
3207 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003208 }
3209
Jorim Jaggife762342016-10-13 14:33:27 +02003210 /**
3211 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
3212 */
3213 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
3214 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003215 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02003216 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
3217 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01003218 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02003219 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003220 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02003221 }
3222
Wale Ogunwale494009b82016-10-21 09:01:38 -07003223 boolean checkWaitingForWindows() {
3224
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003225 mHaveBootMsg = false;
3226 mHaveApp = false;
3227 mHaveWallpaper = false;
3228 mHaveKeyguard = true;
3229
3230 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003231 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
3232 return true;
3233 }
3234 if (w.isDrawnLw()) {
3235 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003236 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003237 } else if (w.mAttrs.type == TYPE_APPLICATION
3238 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003239 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003240 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003241 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003242 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003243 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003244 }
3245 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003246 return false;
3247 });
3248
3249 if (visibleWindow != null) {
3250 // We have a visible window.
3251 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003252 }
3253
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003254 // if the wallpaper service is disabled on the device, we're never going to have
3255 // wallpaper, don't bother waiting for it
3256 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
3257 com.android.internal.R.bool.config_enableWallpaperService)
Daichi Hironoc1432222018-03-29 13:06:07 +09003258 && mService.mContext.getResources().getBoolean(
3259 com.android.internal.R.bool.config_checkWallpaperAtBoot)
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003260 && !mService.mOnlyCore;
3261
Wale Ogunwale494009b82016-10-21 09:01:38 -07003262 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
3263 "******** booted=" + mService.mSystemBooted
3264 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003265 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
3266 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
3267 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003268
3269 // If we are turning on the screen to show the boot message, don't do it until the boot
3270 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003271 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003272 return true;
3273 }
3274
3275 // If we are turning on the screen after the boot is completed normally, don't do so until
3276 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003277 if (mService.mSystemBooted
3278 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003279 return true;
3280 }
3281
3282 return false;
3283 }
3284
Jorim Jaggi8f520872018-08-14 17:00:20 +02003285 void updateWindowsForAnimator() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003286 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003287 }
3288
Jorim Jaggi8f520872018-08-14 17:00:20 +02003289 /**
3290 * Updates the {@link TaskStack#setAnimationBackground} for all windows.
3291 */
3292 void updateBackgroundForAnimator() {
Jorim Jaggi7156b652016-10-25 08:31:58 -07003293 resetAnimationBackgroundAnimator();
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003294 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003295 }
3296
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003297 boolean isInputMethodClientFocus(int uid, int pid) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003298 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003299 if (imFocus == null) {
3300 return false;
3301 }
3302
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003303 if (DEBUG_INPUT_METHOD) {
3304 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003305 Slog.i(TAG_WM, "Current focus: " + mCurrentFocus + " displayId=" + mDisplayId);
3306 Slog.i(TAG_WM, "Last focus: " + mLastFocus + " displayId=" + mDisplayId);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003307 }
3308
Wale Ogunwale494009b82016-10-21 09:01:38 -07003309 if (DEBUG_INPUT_METHOD) {
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003310 Slog.i(TAG_WM, "IM target uid/pid: " + imFocus.mSession.mUid
3311 + "/" + imFocus.mSession.mPid);
3312 Slog.i(TAG_WM, "Requesting client uid/pid: " + uid + "/" + pid);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003313 }
3314
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003315 return imFocus.mSession.mUid == uid && imFocus.mSession.mPid == pid;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003316 }
3317
3318 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003319 final WindowState win = getWindow(
3320 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
3321 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003322 }
3323
3324 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003325 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003326 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003327 final int curValue = w.mSystemUiVisibility;
3328 final int diff = (curValue ^ visibility) & globalDiff;
3329 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003330 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003331 w.mSeq++;
3332 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003333 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003334 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
3335 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07003336 visibility, newValue, diff);
3337 }
3338 } catch (RemoteException e) {
3339 // so sorry
3340 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003341 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003342 }
3343
3344 void onWindowFreezeTimeout() {
3345 Slog.w(TAG_WM, "Window freeze timeout expired.");
3346 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003347
3348 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07003349 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003350 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003351 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07003352 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003353 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
3354 - mService.mDisplayFreezeTime);
3355 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003356 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003357 mService.mWindowPlacerLocked.performSurfacePlacement();
3358 }
3359
3360 void waitForAllWindowsDrawn() {
3361 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003362 forAllWindows(w -> {
3363 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
3364 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
3365 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003366 // Force add to mResizingWindows.
chaviw9c81e632018-07-31 11:17:52 -07003367 w.resetLastContentInsets();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003368 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003369 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003370 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003371 }
3372
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003373 // TODO: Super crazy long method that should be broken down...
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003374 void applySurfaceChangesTransaction(boolean recoveringMemory) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003375
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003376 final int dw = mDisplayInfo.logicalWidth;
3377 final int dh = mDisplayInfo.logicalHeight;
3378 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
3379
3380 mTmpUpdateAllDrawn.clear();
3381
3382 int repeats = 0;
3383 do {
3384 repeats++;
3385 if (repeats > 6) {
3386 Slog.w(TAG, "Animation repeat aborted after too many iterations");
3387 clearLayoutNeeded();
3388 break;
3389 }
3390
3391 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
3392 pendingLayoutChanges);
3393
wilsonshihc32538e2018-11-07 17:27:34 +08003394 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
3395 mWallpaperController.adjustWallpaperWindows();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003396 }
3397
Riddle Hsu654a6f92018-07-13 22:59:36 +08003398 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003399 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
Riddle Hsu4e611772018-10-31 18:58:28 +08003400 if (updateOrientationFromAppTokens()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003401 setLayoutNeeded();
Riddle Hsu4e611772018-10-31 18:58:28 +08003402 sendNewConfiguration();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003403 }
3404 }
3405
3406 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
3407 setLayoutNeeded();
3408 }
3409
3410 // FIRST LOOP: Perform a layout, if needed.
3411 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
3412 performLayout(repeats == 1, false /* updateInputWindows */);
3413 } else {
3414 Slog.w(TAG, "Layout repeat skipped after too many iterations");
3415 }
3416
3417 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
3418 pendingLayoutChanges = 0;
3419
3420 if (isDefaultDisplay) {
3421 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003422 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003423 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
3424 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
3425 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
3426 }
3427 } while (pendingLayoutChanges != 0);
3428
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003429 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003430
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003431 mTmpRecoveringMemory = recoveringMemory;
3432 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01003433 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003434
Jorim Jaggif1292892018-09-10 11:58:13 +02003435 mLastHasContent = mTmpApplySurfaceChangesTransactionState.displayHasContent;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003436 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Jorim Jaggif1292892018-09-10 11:58:13 +02003437 mLastHasContent,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003438 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
3439 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003440 true /* inTraversal, must call performTraversalInTrans... below */);
3441
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003442 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
3443 if (wallpaperVisible != mLastWallpaperVisible) {
3444 mLastWallpaperVisible = wallpaperVisible;
3445 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
3446 }
3447
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003448 while (!mTmpUpdateAllDrawn.isEmpty()) {
3449 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
3450 // See if any windows have been drawn, so they (and others associated with them)
3451 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07003452 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003453 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003454 }
3455
Bryce Leef3c6a472017-11-14 14:53:06 -08003456 private void updateBounds() {
Evan Rosky39b6f232018-10-30 18:35:41 -07003457 calculateBounds(mDisplayInfo, mTmpBounds);
Bryce Leef3c6a472017-11-14 14:53:06 -08003458 setBounds(mTmpBounds);
3459 }
3460
3461 // Determines the current display bounds based on the current state
Evan Rosky39b6f232018-10-30 18:35:41 -07003462 private void calculateBounds(DisplayInfo displayInfo, Rect out) {
Bryce Leef3c6a472017-11-14 14:53:06 -08003463 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Evan Rosky39b6f232018-10-30 18:35:41 -07003464 final int rotation = displayInfo.rotation;
3465 boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Bryce Leef3c6a472017-11-14 14:53:06 -08003466 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
3467 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Evan Rosky39b6f232018-10-30 18:35:41 -07003468 int width = displayInfo.logicalWidth;
Bryce Leef3c6a472017-11-14 14:53:06 -08003469 int left = (physWidth - width) / 2;
Evan Rosky39b6f232018-10-30 18:35:41 -07003470 int height = displayInfo.logicalHeight;
Bryce Leef3c6a472017-11-14 14:53:06 -08003471 int top = (physHeight - height) / 2;
3472 out.set(left, top, left + width, top + height);
3473 }
3474
3475 @Override
3476 public void getBounds(Rect out) {
Evan Rosky39b6f232018-10-30 18:35:41 -07003477 calculateBounds(mDisplayInfo, out);
Bryce Leef3c6a472017-11-14 14:53:06 -08003478 }
3479
3480 private void getBounds(Rect out, int orientation) {
3481 getBounds(out);
3482
3483 // Rotate the Rect if needed.
3484 final int currentRotation = mDisplayInfo.rotation;
3485 final int rotationDelta = deltaRotation(currentRotation, orientation);
3486 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
3487 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
3488 mTmpRectF.set(out);
3489 mTmpMatrix.mapRect(mTmpRectF);
3490 mTmpRectF.round(out);
3491 }
3492 }
3493
Louis Chang7501e332018-08-20 13:08:39 +08003494 int getSurfaceSize() {
3495 return mSurfaceSize;
3496 }
3497
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003498 void performLayout(boolean initial, boolean updateInputWindows) {
3499 if (!isLayoutNeeded()) {
3500 return;
3501 }
3502 clearLayoutNeeded();
3503
3504 final int dw = mDisplayInfo.logicalWidth;
3505 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003506 if (DEBUG_LAYOUT) {
3507 Slog.v(TAG, "-------------------------------------");
3508 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
3509 }
3510
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01003511 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
3512 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00003513 // TODO: Not sure if we really need to set the rotation here since we are updating from the
3514 // display info above...
3515 mDisplayFrames.mRotation = mRotation;
3516 mService.mPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003517 if (isDefaultDisplay) {
3518 // Not needed on non-default displays.
3519 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
3520 mService.mScreenRect.set(0, 0, dw, dh);
3521 }
3522
Adrian Roos5251b1d2018-03-23 18:57:43 +01003523 int seq = mLayoutSeq + 1;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003524 if (seq < 0) seq = 0;
Adrian Roos5251b1d2018-03-23 18:57:43 +01003525 mLayoutSeq = seq;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003526
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003527 // Used to indicate that we have processed the dream window and all additional windows are
3528 // behind it.
3529 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003530 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003531
3532 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003533 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003534
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003535 // Used to indicate that we have processed the dream window and all additional attached
3536 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003537 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003538 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003539
3540 // Now perform layout of attached windows, which usually depend on the position of the
3541 // window they are attached to. XXX does not deal with windows that are attached to windows
3542 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003543 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003544
3545 // Window frames may have changed. Tell the input dispatcher about it.
Arthur Hung95b38a92018-07-20 18:56:12 +08003546 mInputMonitor.layoutInputConsumers(dw, dh);
3547 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003548 if (updateInputWindows) {
Arthur Hung95b38a92018-07-20 18:56:12 +08003549 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003550 }
3551
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003552 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
3553 }
3554
3555 /**
3556 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
3557 * In portrait mode, it grabs the full screenshot.
3558 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003559 * @param config of the output bitmap
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003560 */
chaviw0315a1a2018-03-05 15:28:35 -08003561 Bitmap screenshotDisplayLocked(Bitmap.Config config) {
3562 if (!mService.mPolicy.isScreenOn()) {
3563 if (DEBUG_SCREENSHOT) {
3564 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003565 }
chaviw0315a1a2018-03-05 15:28:35 -08003566 return null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003567 }
chaviwfbe47df2017-11-10 16:14:49 -08003568
chaviw0315a1a2018-03-05 15:28:35 -08003569 int dw = mDisplayInfo.logicalWidth;
3570 int dh = mDisplayInfo.logicalHeight;
chaviwfbe47df2017-11-10 16:14:49 -08003571
chaviw0315a1a2018-03-05 15:28:35 -08003572 if (dw <= 0 || dh <= 0) {
3573 return null;
3574 }
chaviwfbe47df2017-11-10 16:14:49 -08003575
chaviw0315a1a2018-03-05 15:28:35 -08003576 final Rect frame = new Rect(0, 0, dw, dh);
chaviwfbe47df2017-11-10 16:14:49 -08003577
chaviw0315a1a2018-03-05 15:28:35 -08003578 // The screenshot API does not apply the current screen rotation.
3579 int rot = mDisplay.getRotation();
chaviwfbe47df2017-11-10 16:14:49 -08003580
chaviw0315a1a2018-03-05 15:28:35 -08003581 if (rot == ROTATION_90 || rot == ROTATION_270) {
3582 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3583 }
chaviwfbe47df2017-11-10 16:14:49 -08003584
chaviw0315a1a2018-03-05 15:28:35 -08003585 // SurfaceFlinger is not aware of orientation, so convert our logical
3586 // crop to SurfaceFlinger's portrait orientation.
3587 convertCropForSurfaceFlinger(frame, rot, dw, dh);
3588
3589 final ScreenRotationAnimation screenRotationAnimation =
3590 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3591 final boolean inRotation = screenRotationAnimation != null &&
3592 screenRotationAnimation.isAnimating();
3593 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
3594
3595 // TODO(b/68392460): We should screenshot Task controls directly
3596 // but it's difficult at the moment as the Task doesn't have the
3597 // correct size set.
chaviw08520a02018-09-10 16:44:56 -07003598 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, inRotation, rot);
chaviw0315a1a2018-03-05 15:28:35 -08003599 if (bitmap == null) {
3600 Slog.w(TAG_WM, "Failed to take screenshot");
3601 return null;
3602 }
3603
3604 // Create a copy of the screenshot that is immutable and backed in ashmem.
3605 // This greatly reduces the overhead of passing the bitmap between processes.
3606 final Bitmap ret = bitmap.createAshmemBitmap(config);
3607 bitmap.recycle();
3608 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003609 }
3610
3611 // TODO: Can this use createRotationMatrix()?
3612 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3613 if (rot == Surface.ROTATION_90) {
3614 final int tmp = crop.top;
3615 crop.top = dw - crop.right;
3616 crop.right = crop.bottom;
3617 crop.bottom = dw - crop.left;
3618 crop.left = tmp;
3619 } else if (rot == Surface.ROTATION_180) {
3620 int tmp = crop.top;
3621 crop.top = dh - crop.bottom;
3622 crop.bottom = dh - tmp;
3623 tmp = crop.right;
3624 crop.right = dw - crop.left;
3625 crop.left = dw - tmp;
3626 } else if (rot == Surface.ROTATION_270) {
3627 final int tmp = crop.top;
3628 crop.top = crop.left;
3629 crop.left = dh - crop.bottom;
3630 crop.bottom = crop.right;
3631 crop.right = dh - tmp;
3632 }
3633 }
3634
3635 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003636 // Used to indicate the layout is needed.
3637 mTmpWindow = null;
3638
3639 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003640 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003641 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003642 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003643 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003644 w.setDisplayLayoutNeeded();
3645 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003646 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003647
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003648 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003649 mService.mWindowPlacerLocked.performSurfacePlacement();
3650 }
3651 }
3652
Wale Ogunwale1666e312016-12-16 11:27:18 -08003653 void setExitingTokensHasVisible(boolean hasVisible) {
3654 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3655 mExitingTokens.get(i).hasVisible = hasVisible;
3656 }
3657
3658 // Initialize state of exiting applications.
3659 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3660 }
3661
3662 void removeExistingTokensIfPossible() {
3663 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3664 final WindowToken token = mExitingTokens.get(i);
3665 if (!token.hasVisible) {
3666 mExitingTokens.remove(i);
3667 }
3668 }
3669
3670 // Time to remove any exiting applications?
3671 mTaskStackContainers.removeExistingAppTokensIfPossible();
3672 }
3673
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003674 @Override
3675 void onDescendantOverrideConfigurationChanged() {
3676 setLayoutNeeded();
3677 mService.requestTraversal();
3678 }
3679
David Stevens9440dc82017-03-16 19:00:20 -07003680 boolean okToDisplay() {
3681 if (mDisplayId == DEFAULT_DISPLAY) {
3682 return !mService.mDisplayFrozen
3683 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3684 }
3685 return mDisplayInfo.state == Display.STATE_ON;
3686 }
3687
3688 boolean okToAnimate() {
3689 return okToDisplay() &&
3690 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3691 }
3692
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003693 static final class TaskForResizePointSearchResult {
3694 boolean searchDone;
3695 Task taskForResize;
3696
3697 void reset() {
3698 searchDone = false;
3699 taskForResize = null;
3700 }
3701 }
Robert Carr3b716242016-08-16 16:02:21 -07003702
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003703 private static final class ApplySurfaceChangesTransactionState {
3704 boolean displayHasContent;
3705 boolean obscured;
3706 boolean syswin;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003707 float preferredRefreshRate;
3708 int preferredModeId;
3709
3710 void reset() {
3711 displayHasContent = false;
3712 obscured = false;
3713 syswin = false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003714 preferredRefreshRate = 0;
3715 preferredModeId = 0;
3716 }
3717 }
3718
3719 private static final class ScreenshotApplicationState {
3720 WindowState appWin;
3721 int maxLayer;
3722 int minLayer;
3723 boolean screenshotReady;
3724
3725 void reset(boolean screenshotReady) {
3726 appWin = null;
3727 maxLayer = 0;
3728 minLayer = 0;
3729 this.screenshotReady = screenshotReady;
3730 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3731 }
3732 }
3733
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003734 /**
3735 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3736 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3737 * homogeneous children type which is currently required by sub-classes of
3738 * {@link WindowContainer} class.
3739 */
3740 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3741
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003742 DisplayChildWindowContainer(WindowManagerService service) {
3743 super(service);
3744 }
3745
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003746 @Override
3747 boolean fillsParent() {
3748 return true;
3749 }
3750
3751 @Override
3752 boolean isVisible() {
3753 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003754 }
3755 }
3756
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003757 /**
3758 * Window container class that contains all containers on this display relating to Apps.
3759 * I.e Activities.
3760 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003761 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003762 /**
3763 * A control placed at the appropriate level for transitions to occur.
3764 */
chaviw23ee71c2017-12-18 11:29:41 -08003765 SurfaceControl mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02003766 SurfaceControl mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02003767 SurfaceControl mHomeAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003768
Robert Carrf7a7ca82017-12-06 13:17:07 -08003769 /**
3770 * Given that the split-screen divider does not have an AppWindowToken, it
3771 * will have to live inside of a "NonAppWindowContainer", in particular
3772 * {@link DisplayContent#mAboveAppWindowsContainers}. However, in visual Z order
3773 * it will need to be interleaved with some of our children, appearing on top of
3774 * both docked stacks but underneath any assistant stacks.
3775 *
3776 * To solve this problem we have this anchor control, which will always exist so
3777 * we can always assign it the correct value in our {@link #assignChildLayers}.
3778 * Likewise since it always exists, {@link AboveAppWindowContainers} can always
3779 * assign the divider a layer relative to it. This way we prevent linking lifecycle
3780 * events between the two containers.
3781 */
3782 SurfaceControl mSplitScreenDividerAnchor = null;
3783
Wale Ogunwale61911492017-10-11 08:50:50 -07003784 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3785 // through the list to find them.
3786 private TaskStack mHomeStack = null;
3787 private TaskStack mPinnedStack = null;
3788 private TaskStack mSplitScreenPrimaryStack = null;
3789
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003790 TaskStackContainers(WindowManagerService service) {
3791 super(service);
3792 }
3793
Wale Ogunwale61911492017-10-11 08:50:50 -07003794 /**
3795 * Returns the topmost stack on the display that is compatible with the input windowing mode
3796 * and activity type. Null is no compatible stack on the display.
3797 */
3798 TaskStack getStack(int windowingMode, int activityType) {
3799 if (activityType == ACTIVITY_TYPE_HOME) {
3800 return mHomeStack;
3801 }
3802 if (windowingMode == WINDOWING_MODE_PINNED) {
3803 return mPinnedStack;
3804 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3805 return mSplitScreenPrimaryStack;
3806 }
3807 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3808 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003809 if (activityType == ACTIVITY_TYPE_UNDEFINED
3810 && windowingMode == stack.getWindowingMode()) {
3811 // Passing in undefined type means we want to match the topmost stack with the
3812 // windowing mode.
3813 return stack;
3814 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003815 if (stack.isCompatible(windowingMode, activityType)) {
3816 return stack;
3817 }
3818 }
3819 return null;
3820 }
3821
3822 @VisibleForTesting
3823 TaskStack getTopStack() {
3824 return mTaskStackContainers.getChildCount() > 0
3825 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3826 }
3827
3828 TaskStack getHomeStack() {
3829 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3830 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3831 }
3832 return mHomeStack;
3833 }
3834
3835 TaskStack getPinnedStack() {
3836 return mPinnedStack;
3837 }
3838
Matthew Ng64e77cf2017-10-31 14:01:31 -07003839 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003840 return mSplitScreenPrimaryStack;
3841 }
3842
Winson Chunge2d72172018-01-25 17:46:20 +00003843 ArrayList<Task> getVisibleTasks() {
3844 final ArrayList<Task> visibleTasks = new ArrayList<>();
3845 forAllTasks(task -> {
3846 if (task.isVisible()) {
3847 visibleTasks.add(task);
3848 }
3849 });
3850 return visibleTasks;
3851 }
3852
Andrii Kulian839def92016-11-02 10:58:58 -07003853 /**
3854 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003855 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003856 */
3857 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003858 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003859 addChild(stack, onTop);
3860 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003861 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003862
Wale Ogunwale61911492017-10-11 08:50:50 -07003863 void onStackWindowingModeChanged(TaskStack stack) {
3864 removeStackReferenceIfNeeded(stack);
3865 addStackReferenceIfNeeded(stack);
3866 if (stack == mPinnedStack && getTopStack() != stack) {
3867 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3868 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3869 }
3870 }
3871
3872 private void addStackReferenceIfNeeded(TaskStack stack) {
3873 if (stack.isActivityTypeHome()) {
3874 if (mHomeStack != null) {
Louis Chang7d0037c2018-08-13 12:42:06 +08003875 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
Wale Ogunwale61911492017-10-11 08:50:50 -07003876 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
Louis Chang7d0037c2018-08-13 12:42:06 +08003877
Wale Ogunwale61911492017-10-11 08:50:50 -07003878 }
Louis Chang7d0037c2018-08-13 12:42:06 +08003879 mHomeStack = stack;
Wale Ogunwale61911492017-10-11 08:50:50 -07003880 }
3881 final int windowingMode = stack.getWindowingMode();
3882 if (windowingMode == WINDOWING_MODE_PINNED) {
3883 if (mPinnedStack != null) {
3884 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3885 + mPinnedStack + " already exist on display=" + this
3886 + " stack=" + stack);
3887 }
3888 mPinnedStack = stack;
3889 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3890 if (mSplitScreenPrimaryStack != null) {
3891 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3892 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3893 + " already exist on display=" + this + " stack=" + stack);
3894 }
3895 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003896 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003897 }
3898 }
3899
3900 private void removeStackReferenceIfNeeded(TaskStack stack) {
3901 if (stack == mHomeStack) {
3902 mHomeStack = null;
3903 } else if (stack == mPinnedStack) {
3904 mPinnedStack = null;
3905 } else if (stack == mSplitScreenPrimaryStack) {
3906 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003907 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3908 mService.setDockedStackCreateStateLocked(
3909 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3910 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003911 }
Andrii Kulian839def92016-11-02 10:58:58 -07003912 }
3913
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003914 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003915 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3916 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003917 addChild(stack, addIndex);
3918 setLayoutNeeded();
3919 }
3920
Wale Ogunwale61911492017-10-11 08:50:50 -07003921 @Override
3922 protected void removeChild(TaskStack stack) {
3923 super.removeChild(stack);
3924 removeStackReferenceIfNeeded(stack);
3925 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003926
3927 @Override
3928 boolean isOnTop() {
3929 // Considered always on top
3930 return true;
3931 }
3932
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003933 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003934 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003935 if (child.getWindowConfiguration().isAlwaysOnTop()
3936 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003937 // This stack is always-on-top, override the default behavior.
3938 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3939
3940 // Moving to its current position, as we must call super but we don't want to
3941 // perform any meaningful action.
3942 final int currentPosition = mChildren.indexOf(child);
3943 super.positionChildAt(currentPosition, child, false /* includingParents */);
3944 return;
3945 }
3946
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003947 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3948 super.positionChildAt(targetPosition, child, includingParents);
3949
Riddle Hsu2f6e1742018-08-23 22:44:36 +08003950 if (includingParents) {
3951 // We still want to move the display of this stack container to top because even the
3952 // target position is adjusted to non-top, the intention of the condition is to have
3953 // higher z-order to gain focus (e.g. moving a task of a fullscreen stack to front
3954 // in a non-top display which is using picture-in-picture mode).
3955 final int topChildPosition = getChildCount() - 1;
3956 if (targetPosition < topChildPosition && position >= topChildPosition) {
3957 getParent().positionChildAt(POSITION_TOP, this /* child */,
3958 true /* includingParents */);
3959 }
3960 }
3961
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003962 setLayoutNeeded();
3963 }
3964
3965 /**
3966 * When stack is added or repositioned, find a proper position for it.
3967 * This will make sure that pinned stack always stays on top.
3968 * @param requestedPosition Position requested by caller.
3969 * @param stack Stack to be added or positioned.
3970 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3971 * @return The proper position for the stack.
3972 */
3973 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09003974 if (stack.inPinnedWindowingMode()) {
Kazuki Takisef85197b2018-06-18 18:18:36 +09003975 return POSITION_TOP;
Kazuki Takise148d00a2018-05-31 15:32:19 +09003976 }
3977
Kazuki Takisef85197b2018-06-18 18:18:36 +09003978 final int topChildPosition = mChildren.size() - 1;
3979 int belowAlwaysOnTopPosition = POSITION_BOTTOM;
3980 for (int i = topChildPosition; i >= 0; --i) {
3981 if (getStacks().get(i) != stack && !getStacks().get(i).isAlwaysOnTop()) {
3982 belowAlwaysOnTopPosition = i;
3983 break;
3984 }
3985 }
3986
3987 // The max possible position we can insert the stack at.
3988 int maxPosition = POSITION_TOP;
3989 // The min possible position we can insert the stack at.
3990 int minPosition = POSITION_BOTTOM;
3991
3992 if (stack.isAlwaysOnTop()) {
3993 if (hasPinnedStack()) {
3994 // Always-on-top stacks go below the pinned stack.
3995 maxPosition = getStacks().indexOf(mPinnedStack) - 1;
3996 }
3997 // Always-on-top stacks need to be above all other stacks.
3998 minPosition = belowAlwaysOnTopPosition !=
3999 POSITION_BOTTOM ? belowAlwaysOnTopPosition : topChildPosition;
4000 } else {
4001 // Other stacks need to be below the always-on-top stacks.
4002 maxPosition = belowAlwaysOnTopPosition !=
Arthur Hung928d8dc2018-07-18 15:38:05 +08004003 POSITION_BOTTOM ? belowAlwaysOnTopPosition : 0;
Kazuki Takisef85197b2018-06-18 18:18:36 +09004004 }
4005
4006 int targetPosition = requestedPosition;
4007 targetPosition = Math.min(targetPosition, maxPosition);
4008 targetPosition = Math.max(targetPosition, minPosition);
4009
4010 int prevPosition = getStacks().indexOf(stack);
4011 // The positions we calculated above (maxPosition, minPosition) do not take into
4012 // consideration the following edge cases.
4013 // 1) We need to adjust the position depending on the value "adding".
4014 // 2) When we are moving a stack to another position, we also need to adjust the
4015 // position depending on whether the stack is moving to a higher or lower position.
4016 if ((targetPosition != requestedPosition) &&
4017 (adding || targetPosition < prevPosition)) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09004018 targetPosition++;
4019 }
4020
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004021 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08004022 }
4023
4024 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004025 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
4026 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004027 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004028 if (super.forAllWindows(callback, traverseTopToBottom)) {
4029 return true;
4030 }
4031 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4032 return true;
4033 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004034 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004035 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4036 return true;
4037 }
4038 if (super.forAllWindows(callback, traverseTopToBottom)) {
4039 return true;
4040 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004041 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004042 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004043 }
4044
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004045 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004046 boolean traverseTopToBottom) {
4047 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
4048 // app tokens.
4049 // TODO: Investigate if we need to continue to do this or if we can just process them
4050 // in-order.
4051 if (traverseTopToBottom) {
4052 for (int i = mChildren.size() - 1; i >= 0; --i) {
4053 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4054 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004055 if (appTokens.get(j).forAllWindowsUnchecked(callback,
4056 traverseTopToBottom)) {
4057 return true;
4058 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004059 }
4060 }
4061 } else {
4062 final int count = mChildren.size();
4063 for (int i = 0; i < count; ++i) {
4064 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4065 final int appTokensCount = appTokens.size();
4066 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004067 if (appTokens.get(j).forAllWindowsUnchecked(callback,
4068 traverseTopToBottom)) {
4069 return true;
4070 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004071 }
4072 }
4073 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004074 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004075 }
4076
Wale Ogunwale1666e312016-12-16 11:27:18 -08004077 void setExitingTokensHasVisible(boolean hasVisible) {
4078 for (int i = mChildren.size() - 1; i >= 0; --i) {
4079 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4080 for (int j = appTokens.size() - 1; j >= 0; --j) {
4081 appTokens.get(j).hasVisible = hasVisible;
4082 }
4083 }
4084 }
4085
4086 void removeExistingAppTokensIfPossible() {
4087 for (int i = mChildren.size() - 1; i >= 0; --i) {
4088 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4089 for (int j = appTokens.size() - 1; j >= 0; --j) {
4090 final AppWindowToken token = appTokens.get(j);
lumark588a3e82018-07-20 18:53:54 +08004091 if (!token.hasVisible && !mClosingApps.contains(token)
Wale Ogunwale1666e312016-12-16 11:27:18 -08004092 && (!token.mIsExiting || token.isEmpty())) {
4093 // Make sure there is no animation running on this token, so any windows
4094 // associated with it will be removed as soon as their animations are
4095 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02004096 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004097 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
4098 "performLayout: App token exiting now removed" + token);
4099 token.removeIfPossible();
4100 }
4101 }
4102 }
4103 }
4104
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004105 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004106 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004107 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
4108 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004109 // Apps and their containers are not allowed to specify an orientation while the
Wale Ogunwaled0c225c2018-06-15 14:47:46 -07004110 // docked or freeform stack is visible...except for the home stack if the docked
4111 // stack is minimized and it actually set something and the bounds is different from
4112 // the display.
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004113 if (mHomeStack != null && mHomeStack.isVisible()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004114 && mDividerControllerLocked.isMinimizedDock()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004115 && !(mDividerControllerLocked.isHomeStackResizable()
4116 && mHomeStack.matchParentBounds())) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004117 final int orientation = mHomeStack.getOrientation();
4118 if (orientation != SCREEN_ORIENTATION_UNSET) {
4119 return orientation;
4120 }
4121 }
4122 return SCREEN_ORIENTATION_UNSPECIFIED;
4123 }
4124
4125 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07004126 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
4127 PackageManager.FEATURE_AUTOMOTIVE);
4128 if (isCar) {
4129 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
4130 // allow anything but the default orientation.
4131 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004132 "Forcing UNSPECIFIED orientation in car for display id=" + mDisplayId
4133 + ". Ignoring " + orientation);
Dean Harding3e5a1522017-10-06 09:19:01 -07004134 return SCREEN_ORIENTATION_UNSPECIFIED;
4135 }
4136
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004137 if (orientation != SCREEN_ORIENTATION_UNSET
4138 && orientation != SCREEN_ORIENTATION_BEHIND) {
4139 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004140 "App is requesting an orientation, return " + orientation
4141 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004142 return orientation;
4143 }
4144
4145 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004146 "No app is requesting an orientation, return " + mLastOrientation
4147 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004148 // The next app has not been requested to be visible, so we keep the current orientation
4149 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08004150 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004151 }
Robert Carrb1579c82017-09-05 14:54:47 -07004152
4153 @Override
4154 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004155 assignStackOrdering(t);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004156
Robert Carr2f8aa392018-01-31 14:46:51 -08004157 for (int i = 0; i < mChildren.size(); i++) {
4158 final TaskStack s = mChildren.get(i);
4159 s.assignChildLayers(t);
4160 }
4161 }
4162
4163 void assignStackOrdering(SurfaceControl.Transaction t) {
Jorim Jaggibe418292018-03-26 16:14:12 +02004164
Robert Carrf7a7ca82017-12-06 13:17:07 -08004165 final int HOME_STACK_STATE = 0;
4166 final int NORMAL_STACK_STATE = 1;
4167 final int ALWAYS_ON_TOP_STATE = 2;
4168
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004169 int layer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08004170 int layerForAnimationLayer = 0;
Jorim Jaggibe418292018-03-26 16:14:12 +02004171 int layerForBoostedAnimationLayer = 0;
Jorim Jaggi391790622018-04-18 15:30:44 +02004172 int layerForHomeAnimationLayer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08004173
Robert Carrf7a7ca82017-12-06 13:17:07 -08004174 for (int state = 0; state <= ALWAYS_ON_TOP_STATE; state++) {
4175 for (int i = 0; i < mChildren.size(); i++) {
4176 final TaskStack s = mChildren.get(i);
Robert Carr2f8aa392018-01-31 14:46:51 -08004177 if (state == HOME_STACK_STATE && !s.isActivityTypeHome()) {
4178 continue;
4179 } else if (state == NORMAL_STACK_STATE && (s.isActivityTypeHome()
4180 || s.isAlwaysOnTop())) {
4181 continue;
4182 } else if (state == ALWAYS_ON_TOP_STATE && !s.isAlwaysOnTop()) {
4183 continue;
4184 }
4185 s.assignLayer(t, layer++);
4186 if (s.inSplitScreenWindowingMode() && mSplitScreenDividerAnchor != null) {
4187 t.setLayer(mSplitScreenDividerAnchor, layer++);
4188 }
Winson Chungd41f71d2018-03-16 15:26:07 -07004189 if ((s.isTaskAnimating() || s.isAppAnimating())
4190 && state != ALWAYS_ON_TOP_STATE) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004191 // Ensure the animation layer ends up above the
4192 // highest animating stack and no higher.
4193 layerForAnimationLayer = layer++;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004194 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004195 if (state != ALWAYS_ON_TOP_STATE) {
4196 layerForBoostedAnimationLayer = layer++;
4197 }
Robert Carrf7a7ca82017-12-06 13:17:07 -08004198 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004199 if (state == HOME_STACK_STATE) {
4200 layerForHomeAnimationLayer = layer++;
4201 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004202 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004203 if (mAppAnimationLayer != null) {
4204 t.setLayer(mAppAnimationLayer, layerForAnimationLayer);
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004205 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004206 if (mBoostedAppAnimationLayer != null) {
4207 t.setLayer(mBoostedAppAnimationLayer, layerForBoostedAnimationLayer);
4208 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004209 if (mHomeAppAnimationLayer != null) {
4210 t.setLayer(mHomeAppAnimationLayer, layerForHomeAnimationLayer);
4211 }
Robert Carrb1579c82017-09-05 14:54:47 -07004212 }
4213
4214 @Override
Jorim Jaggi391790622018-04-18 15:30:44 +02004215 SurfaceControl getAppAnimationLayer(@AnimationLayer int animationLayer) {
4216 switch (animationLayer) {
4217 case ANIMATION_LAYER_BOOSTED:
4218 return mBoostedAppAnimationLayer;
4219 case ANIMATION_LAYER_HOME:
4220 return mHomeAppAnimationLayer;
4221 case ANIMATION_LAYER_STANDARD:
4222 default:
4223 return mAppAnimationLayer;
4224 }
chaviw23ee71c2017-12-18 11:29:41 -08004225 }
4226
Robert Carrf7a7ca82017-12-06 13:17:07 -08004227 SurfaceControl getSplitScreenDividerAnchor() {
4228 return mSplitScreenDividerAnchor;
4229 }
4230
chaviw23ee71c2017-12-18 11:29:41 -08004231 @Override
Robert Carrb1579c82017-09-05 14:54:47 -07004232 void onParentSet() {
4233 super.onParentSet();
4234 if (getParent() != null) {
chaviw23ee71c2017-12-18 11:29:41 -08004235 mAppAnimationLayer = makeChildSurface(null)
4236 .setName("animationLayer")
4237 .build();
Jorim Jaggibe418292018-03-26 16:14:12 +02004238 mBoostedAppAnimationLayer = makeChildSurface(null)
4239 .setName("boostedAnimationLayer")
4240 .build();
Jorim Jaggi391790622018-04-18 15:30:44 +02004241 mHomeAppAnimationLayer = makeChildSurface(null)
4242 .setName("homeAnimationLayer")
4243 .build();
Robert Carrf7a7ca82017-12-06 13:17:07 -08004244 mSplitScreenDividerAnchor = makeChildSurface(null)
4245 .setName("splitScreenDividerAnchor")
4246 .build();
4247 getPendingTransaction()
4248 .show(mAppAnimationLayer)
Jorim Jaggibe418292018-03-26 16:14:12 +02004249 .show(mBoostedAppAnimationLayer)
Jorim Jaggi391790622018-04-18 15:30:44 +02004250 .show(mHomeAppAnimationLayer)
Robert Carrf7a7ca82017-12-06 13:17:07 -08004251 .show(mSplitScreenDividerAnchor);
chaviw23ee71c2017-12-18 11:29:41 -08004252 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07004253 } else {
chaviw23ee71c2017-12-18 11:29:41 -08004254 mAppAnimationLayer.destroy();
4255 mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02004256 mBoostedAppAnimationLayer.destroy();
4257 mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02004258 mHomeAppAnimationLayer.destroy();
4259 mHomeAppAnimationLayer = null;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004260 mSplitScreenDividerAnchor.destroy();
4261 mSplitScreenDividerAnchor = null;
Robert Carrb1579c82017-09-05 14:54:47 -07004262 }
4263 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004264 }
4265
Robert Carree4d4b92017-11-22 12:21:46 -08004266 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004267 AboveAppWindowContainers(String name, WindowManagerService service) {
4268 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08004269 }
4270
Robert Carrb9506032018-02-13 13:54:00 -08004271 @Override
Vishnu Nair83537a72018-07-19 21:27:48 -07004272 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
4273 final SurfaceControl.Builder builder = super.makeChildSurface(child);
4274 if (child instanceof WindowToken && ((WindowToken) child).mRoundedCornerOverlay) {
4275 // To draw above the ColorFade layer during the screen off transition, the
4276 // rounded corner overlays need to be at the root of the surface hierarchy.
4277 // TODO: move the ColorLayer into the display overlay layer such that this is not
4278 // necessary anymore.
4279 builder.setParent(null);
4280 }
4281 return builder;
4282 }
4283
4284 @Override
Robert Carrb9506032018-02-13 13:54:00 -08004285 void assignChildLayers(SurfaceControl.Transaction t) {
4286 assignChildLayers(t, null /* imeContainer */);
4287 }
4288
Robert Carree4d4b92017-11-22 12:21:46 -08004289 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
4290 boolean needAssignIme = imeContainer != null
4291 && imeContainer.getSurfaceControl() != null;
4292 for (int j = 0; j < mChildren.size(); ++j) {
4293 final WindowToken wt = mChildren.get(j);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004294
4295 // See {@link mSplitScreenDividerAnchor}
4296 if (wt.windowType == TYPE_DOCK_DIVIDER) {
4297 wt.assignRelativeLayer(t, mTaskStackContainers.getSplitScreenDividerAnchor(), 1);
4298 continue;
4299 }
Vishnu Nair83537a72018-07-19 21:27:48 -07004300 if (wt.mRoundedCornerOverlay) {
4301 wt.assignLayer(t, WindowManagerPolicy.COLOR_FADE_LAYER + 1);
4302 continue;
4303 }
Robert Carree4d4b92017-11-22 12:21:46 -08004304 wt.assignLayer(t, j);
4305 wt.assignChildLayers(t);
4306
4307 int layer = mService.mPolicy.getWindowLayerFromTypeLw(
4308 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08004309
4310 if (needAssignIme && layer >= mService.mPolicy.getWindowLayerFromTypeLw(
4311 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004312 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08004313 needAssignIme = false;
4314 }
4315 }
4316 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004317 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08004318 }
4319 }
4320 }
4321
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004322 /**
4323 * Window container class that contains all containers on this display that are not related to
4324 * Apps. E.g. status bar.
4325 */
Robert Carree4d4b92017-11-22 12:21:46 -08004326 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07004327 /**
4328 * Compares two child window tokens returns -1 if the first is lesser than the second in
4329 * terms of z-order and 1 otherwise.
4330 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004331 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07004332 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004333 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
4334 token1.mOwnerCanManageAppTokens)
4335 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
4336 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004337
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004338 private final Predicate<WindowState> mGetOrientingWindow = w -> {
4339 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
4340 return false;
4341 }
4342 final int req = w.mAttrs.screenOrientation;
4343 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
4344 || req == SCREEN_ORIENTATION_UNSET) {
4345 return false;
4346 }
4347 return true;
4348 };
4349
Wale Ogunwale3a931692016-11-02 16:49:48 -07004350 private final String mName;
chaviwf29223e2018-01-31 13:23:51 -08004351 private final Dimmer mDimmer = new Dimmer(this);
4352 private final Rect mTmpDimBoundsRect = new Rect();
4353
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004354 NonAppWindowContainers(String name, WindowManagerService service) {
4355 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07004356 mName = name;
4357 }
4358
4359 void addChild(WindowToken token) {
4360 addChild(token, mWindowComparator);
4361 }
4362
4363 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004364 int getOrientation() {
4365 final WindowManagerPolicy policy = mService.mPolicy;
4366 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004367 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004368
4369 if (win != null) {
4370 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004371 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004372 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07004373 if (mService.mKeyguardGoingAway) {
4374 // Keyguard can't affect the orientation if it is going away...
4375 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
4376 return SCREEN_ORIENTATION_UNSET;
4377 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004378 }
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004379 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req
4380 + " for display id=" + mDisplayId);
Andrii Kulian8ee72852017-03-10 10:36:45 -08004381 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004382 }
4383
Andrii Kulian8ee72852017-03-10 10:36:45 -08004384 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004385
Jorim Jaggi1c530592018-04-06 15:11:47 +02004386 // Only allow force setting the orientation when all unknown visibilities have been
4387 // resolved, as otherwise we just may be starting another occluding activity.
4388 final boolean isUnoccluding =
lumark588a3e82018-07-20 18:53:54 +08004389 mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
4390 && mUnknownAppVisibilityController.allResolved();
Jorim Jaggi1c530592018-04-06 15:11:47 +02004391 if (policy.isKeyguardShowingAndNotOccluded() || isUnoccluding) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004392 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004393 }
4394
4395 return SCREEN_ORIENTATION_UNSET;
4396 }
4397
4398 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07004399 String getName() {
4400 return mName;
4401 }
chaviwf29223e2018-01-31 13:23:51 -08004402
4403 @Override
4404 Dimmer getDimmer() {
4405 return mDimmer;
4406 }
4407
4408 @Override
4409 void prepareSurfaces() {
4410 mDimmer.resetDimStates();
4411 super.prepareSurfaces();
4412 getBounds(mTmpDimBoundsRect);
4413
4414 if (mDimmer.updateDims(getPendingTransaction(), mTmpDimBoundsRect)) {
4415 scheduleAnimation();
4416 }
4417 }
Robert Carr3b716242016-08-16 16:02:21 -07004418 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01004419
Robert Carr9034d962018-01-04 18:27:42 -08004420 private class NonMagnifiableWindowContainers extends NonAppWindowContainers {
4421 NonMagnifiableWindowContainers(String name, WindowManagerService service) {
4422 super(name, service);
4423 }
4424
4425 @Override
4426 void applyMagnificationSpec(Transaction t, MagnificationSpec spec) {
4427 }
4428 };
4429
Robert Carrb1579c82017-09-05 14:54:47 -07004430 SurfaceControl.Builder makeSurface(SurfaceSession s) {
4431 return mService.makeSurfaceBuilder(s)
4432 .setParent(mWindowingLayer);
4433 }
4434
4435 @Override
4436 SurfaceSession getSession() {
4437 return mSession;
4438 }
4439
4440 @Override
4441 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07004442 SurfaceSession s = child != null ? child.getSession() : getSession();
4443 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrb1579c82017-09-05 14:54:47 -07004444 b.setSize(mSurfaceSize, mSurfaceSize);
Robert Carrf59b8dd2017-10-02 18:58:36 -07004445
4446 if (child == null) {
4447 return b;
4448 }
4449
Robert Carree4d4b92017-11-22 12:21:46 -08004450 return b.setName(child.getName())
4451 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07004452 }
4453
4454 /**
4455 * The makeSurface variants are for use by the window-container
4456 * hierarchy. makeOverlay here is a function for various non windowing
4457 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
4458 * and other potpourii.
4459 */
4460 SurfaceControl.Builder makeOverlay() {
4461 return mService.makeSurfaceBuilder(mSession)
4462 .setParent(mOverlayLayer);
4463 }
4464
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004465 /**
4466 * Reparents the given surface to mOverlayLayer.
4467 */
4468 void reparentToOverlay(Transaction transaction, SurfaceControl surface) {
4469 transaction.reparent(surface, mOverlayLayer.getHandle());
4470 }
4471
Robert Carrb1579c82017-09-05 14:54:47 -07004472 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carr24be9ab2018-04-30 17:54:53 -07004473 if (spec.scale != 1.0) {
4474 mMagnificationSpec = spec;
4475 } else {
4476 mMagnificationSpec = null;
4477 }
4478
Robert Carrf59b8dd2017-10-02 18:58:36 -07004479 applyMagnificationSpec(getPendingTransaction(), spec);
4480 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07004481 }
4482
Robert Carr24be9ab2018-04-30 17:54:53 -07004483 void reapplyMagnificationSpec() {
4484 if (mMagnificationSpec != null) {
4485 applyMagnificationSpec(getPendingTransaction(), mMagnificationSpec);
4486 }
4487 }
4488
Robert Carrb1579c82017-09-05 14:54:47 -07004489 @Override
4490 void onParentSet() {
4491 // Since we are the top of the SurfaceControl hierarchy here
4492 // we create the root surfaces explicitly rather than chaining
4493 // up as the default implementation in onParentSet does. So we
4494 // explicitly do NOT call super here.
4495 }
4496
4497 @Override
4498 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07004499
4500 // These are layers as children of "mWindowingLayer"
4501 mBelowAppWindowsContainers.assignLayer(t, 0);
4502 mTaskStackContainers.assignLayer(t, 1);
4503 mAboveAppWindowsContainers.assignLayer(t, 2);
4504
lumarkff0ab692018-11-05 20:32:30 +08004505 final WindowState imeTarget = mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08004506 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08004507
Robert Carree4d4b92017-11-22 12:21:46 -08004508 // In the case where we have an IME target that is not in split-screen
4509 // mode IME assignment is easy. We just need the IME to go directly above
4510 // the target. This way children of the target will naturally go above the IME
4511 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08004512 //
Robert Carree4d4b92017-11-22 12:21:46 -08004513 // In the case of split-screen windowing mode, we need to elevate the IME above the
4514 // docked divider while keeping the app itself below the docked divider, so instead
4515 // we use relative layering of the IME targets child windows, and place the
4516 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08004517 //
Robert Carr234b6332018-03-20 13:38:16 -07004518 // In the case the IME target is animating, the animation Z order may be different
4519 // than the WindowContainer Z order, so it's difficult to be sure we have the correct
4520 // IME target. In this case we just layer the IME over all transitions by placing it in the
4521 // above applications layer.
4522 //
Robert Carree4d4b92017-11-22 12:21:46 -08004523 // In the case where we have no IME target we assign it where it's base layer would
4524 // place it in the AboveAppWindowContainers.
Robert Carr234b6332018-03-20 13:38:16 -07004525 if (imeTarget != null && !(imeTarget.inSplitScreenWindowingMode()
4526 || imeTarget.mToken.isAppAnimating())
Robert Carree4d4b92017-11-22 12:21:46 -08004527 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004528 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07004529 // TODO: We need to use an extra level on the app surface to ensure
4530 // this is always above SurfaceView but always below attached window.
4531 1);
Robert Carree4d4b92017-11-22 12:21:46 -08004532 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07004533 }
4534
4535 // Above we have assigned layers to our children, now we ask them to assign
4536 // layers to their children.
4537 mBelowAppWindowsContainers.assignChildLayers(t);
4538 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08004539 mAboveAppWindowsContainers.assignChildLayers(t,
4540 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07004541 mImeWindowsContainers.assignChildLayers(t);
4542 }
4543
4544 /**
4545 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
4546 * that the IME target is one of the docked applications. We'd like the docked divider to be
4547 * above both of the applications, and we'd like the IME to be above the docked divider.
4548 * However we need child windows of the applications to be above the IME (Text drag handles).
4549 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
4550 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
4551 * with {@link #WindowState#assignLayer}
4552 */
4553 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
Robert Carrbb5de662018-04-26 13:29:51 -07004554 child.assignRelativeLayer(t, mImeWindowsContainers.getSurfaceControl(), 1);
Robert Carrb1579c82017-09-05 14:54:47 -07004555 }
4556
4557 @Override
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004558 void prepareSurfaces() {
4559 final ScreenRotationAnimation screenRotationAnimation =
4560 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
4561 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
4562 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
4563 mPendingTransaction.setMatrix(mWindowingLayer,
4564 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
4565 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
4566 mPendingTransaction.setPosition(mWindowingLayer,
4567 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
4568 mPendingTransaction.setAlpha(mWindowingLayer,
4569 screenRotationAnimation.getEnterTransformation().getAlpha());
4570 }
Chavi Weingartenb736e322018-02-23 00:27:54 +00004571
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004572 super.prepareSurfaces();
4573 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004574
Jorim Jaggibe418292018-03-26 16:14:12 +02004575 void assignStackOrdering() {
4576 mTaskStackContainers.assignStackOrdering(getPendingTransaction());
Robert Carr2f8aa392018-01-31 14:46:51 -08004577 }
Chavi Weingarten3a748552018-05-14 17:32:42 +00004578
4579 /**
4580 * Increment the deferral count to determine whether to update the IME target.
4581 */
4582 void deferUpdateImeTarget() {
4583 mDeferUpdateImeTargetCount++;
4584 }
4585
4586 /**
4587 * Decrement the deferral count to determine whether to update the IME target. If the count
4588 * reaches 0, a new ime target will get computed.
4589 */
4590 void continueUpdateImeTarget() {
4591 if (mDeferUpdateImeTargetCount == 0) {
4592 return;
4593 }
4594
4595 mDeferUpdateImeTargetCount--;
4596 if (mDeferUpdateImeTargetCount == 0) {
4597 computeImeTarget(true /* updateImeTarget */);
4598 }
4599 }
4600
4601 /**
4602 * @return Whether a new IME target should be computed.
4603 */
4604 private boolean canUpdateImeTarget() {
4605 return mDeferUpdateImeTargetCount == 0;
4606 }
Arthur Hung95b38a92018-07-20 18:56:12 +08004607
4608 InputMonitor getInputMonitor() {
4609 return mInputMonitor;
4610 }
Jorim Jaggif1292892018-09-10 11:58:13 +02004611
4612 /**
4613 * @return Cached value whether we told display manager that we have content.
4614 */
4615 boolean getLastHasContent() {
4616 return mLastHasContent;
4617 }
Arthur Hungbe5ce212018-09-13 18:41:56 +08004618
4619 void registerPointerEventListener(@NonNull PointerEventListener listener) {
4620 if (mPointerEventDispatcher != null) {
4621 mPointerEventDispatcher.registerInputEventListener(listener);
4622 }
4623 }
4624
4625 void unregisterPointerEventListener(@NonNull PointerEventListener listener) {
4626 if (mPointerEventDispatcher != null) {
4627 mPointerEventDispatcher.unregisterInputEventListener(listener);
4628 }
4629 }
lumark588a3e82018-07-20 18:53:54 +08004630
4631 void prepareAppTransition(@WindowManager.TransitionType int transit,
4632 boolean alwaysKeepCurrent, @WindowManager.TransitionFlags int flags,
4633 boolean forceOverride) {
4634 final boolean prepared = mAppTransition.prepareAppTransitionLocked(
4635 transit, alwaysKeepCurrent, flags, forceOverride);
4636 if (prepared && okToAnimate()) {
4637 mSkipAppTransitionAnimation = false;
4638 }
4639 }
4640
4641 void executeAppTransition() {
4642 if (mAppTransition.isTransitionSet()) {
4643 if (DEBUG_APP_TRANSITIONS) {
4644 Slog.w(TAG_WM, "Execute app transition: " + mAppTransition + ", displayId: "
4645 + mDisplayId + " Callers=" + Debug.getCallers(5));
4646 }
4647 mAppTransition.setReady();
4648 mService.mWindowPlacerLocked.requestTraversal();
4649 }
4650 }
4651
4652 /**
4653 * Update pendingLayoutChanges after app transition has finished.
4654 */
4655 void handleAnimatingStoppedAndTransition() {
4656 int changes = 0;
4657
4658 mAppTransition.setIdle();
4659
4660 for (int i = mNoAnimationNotifyOnTransitionFinished.size() - 1; i >= 0; i--) {
4661 final IBinder token = mNoAnimationNotifyOnTransitionFinished.get(i);
4662 mAppTransition.notifyAppTransitionFinishedLocked(token);
4663 }
4664 mNoAnimationNotifyOnTransitionFinished.clear();
4665
4666 mWallpaperController.hideDeferredWallpapersIfNeeded();
4667
4668 onAppTransitionDone();
4669
4670 changes |= FINISH_LAYOUT_REDO_LAYOUT;
4671 if (DEBUG_WALLPAPER_LIGHT) {
4672 Slog.v(TAG_WM, "Wallpaper layer changed: assigning layers + relayout");
4673 }
4674 computeImeTarget(true /* updateImeTarget */);
wilsonshihc32538e2018-11-07 17:27:34 +08004675 mWallpaperMayChange = true;
lumark588a3e82018-07-20 18:53:54 +08004676 // Since the window list has been rebuilt, focus might have to be recomputed since the
4677 // actual order of windows might have changed again.
4678 mService.mFocusMayChange = true;
4679
4680 pendingLayoutChanges |= changes;
4681 }
Andrii Kulian15cfb422018-11-07 13:38:49 -08004682
4683 /**
4684 * @see Display#FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS
4685 */
4686 boolean supportsSystemDecorations() {
4687 // TODO(b/114338689): Read the setting from DisplaySettings.
4688 return mDisplay.supportsSystemDecorations()
4689 // TODO (b/111363427): Remove this and set the new FLAG_SHOULD_SHOW_LAUNCHER flag
4690 // (b/114338689) whenever vr 2d display id is set.
4691 || mDisplayId == mService.mVr2dDisplayId
4692 || mService.mForceDesktopModeOnExternalDisplays;
4693 }
Craig Mautner59c00972012-07-30 12:10:24 -07004694}