blob: a834ef16f426d312fa21329baa15f8310688677c [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;
81import static com.android.server.wm.DisplayContentProto.WINDOW_CONTAINER;
Wale Ogunwale1666e312016-12-16 11:27:18 -080082import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
lumark588a3e82018-07-20 18:53:54 +080083import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_APP_TRANSITIONS;
Wale Ogunwale494009b82016-10-21 09:01:38 -070084import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070085import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070086import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
87import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070088import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070089import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070090import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020091import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070092import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070093import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070094import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -080095import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070096import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070097import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070098import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070099import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800100import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700101import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800102import static com.android.server.wm.WindowManagerService.H.REPORT_LOSING_FOCUS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700103import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
104import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700105import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700106import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700107import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
108import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800109import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_PLACING_SURFACES;
110import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_REMOVING_FOCUS;
111import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_ASSIGN_LAYERS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700112import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700113import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700114import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700115import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700116import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700117import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700118import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700119import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700120import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700121
lumark588a3e82018-07-20 18:53:54 +0800122import android.animation.AnimationHandler;
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700123import android.annotation.CallSuper;
Riddle Hsuf53da812018-08-15 22:00:27 +0800124import android.annotation.IntDef;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700125import android.annotation.NonNull;
Dean Harding3e5a1522017-10-06 09:19:01 -0700126import android.content.pm.PackageManager;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700127import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700128import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700129import android.graphics.Bitmap;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700130import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800131import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700132import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700133import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100134import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700135import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700136import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700137import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700138import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700139import android.os.RemoteException;
140import android.os.SystemClock;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100141import android.os.Trace;
Riddle Hsuf53da812018-08-15 22:00:27 +0800142import android.os.UserHandle;
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800143import android.util.ArraySet;
Chong Zhang8e89b312015-09-09 15:09:30 -0700144import android.util.DisplayMetrics;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700145import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700146import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700147import android.view.Display;
Adrian Roos1cf585052018-01-03 18:43:27 +0100148import android.view.DisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700149import android.view.DisplayInfo;
Riddle Hsua4d6fa22018-08-11 00:50:39 +0800150import android.view.Gravity;
Arthur Hungbe5ce212018-09-13 18:41:56 +0800151import android.view.InputChannel;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700152import android.view.InputDevice;
Robert Carrb1579c82017-09-05 14:54:47 -0700153import android.view.MagnificationSpec;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700154import android.view.Surface;
155import android.view.SurfaceControl;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100156import android.view.SurfaceControl.Transaction;
Robert Carrb1579c82017-09-05 14:54:47 -0700157import android.view.SurfaceSession;
Tiger Huang7c610aa2018-10-27 00:01:01 +0800158import android.view.View;
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.
Craig Mautner59c00972012-07-30 12:10:24 -0700185 */
Riddle Hsuad256a12018-07-18 16:11:30 +0800186class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer>
187 implements WindowManagerPolicy.DisplayContentInfo {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700188 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700189
Riddle Hsuf53da812018-08-15 22:00:27 +0800190 /** The default scaling mode that scales content automatically. */
191 static final int FORCE_SCALING_MODE_AUTO = 0;
192 /** For {@link #setForcedScalingMode} to apply flag {@link Display#FLAG_SCALING_DISABLED}. */
193 static final int FORCE_SCALING_MODE_DISABLED = 1;
194
195 @IntDef(prefix = { "FORCE_SCALING_MODE_" }, value = {
196 FORCE_SCALING_MODE_AUTO,
197 FORCE_SCALING_MODE_DISABLED
198 })
199 @Retention(RetentionPolicy.SOURCE)
200 @interface ForceScalingMode {}
201
Craig Mautner59c00972012-07-30 12:10:24 -0700202 /** Unique identifier of this stack. */
203 private final int mDisplayId;
204
Wale Ogunwale3a931692016-11-02 16:49:48 -0700205 /** The containers below are the only child containers the display can have. */
206 // Contains all window containers that are related to apps (Activities)
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100207 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers(mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700208 // Contains all non-app window containers that should be displayed above the app containers
209 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800210 private final AboveAppWindowContainers mAboveAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100211 new AboveAppWindowContainers("mAboveAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700212 // Contains all non-app window containers that should be displayed below the app containers
213 // (e.g. Wallpaper).
214 private final NonAppWindowContainers mBelowAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100215 new NonAppWindowContainers("mBelowAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700216 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
217 // 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 -0800218 // window containers together and move them in-sync if/when needed. We use a subclass of
219 // WindowContainer which is omitted from screen magnification, as the IME is never magnified.
220 private final NonMagnifiableWindowContainers mImeWindowsContainers =
221 new NonMagnifiableWindowContainers("mImeWindowsContainers", mService);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700222
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000223 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800224 private WindowState mTmpWindow2;
225 private WindowAnimator mTmpWindowAnimator;
226 private boolean mTmpRecoveringMemory;
227 private boolean mUpdateImeTarget;
228 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700229 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700230
lumark588a3e82018-07-20 18:53:54 +0800231 final AppTransition mAppTransition;
232 final AppTransitionController mAppTransitionController;
233 boolean mSkipAppTransitionAnimation = false;
234
235 final ArraySet<AppWindowToken> mOpeningApps = new ArraySet<>();
236 final ArraySet<AppWindowToken> mClosingApps = new ArraySet<>();
237 final UnknownAppVisibilityController mUnknownAppVisibilityController;
238 BoundsAnimationController mBoundsAnimationController;
239
240 /**
241 * List of clients without a transtiton animation that we notify once we are done
242 * transitioning since they won't be notified through the app window animator.
243 */
244 final List<IBinder> mNoAnimationNotifyOnTransitionFinished = new ArrayList<>();
245
Wale Ogunwale02319a62016-09-26 15:21:22 -0700246 // Mapping from a token IBinder to a WindowToken object on this display.
247 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
248
Andrii Kuliancd097992017-03-23 18:31:59 -0700249 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700250 int mInitialDisplayWidth = 0;
251 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700252 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700253
Adrian Roos1cf585052018-01-03 18:43:27 +0100254 DisplayCutout mInitialDisplayCutout;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100255 private final RotationCache<DisplayCutout, WmDisplayCutout> mDisplayCutoutCache
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100256 = new RotationCache<>(this::calculateDisplayCutoutForRotationUncached);
Adrian Roos1cf585052018-01-03 18:43:27 +0100257
Andrii Kuliancd097992017-03-23 18:31:59 -0700258 /**
259 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
260 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
261 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
262 */
Craig Mautner59c00972012-07-30 12:10:24 -0700263 int mBaseDisplayWidth = 0;
264 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700265 /**
266 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
267 * but can be set from Settings or via shell command "adb shell wm density".
268 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
269 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700270 int mBaseDisplayDensity = 0;
Riddle Hsuf53da812018-08-15 22:00:27 +0800271
272 /**
273 * Whether to disable display scaling. This can be set via shell command "adb shell wm scaling".
274 * @see WindowManagerService#setForcedDisplayScalingMode(int, int)
275 */
Jeff Brownd46747a2015-04-15 19:02:36 -0700276 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700277 private final DisplayInfo mDisplayInfo = new DisplayInfo();
278 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700279 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800280 private final DisplayPolicy mDisplayPolicy;
281 private DisplayRotation mDisplayRotation;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000282 DisplayFrames mDisplayFrames;
283
Andrii Kulian06d07d62017-03-14 11:11:47 -0700284 /**
285 * For default display it contains real metrics, empty for others.
286 * @see WindowManagerService#createWatermarkInTransaction()
287 */
288 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800289
290 /** @see #computeCompatSmallestWidth(boolean, int, int, int, DisplayCutout) */
Andrii Kulian06d07d62017-03-14 11:11:47 -0700291 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700292
Andrii Kulian06d07d62017-03-14 11:11:47 -0700293 /**
294 * Compat metrics computed based on {@link #mDisplayMetrics}.
295 * @see #updateDisplayAndOrientation(int)
296 */
297 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
298
299 /** The desired scaling factor for compatible apps. */
300 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700301
Andrii Kulian8ee72852017-03-10 10:36:45 -0800302 /**
303 * Current rotation of the display.
304 * Constants as per {@link android.view.Surface.Rotation}.
305 *
Robert Carrae606b42018-02-15 15:36:23 -0800306 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800307 */
308 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700309
Andrii Kulian8ee72852017-03-10 10:36:45 -0800310 /**
311 * Last applied orientation of the display.
312 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
313 *
Riddle Hsu4e611772018-10-31 18:58:28 +0800314 * @see #updateOrientationFromAppTokens()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800315 */
316 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700317
Andrii Kulian8ee72852017-03-10 10:36:45 -0800318 /**
319 * Flag indicating that the application is receiving an orientation that has different metrics
320 * than it expected. E.g. Portrait instead of Landscape.
321 *
Robert Carrae606b42018-02-15 15:36:23 -0800322 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800323 */
324 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700325
Andrii Kulian8ee72852017-03-10 10:36:45 -0800326 /**
327 * Orientation forced by some window. If there is no visible window that specifies orientation
328 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
329 *
330 * @see NonAppWindowContainers#getOrientation()
331 */
332 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700333
Andrii Kulian8ee72852017-03-10 10:36:45 -0800334 /**
335 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
336 * occluded.
337 *
338 * @see NonAppWindowContainers#getOrientation()
339 */
340 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
341
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700342 /**
343 * Keep track of wallpaper visibility to notify changes.
344 */
345 private boolean mLastWallpaperVisible = false;
346
Andrii Kulian06d07d62017-03-14 11:11:47 -0700347 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700348
Craig Mautner39834192012-09-02 07:47:24 -0700349 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700350 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700351 int pendingLayoutChanges;
Riddle Hsu654a6f92018-07-13 22:59:36 +0800352 int mDeferredRotationPauseCount;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800353
Vishnu Nairba183352018-11-21 11:16:49 -0800354 /**
355 * Used to gate application window layout until we have sent the complete configuration.
356 * TODO: There are still scenarios where we may be out of sync with the client. Ideally
357 * we want to replace this flag with a mechanism that will confirm the configuration
358 * applied by the client is the one expected by the system server.
359 */
360 boolean mWaitingForConfig;
361
Andrii Kulian839def92016-11-02 10:58:58 -0700362 // TODO(multi-display): remove some of the usages.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800363 @VisibleForTesting
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800364 boolean isDefaultDisplay;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800365
Andrii Kulianf0379de2018-03-14 16:24:07 -0700366 /**
367 * Flag indicating whether WindowManager should override info for this display in
368 * DisplayManager.
369 */
370 boolean mShouldOverrideDisplayConfiguration = true;
Craig Mautner39834192012-09-02 07:47:24 -0700371
Craig Mautnerdc548482014-02-05 13:35:24 -0800372 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700373 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800374
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700375 /** Detect user tapping outside of current focused task bounds .*/
376 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700377
Craig Mautner6601b7b2013-04-29 10:29:11 -0700378 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700379 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700380
Craig Mautner6601b7b2013-04-29 10:29:11 -0700381 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800382 private final Rect mTmpRect = new Rect();
383 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700384 private final RectF mTmpRectF = new RectF();
385 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800386 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700387
Issei Suzuki43190bd2018-08-20 17:28:41 +0200388
Bryce Leef3c6a472017-11-14 14:53:06 -0800389 /** Used for handing back size of display */
390 private final Rect mTmpBounds = new Rect();
391
Craig Mautner95da1082014-02-24 17:54:35 -0800392 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700393 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800394
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700395 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700396 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700397
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800398 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800399 /** A collection of windows that provide tap exclude regions inside of them. */
400 final ArraySet<WindowState> mTapExcludeProvidingWindows = new ArraySet<>();
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800401
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000402 private boolean mHaveBootMsg = false;
403 private boolean mHaveApp = false;
404 private boolean mHaveWallpaper = false;
405 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700406
407 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
408
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700409 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
410 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000411 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
412 new ApplySurfaceChangesTransactionState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700413
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700414 // True if this display is in the process of being removed. Used to determine if the removal of
415 // the display's direct children should be allowed.
416 private boolean mRemovingDisplay = false;
417
Bryce Leed1871262017-06-12 14:12:29 -0700418 // {@code false} if this display is in the processing of being created.
419 private boolean mDisplayReady = false;
420
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800421 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700422
wilsonshihc32538e2018-11-07 17:27:34 +0800423 boolean mWallpaperMayChange = false;
424
Robert Carrb1579c82017-09-05 14:54:47 -0700425 private final SurfaceSession mSession = new SurfaceSession();
426
427 /**
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800428 * Window that is currently interacting with the user. This window is responsible for receiving
429 * key events and pointer events from the user.
430 */
431 WindowState mCurrentFocus = null;
432
433 /**
434 * The last focused window that we've notified the client that the focus is changed.
435 */
436 WindowState mLastFocus = null;
437
438 /**
439 * Windows that have lost input focus and are waiting for the new focus window to be displayed
440 * before they are told about this.
441 */
442 ArrayList<WindowState> mLosingFocus = new ArrayList<>();
443
444 /**
445 * The foreground app of this display. Windows below this app cannot be the focused window. If
446 * the user taps on the area outside of the task of the focused app, we will notify AM about the
447 * new task the user wants to interact with.
448 */
449 AppWindowToken mFocusedApp = null;
450
451 /** Windows added since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
452 final ArrayList<WindowState> mWinAddedSinceNullFocus = new ArrayList<>();
453
454 /** Windows removed since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
455 final ArrayList<WindowState> mWinRemovedSinceNullFocus = new ArrayList<>();
456
457 /**
Robert Carrb1579c82017-09-05 14:54:47 -0700458 * We organize all top-level Surfaces in to the following layers.
459 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800460 * and omitted from Screen-Magnification, for example the strict mode flash or
461 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700462 * {@link #mWindowingLayer} contains everything else.
463 */
464 private SurfaceControl mOverlayLayer;
465
466 /**
467 * See {@link #mOverlayLayer}
468 */
469 private SurfaceControl mWindowingLayer;
470
471 /**
Adrian Roos5251b1d2018-03-23 18:57:43 +0100472 * Sequence number for the current layout pass.
473 */
474 int mLayoutSeq = 0;
475
Chavi Weingarten3a748552018-05-14 17:32:42 +0000476 /**
477 * Specifies the count to determine whether to defer updating the IME target until ready.
478 */
479 private int mDeferUpdateImeTargetCount;
480
Jorim Jaggi9af095b2017-12-12 17:18:57 +0100481 /** Temporary float array to retrieve 3x3 matrix values. */
482 private final float[] mTmpFloats = new float[9];
483
Robert Carr24be9ab2018-04-30 17:54:53 -0700484 private MagnificationSpec mMagnificationSpec;
485
Arthur Hung95b38a92018-07-20 18:56:12 +0800486 private InputMonitor mInputMonitor;
487
Jorim Jaggif1292892018-09-10 11:58:13 +0200488 /** Caches the value whether told display manager that we have content. */
489 private boolean mLastHasContent;
490
Issei Suzuki43190bd2018-08-20 17:28:41 +0200491 private DisplayRotationUtil mRotationUtil = new DisplayRotationUtil();
492
lumark90120a82018-08-15 00:33:03 +0800493 /**
494 * The input method window for this display.
495 */
496 WindowState mInputMethodWindow;
497
lumarkff0ab692018-11-05 20:32:30 +0800498 /**
499 * This just indicates the window the input method is on top of, not
500 * necessarily the window its input is going to.
501 */
502 WindowState mInputMethodTarget;
503
504 /** If true hold off on modifying the animation layer of mInputMethodTarget */
505 boolean mInputMethodTargetWaitingAnim;
506
Arthur Hungbe5ce212018-09-13 18:41:56 +0800507 private final PointerEventDispatcher mPointerEventDispatcher;
508
Tiger Huang7c610aa2018-10-27 00:01:01 +0800509 // Last systemUiVisibility we received from status bar.
510 private int mLastStatusBarVisibility = 0;
511 // Last systemUiVisibility we dispatched to windows.
512 private int mLastDispatchedSystemUiVisibility = 0;
513
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800514 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
515 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800516 final AppWindowToken atoken = w.mAppToken;
517 if (winAnimator.mDrawState == READY_TO_SHOW) {
Jorim Jaggiaf0d6d22018-06-08 15:25:35 +0200518 if (atoken == null || atoken.canShowWindows()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800519 if (w.performShowLocked()) {
520 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
521 if (DEBUG_LAYOUT_REPEATS) {
522 mService.mWindowPlacerLocked.debugLayoutRepeats(
523 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
524 }
525 }
526 }
527 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800528 };
529
530 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
531 final WindowStateAnimator winAnimator = w.mWinAnimator;
532 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
533 return;
534 }
535
Jorim Jaggi8f520872018-08-14 17:00:20 +0200536 // If this window is animating, ensure the animation background is set.
537 final AnimationAdapter anim = w.mAppToken != null
538 ? w.mAppToken.getAnimation()
539 : w.getAnimation();
540 if (anim != null) {
541 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800542 if (color != 0) {
543 final TaskStack stack = w.getStack();
544 if (stack != null) {
545 stack.setAnimationBackground(winAnimator, color);
546 }
547 }
548 }
549 };
550
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800551 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
552 final int lostFocusUid = mTmpWindow.mOwnerUid;
553 final Handler handler = mService.mH;
554 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
555 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
556 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
557 w.mAttrs.hideTimeoutMilliseconds);
558 }
559 }
560 };
561
562 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800563 final AppWindowToken focusedApp = mFocusedApp;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800564 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
565 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
566
567 if (!w.canReceiveKeys()) {
568 return false;
569 }
570
571 final AppWindowToken wtoken = w.mAppToken;
572
573 // If this window's application has been removed, just skip it.
574 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
575 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
576 + (wtoken.removed ? "removed" : "sendingToBottom"));
577 return false;
578 }
579
580 if (focusedApp == null) {
581 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
582 + " using new focus @ " + w);
583 mTmpWindow = w;
584 return true;
585 }
586
587 if (!focusedApp.windowsAreFocusable()) {
588 // Current focused app windows aren't focusable...
589 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
590 + " focusable using new focus @ " + w);
591 mTmpWindow = w;
592 return true;
593 }
594
595 // Descend through all of the app tokens and find the first that either matches
596 // win.mAppToken (return win) or mFocusedApp (return null).
597 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
598 if (focusedApp.compareTo(wtoken) > 0) {
599 // App stack below focused app stack. No focus for you!!!
600 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
601 "findFocusedWindow: Reached focused app=" + focusedApp);
602 mTmpWindow = null;
603 return true;
604 }
605 }
606
607 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
608 mTmpWindow = w;
609 return true;
610 };
611
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800612 private final Consumer<WindowState> mPerformLayout = w -> {
613 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
614 // wasting time and funky changes while a window is animating away.
615 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
616 || w.isGoneForLayoutLw();
617
618 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
619 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
620 + " mLayoutAttached=" + w.mLayoutAttached
621 + " screen changed=" + w.isConfigChanged());
622 final AppWindowToken atoken = w.mAppToken;
623 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200624 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800625 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
626 + " parentHidden=" + w.isParentWindowHidden());
627 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200628 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800629 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
630 + " parentHidden=" + w.isParentWindowHidden());
631 }
632
633 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
634 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
635 // since that means "perform layout as normal, just don't display").
636 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
637 || ((w.isConfigChanged() || w.setReportResizeHints())
638 && !w.isGoneForLayoutLw() &&
639 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
640 (w.mHasSurface && w.mAppToken != null &&
641 w.mAppToken.layoutConfigChanges)))) {
642 if (!w.mLayoutAttached) {
643 if (mTmpInitial) {
644 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700645 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800646 }
647 if (w.mAttrs.type == TYPE_DREAM) {
648 // Don't layout windows behind a dream, so that if it does stuff like hide
649 // the status bar we won't get a bad transition when it goes away.
650 mTmpWindow = w;
651 }
652 w.mLayoutNeeded = false;
653 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200654 final boolean firstLayout = !w.isLaidOut();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800655 getDisplayPolicy().layoutWindowLw(w, null, mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100656 w.mLayoutSeq = mLayoutSeq;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800657
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200658 // If this is the first layout, we need to initialize the last inset values as
659 // otherwise we'd immediately cause an unnecessary resize.
660 if (firstLayout) {
661 w.updateLastInsetValues();
662 }
663
Adrian Roos23df3a32018-03-15 15:41:13 +0100664 if (w.mAppToken != null) {
665 w.mAppToken.layoutLetterbox(w);
666 }
667
chaviw492139a2018-07-16 16:07:35 -0700668 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700669 + " mContainingFrame=" + w.getContainingFrame()
670 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800671 }
672 }
673 };
674
675 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
676 if (w.mLayoutAttached) {
677 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
678 + " mViewVisibility=" + w.mViewVisibility
679 + " mRelayoutCalled=" + w.mRelayoutCalled);
680 // If this view is GONE, then skip it -- keep the current frame, and let the caller
681 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
682 // windows, since that means "perform layout as normal, just don't display").
683 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
684 return;
685 }
686 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
687 || w.mLayoutNeeded) {
688 if (mTmpInitial) {
689 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700690 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800691 }
692 w.mLayoutNeeded = false;
693 w.prelayout();
Tiger Huang7c610aa2018-10-27 00:01:01 +0800694 getDisplayPolicy().layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100695 w.mLayoutSeq = mLayoutSeq;
chaviw492139a2018-07-16 16:07:35 -0700696 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700697 + " mContainingFrame=" + w.getContainingFrame()
698 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800699 }
700 } else if (w.mAttrs.type == TYPE_DREAM) {
701 // Don't layout windows behind a dream, so that if it does stuff like hide the
702 // status bar we won't get a bad transition when it goes away.
703 mTmpWindow = mTmpWindow2;
704 }
705 };
706
707 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
708 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
709 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
710 return w.canBeImeTarget();
711 };
712
713 private final Consumer<WindowState> mApplyPostLayoutPolicy =
Tiger Huang7c610aa2018-10-27 00:01:01 +0800714 w -> getDisplayPolicy().applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
lumarkff0ab692018-11-05 20:32:30 +0800715 mInputMethodTarget);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800716
717 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
718 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
719 final boolean obscuredChanged = w.mObscured !=
720 mTmpApplySurfaceChangesTransactionState.obscured;
721 final RootWindowContainer root = mService.mRoot;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800722
723 // Update effect.
724 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
725 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
726 final boolean isDisplayed = w.isDisplayedLw();
727
728 if (isDisplayed && w.isObscuringDisplay()) {
729 // This window completely covers everything behind it, so we want to leave all
730 // of them as undimmed (for performance reasons).
731 root.mObscuringWindow = w;
732 mTmpApplySurfaceChangesTransactionState.obscured = true;
733 }
734
735 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
736 root.handleNotObscuredLocked(w,
737 mTmpApplySurfaceChangesTransactionState.obscured,
738 mTmpApplySurfaceChangesTransactionState.syswin);
739
740 if (w.mHasSurface && isDisplayed) {
741 final int type = w.mAttrs.type;
742 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
743 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
744 mTmpApplySurfaceChangesTransactionState.syswin = true;
745 }
746 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
747 && w.mAttrs.preferredRefreshRate != 0) {
748 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
749 = w.mAttrs.preferredRefreshRate;
750 }
751 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
752 && w.mAttrs.preferredDisplayModeId != 0) {
753 mTmpApplySurfaceChangesTransactionState.preferredModeId
754 = w.mAttrs.preferredDisplayModeId;
755 }
756 }
757 }
758
wilsonshihc32538e2018-11-07 17:27:34 +0800759 if (obscuredChanged && w.isVisibleLw() && mWallpaperController.isWallpaperTarget(w)) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800760 // This is the wallpaper target and its obscured state changed... make sure the
761 // current wallpaper's visibility has been updated accordingly.
762 mWallpaperController.updateWallpaperVisibility();
763 }
764
chaviw161ea3e2018-01-31 12:01:12 -0800765 w.handleWindowMovedIfNeeded();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800766
767 final WindowStateAnimator winAnimator = w.mWinAnimator;
768
769 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
chaviw1454b392018-08-06 09:54:04 -0700770 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800771
772 // Moved from updateWindowsAndWallpaperLocked().
773 if (w.mHasSurface) {
774 // Take care of the window being ready to display.
775 final boolean committed = winAnimator.commitFinishDrawingLocked();
776 if (isDefaultDisplay && committed) {
777 if (w.mAttrs.type == TYPE_DREAM) {
778 // HACK: When a dream is shown, it may at that point hide the lock screen.
779 // So we need to redo the layout to let the phone window manager make this
780 // happen.
781 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
782 if (DEBUG_LAYOUT_REPEATS) {
783 surfacePlacer.debugLayoutRepeats(
784 "dream and commitFinishDrawingLocked true",
785 pendingLayoutChanges);
786 }
787 }
788 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
789 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
790 "First draw done in potential wallpaper target " + w);
wilsonshihc32538e2018-11-07 17:27:34 +0800791 mWallpaperMayChange = true;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800792 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
793 if (DEBUG_LAYOUT_REPEATS) {
794 surfacePlacer.debugLayoutRepeats(
795 "wallpaper and commitFinishDrawingLocked true",
796 pendingLayoutChanges);
797 }
798 }
799 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800800 }
801
802 final AppWindowToken atoken = w.mAppToken;
803 if (atoken != null) {
Adrian Roos23df3a32018-03-15 15:41:13 +0100804 atoken.updateLetterboxSurface(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800805 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
806 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
807 mTmpUpdateAllDrawn.add(atoken);
808 }
809 }
810
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800811 if (!mLosingFocus.isEmpty() && w.isFocused() && w.isDisplayedLw()) {
812 mService.mH.obtainMessage(REPORT_LOSING_FOCUS, this).sendToTarget();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800813 }
814
815 w.updateResizingWindowIfNeeded();
816 };
817
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800818 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800819 * Create new {@link DisplayContent} instance, add itself to the root window container and
820 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700821 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800822 * @param service You know.
wilsonshihc32538e2018-11-07 17:27:34 +0800823 * @param controller The controller for the display container.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700824 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700825 DisplayContent(Display display, WindowManagerService service,
wilsonshihc32538e2018-11-07 17:27:34 +0800826 DisplayWindowController controller) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100827 super(service);
Bryce Leef19cbe22018-02-02 15:09:21 -0800828 setController(controller);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800829 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
830 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
831 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
832 + " new=" + display);
833 }
834
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700835 mDisplay = display;
836 mDisplayId = display.getDisplayId();
wilsonshihc32538e2018-11-07 17:27:34 +0800837 mWallpaperController = new WallpaperController(mService, this);
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700838 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700839 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700840 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100841 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo,
842 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700843 initializeDisplayBaseInfo();
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700844
lumark588a3e82018-07-20 18:53:54 +0800845 mAppTransition = new AppTransition(service.mContext, service, this);
846 mAppTransition.registerListenerLocked(service.mActivityManagerAppTransitionNotifier);
847 mAppTransitionController = new AppTransitionController(service, this);
848 mUnknownAppVisibilityController = new UnknownAppVisibilityController(service, this);
849
850 AnimationHandler animationHandler = new AnimationHandler();
851 mBoundsAnimationController = new BoundsAnimationController(service.mContext,
852 mAppTransition, SurfaceAnimationThread.getHandler(), animationHandler);
853
Tiger Huang7c610aa2018-10-27 00:01:01 +0800854 if (mService.mInputManager != null) {
855 final InputChannel inputChannel = mService.mInputManager.monitorInput("Display "
856 + mDisplayId, mDisplayId);
857 mPointerEventDispatcher = inputChannel != null
858 ? new PointerEventDispatcher(inputChannel) : null;
859 } else {
860 mPointerEventDispatcher = null;
861 }
862 mDisplayPolicy = new DisplayPolicy(service, this);
863 mDisplayRotation = new DisplayRotation(service, this);
864 if (isDefaultDisplay) {
865 // The policy may be invoked right after here, so it requires the necessary default
866 // fields of this display content.
867 mService.mPolicy.setDefaultDisplay(this);
868 }
869 if (mService.mDisplayReady) {
870 mDisplayPolicy.onConfigurationChanged();
871 }
872 if (mService.mSystemReady) {
873 mDisplayPolicy.systemReady();
874 }
875 mDividerControllerLocked = new DockedStackDividerController(service, this);
876 mPinnedStackControllerLocked = new PinnedStackController(service, this);
877
Vishnu Naire86bd982018-11-28 13:23:17 -0800878 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession).setOpaque(true);
Robert Carrb1579c82017-09-05 14:54:47 -0700879 mWindowingLayer = b.setName("Display Root").build();
880 mOverlayLayer = b.setName("Display Overlays").build();
881
Robert Carrf59b8dd2017-10-02 18:58:36 -0700882 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700883 .setLayerStack(mWindowingLayer, mDisplayId)
884 .show(mWindowingLayer)
885 .setLayer(mOverlayLayer, 1)
886 .setLayerStack(mOverlayLayer, mDisplayId)
887 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700888 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700889
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700890 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700891 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700892 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700893 super.addChild(mAboveAppWindowsContainers, null);
894 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800895
896 // Add itself as a child to the root container.
897 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700898
899 // TODO(b/62541591): evaluate whether this is the best spot to declare the
900 // {@link DisplayContent} ready for use.
901 mDisplayReady = true;
Arthur Hung95b38a92018-07-20 18:56:12 +0800902
Riddle Hsuf53da812018-08-15 22:00:27 +0800903 mService.mAnimator.addDisplayLocked(mDisplayId);
Arthur Hung39134b22018-08-14 11:58:28 +0800904 mInputMonitor = new InputMonitor(service, mDisplayId);
Bryce Leed1871262017-06-12 14:12:29 -0700905 }
906
907 boolean isReady() {
908 // The display is ready when the system and the individual display are both ready.
909 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700910 }
911
912 int getDisplayId() {
913 return mDisplayId;
914 }
915
Wale Ogunwale02319a62016-09-26 15:21:22 -0700916 WindowToken getWindowToken(IBinder binder) {
917 return mTokenMap.get(binder);
918 }
919
920 AppWindowToken getAppWindowToken(IBinder binder) {
921 final WindowToken token = getWindowToken(binder);
922 if (token == null) {
923 return null;
924 }
925 return token.asAppWindowToken();
926 }
927
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700928 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700929 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
930 if (dc != null) {
931 // We currently don't support adding a window token to the display if the display
932 // already has the binder mapped to another token. If there is a use case for supporting
933 // this moving forward we will either need to merge the WindowTokens some how or have
934 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700935 throw new IllegalArgumentException("Can't map token=" + token + " to display="
936 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700937 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700938 if (binder == null) {
939 throw new IllegalArgumentException("Can't map token=" + token + " to display="
940 + getName() + " binder is null");
941 }
942 if (token == null) {
943 throw new IllegalArgumentException("Can't map null token to display="
944 + getName() + " binder=" + binder);
945 }
946
Wale Ogunwale02319a62016-09-26 15:21:22 -0700947 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700948
949 if (token.asAppWindowToken() == null) {
950 // Add non-app token to container hierarchy on the display. App tokens are added through
951 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700952 switch (token.windowType) {
953 case TYPE_WALLPAPER:
954 mBelowAppWindowsContainers.addChild(token);
955 break;
956 case TYPE_INPUT_METHOD:
957 case TYPE_INPUT_METHOD_DIALOG:
958 mImeWindowsContainers.addChild(token);
959 break;
960 default:
961 mAboveAppWindowsContainers.addChild(token);
962 break;
963 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700964 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700965 }
966
967 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700968 final WindowToken token = mTokenMap.remove(binder);
969 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800970 token.setExiting();
971 }
972 return token;
973 }
974
975 /** Changes the display the input window token is housed on to this one. */
976 void reParentWindowToken(WindowToken token) {
977 final DisplayContent prevDc = token.getDisplayContent();
978 if (prevDc == this) {
979 return;
980 }
Riddle Hsu85bd04b2018-11-17 00:34:36 +0800981 if (prevDc != null) {
982 if (prevDc.mTokenMap.remove(token.token) != null && token.asAppWindowToken() == null) {
983 // Removed the token from the map, but made sure it's not an app token before
984 // removing from parent.
985 token.getParent().removeChild(token);
986 }
987 if (prevDc.mLastFocus == mCurrentFocus) {
988 // The window has become the focus of this display, so it should not be notified
989 // that it lost focus from the previous display.
990 prevDc.mLastFocus = null;
991 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700992 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800993
994 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700995 }
996
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700997 void removeAppToken(IBinder binder) {
998 final WindowToken token = removeWindowToken(binder);
999 if (token == null) {
1000 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
1001 return;
1002 }
1003
1004 final AppWindowToken appToken = token.asAppWindowToken();
1005
1006 if (appToken == null) {
1007 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
1008 return;
1009 }
1010
1011 appToken.onRemovedFromDisplay();
1012 }
1013
Riddle Hsuad256a12018-07-18 16:11:30 +08001014 @Override
Riddle Hsu86cb7de2018-08-13 23:29:58 +08001015 DisplayWindowController getController() {
1016 return (DisplayWindowController) super.getController();
1017 }
1018
1019 @Override
Riddle Hsuad256a12018-07-18 16:11:30 +08001020 public Display getDisplay() {
Craig Mautnerb47bbc32012-08-22 17:41:48 -07001021 return mDisplay;
1022 }
1023
Craig Mautner59c00972012-07-30 12:10:24 -07001024 DisplayInfo getDisplayInfo() {
1025 return mDisplayInfo;
1026 }
1027
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001028 DisplayMetrics getDisplayMetrics() {
1029 return mDisplayMetrics;
1030 }
1031
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001032 DisplayPolicy getDisplayPolicy() {
1033 return mDisplayPolicy;
1034 }
1035
Riddle Hsuad256a12018-07-18 16:11:30 +08001036 @Override
1037 public DisplayRotation getDisplayRotation() {
1038 return mDisplayRotation;
1039 }
1040
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001041 @VisibleForTesting
1042 void setDisplayRotation(DisplayRotation displayRotation) {
1043 mDisplayRotation = displayRotation;
1044 }
1045
Andrii Kulian8ee72852017-03-10 10:36:45 -08001046 int getRotation() {
1047 return mRotation;
1048 }
1049
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001050 @VisibleForTesting
Andrii Kulian8ee72852017-03-10 10:36:45 -08001051 void setRotation(int newRotation) {
1052 mRotation = newRotation;
Riddle Hsuad256a12018-07-18 16:11:30 +08001053 mDisplayRotation.setRotation(newRotation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08001054 }
1055
1056 int getLastOrientation() {
1057 return mLastOrientation;
1058 }
1059
Andrii Kulian8ee72852017-03-10 10:36:45 -08001060 boolean getAltOrientation() {
1061 return mAltOrientation;
1062 }
1063
Andrii Kulian8ee72852017-03-10 10:36:45 -08001064 int getLastWindowForcedOrientation() {
1065 return mLastWindowForcedOrientation;
1066 }
1067
Andrii Kulian06d07d62017-03-14 11:11:47 -07001068 /**
Riddle Hsu654a6f92018-07-13 22:59:36 +08001069 * Temporarily pauses rotation changes until resumed.
1070 *
1071 * This can be used to prevent rotation changes from occurring while the user is
1072 * performing certain operations, such as drag and drop.
1073 *
1074 * This call nests and must be matched by an equal number of calls to
1075 * {@link #resumeRotationLocked}.
1076 */
1077 void pauseRotationLocked() {
1078 mDeferredRotationPauseCount++;
1079 }
1080
1081 /**
1082 * Resumes normal rotation changes after being paused.
1083 */
1084 void resumeRotationLocked() {
1085 if (mDeferredRotationPauseCount <= 0) {
1086 return;
1087 }
1088
1089 mDeferredRotationPauseCount--;
1090 if (mDeferredRotationPauseCount == 0) {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001091 updateRotationAndSendNewConfigIfNeeded();
Riddle Hsu654a6f92018-07-13 22:59:36 +08001092 }
1093 }
1094
1095 /**
Riddle Hsuad256a12018-07-18 16:11:30 +08001096 * If this is true we have updated our desired orientation, but not yet changed the real
1097 * orientation our applied our screen rotation animation. For example, because a previous
1098 * screen rotation was in progress.
1099 *
1100 * @return {@code true} if the there is an ongoing rotation change.
1101 */
1102 boolean rotationNeedsUpdate() {
1103 final int lastOrientation = getLastOrientation();
1104 final int oldRotation = getRotation();
1105 final boolean oldAltOrientation = getAltOrientation();
1106
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001107 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Riddle Hsuad256a12018-07-18 16:11:30 +08001108 final boolean altOrientation = !mDisplayRotation.rotationHasCompatibleMetrics(
1109 lastOrientation, rotation);
1110 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1111 return false;
1112 }
1113 return true;
1114 }
1115
Riddle Hsu4e611772018-10-31 18:58:28 +08001116 /** Notify the configuration change of this display. */
1117 void sendNewConfiguration() {
1118 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, this).sendToTarget();
1119 }
1120
1121 /**
1122 * Determine the new desired orientation of this display.
1123 *
1124 * The orientation is computed from non-application windows first. If none of the
1125 * non-application windows specify orientation, the orientation is computed from application
1126 * tokens.
1127 *
1128 * @return {@code true} if the orientation is changed.
1129 */
1130 boolean updateOrientationFromAppTokens() {
1131 return updateOrientationFromAppTokens(false /* forceUpdate */);
1132 }
1133
1134 boolean updateOrientationFromAppTokens(boolean forceUpdate) {
1135 final int req = getOrientation();
1136 if (req != mLastOrientation || forceUpdate) {
1137 mLastOrientation = req;
1138 mDisplayRotation.setCurrentOrientation(req);
1139 return updateRotationUnchecked(forceUpdate);
1140 }
1141 return false;
1142 }
1143
Riddle Hsuad256a12018-07-18 16:11:30 +08001144 /**
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001145 * Update rotation of the display and send configuration if the rotation is changed.
1146 *
1147 * @return {@code true} if the rotation has been changed and the new config is sent.
1148 */
1149 boolean updateRotationAndSendNewConfigIfNeeded() {
1150 final boolean changed = updateRotationUnchecked(false /* forceUpdate */);
1151 if (changed) {
Riddle Hsu4e611772018-10-31 18:58:28 +08001152 sendNewConfiguration();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001153 }
1154 return changed;
1155 }
1156
1157 /**
Andrii Kulian06d07d62017-03-14 11:11:47 -07001158 * Update rotation of the display.
1159 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001160 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
1161 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001162 */
Robert Carrae606b42018-02-15 15:36:23 -08001163 boolean updateRotationUnchecked() {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001164 return updateRotationUnchecked(false /* forceUpdate */);
1165 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001166
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001167 /**
Evan Roskye747c3e2018-10-30 20:06:41 -07001168 * Update rotation of the DisplayContent with an option to force the update. This updates
1169 * the container's perception of rotation and, depending on the top activities, will freeze
1170 * the screen or start seamless rotation. The display itself gets rotated in
1171 * {@link #applyRotationLocked} during {@link WindowManagerService#sendNewConfiguration}.
1172 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001173 * @param forceUpdate Force the rotation update. Sometimes in WM we might skip updating
1174 * orientation because we're waiting for some rotation to finish or display
1175 * to unfreeze, which results in configuration of the previously visible
1176 * activity being applied to a newly visible one. Forcing the rotation
1177 * update allows to workaround this issue.
1178 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
Evan Roskye747c3e2018-10-30 20:06:41 -07001179 * {@link WindowManagerService#sendNewConfiguration(int)} TO COMPLETE THE ROTATION AND
1180 * UNFREEZE THE SCREEN.
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001181 */
1182 boolean updateRotationUnchecked(boolean forceUpdate) {
1183 ScreenRotationAnimation screenRotationAnimation;
1184 if (!forceUpdate) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001185 if (mDeferredRotationPauseCount > 0) {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001186 // Rotation updates have been paused temporarily. Defer the update until
1187 // updates have been resumed.
1188 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
1189 return false;
1190 }
1191
1192 screenRotationAnimation =
1193 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
1194 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
1195 // Rotation updates cannot be performed while the previous rotation change
1196 // animation is still in progress. Skip this update. We will try updating
1197 // again after the animation is finished and the display is unfrozen.
1198 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
1199 return false;
1200 }
1201 if (mService.mDisplayFrozen) {
1202 // Even if the screen rotation animation has finished (e.g. isAnimating
1203 // returns false), there is still some time where we haven't yet unfrozen
1204 // the display. We also need to abort rotation here.
1205 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
1206 "Deferring rotation, still finishing previous rotation");
1207 return false;
1208 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001209 }
1210
1211 if (!mService.mDisplayEnabled) {
1212 // No point choosing a rotation if the display is not enabled.
1213 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
1214 return false;
1215 }
1216
1217 final int oldRotation = mRotation;
1218 final int lastOrientation = mLastOrientation;
1219 final boolean oldAltOrientation = mAltOrientation;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001220 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001221 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Computed rotation=" + rotation + " for display id="
1222 + mDisplayId + " based on lastOrientation=" + lastOrientation
1223 + " and oldRotation=" + oldRotation);
Tiger Huang7c610aa2018-10-27 00:01:01 +08001224 boolean mayRotateSeamlessly = mDisplayPolicy.shouldRotateSeamlessly(mDisplayRotation,
Riddle Hsuad256a12018-07-18 16:11:30 +08001225 oldRotation, rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001226
Robert Carr0e007272017-10-02 13:00:55 -07001227 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001228 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001229 if (seamlessRotated != null && !forceUpdate) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001230 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
1231 // to complete (that is, waiting for windows to redraw). It's tempting to check
1232 // w.mSeamlessRotationCount but that could be incorrect in the case of
1233 // window-removal.
1234 return false;
1235 }
Robert Carr0e007272017-10-02 13:00:55 -07001236
1237 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001238 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -07001239 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -07001240 mayRotateSeamlessly = false;
1241 }
1242 for (int i = 0; i < mService.mSessions.size(); i++) {
1243 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
1244 mayRotateSeamlessly = false;
1245 break;
1246 }
1247 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001248 }
Robert Carr0e007272017-10-02 13:00:55 -07001249 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001250
1251 // TODO: Implement forced rotation changes.
1252 // Set mAltOrientation to indicate that the application is receiving
1253 // an orientation that has different metrics than it expected.
1254 // eg. Portrait instead of Landscape.
1255
Riddle Hsuad256a12018-07-18 16:11:30 +08001256 final boolean altOrientation = !mDisplayRotation.rotationHasCompatibleMetrics(
Andrii Kulian06d07d62017-03-14 11:11:47 -07001257 lastOrientation, rotation);
1258
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001259 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1260 + " selected orientation " + lastOrientation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001261 + ", got rotation " + rotation + " which has "
1262 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1263
1264 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1265 // No change.
1266 return false;
1267 }
1268
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001269 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1270 + " rotation changed to " + rotation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001271 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1272 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1273
1274 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
Vishnu Nairba183352018-11-21 11:16:49 -08001275 mWaitingForConfig = true;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001276 }
1277
Evan Roskye747c3e2018-10-30 20:06:41 -07001278 mRotation = rotation;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001279 mAltOrientation = altOrientation;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001280
1281 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
Riddle Hsu654a6f92018-07-13 22:59:36 +08001282 mService.mH.sendNewMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1283 this, WINDOW_FREEZE_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001284
1285 setLayoutNeeded();
1286 final int[] anim = new int[2];
Tiger Huang7c610aa2018-10-27 00:01:01 +08001287 mDisplayPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001288
1289 if (!rotateSeamlessly) {
Robert Carrae606b42018-02-15 15:36:23 -08001290 mService.startFreezingDisplayLocked(anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001291 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001292 } else {
1293 // The screen rotation animation uses a screenshot to freeze the screen
1294 // while windows resize underneath.
1295 // When we are rotating seamlessly, we allow the elements to transition
1296 // to their rotated state independently and without a freeze required.
Robert Carrefc75702018-02-16 11:46:16 -08001297 mService.startSeamlessRotation();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001298 }
1299
Evan Roskye747c3e2018-10-30 20:06:41 -07001300 return true;
1301 }
1302
1303 /**
1304 * Applies the rotation transaction. This must be called after {@link #updateRotationUnchecked}
1305 * (if it returned {@code true}) to actually finish the rotation.
1306 *
1307 * @param oldRotation the rotation we are coming from.
1308 * @param rotation the rotation to apply.
1309 */
1310 void applyRotationLocked(final int oldRotation, final int rotation) {
1311 mDisplayRotation.setRotation(rotation);
1312 final boolean rotateSeamlessly = mService.isRotatingSeamlessly();
1313 ScreenRotationAnimation screenRotationAnimation = rotateSeamlessly
1314 ? null : mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001315 // We need to update our screen size information to match the new rotation. If the rotation
1316 // has actually changed then this method will return true and, according to the comment at
1317 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1318 // By updating the Display info here it will be available to
1319 // #computeScreenConfiguration() later.
1320 updateDisplayAndOrientation(getConfiguration().uiMode);
1321
Robert Carrae606b42018-02-15 15:36:23 -08001322 // NOTE: We disable the rotation in the emulator because
1323 // it doesn't support hardware OpenGL emulation yet.
1324 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1325 && screenRotationAnimation.hasScreenshot()) {
1326 if (screenRotationAnimation.setRotation(getPendingTransaction(), rotation,
1327 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1328 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1329 mService.scheduleAnimationLocked();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001330 }
1331 }
1332
Vishnu Nair83537a72018-07-19 21:27:48 -07001333 forAllWindows(w -> {
Vishnu Nairddd80742018-08-21 14:12:46 -07001334 w.seamlesslyRotateIfAllowed(getPendingTransaction(), oldRotation, rotation,
1335 rotateSeamlessly);
Vishnu Nair83537a72018-07-19 21:27:48 -07001336 }, true /* traverseTopToBottom */);
1337
Robert Carrae606b42018-02-15 15:36:23 -08001338 mService.mDisplayManagerInternal.performTraversal(getPendingTransaction());
1339 scheduleAnimation();
1340
Andrii Kulian06d07d62017-03-14 11:11:47 -07001341 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001342 if (w.mHasSurface && !rotateSeamlessly) {
1343 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001344 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001345 mService.mRoot.mOrientationChangeComplete = false;
1346 w.mLastFreezeDuration = 0;
1347 }
1348 w.mReportOrientationChanged = true;
1349 }, true /* traverseTopToBottom */);
1350
1351 if (rotateSeamlessly) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001352 mService.mH.sendNewMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1353 this, SEAMLESS_ROTATION_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001354 }
1355
1356 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1357 final WindowManagerService.RotationWatcher rotationWatcher
1358 = mService.mRotationWatchers.get(i);
1359 if (rotationWatcher.mDisplayId == mDisplayId) {
1360 try {
1361 rotationWatcher.mWatcher.onRotationChanged(rotation);
1362 } catch (RemoteException e) {
1363 // Ignore
1364 }
1365 }
1366 }
1367
1368 // TODO (multi-display): Magnification is supported only for the default display.
1369 // Announce rotation only if we will not animate as we already have the
1370 // windows in final state. Otherwise, we make this call at the rotation end.
1371 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1372 && isDefaultDisplay) {
1373 mService.mAccessibilityController.onRotationChangedLocked(this);
1374 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001375 }
1376
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001377 void configureDisplayPolicy() {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001378 final int width = mBaseDisplayWidth;
1379 final int height = mBaseDisplayHeight;
1380 final int shortSize;
1381 final int longSize;
1382 if (width > height) {
1383 shortSize = height;
1384 longSize = width;
1385 } else {
1386 shortSize = width;
1387 longSize = height;
1388 }
1389
1390 final int shortSizeDp = shortSize * DisplayMetrics.DENSITY_DEFAULT / mBaseDisplayDensity;
1391 final int longSizeDp = longSize * DisplayMetrics.DENSITY_DEFAULT / mBaseDisplayDensity;
1392
1393 mDisplayRotation.configure(width, height, shortSizeDp, longSizeDp);
1394 mDisplayPolicy.configure(width, height, shortSizeDp);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001395
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001396 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
1397 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Arthur Hungbe5ce212018-09-13 18:41:56 +08001398
1399 // Tap Listeners are supported for:
1400 // 1. All physical displays (multi-display).
1401 // 2. VirtualDisplays on VR, AA (and everything else).
1402 if (mPointerEventDispatcher != null && mTapDetector == null) {
1403 if (DEBUG_DISPLAY) {
1404 Slog.d(TAG,
1405 "Registering PointerEventListener for DisplayId: " + mDisplayId);
1406 }
1407 mTapDetector = new TaskTapPointerEventListener(mService, this);
1408 registerPointerEventListener(mTapDetector);
1409 registerPointerEventListener(mService.mMousePositionTracker);
1410 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001411 }
1412
Andrii Kulian06d07d62017-03-14 11:11:47 -07001413 /**
1414 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1415 * changed.
1416 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1417 */
1418 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1419 // Use the effective "visual" dimensions based on current rotation
1420 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1421 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1422 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1423 int dw = realdw;
1424 int dh = realdh;
1425
1426 if (mAltOrientation) {
1427 if (realdw > realdh) {
1428 // Turn landscape into portrait.
1429 int maxw = (int)(realdh/1.3f);
1430 if (maxw < realdw) {
1431 dw = maxw;
1432 }
1433 } else {
1434 // Turn portrait into landscape.
1435 int maxh = (int)(realdw/1.3f);
1436 if (maxh < realdh) {
1437 dh = maxh;
1438 }
1439 }
1440 }
1441
1442 // Update application display metrics.
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001443 final WmDisplayCutout wmDisplayCutout = calculateDisplayCutoutForRotation(mRotation);
1444 final DisplayCutout displayCutout = wmDisplayCutout.getDisplayCutout();
1445
Tiger Huang7c610aa2018-10-27 00:01:01 +08001446 final int appWidth = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
1447 displayCutout);
1448 final int appHeight = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
1449 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001450 mDisplayInfo.rotation = mRotation;
1451 mDisplayInfo.logicalWidth = dw;
1452 mDisplayInfo.logicalHeight = dh;
1453 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1454 mDisplayInfo.appWidth = appWidth;
1455 mDisplayInfo.appHeight = appHeight;
1456 if (isDefaultDisplay) {
1457 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1458 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1459 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001460 mDisplayInfo.displayCutout = displayCutout.isEmpty() ? null : displayCutout;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001461 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1462 if (mDisplayScalingDisabled) {
1463 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1464 } else {
1465 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1466 }
1467
Andrii Kulianf0379de2018-03-14 16:24:07 -07001468 // We usually set the override info in DisplayManager so that we get consistent display
1469 // metrics values when displays are changing and don't send out new values until WM is aware
1470 // of them. However, we don't do this for displays that serve as containers for ActivityView
1471 // because we don't want letter-/pillar-boxing during resize.
1472 final DisplayInfo overrideDisplayInfo = mShouldOverrideDisplayConfiguration
1473 ? mDisplayInfo : null;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001474 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
Andrii Kulianf0379de2018-03-14 16:24:07 -07001475 overrideDisplayInfo);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001476
1477 mBaseDisplayRect.set(0, 0, dw, dh);
1478
1479 if (isDefaultDisplay) {
1480 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1481 mCompatDisplayMetrics);
1482 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001483
Andrii Kulian06d07d62017-03-14 11:11:47 -07001484 return mDisplayInfo;
1485 }
1486
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001487 WmDisplayCutout calculateDisplayCutoutForRotation(int rotation) {
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001488 return mDisplayCutoutCache.getOrCompute(mInitialDisplayCutout, rotation);
1489 }
1490
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001491 private WmDisplayCutout calculateDisplayCutoutForRotationUncached(
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001492 DisplayCutout cutout, int rotation) {
Adrian Roos24264212018-02-19 16:26:15 +01001493 if (cutout == null || cutout == DisplayCutout.NO_CUTOUT) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001494 return WmDisplayCutout.NO_CUTOUT;
Adrian Roos1cf585052018-01-03 18:43:27 +01001495 }
Adrian Roos11c25582018-02-19 18:06:36 +01001496 if (rotation == ROTATION_0) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001497 return WmDisplayCutout.computeSafeInsets(
1498 cutout, mInitialDisplayWidth, mInitialDisplayHeight);
Adrian Roos24264212018-02-19 16:26:15 +01001499 }
Adrian Roosbed538e2018-02-21 17:50:07 +01001500 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Issei Suzuki43190bd2018-08-20 17:28:41 +02001501 final Rect[] newBounds = mRotationUtil.getRotatedBounds(
1502 WmDisplayCutout.computeSafeInsets(
Jorim Jaggi60640512018-06-29 01:14:31 +02001503 cutout, mInitialDisplayWidth, mInitialDisplayHeight)
Issei Suzuki43190bd2018-08-20 17:28:41 +02001504 .getDisplayCutout().getBoundingRectsAll(),
1505 rotation, mInitialDisplayWidth, mInitialDisplayHeight);
1506 return WmDisplayCutout.computeSafeInsets(DisplayCutout.fromBounds(newBounds),
Adrian Roos24264212018-02-19 16:26:15 +01001507 rotated ? mInitialDisplayHeight : mInitialDisplayWidth,
1508 rotated ? mInitialDisplayWidth : mInitialDisplayHeight);
Adrian Roos1cf585052018-01-03 18:43:27 +01001509 }
1510
Andrii Kulian06d07d62017-03-14 11:11:47 -07001511 /**
1512 * Compute display configuration based on display properties and policy settings.
1513 * Do not call if mDisplayReady == false.
1514 */
1515 void computeScreenConfiguration(Configuration config) {
1516 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
Evan Roskye747c3e2018-10-30 20:06:41 -07001517 calculateBounds(displayInfo, mTmpBounds);
1518 config.windowConfiguration.setBounds(mTmpBounds);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001519
1520 final int dw = displayInfo.logicalWidth;
1521 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001522 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
Garfield Tane0846042018-07-26 13:42:04 -07001523 config.windowConfiguration.setWindowingMode(getWindowingMode());
Evan Roskye747c3e2018-10-30 20:06:41 -07001524 config.windowConfiguration.setRotation(displayInfo.rotation);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001525
Adrian Roos11c25582018-02-19 18:06:36 +01001526 final float density = mDisplayMetrics.density;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001527 config.screenWidthDp =
Tiger Huang7c610aa2018-10-27 00:01:01 +08001528 (int)(mDisplayPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
1529 config.uiMode, displayInfo.displayCutout) / density);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001530 config.screenHeightDp =
Tiger Huang7c610aa2018-10-27 00:01:01 +08001531 (int)(mDisplayPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
1532 config.uiMode, displayInfo.displayCutout) / density);
Bryce Lee7566d762017-03-30 09:34:15 -07001533
Tiger Huang7c610aa2018-10-27 00:01:01 +08001534 mDisplayPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh,
Adrian Roos11c25582018-02-19 18:06:36 +01001535 displayInfo.displayCutout, mTmpRect);
Bryce Lee7566d762017-03-30 09:34:15 -07001536 final int leftInset = mTmpRect.left;
1537 final int topInset = mTmpRect.top;
1538 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001539 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1540 leftInset + displayInfo.appWidth /* right */,
1541 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001542 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1543 || displayInfo.rotation == Surface.ROTATION_270);
1544
Tiger Huang7c610aa2018-10-27 00:01:01 +08001545 computeSizeRangesAndScreenLayout(displayInfo, rotated, config.uiMode, dw, dh, density,
1546 config);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001547
1548 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1549 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1550 ? Configuration.SCREENLAYOUT_ROUND_YES
1551 : Configuration.SCREENLAYOUT_ROUND_NO);
1552
1553 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1554 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1555 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001556 dh, displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001557 config.densityDpi = displayInfo.logicalDensityDpi;
1558
1559 config.colorMode =
Chia-I Wuf739bad2018-05-09 12:36:57 -07001560 ((displayInfo.isHdr() && mService.hasHdrSupport())
Andrii Kulian06d07d62017-03-14 11:11:47 -07001561 ? Configuration.COLOR_MODE_HDR_YES
1562 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001563 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001564 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1565 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1566
1567 // Update the configuration based on available input devices, lid switch,
1568 // and platform configuration.
1569 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1570 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1571 config.navigation = Configuration.NAVIGATION_NONAV;
1572
1573 int keyboardPresence = 0;
1574 int navigationPresence = 0;
1575 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1576 final int len = devices != null ? devices.length : 0;
1577 for (int i = 0; i < len; i++) {
1578 InputDevice device = devices[i];
1579 if (!device.isVirtual()) {
1580 final int sources = device.getSources();
1581 final int presenceFlag = device.isExternal() ?
1582 WindowManagerPolicy.PRESENCE_EXTERNAL :
1583 WindowManagerPolicy.PRESENCE_INTERNAL;
1584
1585 // TODO(multi-display): Configure on per-display basis.
1586 if (mService.mIsTouchDevice) {
1587 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1588 InputDevice.SOURCE_TOUCHSCREEN) {
1589 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1590 }
1591 } else {
1592 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1593 }
1594
1595 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1596 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1597 navigationPresence |= presenceFlag;
1598 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1599 && config.navigation == Configuration.NAVIGATION_NONAV) {
1600 config.navigation = Configuration.NAVIGATION_DPAD;
1601 navigationPresence |= presenceFlag;
1602 }
1603
1604 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1605 config.keyboard = Configuration.KEYBOARD_QWERTY;
1606 keyboardPresence |= presenceFlag;
1607 }
1608 }
1609 }
1610
1611 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1612 config.navigation = Configuration.NAVIGATION_DPAD;
1613 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1614 }
1615
1616 // Determine whether a hard keyboard is available and enabled.
1617 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1618 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1619 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1620 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1621 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1622 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1623 }
1624
Tiger Huang7c610aa2018-10-27 00:01:01 +08001625 mDisplayPolicy.updateConfigurationDependentBehaviors();
1626
Andrii Kulian06d07d62017-03-14 11:11:47 -07001627 // 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,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001635 DisplayCutout displayCutout) {
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,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001647 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001648 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001649 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001650 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001651 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001652 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001653 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001654 return sw;
1655 }
1656
1657 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001658 DisplayMetrics dm, int dw, int dh, DisplayCutout displayCutout) {
1659 dm.noncompatWidthPixels = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
1660 displayCutout);
1661 dm.noncompatHeightPixels = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode,
1662 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
Tiger Huang7c610aa2018-10-27 00:01:01 +08001671 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, boolean rotated,
1672 int uiMode, int dw, int dh, float density, Configuration outConfig) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001673
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;
Tiger Huang7c610aa2018-10-27 00:01:01 +08001690 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_0, uiMode, unrotDw, unrotDh);
1691 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_90, uiMode, unrotDh, unrotDw);
1692 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_180, uiMode, unrotDw, unrotDh);
1693 adjustDisplaySizeRanges(displayInfo, Surface.ROTATION_270, uiMode, unrotDh, unrotDw);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001694 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1695 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001696 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001697 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001698 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001699 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001700 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001701 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001702 displayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001703 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1704 outConfig.screenLayout = sl;
1705 }
1706
1707 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
Tiger Huang7c610aa2018-10-27 00:01:01 +08001708 int uiMode, DisplayCutout displayCutout) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001709 // Get the app screen size at this rotation.
Tiger Huang7c610aa2018-10-27 00:01:01 +08001710 int w = mDisplayPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayCutout);
1711 int h = mDisplayPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001712
1713 // Compute the screen layout size class for this rotation.
1714 int longSize = w;
1715 int shortSize = h;
1716 if (longSize < shortSize) {
1717 int tmp = longSize;
1718 longSize = shortSize;
1719 shortSize = tmp;
1720 }
1721 longSize = (int)(longSize/density);
1722 shortSize = (int)(shortSize/density);
1723 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1724 }
1725
Tiger Huang7c610aa2018-10-27 00:01:01 +08001726 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int rotation,
Andrii Kulian06d07d62017-03-14 11:11:47 -07001727 int uiMode, int dw, int dh) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001728 final DisplayCutout displayCutout = calculateDisplayCutoutForRotation(
1729 rotation).getDisplayCutout();
Tiger Huang7c610aa2018-10-27 00:01:01 +08001730 final int width = mDisplayPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
1731 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001732 if (width < displayInfo.smallestNominalAppWidth) {
1733 displayInfo.smallestNominalAppWidth = width;
1734 }
1735 if (width > displayInfo.largestNominalAppWidth) {
1736 displayInfo.largestNominalAppWidth = width;
1737 }
Tiger Huang7c610aa2018-10-27 00:01:01 +08001738 final int height = mDisplayPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
1739 displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001740 if (height < displayInfo.smallestNominalAppHeight) {
1741 displayInfo.smallestNominalAppHeight = height;
1742 }
1743 if (height > displayInfo.largestNominalAppHeight) {
1744 displayInfo.largestNominalAppHeight = height;
1745 }
1746 }
1747
Riddle Hsua4d6fa22018-08-11 00:50:39 +08001748 /**
1749 * Apps that use the compact menu panel (as controlled by the panelMenuIsCompact
1750 * theme attribute) on devices that feature a physical options menu key attempt to position
1751 * their menu panel window along the edge of the screen nearest the physical menu key.
1752 * This lowers the travel distance between invoking the menu panel and selecting
1753 * a menu option.
1754 *
1755 * This method helps control where that menu is placed. Its current implementation makes
1756 * assumptions about the menu key and its relationship to the screen based on whether
1757 * the device's natural orientation is portrait (width < height) or landscape.
1758 *
1759 * The menu key is assumed to be located along the bottom edge of natural-portrait
1760 * devices and along the right edge of natural-landscape devices. If these assumptions
1761 * do not hold for the target device, this method should be changed to reflect that.
1762 *
1763 * @return A {@link Gravity} value for placing the options menu window.
1764 */
1765 int getPreferredOptionsPanelGravity() {
1766 final int rotation = getRotation();
1767 if (mInitialDisplayWidth < mInitialDisplayHeight) {
1768 // On devices with a natural orientation of portrait.
1769 switch (rotation) {
1770 default:
1771 case Surface.ROTATION_0:
1772 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1773 case Surface.ROTATION_90:
1774 return Gravity.RIGHT | Gravity.BOTTOM;
1775 case Surface.ROTATION_180:
1776 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1777 case Surface.ROTATION_270:
1778 return Gravity.START | Gravity.BOTTOM;
1779 }
1780 }
1781
1782 // On devices with a natural orientation of landscape.
1783 switch (rotation) {
1784 default:
1785 case Surface.ROTATION_0:
1786 return Gravity.RIGHT | Gravity.BOTTOM;
1787 case Surface.ROTATION_90:
1788 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1789 case Surface.ROTATION_180:
1790 return Gravity.START | Gravity.BOTTOM;
1791 case Surface.ROTATION_270:
1792 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1793 }
1794 }
1795
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001796 DockedStackDividerController getDockedDividerController() {
1797 return mDividerControllerLocked;
1798 }
1799
Winson Chung655332c2016-10-31 13:14:28 -07001800 PinnedStackController getPinnedStackController() {
1801 return mPinnedStackControllerLocked;
1802 }
1803
Jeff Browna506a6e2013-06-04 00:02:38 -07001804 /**
1805 * Returns true if the specified UID has access to this display.
1806 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001807 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001808 return mDisplay.hasAccess(uid);
1809 }
1810
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001811 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001812 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001813 }
1814
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001815 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001816 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001817 }
1818
Wale Ogunwale61911492017-10-11 08:50:50 -07001819 /**
1820 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1821 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001822 TaskStack getSplitScreenPrimaryStack() {
1823 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001824 return (stack != null && stack.isVisible()) ? stack : null;
1825 }
1826
Robert Carr9785cf32018-04-25 15:06:07 -07001827 boolean hasSplitScreenPrimaryStack() {
1828 return getSplitScreenPrimaryStack() != null;
1829 }
1830
Wale Ogunwale61911492017-10-11 08:50:50 -07001831 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001832 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001833 * not visible.
1834 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001835 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1836 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001837 }
1838
1839 TaskStack getPinnedStack() {
1840 return mTaskStackContainers.getPinnedStack();
1841 }
1842
1843 private boolean hasPinnedStack() {
1844 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001845 }
1846
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001847 /**
1848 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1849 * Null is no compatible stack on the display.
1850 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001851 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001852 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1853 }
1854
1855 /**
1856 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1857 * activity type. Null is no compatible stack on the display.
1858 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001859 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001860 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001861 }
1862
Bryce Lee48f4b572017-04-10 10:54:15 -07001863 @VisibleForTesting
Kazuki Takise148d00a2018-05-31 15:32:19 +09001864 WindowList<TaskStack> getStacks() {
1865 return mTaskStackContainers.mChildren;
1866 }
1867
1868 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001869 TaskStack getTopStack() {
1870 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001871 }
1872
Winson Chunge2d72172018-01-25 17:46:20 +00001873 ArrayList<Task> getVisibleTasks() {
1874 return mTaskStackContainers.getVisibleTasks();
1875 }
1876
Wale Ogunwale61911492017-10-11 08:50:50 -07001877 void onStackWindowingModeChanged(TaskStack stack) {
1878 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001879 }
1880
Andrii Kulian441e4492016-09-29 15:25:00 -07001881 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001882 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001883 super.onConfigurationChanged(newParentConfig);
Tiger Huang7c610aa2018-10-27 00:01:01 +08001884 if (mDisplayPolicy != null) {
1885 mDisplayPolicy.onConfigurationChanged();
1886 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001887
Evan Roskye747c3e2018-10-30 20:06:41 -07001888 // If there was no pinned stack, we still need to notify the controller of the display info
1889 // update as a result of the config change.
1890 if (mPinnedStackControllerLocked != null && !hasPinnedStack()) {
1891 mPinnedStackControllerLocked.onDisplayInfoChanged(getDisplayInfo());
1892 }
Evan Roskye747c3e2018-10-30 20:06:41 -07001893 }
1894
1895 /**
1896 * Updates the resources used by docked/pinned controllers. This needs to be called at the
1897 * beginning of a configuration update cascade since the metrics from these resources are used
1898 * for bounds calculations. Since ActivityDisplay initiates the configuration update, this
1899 * should be called from there instead of DisplayContent's onConfigurationChanged.
1900 */
1901 void preOnConfigurationChanged() {
Bryce Leef3c6a472017-11-14 14:53:06 -08001902 final DockedStackDividerController dividerController = getDockedDividerController();
1903
1904 if (dividerController != null) {
1905 getDockedDividerController().onConfigurationChanged();
1906 }
1907
1908 final PinnedStackController pinnedStackController = getPinnedStackController();
1909
1910 if (pinnedStackController != null) {
1911 getPinnedStackController().onConfigurationChanged();
1912 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001913 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001914
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001915 @Override
1916 boolean fillsParent() {
1917 return true;
1918 }
1919
1920 @Override
1921 boolean isVisible() {
1922 return true;
1923 }
1924
1925 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001926 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001927 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001928 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001929 }
1930
Robert Carr9785cf32018-04-25 15:06:07 -07001931 /**
1932 * In split-screen mode we process the IME containers above the docked divider
1933 * rather than directly above their target.
1934 */
1935 private boolean skipTraverseChild(WindowContainer child) {
lumarkff0ab692018-11-05 20:32:30 +08001936 if (child == mImeWindowsContainers && mInputMethodTarget != null
Robert Carr9785cf32018-04-25 15:06:07 -07001937 && !hasSplitScreenPrimaryStack()) {
1938 return true;
1939 }
1940 return false;
1941 }
1942
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001943 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001944 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1945 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1946 // target, or here in order if there isn't an IME target.
1947 if (traverseTopToBottom) {
1948 for (int i = mChildren.size() - 1; i >= 0; --i) {
1949 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001950 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001951 continue;
1952 }
Robert Carr9785cf32018-04-25 15:06:07 -07001953
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001954 if (child.forAllWindows(callback, traverseTopToBottom)) {
1955 return true;
1956 }
1957 }
1958 } else {
1959 final int count = mChildren.size();
1960 for (int i = 0; i < count; i++) {
1961 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001962 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001963 continue;
1964 }
Robert Carr9785cf32018-04-25 15:06:07 -07001965
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001966 if (child.forAllWindows(callback, traverseTopToBottom)) {
1967 return true;
1968 }
1969 }
1970 }
1971 return false;
1972 }
1973
1974 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1975 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1976 }
1977
Wale Ogunwale399c8692017-05-08 14:22:42 -07001978 @Override
1979 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001980 final WindowManagerPolicy policy = mService.mPolicy;
1981
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001982 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001983 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001984 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1985 + " is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001986 // If the display is frozen, some activities may be in the middle of restarting, and
1987 // thus have removed their old window. If the window has the flag to hide the lock
1988 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1989 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001990 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001991 } else if (policy.isKeyguardLocked()) {
1992 // Use the last orientation the while the display is frozen with the keyguard
1993 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1994 // window. We don't want to check the show when locked window directly though as
1995 // things aren't stable while the display is frozen, for example the window could be
1996 // momentarily unavailable due to activity relaunch.
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001997 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1998 + " is frozen while keyguard locked, return " + mLastOrientation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08001999 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07002000 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002001 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002002 final int orientation = mAboveAppWindowsContainers.getOrientation();
2003 if (orientation != SCREEN_ORIENTATION_UNSET) {
2004 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07002005 }
Wale Ogunwale51362492016-09-08 17:49:17 -07002006 }
2007
Wale Ogunwale399c8692017-05-08 14:22:42 -07002008 // Top system windows are not requesting an orientation. Start searching from apps.
2009 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07002010 }
2011
Craig Mautner46ac6fa2013-08-01 10:06:34 -07002012 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07002013 // Check if display metrics changed and update base values if needed.
2014 updateBaseDisplayMetricsIfNeeded();
2015
Craig Mautner722285e2012-09-07 13:55:58 -07002016 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07002017 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07002018
Garfield Tan2f145f22018-11-01 15:27:03 -07002019 onDisplayChanged(this);
Craig Mautner722285e2012-09-07 13:55:58 -07002020 }
2021
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002022 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002023 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
2024 if (displayManagerInternal != null) {
2025 // Bootstrap the default logical display from the display manager.
2026 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
2027 if (newDisplayInfo != null) {
2028 mDisplayInfo.copyFrom(newDisplayInfo);
2029 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002030 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07002031
Andrii Kuliancd097992017-03-23 18:31:59 -07002032 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
2033 mDisplayInfo.logicalDensityDpi);
2034 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
2035 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
2036 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002037 mInitialDisplayCutout = mDisplayInfo.displayCutout;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07002038 }
2039
Andrii Kuliancd097992017-03-23 18:31:59 -07002040 /**
2041 * If display metrics changed, overrides are not set and it's not just a rotation - update base
2042 * values.
2043 */
2044 private void updateBaseDisplayMetricsIfNeeded() {
2045 // Get real display metrics without overrides from WM.
2046 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
2047 final int orientation = mDisplayInfo.rotation;
2048 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2049 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
2050 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
2051 final int newDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01002052 final DisplayCutout newCutout = mDisplayInfo.displayCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07002053
2054 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
2055 || mInitialDisplayHeight != newHeight
Adrian Roos1cf585052018-01-03 18:43:27 +01002056 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi
2057 || !Objects.equals(mInitialDisplayCutout, newCutout);
Andrii Kuliancd097992017-03-23 18:31:59 -07002058
2059 if (displayMetricsChanged) {
2060 // Check if display size or density is forced.
2061 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
2062 || mBaseDisplayHeight != mInitialDisplayHeight;
2063 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
2064
2065 // If there is an override set for base values - use it, otherwise use new values.
2066 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
2067 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
2068 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
2069
2070 // Real display metrics changed, so we should also update initial values.
2071 mInitialDisplayWidth = newWidth;
2072 mInitialDisplayHeight = newHeight;
2073 mInitialDisplayDensity = newDensity;
Adrian Roos1cf585052018-01-03 18:43:27 +01002074 mInitialDisplayCutout = newCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07002075 mService.reconfigureDisplayLocked(this);
2076 }
2077 }
2078
Bryce Lee27cec322017-03-21 09:41:37 -07002079 /** Sets the maximum width the screen resolution can be */
2080 void setMaxUiWidth(int width) {
2081 if (DEBUG_DISPLAY) {
2082 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
2083 }
2084
2085 mMaxUiWidth = width;
2086
2087 // Update existing metrics.
2088 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
2089 }
2090
2091 /** Update base (override) display metrics. */
2092 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
2093 mBaseDisplayWidth = baseWidth;
2094 mBaseDisplayHeight = baseHeight;
2095 mBaseDisplayDensity = baseDensity;
2096
2097 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
2098 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
2099 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
2100 mBaseDisplayWidth = mMaxUiWidth;
2101
2102 if (DEBUG_DISPLAY) {
2103 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
2104 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
2105 + " on display:" + getDisplayId());
2106 }
2107 }
2108
2109 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08002110
2111 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07002112 }
2113
Riddle Hsuf53da812018-08-15 22:00:27 +08002114 /**
2115 * Forces this display to use the specified density.
2116 *
2117 * @param density The density in DPI to use. If the value equals to initial density, the setting
2118 * will be cleared.
2119 * @param userId The target user to apply. Only meaningful when this is default display. If the
2120 * user id is {@link UserHandle#USER_CURRENT}, it means to apply current settings
2121 * so only need to configure display.
2122 */
2123 void setForcedDensity(int density, int userId) {
2124 final boolean clear = density == mInitialDisplayDensity;
2125 final boolean updateCurrent = userId == UserHandle.USER_CURRENT;
2126 if (mService.mCurrentUserId == userId || updateCurrent) {
2127 mBaseDisplayDensity = density;
2128 mService.reconfigureDisplayLocked(this);
2129 }
2130 if (updateCurrent) {
2131 // We are applying existing settings so no need to save it again.
2132 return;
2133 }
2134
2135 if (density == mInitialDisplayDensity) {
2136 density = 0;
2137 }
Chilun8753ad32018-10-09 15:56:45 +08002138 mService.mDisplayWindowSettings.setForcedDensity(this, density, userId);
Riddle Hsuf53da812018-08-15 22:00:27 +08002139 }
2140
2141 /** @param mode {@link #FORCE_SCALING_MODE_AUTO} or {@link #FORCE_SCALING_MODE_DISABLED}. */
2142 void setForcedScalingMode(@ForceScalingMode int mode) {
2143 if (mode != FORCE_SCALING_MODE_DISABLED) {
2144 mode = FORCE_SCALING_MODE_AUTO;
2145 }
2146
2147 mDisplayScalingDisabled = (mode != FORCE_SCALING_MODE_AUTO);
2148 Slog.i(TAG_WM, "Using display scaling mode: " + (mDisplayScalingDisabled ? "off" : "auto"));
2149 mService.reconfigureDisplayLocked(this);
2150
Chilun8753ad32018-10-09 15:56:45 +08002151 mService.mDisplayWindowSettings.setForcedScalingMode(this, mode);
Riddle Hsuf53da812018-08-15 22:00:27 +08002152 }
2153
2154 /** If the given width and height equal to initial size, the setting will be cleared. */
2155 void setForcedSize(int width, int height) {
2156 final boolean clear = mInitialDisplayWidth == width && mInitialDisplayHeight == height;
2157 if (!clear) {
2158 // Set some sort of reasonable bounds on the size of the display that we will try
2159 // to emulate.
2160 final int minSize = 200;
2161 final int maxScale = 2;
2162 width = Math.min(Math.max(width, minSize), mInitialDisplayWidth * maxScale);
2163 height = Math.min(Math.max(height, minSize), mInitialDisplayHeight * maxScale);
2164 }
2165
2166 Slog.i(TAG_WM, "Using new display size: " + width + "x" + height);
2167 updateBaseDisplayMetrics(width, height, mBaseDisplayDensity);
2168 mService.reconfigureDisplayLocked(this);
2169
2170 if (clear) {
2171 width = height = 0;
2172 }
Chilun8753ad32018-10-09 15:56:45 +08002173 mService.mDisplayWindowSettings.setForcedSize(this, width, height);
Riddle Hsuf53da812018-08-15 22:00:27 +08002174 }
2175
Wale Ogunwaledb506192017-12-08 10:57:32 -08002176 void getStableRect(Rect out) {
2177 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08002178 }
2179
Wale Ogunwale61911492017-10-11 08:50:50 -07002180 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002181 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
2182 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07002183
Wale Ogunwale61911492017-10-11 08:50:50 -07002184 final TaskStack stack = new TaskStack(mService, stackId, controller);
2185 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002186 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07002187 }
2188
Andrii Kulian51c1b672017-04-07 18:39:32 -07002189 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002190 final DisplayContent prevDc = stack.getDisplayContent();
2191 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07002192 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
2193 + " which is not currently attached to any display");
2194 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002195 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07002196 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
2197 + " to its current displayId=" + mDisplayId);
2198 }
2199
lumark588a3e82018-07-20 18:53:54 +08002200 // Clean up all pending transitions when stack reparent to another display.
2201 stack.forAllAppWindows(AppWindowToken::removeFromPendingTransition);
2202
Wale Ogunwale61911492017-10-11 08:50:50 -07002203 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07002204 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08002205 }
2206
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002207 @Override
2208 protected void addChild(DisplayChildWindowContainer child,
2209 Comparator<DisplayChildWindowContainer> comparator) {
2210 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2211 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002212
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002213 @Override
2214 protected void addChild(DisplayChildWindowContainer child, int index) {
2215 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2216 }
2217
2218 @Override
2219 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002220 // Only allow removal of direct children from this display if the display is in the process
2221 // of been removed.
2222 if (mRemovingDisplay) {
2223 super.removeChild(child);
2224 return;
2225 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002226 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002227 }
2228
Andrii Kuliand2765632016-12-12 22:26:34 -08002229 @Override
2230 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
2231 // Children of the display are statically ordered, so the real intention here is to perform
2232 // the operation on the display and not the static direct children.
2233 getParent().positionChildAt(position, this, includingParents);
2234 }
2235
Riddle Hsu57831b52018-07-27 00:31:48 +08002236 void positionStackAt(int position, TaskStack child, boolean includingParents) {
2237 mTaskStackContainers.positionChildAt(position, child, includingParents);
Winson Chung59a47ded2018-01-25 17:46:06 +00002238 layoutAndAssignWindowLayersIfNeeded();
2239 }
2240
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002241 /**
2242 * Used to obtain task ID when user taps on coordinate (x, y) in this display, and outside
2243 * current task in focus.
2244 *
2245 * This returns the task ID of the foremost task at (x, y) if the task is not home. Otherwise it
2246 * returns -1.
2247 *
2248 * @param x horizontal coordinate of the tap position
2249 * @param y vertical coordinate of the tap position
2250 * @return the task ID if a non-home task is found; -1 if not
2251 */
2252 int taskForTapOutside(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002253 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2254 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Garfield Tanf2e0a9e2018-10-16 09:42:02 -07002255 if (stack.isActivityTypeHome() && !stack.inMultiWindowMode()) {
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002256 // We skip not only home stack, but also everything behind home because user can't
Garfield Tanf2e0a9e2018-10-16 09:42:02 -07002257 // see them when home stack is isn't in multi-window mode.
Garfield Tan4f71c5a2018-10-10 10:12:02 -07002258 break;
2259 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002260 final int taskId = stack.taskIdFromPoint(x, y);
2261 if (taskId != -1) {
2262 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07002263 }
2264 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002265 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07002266 }
2267
Chong Zhang8e89b312015-09-09 15:09:30 -07002268 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08002269 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07002270 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07002271 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07002272 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002273 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002274 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07002275 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2276 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002277 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002278 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002279 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002280
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002281 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
2282 if (mTmpTaskForResizePointSearchResult.searchDone) {
2283 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07002284 }
2285 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002286 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002287 }
2288
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002289 void updateTouchExcludeRegion() {
2290 final Task focusedTask = (mFocusedApp != null ? mFocusedApp.getTask() : null);
David Stevensee9e2772017-02-09 16:30:27 -08002291 if (focusedTask == null) {
2292 mTouchExcludeRegion.setEmpty();
2293 } else {
2294 mTouchExcludeRegion.set(mBaseDisplayRect);
2295 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
2296 mTmpRect2.setEmpty();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002297 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0;
2298 --stackNdx) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002299 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002300 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
2301 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08002302 }
2303 // If we removed the focused task above, add it back and only leave its
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002304 // outside touch area in the exclusion. TapDetector is not interested in
David Stevensee9e2772017-02-09 16:30:27 -08002305 // any touch inside the focused task itself.
2306 if (!mTmpRect2.isEmpty()) {
2307 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
2308 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08002309 }
lumark90120a82018-08-15 00:33:03 +08002310 if (mInputMethodWindow != null && mInputMethodWindow.isVisibleLw()) {
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002311 // If the input method is visible and the user is typing, we don't want these touch
2312 // events to be intercepted and used to change focus. This would likely cause a
2313 // disappearance of the input method.
lumark90120a82018-08-15 00:33:03 +08002314 mInputMethodWindow.getTouchableRegion(mTmpRegion);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002315 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002316 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002317 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002318 final WindowState win = mTapExcludedWindows.get(i);
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002319 win.getTouchableRegion(mTmpRegion);
2320 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
2321 }
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002322 for (int i = mTapExcludeProvidingWindows.size() - 1; i >= 0; i--) {
2323 final WindowState win = mTapExcludeProvidingWindows.valueAt(i);
2324 win.amendTapExcludeRegion(mTouchExcludeRegion);
2325 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08002326 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07002327 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002328 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07002329 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002330 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
2331 }
Craig Mautner1bef3892015-02-17 15:09:47 -08002332 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07002333 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08002334 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07002335 }
2336
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002337 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07002338 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002339 super.switchUser();
2340 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07002341 }
2342
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07002343 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002344 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2345 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07002346 }
2347 }
2348
Wale Ogunwale10124582016-09-15 20:25:50 -07002349 @Override
2350 void removeIfPossible() {
2351 if (isAnimating()) {
2352 mDeferredRemoval = true;
2353 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07002354 }
Wale Ogunwale10124582016-09-15 20:25:50 -07002355 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07002356 }
2357
Wale Ogunwale10124582016-09-15 20:25:50 -07002358 @Override
2359 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002360 mRemovingDisplay = true;
2361 try {
lumark588a3e82018-07-20 18:53:54 +08002362 // Clear all transitions & screen frozen states when removing display.
2363 mOpeningApps.clear();
2364 mClosingApps.clear();
2365 mUnknownAppVisibilityController.clear();
2366 mAppTransition.removeAppTransitionTimeoutCallbacks();
2367 handleAnimatingStoppedAndTransition();
2368 mService.stopFreezingDisplayLocked();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002369 super.removeImmediately();
2370 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Arthur Hungbe5ce212018-09-13 18:41:56 +08002371 if (mPointerEventDispatcher != null && mTapDetector != null) {
2372 unregisterPointerEventListener(mTapDetector);
2373 unregisterPointerEventListener(mService.mMousePositionTracker);
2374 mTapDetector = null;
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002375 }
Jorim Jaggia3844032018-01-03 15:54:43 +01002376 mService.mAnimator.removeDisplayLocked(mDisplayId);
Louis Chang207f9112018-07-30 08:58:54 +08002377 mWindowingLayer.release();
2378 mOverlayLayer.release();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002379 } finally {
Riddle Hsu4e611772018-10-31 18:58:28 +08002380 mDisplayReady = false;
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002381 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08002382 }
Robert Carr679ccb02018-08-08 15:32:35 -07002383
Tiger Huang7c610aa2018-10-27 00:01:01 +08002384 mDisplayPolicy.onDisplayRemoved();
Riddle Hsuf53da812018-08-15 22:00:27 +08002385 mService.mWindowPlacerLocked.requestTraversal();
Craig Mautner95da1082014-02-24 17:54:35 -08002386 }
2387
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002388 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07002389 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002390 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002391 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
2392
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002393 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002394 removeImmediately();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002395 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08002396 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002397 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08002398 }
2399
Andrii Kulian0214ed92017-05-16 13:44:05 -07002400 /** @return 'true' if removal of this display content is deferred due to active animation. */
2401 boolean isRemovalDeferred() {
2402 return mDeferredRemoval;
2403 }
2404
Wale Ogunwale10124582016-09-15 20:25:50 -07002405 boolean animateForIme(float interpolatedValue, float animationTarget,
2406 float dividerAnimationTarget) {
2407 boolean updated = false;
2408
Wale Ogunwale61911492017-10-11 08:50:50 -07002409 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2410 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002411 if (stack == null || !stack.isAdjustedForIme()) {
2412 continue;
2413 }
2414
2415 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
2416 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2417 updated = true;
2418 } else {
2419 mDividerControllerLocked.mLastAnimationProgress =
2420 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
2421 mDividerControllerLocked.mLastDividerProgress =
2422 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
2423 updated |= stack.updateAdjustForIme(
2424 mDividerControllerLocked.mLastAnimationProgress,
2425 mDividerControllerLocked.mLastDividerProgress,
2426 false /* force */);
2427 }
2428 if (interpolatedValue >= 1f) {
2429 stack.endImeAdjustAnimation();
2430 }
2431 }
2432
2433 return updated;
2434 }
2435
2436 boolean clearImeAdjustAnimation() {
2437 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002438 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2439 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002440 if (stack != null && stack.isAdjustedForIme()) {
2441 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2442 changed = true;
2443 }
2444 }
2445 return changed;
2446 }
2447
2448 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002449 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2450 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002451 if (stack.isVisible() && stack.isAdjustedForIme()) {
2452 stack.beginImeAdjustAnimation();
2453 }
2454 }
2455 }
2456
2457 void adjustForImeIfNeeded() {
lumark90120a82018-08-15 00:33:03 +08002458 final WindowState imeWin = mInputMethodWindow;
Wale Ogunwale10124582016-09-15 20:25:50 -07002459 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2460 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002461 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002462 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2463 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2464 imeTargetStack.getDockSide() : DOCKED_INVALID;
2465 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2466 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2467 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002468 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002469 final boolean imeHeightChanged = imeVisible &&
2470 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2471
2472 // The divider could be adjusted for IME position, or be thinner than usual,
2473 // or both. There are three possible cases:
2474 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2475 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2476 // - If IME is not visible, divider is not moved and is normal width.
2477
2478 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002479 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2480 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002481 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002482 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2483 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002484 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2485 } else {
2486 stack.resetAdjustedForIme(false);
2487 }
2488 }
2489 mDividerControllerLocked.setAdjustedForIme(
2490 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2491 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002492 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2493 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002494 stack.resetAdjustedForIme(!dockVisible);
2495 }
2496 mDividerControllerLocked.setAdjustedForIme(
2497 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2498 }
Winson Chung655332c2016-10-31 13:14:28 -07002499 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002500 }
2501
2502 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002503 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2504 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002505 stack.prepareFreezingTaskBounds();
2506 }
2507 }
2508
Wale Ogunwale94744212015-09-21 19:01:47 -07002509 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002510 getBounds(mTmpRect, newRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002511 rotateBounds(mTmpRect, oldRotation, newRotation, bounds);
2512 }
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002513
Evan Rosky39b6f232018-10-30 18:35:41 -07002514 void rotateBounds(Rect parentBounds, int oldRotation, int newRotation, Rect bounds) {
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002515 // Compute a transform matrix to undo the coordinate space transformation,
2516 // and present the window at the same physical position it previously occupied.
2517 final int deltaRotation = deltaRotation(newRotation, oldRotation);
Evan Rosky39b6f232018-10-30 18:35:41 -07002518 createRotationMatrix(
2519 deltaRotation, parentBounds.width(), parentBounds.height(), mTmpMatrix);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002520
2521 mTmpRectF.set(bounds);
2522 mTmpMatrix.mapRect(mTmpRectF);
2523 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002524 }
2525
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002526 static int deltaRotation(int oldRotation, int newRotation) {
2527 int delta = newRotation - oldRotation;
2528 if (delta < 0) delta += 4;
2529 return delta;
2530 }
2531
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002532 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002533 Matrix outMatrix) {
2534 // For rotations without Z-ordering we don't need the target rectangle's position.
2535 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2536 displayHeight, outMatrix);
2537 }
2538
2539 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2540 float displayWidth, float displayHeight, Matrix outMatrix) {
2541 switch (rotation) {
2542 case ROTATION_0:
2543 outMatrix.reset();
2544 break;
2545 case ROTATION_270:
2546 outMatrix.setRotate(270, 0, 0);
2547 outMatrix.postTranslate(0, displayHeight);
2548 outMatrix.postTranslate(rectTop, 0);
2549 break;
2550 case ROTATION_180:
2551 outMatrix.reset();
2552 break;
2553 case ROTATION_90:
2554 outMatrix.setRotate(90, 0, 0);
2555 outMatrix.postTranslate(displayWidth, 0);
2556 outMatrix.postTranslate(-rectTop, rectLeft);
2557 break;
2558 }
2559 }
2560
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002561 @CallSuper
2562 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002563 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002564 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002565 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002566 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002567 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2568 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002569 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002570 }
2571 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2572 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002573 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2574 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002575 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002576 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002577 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2578 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002579 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002580 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002581 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2582 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002583 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002584 }
2585 proto.write(DPI, mBaseDisplayDensity);
2586 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002587 proto.write(ROTATION, mRotation);
2588 final ScreenRotationAnimation screenRotationAnimation =
2589 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2590 if (screenRotationAnimation != null) {
2591 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2592 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002593 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
lumark588a3e82018-07-20 18:53:54 +08002594 mAppTransition.writeToProto(proto, APP_TRANSITION);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002595 if (mFocusedApp != null) {
2596 mFocusedApp.writeNameToProto(proto, FOCUSED_APP);
2597 }
Steven Timotiusaf03df62017-07-18 16:56:43 -07002598 proto.end(token);
2599 }
2600
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002601 @Override
2602 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2603 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002604 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2605 final String subPrefix = " " + prefix;
2606 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2607 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2608 pw.print("dpi");
2609 if (mInitialDisplayWidth != mBaseDisplayWidth
2610 || mInitialDisplayHeight != mBaseDisplayHeight
2611 || mInitialDisplayDensity != mBaseDisplayDensity) {
2612 pw.print(" base=");
2613 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2614 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2615 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002616 if (mDisplayScalingDisabled) {
2617 pw.println(" noscale");
2618 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002619 pw.print(" cur=");
2620 pw.print(mDisplayInfo.logicalWidth);
2621 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2622 pw.print(" app=");
2623 pw.print(mDisplayInfo.appWidth);
2624 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2625 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2626 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2627 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2628 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002629 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002630 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002631 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002632
Craig Mautnerdc548482014-02-05 13:35:24 -08002633 pw.println();
Adrian Roos5251b1d2018-03-23 18:57:43 +01002634 pw.print(prefix); pw.print("mLayoutSeq="); pw.println(mLayoutSeq);
Riddle Hsu654a6f92018-07-13 22:59:36 +08002635 pw.print(prefix);
2636 pw.print("mDeferredRotationPauseCount="); pw.println(mDeferredRotationPauseCount);
Adrian Roos5251b1d2018-03-23 18:57:43 +01002637
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002638 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
2639 if (mLastFocus != mCurrentFocus) {
2640 pw.print(" mLastFocus="); pw.println(mLastFocus);
2641 }
2642 if (mLosingFocus.size() > 0) {
2643 pw.println();
2644 pw.println(" Windows losing focus:");
2645 for (int i = mLosingFocus.size() - 1; i >= 0; i--) {
2646 final WindowState w = mLosingFocus.get(i);
2647 pw.print(" Losing #"); pw.print(i); pw.print(' ');
2648 pw.print(w);
2649 if (dumpAll) {
2650 pw.println(":");
2651 w.dump(pw, " ", true);
2652 } else {
2653 pw.println();
2654 }
2655 }
2656 }
2657 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
Tiger Huang7c610aa2018-10-27 00:01:01 +08002658 if (mLastStatusBarVisibility != 0) {
2659 pw.print(" mLastStatusBarVisibility=0x");
2660 pw.println(Integer.toHexString(mLastStatusBarVisibility));
2661 }
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002662
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
Tiger Huang7c610aa2018-10-27 00:01:01 +08002855 int focusChanged = getDisplayPolicy().focusChangedLw(oldFocus, newFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002856
2857 if (imWindowChanged && oldFocus != mInputMethodWindow) {
2858 // Focus of the input method window changed. Perform layout if needed.
2859 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2860 performLayout(true /*initial*/, updateInputWindows);
2861 focusChanged &= ~FINISH_LAYOUT_REDO_LAYOUT;
2862 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2863 // Client will do the layout, but we need to assign layers
2864 // for handleNewWindowLocked() below.
2865 assignWindowLayers(false /* setLayoutNeeded */);
2866 }
2867 }
2868
2869 if ((focusChanged & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2870 // The change in focus caused us to need to do a layout. Okay.
2871 setLayoutNeeded();
2872 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2873 performLayout(true /*initial*/, updateInputWindows);
2874 } else if (mode == UPDATE_FOCUS_REMOVING_FOCUS) {
2875 mService.mRoot.performSurfacePlacement(false);
2876 }
2877 }
2878
2879 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
2880 // If we defer assigning layers, then the caller is responsible for doing this part.
2881 getInputMonitor().setInputFocusLw(newFocus, updateInputWindows);
2882 }
2883
2884 adjustForImeIfNeeded();
2885
2886 // We may need to schedule some toast windows to be removed. The toasts for an app that
2887 // does not have input focus are removed within a timeout to prevent apps to redress
2888 // other apps' UI.
2889 scheduleToastWindowsTimeoutIfNeededLocked(oldFocus, newFocus);
2890
2891 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2892 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
2893 }
2894 return true;
2895 }
2896
2897 /**
2898 * Set the new focused app to this display.
2899 *
2900 * @param newFocus the new focused AppWindowToken.
2901 * @return true if the focused app is changed.
2902 */
2903 boolean setFocusedApp(AppWindowToken newFocus) {
2904 if (newFocus != null) {
2905 final DisplayContent appDisplay = newFocus.getDisplayContent();
2906 if (appDisplay != this) {
2907 throw new IllegalStateException(newFocus + " is not on " + getName()
2908 + " but " + ((appDisplay != null) ? appDisplay.getName() : "none"));
2909 }
2910 }
2911 if (mFocusedApp == newFocus) {
2912 return false;
2913 }
2914 mFocusedApp = newFocus;
2915 getInputMonitor().setFocusedAppLw(newFocus);
2916 updateTouchExcludeRegion();
2917 return true;
2918 }
2919
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002920 /** Updates the layer assignment of windows on this display. */
2921 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002922 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07002923 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002924 if (setLayoutNeeded) {
2925 setLayoutNeeded();
2926 }
Robert Carrb1579c82017-09-05 14:54:47 -07002927
Robert Carrf59b8dd2017-10-02 18:58:36 -07002928 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002929 // the application of this transaction until the animation pass triggers
2930 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2931 // the hiding and showing of surfaces.
2932 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002933 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002934 }
2935
Wale Ogunwale1666e312016-12-16 11:27:18 -08002936 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2937 // moving containers or resizing them. Need to investigate the best way to have it automatically
2938 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002939 void layoutAndAssignWindowLayersIfNeeded() {
2940 mService.mWindowsChanged = true;
2941 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002942
2943 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2944 false /*updateInputWindows*/)) {
2945 assignWindowLayers(false /* setLayoutNeeded */);
2946 }
2947
Arthur Hung95b38a92018-07-20 18:56:12 +08002948 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002949 mService.mWindowPlacerLocked.performSurfacePlacement();
Arthur Hung95b38a92018-07-20 18:56:12 +08002950 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002951 }
2952
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002953 /** Returns true if a leaked surface was destroyed */
2954 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002955 // Used to indicate that a surface was leaked.
2956 mTmpWindow = null;
2957 forAllWindows(w -> {
2958 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002959 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002960 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002961 }
2962 if (!mService.mSessions.contains(wsa.mSession)) {
2963 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002964 + w + " surface=" + wsa.mSurfaceController
2965 + " token=" + w.mToken
2966 + " pid=" + w.mSession.mPid
2967 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002968 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002969 mService.mForceRemoves.add(w);
2970 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002971 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002972 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002973 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002974 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002975 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002976 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002977 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002978 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002979 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002980
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002981 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002982 }
2983
2984 /**
lumark90120a82018-08-15 00:33:03 +08002985 * Set input method window for the display.
2986 * @param win Set when window added or Null when destroyed.
2987 */
2988 void setInputMethodWindowLocked(WindowState win) {
2989 mInputMethodWindow = win;
lumark70865a82018-09-17 16:56:26 +08002990 // Update display configuration for IME process.
2991 if (mInputMethodWindow != null) {
2992 final int imePid = mInputMethodWindow.mSession.mPid;
2993 mService.mAtmInternal.onImeWindowSetOnDisplay(imePid,
2994 mInputMethodWindow.getDisplayId());
2995 }
lumark90120a82018-08-15 00:33:03 +08002996 computeImeTarget(true /* updateImeTarget */);
2997 }
2998
2999 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003000 * Determine and return the window that should be the IME target.
3001 * @param updateImeTarget If true the system IME target will be updated to match what we found.
3002 * @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 +00003003 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003004 WindowState computeImeTarget(boolean updateImeTarget) {
lumark90120a82018-08-15 00:33:03 +08003005 if (mInputMethodWindow == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003006 // There isn't an IME so there shouldn't be a target...That was easy!
3007 if (updateImeTarget) {
3008 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
lumarkff0ab692018-11-05 20:32:30 +08003009 + mInputMethodTarget + " to null since mInputMethodWindow is null");
3010 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003011 }
3012 return null;
3013 }
3014
lumarkff0ab692018-11-05 20:32:30 +08003015 final WindowState curTarget = mInputMethodTarget;
Chavi Weingarten3a748552018-05-14 17:32:42 +00003016 if (!canUpdateImeTarget()) {
3017 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Defer updating IME target");
3018 return curTarget;
3019 }
3020
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003021 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
3022 // same display. Or even when the current IME/target are not on the same screen as the next
3023 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08003024 mUpdateImeTarget = updateImeTarget;
3025 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003026
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003027
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003028 // Yet more tricksyness! If this window is a "starting" window, we do actually want
3029 // to be on top of it, but it is not -really- where input will go. So look down below
3030 // for a real window to target...
3031 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
3032 final AppWindowToken token = target.mAppToken;
3033 if (token != null) {
3034 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
3035 if (betterTarget != null) {
3036 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003037 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003038 }
3039 }
3040
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003041 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
lumarkff0ab692018-11-05 20:32:30 +08003042 "Proposed new IME target: " + target + " for display: " + getDisplayId());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003043
chaviw1c13ad32018-06-12 16:44:23 -07003044 // Now, a special case -- if the last target's window is in the process of exiting, but
3045 // not removed, and the new target is home, keep on the last target to avoid flicker.
3046 // Home is a special case since its above other stacks in the ordering list, but layed
3047 // out below the others.
3048 if (curTarget != null && !curTarget.mRemoved && curTarget.isDisplayedLw()
3049 && curTarget.isClosing() && (target == null || target.isActivityTypeHome())) {
chaviw4ad54912018-05-30 11:05:44 -07003050 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "New target is home while current target is "
Chavi Weingarten3a748552018-05-14 17:32:42 +00003051 + "closing, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003052 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003053 }
3054
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003055 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
3056 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003057
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003058 if (target == null) {
3059 if (updateImeTarget) {
3060 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
3061 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
3062 + Debug.getCallers(4) : ""));
lumarkff0ab692018-11-05 20:32:30 +08003063 setInputMethodTarget(null, mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003064 }
3065
3066 return null;
3067 }
3068
3069 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003070 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
3071 if (token != null) {
3072
3073 // Now some fun for dealing with window animations that modify the Z order. We need
3074 // to look at all windows below the current target that are in this app, finding the
3075 // highest visible one in layering.
3076 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003077 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003078 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003079 }
3080
3081 if (highestTarget != null) {
lumark588a3e82018-07-20 18:53:54 +08003082 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, mAppTransition + " " + highestTarget
Jorim Jaggi35d328a2018-08-14 17:00:20 +02003083 + " animating=" + highestTarget.isAnimating());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003084
lumark588a3e82018-07-20 18:53:54 +08003085 if (mAppTransition.isTransitionSet()) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003086 // If we are currently setting up for an animation, hold everything until we
3087 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003088 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003089 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003090 }
3091 }
3092 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003093
3094 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
3095 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003096 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003097 }
3098
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003099 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003100 }
3101
lumarkff0ab692018-11-05 20:32:30 +08003102 /**
3103 * Calling {@link #computeImeTarget(boolean)} to update the input method target window in
3104 * the candidate app window token if needed.
3105 */
3106 void computeImeTargetIfNeeded(AppWindowToken candidate) {
3107 if (mInputMethodTarget != null && mInputMethodTarget.mAppToken == candidate) {
3108 computeImeTarget(true /* updateImeTarget */);
3109 }
3110 }
3111
Jorim Jaggib0fc8172017-11-23 17:04:08 +00003112 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
lumarkff0ab692018-11-05 20:32:30 +08003113 if (target == mInputMethodTarget && mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003114 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003115 }
3116
lumarkff0ab692018-11-05 20:32:30 +08003117 mInputMethodTarget = target;
3118 mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003119 assignWindowLayers(false /* setLayoutNeeded */);
3120 }
3121
3122 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
3123 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3124 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
3125 }
3126
3127 // Used to indicate we have reached the first window in the range we are interested in.
3128 mTmpWindow = null;
3129
3130 // TODO: Figure-out a more efficient way to do this.
3131 final WindowState candidate = getWindow(w -> {
3132 if (w == top) {
3133 // Reached the first window in the range we are interested in.
3134 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003135 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003136 if (mTmpWindow == null) {
3137 return false;
3138 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003139
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003140 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
3141 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003142 }
3143 // If we reached the bottom of the range of windows we are considering,
3144 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003145 if (w == bottom) {
3146 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003147 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003148 return false;
3149 });
3150
3151 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07003152 }
3153
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003154 void setLayoutNeeded() {
3155 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
3156 mLayoutNeeded = true;
3157 }
3158
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003159 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07003160 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
3161 mLayoutNeeded = false;
3162 }
3163
3164 boolean isLayoutNeeded() {
3165 return mLayoutNeeded;
3166 }
3167
Wale Ogunwale02319a62016-09-26 15:21:22 -07003168 void dumpTokens(PrintWriter pw, boolean dumpAll) {
3169 if (mTokenMap.isEmpty()) {
3170 return;
3171 }
3172 pw.println(" Display #" + mDisplayId);
3173 final Iterator<WindowToken> it = mTokenMap.values().iterator();
3174 while (it.hasNext()) {
3175 final WindowToken token = it.next();
3176 pw.print(" ");
3177 pw.print(token);
3178 if (dumpAll) {
3179 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003180 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07003181 } else {
3182 pw.println();
3183 }
3184 }
lumark588a3e82018-07-20 18:53:54 +08003185
3186 if (!mOpeningApps.isEmpty() || !mClosingApps.isEmpty()) {
3187 pw.println();
3188 if (mOpeningApps.size() > 0) {
3189 pw.print(" mOpeningApps="); pw.println(mOpeningApps);
3190 }
3191 if (mClosingApps.size() > 0) {
3192 pw.print(" mClosingApps="); pw.println(mClosingApps);
3193 }
3194 }
3195
3196 mUnknownAppVisibilityController.dump(pw, " ");
Wale Ogunwale02319a62016-09-26 15:21:22 -07003197 }
3198
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003199 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003200 final int[] index = new int[1];
3201 forAllWindows(w -> {
3202 final WindowStateAnimator wAnim = w.mWinAnimator;
3203 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
3204 index[0] = index[0] + 1;
3205 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003206 }
3207
Jorim Jaggife762342016-10-13 14:33:27 +02003208 /**
3209 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
3210 */
3211 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
3212 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003213 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02003214 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
3215 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01003216 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02003217 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003218 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02003219 }
3220
Wale Ogunwale494009b82016-10-21 09:01:38 -07003221 boolean checkWaitingForWindows() {
3222
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003223 mHaveBootMsg = false;
3224 mHaveApp = false;
3225 mHaveWallpaper = false;
3226 mHaveKeyguard = true;
3227
3228 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003229 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
3230 return true;
3231 }
3232 if (w.isDrawnLw()) {
3233 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003234 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003235 } else if (w.mAttrs.type == TYPE_APPLICATION
3236 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003237 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003238 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003239 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003240 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003241 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003242 }
3243 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003244 return false;
3245 });
3246
3247 if (visibleWindow != null) {
3248 // We have a visible window.
3249 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003250 }
3251
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003252 // if the wallpaper service is disabled on the device, we're never going to have
3253 // wallpaper, don't bother waiting for it
3254 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
3255 com.android.internal.R.bool.config_enableWallpaperService)
Daichi Hironoc1432222018-03-29 13:06:07 +09003256 && mService.mContext.getResources().getBoolean(
3257 com.android.internal.R.bool.config_checkWallpaperAtBoot)
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003258 && !mService.mOnlyCore;
3259
Wale Ogunwale494009b82016-10-21 09:01:38 -07003260 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
3261 "******** booted=" + mService.mSystemBooted
3262 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003263 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
3264 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
3265 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003266
3267 // If we are turning on the screen to show the boot message, don't do it until the boot
3268 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003269 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003270 return true;
3271 }
3272
3273 // If we are turning on the screen after the boot is completed normally, don't do so until
3274 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003275 if (mService.mSystemBooted
3276 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003277 return true;
3278 }
3279
3280 return false;
3281 }
3282
Jorim Jaggi8f520872018-08-14 17:00:20 +02003283 void updateWindowsForAnimator() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003284 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003285 }
3286
Jorim Jaggi8f520872018-08-14 17:00:20 +02003287 /**
3288 * Updates the {@link TaskStack#setAnimationBackground} for all windows.
3289 */
3290 void updateBackgroundForAnimator() {
Jorim Jaggi7156b652016-10-25 08:31:58 -07003291 resetAnimationBackgroundAnimator();
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003292 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003293 }
3294
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003295 boolean isInputMethodClientFocus(int uid, int pid) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003296 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003297 if (imFocus == null) {
3298 return false;
3299 }
3300
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003301 if (DEBUG_INPUT_METHOD) {
3302 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003303 Slog.i(TAG_WM, "Current focus: " + mCurrentFocus + " displayId=" + mDisplayId);
3304 Slog.i(TAG_WM, "Last focus: " + mLastFocus + " displayId=" + mDisplayId);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003305 }
3306
Wale Ogunwale494009b82016-10-21 09:01:38 -07003307 if (DEBUG_INPUT_METHOD) {
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003308 Slog.i(TAG_WM, "IM target uid/pid: " + imFocus.mSession.mUid
3309 + "/" + imFocus.mSession.mPid);
3310 Slog.i(TAG_WM, "Requesting client uid/pid: " + uid + "/" + pid);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003311 }
3312
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003313 return imFocus.mSession.mUid == uid && imFocus.mSession.mPid == pid;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003314 }
3315
3316 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003317 final WindowState win = getWindow(
3318 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
3319 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003320 }
3321
Tiger Huang7c610aa2018-10-27 00:01:01 +08003322 void statusBarVisibilityChanged(int visibility) {
3323 mLastStatusBarVisibility = visibility;
3324 visibility = getDisplayPolicy().adjustSystemUiVisibilityLw(visibility);
3325 updateStatusBarVisibilityLocked(visibility);
3326 }
3327
3328 private boolean updateStatusBarVisibilityLocked(int visibility) {
3329 if (mLastDispatchedSystemUiVisibility == visibility) {
3330 return false;
3331 }
3332 final int globalDiff = (visibility ^ mLastDispatchedSystemUiVisibility)
3333 // We are only interested in differences of one of the
3334 // clearable flags...
3335 & View.SYSTEM_UI_CLEARABLE_FLAGS
3336 // ...if it has actually been cleared.
3337 & ~visibility;
3338
3339 mLastDispatchedSystemUiVisibility = visibility;
3340 if (isDefaultDisplay) {
3341 mService.mInputManager.setSystemUiVisibility(visibility);
3342 }
3343 updateSystemUiVisibility(visibility, globalDiff);
3344 return true;
3345 }
3346
Wale Ogunwale494009b82016-10-21 09:01:38 -07003347 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003348 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003349 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003350 final int curValue = w.mSystemUiVisibility;
3351 final int diff = (curValue ^ visibility) & globalDiff;
3352 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003353 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003354 w.mSeq++;
3355 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003356 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003357 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
3358 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07003359 visibility, newValue, diff);
3360 }
3361 } catch (RemoteException e) {
3362 // so sorry
3363 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003364 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003365 }
3366
Tiger Huang7c610aa2018-10-27 00:01:01 +08003367 void reevaluateStatusBarVisibility() {
3368 int visibility = getDisplayPolicy().adjustSystemUiVisibilityLw(mLastStatusBarVisibility);
3369 if (updateStatusBarVisibilityLocked(visibility)) {
3370 mService.mWindowPlacerLocked.requestTraversal();
3371 }
3372 }
3373
Wale Ogunwale494009b82016-10-21 09:01:38 -07003374 void onWindowFreezeTimeout() {
3375 Slog.w(TAG_WM, "Window freeze timeout expired.");
3376 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003377
3378 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07003379 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003380 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003381 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07003382 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003383 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
3384 - mService.mDisplayFreezeTime);
3385 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003386 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003387 mService.mWindowPlacerLocked.performSurfacePlacement();
3388 }
3389
3390 void waitForAllWindowsDrawn() {
3391 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003392 forAllWindows(w -> {
3393 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
3394 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
3395 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003396 // Force add to mResizingWindows.
chaviw9c81e632018-07-31 11:17:52 -07003397 w.resetLastContentInsets();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003398 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003399 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003400 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003401 }
3402
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003403 // TODO: Super crazy long method that should be broken down...
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003404 void applySurfaceChangesTransaction(boolean recoveringMemory) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003405 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
3406
3407 mTmpUpdateAllDrawn.clear();
3408
3409 int repeats = 0;
3410 do {
3411 repeats++;
3412 if (repeats > 6) {
3413 Slog.w(TAG, "Animation repeat aborted after too many iterations");
3414 clearLayoutNeeded();
3415 break;
3416 }
3417
3418 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
3419 pendingLayoutChanges);
3420
wilsonshihc32538e2018-11-07 17:27:34 +08003421 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
3422 mWallpaperController.adjustWallpaperWindows();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003423 }
3424
Riddle Hsu654a6f92018-07-13 22:59:36 +08003425 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003426 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
Riddle Hsu4e611772018-10-31 18:58:28 +08003427 if (updateOrientationFromAppTokens()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003428 setLayoutNeeded();
Riddle Hsu4e611772018-10-31 18:58:28 +08003429 sendNewConfiguration();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003430 }
3431 }
3432
3433 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
3434 setLayoutNeeded();
3435 }
3436
3437 // FIRST LOOP: Perform a layout, if needed.
3438 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
3439 performLayout(repeats == 1, false /* updateInputWindows */);
3440 } else {
3441 Slog.w(TAG, "Layout repeat skipped after too many iterations");
3442 }
3443
3444 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
3445 pendingLayoutChanges = 0;
3446
Tiger Huang7c610aa2018-10-27 00:01:01 +08003447 mDisplayPolicy.beginPostLayoutPolicyLw();
3448 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
3449 pendingLayoutChanges |= mDisplayPolicy.finishPostLayoutPolicyLw();
3450 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
3451 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003452 } while (pendingLayoutChanges != 0);
3453
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003454 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003455
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003456 mTmpRecoveringMemory = recoveringMemory;
3457 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01003458 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003459
Jorim Jaggif1292892018-09-10 11:58:13 +02003460 mLastHasContent = mTmpApplySurfaceChangesTransactionState.displayHasContent;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003461 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Jorim Jaggif1292892018-09-10 11:58:13 +02003462 mLastHasContent,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003463 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
3464 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003465 true /* inTraversal, must call performTraversalInTrans... below */);
3466
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003467 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
3468 if (wallpaperVisible != mLastWallpaperVisible) {
3469 mLastWallpaperVisible = wallpaperVisible;
3470 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
3471 }
3472
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003473 while (!mTmpUpdateAllDrawn.isEmpty()) {
3474 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
3475 // See if any windows have been drawn, so they (and others associated with them)
3476 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07003477 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003478 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003479 }
3480
Bryce Leef3c6a472017-11-14 14:53:06 -08003481 private void updateBounds() {
Evan Rosky39b6f232018-10-30 18:35:41 -07003482 calculateBounds(mDisplayInfo, mTmpBounds);
Bryce Leef3c6a472017-11-14 14:53:06 -08003483 setBounds(mTmpBounds);
3484 }
3485
3486 // Determines the current display bounds based on the current state
Evan Rosky39b6f232018-10-30 18:35:41 -07003487 private void calculateBounds(DisplayInfo displayInfo, Rect out) {
Bryce Leef3c6a472017-11-14 14:53:06 -08003488 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
Evan Rosky39b6f232018-10-30 18:35:41 -07003489 final int rotation = displayInfo.rotation;
3490 boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Bryce Leef3c6a472017-11-14 14:53:06 -08003491 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
3492 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
Evan Rosky39b6f232018-10-30 18:35:41 -07003493 int width = displayInfo.logicalWidth;
Bryce Leef3c6a472017-11-14 14:53:06 -08003494 int left = (physWidth - width) / 2;
Evan Rosky39b6f232018-10-30 18:35:41 -07003495 int height = displayInfo.logicalHeight;
Bryce Leef3c6a472017-11-14 14:53:06 -08003496 int top = (physHeight - height) / 2;
3497 out.set(left, top, left + width, top + height);
3498 }
3499
3500 @Override
3501 public void getBounds(Rect out) {
Evan Rosky39b6f232018-10-30 18:35:41 -07003502 calculateBounds(mDisplayInfo, out);
Bryce Leef3c6a472017-11-14 14:53:06 -08003503 }
3504
3505 private void getBounds(Rect out, int orientation) {
3506 getBounds(out);
3507
3508 // Rotate the Rect if needed.
3509 final int currentRotation = mDisplayInfo.rotation;
3510 final int rotationDelta = deltaRotation(currentRotation, orientation);
3511 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
3512 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
3513 mTmpRectF.set(out);
3514 mTmpMatrix.mapRect(mTmpRectF);
3515 mTmpRectF.round(out);
3516 }
3517 }
3518
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003519 void performLayout(boolean initial, boolean updateInputWindows) {
3520 if (!isLayoutNeeded()) {
3521 return;
3522 }
3523 clearLayoutNeeded();
3524
3525 final int dw = mDisplayInfo.logicalWidth;
3526 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003527 if (DEBUG_LAYOUT) {
3528 Slog.v(TAG, "-------------------------------------");
3529 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
3530 }
3531
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01003532 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
3533 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00003534 // TODO: Not sure if we really need to set the rotation here since we are updating from the
3535 // display info above...
3536 mDisplayFrames.mRotation = mRotation;
Tiger Huang7c610aa2018-10-27 00:01:01 +08003537 mDisplayPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003538
Adrian Roos5251b1d2018-03-23 18:57:43 +01003539 int seq = mLayoutSeq + 1;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003540 if (seq < 0) seq = 0;
Adrian Roos5251b1d2018-03-23 18:57:43 +01003541 mLayoutSeq = seq;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003542
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003543 // Used to indicate that we have processed the dream window and all additional windows are
3544 // behind it.
3545 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003546 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003547
3548 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003549 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003550
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003551 // Used to indicate that we have processed the dream window and all additional attached
3552 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003553 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003554 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003555
3556 // Now perform layout of attached windows, which usually depend on the position of the
3557 // window they are attached to. XXX does not deal with windows that are attached to windows
3558 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003559 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003560
3561 // Window frames may have changed. Tell the input dispatcher about it.
Arthur Hung95b38a92018-07-20 18:56:12 +08003562 mInputMonitor.layoutInputConsumers(dw, dh);
3563 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003564 if (updateInputWindows) {
Arthur Hung95b38a92018-07-20 18:56:12 +08003565 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003566 }
3567
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003568 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
3569 }
3570
3571 /**
3572 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
3573 * In portrait mode, it grabs the full screenshot.
3574 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003575 * @param config of the output bitmap
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003576 */
chaviw0315a1a2018-03-05 15:28:35 -08003577 Bitmap screenshotDisplayLocked(Bitmap.Config config) {
3578 if (!mService.mPolicy.isScreenOn()) {
3579 if (DEBUG_SCREENSHOT) {
3580 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003581 }
chaviw0315a1a2018-03-05 15:28:35 -08003582 return null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003583 }
chaviwfbe47df2017-11-10 16:14:49 -08003584
chaviw0315a1a2018-03-05 15:28:35 -08003585 int dw = mDisplayInfo.logicalWidth;
3586 int dh = mDisplayInfo.logicalHeight;
chaviwfbe47df2017-11-10 16:14:49 -08003587
chaviw0315a1a2018-03-05 15:28:35 -08003588 if (dw <= 0 || dh <= 0) {
3589 return null;
3590 }
chaviwfbe47df2017-11-10 16:14:49 -08003591
chaviw0315a1a2018-03-05 15:28:35 -08003592 final Rect frame = new Rect(0, 0, dw, dh);
chaviwfbe47df2017-11-10 16:14:49 -08003593
chaviw0315a1a2018-03-05 15:28:35 -08003594 // The screenshot API does not apply the current screen rotation.
3595 int rot = mDisplay.getRotation();
chaviwfbe47df2017-11-10 16:14:49 -08003596
chaviw0315a1a2018-03-05 15:28:35 -08003597 if (rot == ROTATION_90 || rot == ROTATION_270) {
3598 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3599 }
chaviwfbe47df2017-11-10 16:14:49 -08003600
chaviw0315a1a2018-03-05 15:28:35 -08003601 // SurfaceFlinger is not aware of orientation, so convert our logical
3602 // crop to SurfaceFlinger's portrait orientation.
3603 convertCropForSurfaceFlinger(frame, rot, dw, dh);
3604
3605 final ScreenRotationAnimation screenRotationAnimation =
3606 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3607 final boolean inRotation = screenRotationAnimation != null &&
3608 screenRotationAnimation.isAnimating();
3609 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
3610
3611 // TODO(b/68392460): We should screenshot Task controls directly
3612 // but it's difficult at the moment as the Task doesn't have the
3613 // correct size set.
chaviw08520a02018-09-10 16:44:56 -07003614 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, inRotation, rot);
chaviw0315a1a2018-03-05 15:28:35 -08003615 if (bitmap == null) {
3616 Slog.w(TAG_WM, "Failed to take screenshot");
3617 return null;
3618 }
3619
3620 // Create a copy of the screenshot that is immutable and backed in ashmem.
3621 // This greatly reduces the overhead of passing the bitmap between processes.
3622 final Bitmap ret = bitmap.createAshmemBitmap(config);
3623 bitmap.recycle();
3624 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003625 }
3626
3627 // TODO: Can this use createRotationMatrix()?
3628 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3629 if (rot == Surface.ROTATION_90) {
3630 final int tmp = crop.top;
3631 crop.top = dw - crop.right;
3632 crop.right = crop.bottom;
3633 crop.bottom = dw - crop.left;
3634 crop.left = tmp;
3635 } else if (rot == Surface.ROTATION_180) {
3636 int tmp = crop.top;
3637 crop.top = dh - crop.bottom;
3638 crop.bottom = dh - tmp;
3639 tmp = crop.right;
3640 crop.right = dw - crop.left;
3641 crop.left = dw - tmp;
3642 } else if (rot == Surface.ROTATION_270) {
3643 final int tmp = crop.top;
3644 crop.top = crop.left;
3645 crop.left = dh - crop.bottom;
3646 crop.bottom = crop.right;
3647 crop.right = dh - tmp;
3648 }
3649 }
3650
3651 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003652 // Used to indicate the layout is needed.
3653 mTmpWindow = null;
3654
3655 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003656 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003657 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003658 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003659 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003660 w.setDisplayLayoutNeeded();
Vishnu Nairba183352018-11-21 11:16:49 -08003661 w.finishSeamlessRotation(true /* timeout */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003662 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003663 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003664
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003665 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003666 mService.mWindowPlacerLocked.performSurfacePlacement();
3667 }
3668 }
3669
Wale Ogunwale1666e312016-12-16 11:27:18 -08003670 void setExitingTokensHasVisible(boolean hasVisible) {
3671 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3672 mExitingTokens.get(i).hasVisible = hasVisible;
3673 }
3674
3675 // Initialize state of exiting applications.
3676 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3677 }
3678
3679 void removeExistingTokensIfPossible() {
3680 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3681 final WindowToken token = mExitingTokens.get(i);
3682 if (!token.hasVisible) {
3683 mExitingTokens.remove(i);
3684 }
3685 }
3686
3687 // Time to remove any exiting applications?
3688 mTaskStackContainers.removeExistingAppTokensIfPossible();
3689 }
3690
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003691 @Override
3692 void onDescendantOverrideConfigurationChanged() {
3693 setLayoutNeeded();
3694 mService.requestTraversal();
3695 }
3696
David Stevens9440dc82017-03-16 19:00:20 -07003697 boolean okToDisplay() {
3698 if (mDisplayId == DEFAULT_DISPLAY) {
3699 return !mService.mDisplayFrozen
3700 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3701 }
3702 return mDisplayInfo.state == Display.STATE_ON;
3703 }
3704
3705 boolean okToAnimate() {
3706 return okToDisplay() &&
3707 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3708 }
3709
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003710 static final class TaskForResizePointSearchResult {
3711 boolean searchDone;
3712 Task taskForResize;
3713
3714 void reset() {
3715 searchDone = false;
3716 taskForResize = null;
3717 }
3718 }
Robert Carr3b716242016-08-16 16:02:21 -07003719
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003720 private static final class ApplySurfaceChangesTransactionState {
3721 boolean displayHasContent;
3722 boolean obscured;
3723 boolean syswin;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003724 float preferredRefreshRate;
3725 int preferredModeId;
3726
3727 void reset() {
3728 displayHasContent = false;
3729 obscured = false;
3730 syswin = false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003731 preferredRefreshRate = 0;
3732 preferredModeId = 0;
3733 }
3734 }
3735
3736 private static final class ScreenshotApplicationState {
3737 WindowState appWin;
3738 int maxLayer;
3739 int minLayer;
3740 boolean screenshotReady;
3741
3742 void reset(boolean screenshotReady) {
3743 appWin = null;
3744 maxLayer = 0;
3745 minLayer = 0;
3746 this.screenshotReady = screenshotReady;
3747 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3748 }
3749 }
3750
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003751 /**
3752 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3753 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3754 * homogeneous children type which is currently required by sub-classes of
3755 * {@link WindowContainer} class.
3756 */
3757 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3758
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003759 DisplayChildWindowContainer(WindowManagerService service) {
3760 super(service);
3761 }
3762
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003763 @Override
3764 boolean fillsParent() {
3765 return true;
3766 }
3767
3768 @Override
3769 boolean isVisible() {
3770 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003771 }
3772 }
3773
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003774 /**
3775 * Window container class that contains all containers on this display relating to Apps.
3776 * I.e Activities.
3777 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003778 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003779 /**
3780 * A control placed at the appropriate level for transitions to occur.
3781 */
chaviw23ee71c2017-12-18 11:29:41 -08003782 SurfaceControl mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02003783 SurfaceControl mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02003784 SurfaceControl mHomeAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003785
Robert Carrf7a7ca82017-12-06 13:17:07 -08003786 /**
3787 * Given that the split-screen divider does not have an AppWindowToken, it
3788 * will have to live inside of a "NonAppWindowContainer", in particular
3789 * {@link DisplayContent#mAboveAppWindowsContainers}. However, in visual Z order
3790 * it will need to be interleaved with some of our children, appearing on top of
3791 * both docked stacks but underneath any assistant stacks.
3792 *
3793 * To solve this problem we have this anchor control, which will always exist so
3794 * we can always assign it the correct value in our {@link #assignChildLayers}.
3795 * Likewise since it always exists, {@link AboveAppWindowContainers} can always
3796 * assign the divider a layer relative to it. This way we prevent linking lifecycle
3797 * events between the two containers.
3798 */
3799 SurfaceControl mSplitScreenDividerAnchor = null;
3800
Wale Ogunwale61911492017-10-11 08:50:50 -07003801 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3802 // through the list to find them.
3803 private TaskStack mHomeStack = null;
3804 private TaskStack mPinnedStack = null;
3805 private TaskStack mSplitScreenPrimaryStack = null;
3806
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003807 TaskStackContainers(WindowManagerService service) {
3808 super(service);
3809 }
3810
Wale Ogunwale61911492017-10-11 08:50:50 -07003811 /**
3812 * Returns the topmost stack on the display that is compatible with the input windowing mode
3813 * and activity type. Null is no compatible stack on the display.
3814 */
3815 TaskStack getStack(int windowingMode, int activityType) {
3816 if (activityType == ACTIVITY_TYPE_HOME) {
3817 return mHomeStack;
3818 }
3819 if (windowingMode == WINDOWING_MODE_PINNED) {
3820 return mPinnedStack;
3821 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3822 return mSplitScreenPrimaryStack;
3823 }
3824 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3825 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003826 if (activityType == ACTIVITY_TYPE_UNDEFINED
3827 && windowingMode == stack.getWindowingMode()) {
3828 // Passing in undefined type means we want to match the topmost stack with the
3829 // windowing mode.
3830 return stack;
3831 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003832 if (stack.isCompatible(windowingMode, activityType)) {
3833 return stack;
3834 }
3835 }
3836 return null;
3837 }
3838
3839 @VisibleForTesting
3840 TaskStack getTopStack() {
3841 return mTaskStackContainers.getChildCount() > 0
3842 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3843 }
3844
3845 TaskStack getHomeStack() {
3846 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3847 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3848 }
3849 return mHomeStack;
3850 }
3851
3852 TaskStack getPinnedStack() {
3853 return mPinnedStack;
3854 }
3855
Matthew Ng64e77cf2017-10-31 14:01:31 -07003856 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003857 return mSplitScreenPrimaryStack;
3858 }
3859
Winson Chunge2d72172018-01-25 17:46:20 +00003860 ArrayList<Task> getVisibleTasks() {
3861 final ArrayList<Task> visibleTasks = new ArrayList<>();
3862 forAllTasks(task -> {
3863 if (task.isVisible()) {
3864 visibleTasks.add(task);
3865 }
3866 });
3867 return visibleTasks;
3868 }
3869
Andrii Kulian839def92016-11-02 10:58:58 -07003870 /**
3871 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003872 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003873 */
3874 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003875 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003876 addChild(stack, onTop);
3877 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003878 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003879
Wale Ogunwale61911492017-10-11 08:50:50 -07003880 void onStackWindowingModeChanged(TaskStack stack) {
3881 removeStackReferenceIfNeeded(stack);
3882 addStackReferenceIfNeeded(stack);
3883 if (stack == mPinnedStack && getTopStack() != stack) {
3884 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3885 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3886 }
3887 }
3888
3889 private void addStackReferenceIfNeeded(TaskStack stack) {
3890 if (stack.isActivityTypeHome()) {
3891 if (mHomeStack != null) {
Louis Chang7d0037c2018-08-13 12:42:06 +08003892 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
Wale Ogunwale61911492017-10-11 08:50:50 -07003893 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
Louis Chang7d0037c2018-08-13 12:42:06 +08003894
Wale Ogunwale61911492017-10-11 08:50:50 -07003895 }
Louis Chang7d0037c2018-08-13 12:42:06 +08003896 mHomeStack = stack;
Wale Ogunwale61911492017-10-11 08:50:50 -07003897 }
3898 final int windowingMode = stack.getWindowingMode();
3899 if (windowingMode == WINDOWING_MODE_PINNED) {
3900 if (mPinnedStack != null) {
3901 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3902 + mPinnedStack + " already exist on display=" + this
3903 + " stack=" + stack);
3904 }
3905 mPinnedStack = stack;
3906 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3907 if (mSplitScreenPrimaryStack != null) {
3908 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3909 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3910 + " already exist on display=" + this + " stack=" + stack);
3911 }
3912 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003913 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003914 }
3915 }
3916
3917 private void removeStackReferenceIfNeeded(TaskStack stack) {
3918 if (stack == mHomeStack) {
3919 mHomeStack = null;
3920 } else if (stack == mPinnedStack) {
3921 mPinnedStack = null;
3922 } else if (stack == mSplitScreenPrimaryStack) {
3923 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003924 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3925 mService.setDockedStackCreateStateLocked(
3926 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3927 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003928 }
Andrii Kulian839def92016-11-02 10:58:58 -07003929 }
3930
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003931 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003932 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3933 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003934 addChild(stack, addIndex);
3935 setLayoutNeeded();
3936 }
3937
Wale Ogunwale61911492017-10-11 08:50:50 -07003938 @Override
3939 protected void removeChild(TaskStack stack) {
3940 super.removeChild(stack);
3941 removeStackReferenceIfNeeded(stack);
3942 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003943
3944 @Override
3945 boolean isOnTop() {
3946 // Considered always on top
3947 return true;
3948 }
3949
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003950 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003951 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003952 if (child.getWindowConfiguration().isAlwaysOnTop()
3953 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003954 // This stack is always-on-top, override the default behavior.
3955 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3956
3957 // Moving to its current position, as we must call super but we don't want to
3958 // perform any meaningful action.
3959 final int currentPosition = mChildren.indexOf(child);
3960 super.positionChildAt(currentPosition, child, false /* includingParents */);
3961 return;
3962 }
3963
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003964 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3965 super.positionChildAt(targetPosition, child, includingParents);
3966
Riddle Hsu2f6e1742018-08-23 22:44:36 +08003967 if (includingParents) {
3968 // We still want to move the display of this stack container to top because even the
3969 // target position is adjusted to non-top, the intention of the condition is to have
3970 // higher z-order to gain focus (e.g. moving a task of a fullscreen stack to front
3971 // in a non-top display which is using picture-in-picture mode).
3972 final int topChildPosition = getChildCount() - 1;
3973 if (targetPosition < topChildPosition && position >= topChildPosition) {
3974 getParent().positionChildAt(POSITION_TOP, this /* child */,
3975 true /* includingParents */);
3976 }
3977 }
3978
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003979 setLayoutNeeded();
3980 }
3981
3982 /**
3983 * When stack is added or repositioned, find a proper position for it.
3984 * This will make sure that pinned stack always stays on top.
3985 * @param requestedPosition Position requested by caller.
3986 * @param stack Stack to be added or positioned.
3987 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3988 * @return The proper position for the stack.
3989 */
3990 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09003991 if (stack.inPinnedWindowingMode()) {
Kazuki Takisef85197b2018-06-18 18:18:36 +09003992 return POSITION_TOP;
Kazuki Takise148d00a2018-05-31 15:32:19 +09003993 }
3994
Kazuki Takisef85197b2018-06-18 18:18:36 +09003995 final int topChildPosition = mChildren.size() - 1;
3996 int belowAlwaysOnTopPosition = POSITION_BOTTOM;
3997 for (int i = topChildPosition; i >= 0; --i) {
3998 if (getStacks().get(i) != stack && !getStacks().get(i).isAlwaysOnTop()) {
3999 belowAlwaysOnTopPosition = i;
4000 break;
4001 }
4002 }
4003
4004 // The max possible position we can insert the stack at.
4005 int maxPosition = POSITION_TOP;
4006 // The min possible position we can insert the stack at.
4007 int minPosition = POSITION_BOTTOM;
4008
4009 if (stack.isAlwaysOnTop()) {
4010 if (hasPinnedStack()) {
4011 // Always-on-top stacks go below the pinned stack.
4012 maxPosition = getStacks().indexOf(mPinnedStack) - 1;
4013 }
4014 // Always-on-top stacks need to be above all other stacks.
4015 minPosition = belowAlwaysOnTopPosition !=
4016 POSITION_BOTTOM ? belowAlwaysOnTopPosition : topChildPosition;
4017 } else {
4018 // Other stacks need to be below the always-on-top stacks.
4019 maxPosition = belowAlwaysOnTopPosition !=
Arthur Hung928d8dc2018-07-18 15:38:05 +08004020 POSITION_BOTTOM ? belowAlwaysOnTopPosition : 0;
Kazuki Takisef85197b2018-06-18 18:18:36 +09004021 }
4022
4023 int targetPosition = requestedPosition;
4024 targetPosition = Math.min(targetPosition, maxPosition);
4025 targetPosition = Math.max(targetPosition, minPosition);
4026
4027 int prevPosition = getStacks().indexOf(stack);
4028 // The positions we calculated above (maxPosition, minPosition) do not take into
4029 // consideration the following edge cases.
4030 // 1) We need to adjust the position depending on the value "adding".
4031 // 2) When we are moving a stack to another position, we also need to adjust the
4032 // position depending on whether the stack is moving to a higher or lower position.
4033 if ((targetPosition != requestedPosition) &&
4034 (adding || targetPosition < prevPosition)) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09004035 targetPosition++;
4036 }
4037
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08004038 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08004039 }
4040
4041 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004042 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
4043 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004044 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004045 if (super.forAllWindows(callback, traverseTopToBottom)) {
4046 return true;
4047 }
4048 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4049 return true;
4050 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004051 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004052 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
4053 return true;
4054 }
4055 if (super.forAllWindows(callback, traverseTopToBottom)) {
4056 return true;
4057 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004058 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004059 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004060 }
4061
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004062 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004063 boolean traverseTopToBottom) {
4064 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
4065 // app tokens.
4066 // TODO: Investigate if we need to continue to do this or if we can just process them
4067 // in-order.
4068 if (traverseTopToBottom) {
4069 for (int i = mChildren.size() - 1; i >= 0; --i) {
4070 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4071 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004072 if (appTokens.get(j).forAllWindowsUnchecked(callback,
4073 traverseTopToBottom)) {
4074 return true;
4075 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004076 }
4077 }
4078 } else {
4079 final int count = mChildren.size();
4080 for (int i = 0; i < count; ++i) {
4081 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4082 final int appTokensCount = appTokens.size();
4083 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004084 if (appTokens.get(j).forAllWindowsUnchecked(callback,
4085 traverseTopToBottom)) {
4086 return true;
4087 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004088 }
4089 }
4090 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08004091 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004092 }
4093
Wale Ogunwale1666e312016-12-16 11:27:18 -08004094 void setExitingTokensHasVisible(boolean hasVisible) {
4095 for (int i = mChildren.size() - 1; i >= 0; --i) {
4096 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4097 for (int j = appTokens.size() - 1; j >= 0; --j) {
4098 appTokens.get(j).hasVisible = hasVisible;
4099 }
4100 }
4101 }
4102
4103 void removeExistingAppTokensIfPossible() {
4104 for (int i = mChildren.size() - 1; i >= 0; --i) {
4105 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
4106 for (int j = appTokens.size() - 1; j >= 0; --j) {
4107 final AppWindowToken token = appTokens.get(j);
lumark588a3e82018-07-20 18:53:54 +08004108 if (!token.hasVisible && !mClosingApps.contains(token)
Wale Ogunwale1666e312016-12-16 11:27:18 -08004109 && (!token.mIsExiting || token.isEmpty())) {
4110 // Make sure there is no animation running on this token, so any windows
4111 // associated with it will be removed as soon as their animations are
4112 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02004113 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08004114 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
4115 "performLayout: App token exiting now removed" + token);
4116 token.removeIfPossible();
4117 }
4118 }
4119 }
4120 }
4121
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07004122 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004123 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07004124 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
4125 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004126 // Apps and their containers are not allowed to specify an orientation while the
Wale Ogunwaled0c225c2018-06-15 14:47:46 -07004127 // docked or freeform stack is visible...except for the home stack if the docked
4128 // stack is minimized and it actually set something and the bounds is different from
4129 // the display.
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004130 if (mHomeStack != null && mHomeStack.isVisible()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004131 && mDividerControllerLocked.isMinimizedDock()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07004132 && !(mDividerControllerLocked.isHomeStackResizable()
4133 && mHomeStack.matchParentBounds())) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004134 final int orientation = mHomeStack.getOrientation();
4135 if (orientation != SCREEN_ORIENTATION_UNSET) {
4136 return orientation;
4137 }
4138 }
4139 return SCREEN_ORIENTATION_UNSPECIFIED;
4140 }
4141
4142 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07004143 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
4144 PackageManager.FEATURE_AUTOMOTIVE);
4145 if (isCar) {
4146 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
4147 // allow anything but the default orientation.
4148 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004149 "Forcing UNSPECIFIED orientation in car for display id=" + mDisplayId
4150 + ". Ignoring " + orientation);
Dean Harding3e5a1522017-10-06 09:19:01 -07004151 return SCREEN_ORIENTATION_UNSPECIFIED;
4152 }
4153
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004154 if (orientation != SCREEN_ORIENTATION_UNSET
4155 && orientation != SCREEN_ORIENTATION_BEHIND) {
4156 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004157 "App is requesting an orientation, return " + orientation
4158 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004159 return orientation;
4160 }
4161
4162 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004163 "No app is requesting an orientation, return " + mLastOrientation
4164 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004165 // The next app has not been requested to be visible, so we keep the current orientation
4166 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08004167 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07004168 }
Robert Carrb1579c82017-09-05 14:54:47 -07004169
4170 @Override
4171 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004172 assignStackOrdering(t);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004173
Robert Carr2f8aa392018-01-31 14:46:51 -08004174 for (int i = 0; i < mChildren.size(); i++) {
4175 final TaskStack s = mChildren.get(i);
4176 s.assignChildLayers(t);
4177 }
4178 }
4179
4180 void assignStackOrdering(SurfaceControl.Transaction t) {
Jorim Jaggibe418292018-03-26 16:14:12 +02004181
Robert Carrf7a7ca82017-12-06 13:17:07 -08004182 final int HOME_STACK_STATE = 0;
4183 final int NORMAL_STACK_STATE = 1;
4184 final int ALWAYS_ON_TOP_STATE = 2;
4185
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004186 int layer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08004187 int layerForAnimationLayer = 0;
Jorim Jaggibe418292018-03-26 16:14:12 +02004188 int layerForBoostedAnimationLayer = 0;
Jorim Jaggi391790622018-04-18 15:30:44 +02004189 int layerForHomeAnimationLayer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08004190
Robert Carrf7a7ca82017-12-06 13:17:07 -08004191 for (int state = 0; state <= ALWAYS_ON_TOP_STATE; state++) {
4192 for (int i = 0; i < mChildren.size(); i++) {
4193 final TaskStack s = mChildren.get(i);
Robert Carr2f8aa392018-01-31 14:46:51 -08004194 if (state == HOME_STACK_STATE && !s.isActivityTypeHome()) {
4195 continue;
4196 } else if (state == NORMAL_STACK_STATE && (s.isActivityTypeHome()
4197 || s.isAlwaysOnTop())) {
4198 continue;
4199 } else if (state == ALWAYS_ON_TOP_STATE && !s.isAlwaysOnTop()) {
4200 continue;
4201 }
4202 s.assignLayer(t, layer++);
4203 if (s.inSplitScreenWindowingMode() && mSplitScreenDividerAnchor != null) {
4204 t.setLayer(mSplitScreenDividerAnchor, layer++);
4205 }
Winson Chungd41f71d2018-03-16 15:26:07 -07004206 if ((s.isTaskAnimating() || s.isAppAnimating())
4207 && state != ALWAYS_ON_TOP_STATE) {
Robert Carr2f8aa392018-01-31 14:46:51 -08004208 // Ensure the animation layer ends up above the
4209 // highest animating stack and no higher.
4210 layerForAnimationLayer = layer++;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004211 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004212 if (state != ALWAYS_ON_TOP_STATE) {
4213 layerForBoostedAnimationLayer = layer++;
4214 }
Robert Carrf7a7ca82017-12-06 13:17:07 -08004215 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004216 if (state == HOME_STACK_STATE) {
4217 layerForHomeAnimationLayer = layer++;
4218 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004219 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004220 if (mAppAnimationLayer != null) {
4221 t.setLayer(mAppAnimationLayer, layerForAnimationLayer);
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004222 }
Jorim Jaggibe418292018-03-26 16:14:12 +02004223 if (mBoostedAppAnimationLayer != null) {
4224 t.setLayer(mBoostedAppAnimationLayer, layerForBoostedAnimationLayer);
4225 }
Jorim Jaggi391790622018-04-18 15:30:44 +02004226 if (mHomeAppAnimationLayer != null) {
4227 t.setLayer(mHomeAppAnimationLayer, layerForHomeAnimationLayer);
4228 }
Robert Carrb1579c82017-09-05 14:54:47 -07004229 }
4230
4231 @Override
Jorim Jaggi391790622018-04-18 15:30:44 +02004232 SurfaceControl getAppAnimationLayer(@AnimationLayer int animationLayer) {
4233 switch (animationLayer) {
4234 case ANIMATION_LAYER_BOOSTED:
4235 return mBoostedAppAnimationLayer;
4236 case ANIMATION_LAYER_HOME:
4237 return mHomeAppAnimationLayer;
4238 case ANIMATION_LAYER_STANDARD:
4239 default:
4240 return mAppAnimationLayer;
4241 }
chaviw23ee71c2017-12-18 11:29:41 -08004242 }
4243
Robert Carrf7a7ca82017-12-06 13:17:07 -08004244 SurfaceControl getSplitScreenDividerAnchor() {
4245 return mSplitScreenDividerAnchor;
4246 }
4247
chaviw23ee71c2017-12-18 11:29:41 -08004248 @Override
Robert Carrb1579c82017-09-05 14:54:47 -07004249 void onParentSet() {
4250 super.onParentSet();
4251 if (getParent() != null) {
chaviw23ee71c2017-12-18 11:29:41 -08004252 mAppAnimationLayer = makeChildSurface(null)
4253 .setName("animationLayer")
4254 .build();
Jorim Jaggibe418292018-03-26 16:14:12 +02004255 mBoostedAppAnimationLayer = makeChildSurface(null)
4256 .setName("boostedAnimationLayer")
4257 .build();
Jorim Jaggi391790622018-04-18 15:30:44 +02004258 mHomeAppAnimationLayer = makeChildSurface(null)
4259 .setName("homeAnimationLayer")
4260 .build();
Robert Carrf7a7ca82017-12-06 13:17:07 -08004261 mSplitScreenDividerAnchor = makeChildSurface(null)
4262 .setName("splitScreenDividerAnchor")
4263 .build();
4264 getPendingTransaction()
4265 .show(mAppAnimationLayer)
Jorim Jaggibe418292018-03-26 16:14:12 +02004266 .show(mBoostedAppAnimationLayer)
Jorim Jaggi391790622018-04-18 15:30:44 +02004267 .show(mHomeAppAnimationLayer)
Robert Carrf7a7ca82017-12-06 13:17:07 -08004268 .show(mSplitScreenDividerAnchor);
chaviw23ee71c2017-12-18 11:29:41 -08004269 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07004270 } else {
chaviw23ee71c2017-12-18 11:29:41 -08004271 mAppAnimationLayer.destroy();
4272 mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02004273 mBoostedAppAnimationLayer.destroy();
4274 mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02004275 mHomeAppAnimationLayer.destroy();
4276 mHomeAppAnimationLayer = null;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004277 mSplitScreenDividerAnchor.destroy();
4278 mSplitScreenDividerAnchor = null;
Robert Carrb1579c82017-09-05 14:54:47 -07004279 }
4280 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004281 }
4282
Robert Carree4d4b92017-11-22 12:21:46 -08004283 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004284 AboveAppWindowContainers(String name, WindowManagerService service) {
4285 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08004286 }
4287
Robert Carrb9506032018-02-13 13:54:00 -08004288 @Override
Vishnu Nair83537a72018-07-19 21:27:48 -07004289 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
4290 final SurfaceControl.Builder builder = super.makeChildSurface(child);
4291 if (child instanceof WindowToken && ((WindowToken) child).mRoundedCornerOverlay) {
4292 // To draw above the ColorFade layer during the screen off transition, the
4293 // rounded corner overlays need to be at the root of the surface hierarchy.
4294 // TODO: move the ColorLayer into the display overlay layer such that this is not
4295 // necessary anymore.
4296 builder.setParent(null);
4297 }
4298 return builder;
4299 }
4300
4301 @Override
Robert Carrb9506032018-02-13 13:54:00 -08004302 void assignChildLayers(SurfaceControl.Transaction t) {
4303 assignChildLayers(t, null /* imeContainer */);
4304 }
4305
Robert Carree4d4b92017-11-22 12:21:46 -08004306 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
4307 boolean needAssignIme = imeContainer != null
4308 && imeContainer.getSurfaceControl() != null;
4309 for (int j = 0; j < mChildren.size(); ++j) {
4310 final WindowToken wt = mChildren.get(j);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004311
4312 // See {@link mSplitScreenDividerAnchor}
4313 if (wt.windowType == TYPE_DOCK_DIVIDER) {
4314 wt.assignRelativeLayer(t, mTaskStackContainers.getSplitScreenDividerAnchor(), 1);
4315 continue;
4316 }
Vishnu Nair83537a72018-07-19 21:27:48 -07004317 if (wt.mRoundedCornerOverlay) {
4318 wt.assignLayer(t, WindowManagerPolicy.COLOR_FADE_LAYER + 1);
4319 continue;
4320 }
Robert Carree4d4b92017-11-22 12:21:46 -08004321 wt.assignLayer(t, j);
4322 wt.assignChildLayers(t);
4323
4324 int layer = mService.mPolicy.getWindowLayerFromTypeLw(
4325 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08004326
4327 if (needAssignIme && layer >= mService.mPolicy.getWindowLayerFromTypeLw(
4328 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004329 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08004330 needAssignIme = false;
4331 }
4332 }
4333 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004334 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08004335 }
4336 }
4337 }
4338
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004339 /**
4340 * Window container class that contains all containers on this display that are not related to
4341 * Apps. E.g. status bar.
4342 */
Robert Carree4d4b92017-11-22 12:21:46 -08004343 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07004344 /**
4345 * Compares two child window tokens returns -1 if the first is lesser than the second in
4346 * terms of z-order and 1 otherwise.
4347 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004348 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07004349 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004350 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
4351 token1.mOwnerCanManageAppTokens)
4352 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
4353 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004354
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004355 private final Predicate<WindowState> mGetOrientingWindow = w -> {
4356 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
4357 return false;
4358 }
4359 final int req = w.mAttrs.screenOrientation;
4360 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
4361 || req == SCREEN_ORIENTATION_UNSET) {
4362 return false;
4363 }
4364 return true;
4365 };
4366
Wale Ogunwale3a931692016-11-02 16:49:48 -07004367 private final String mName;
chaviwf29223e2018-01-31 13:23:51 -08004368 private final Dimmer mDimmer = new Dimmer(this);
4369 private final Rect mTmpDimBoundsRect = new Rect();
4370
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004371 NonAppWindowContainers(String name, WindowManagerService service) {
4372 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07004373 mName = name;
4374 }
4375
4376 void addChild(WindowToken token) {
4377 addChild(token, mWindowComparator);
4378 }
4379
4380 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004381 int getOrientation() {
4382 final WindowManagerPolicy policy = mService.mPolicy;
4383 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004384 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004385
4386 if (win != null) {
4387 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004388 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004389 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07004390 if (mService.mKeyguardGoingAway) {
4391 // Keyguard can't affect the orientation if it is going away...
4392 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
4393 return SCREEN_ORIENTATION_UNSET;
4394 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004395 }
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004396 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req
4397 + " for display id=" + mDisplayId);
Andrii Kulian8ee72852017-03-10 10:36:45 -08004398 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004399 }
4400
Andrii Kulian8ee72852017-03-10 10:36:45 -08004401 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004402
Jorim Jaggi1c530592018-04-06 15:11:47 +02004403 // Only allow force setting the orientation when all unknown visibilities have been
4404 // resolved, as otherwise we just may be starting another occluding activity.
4405 final boolean isUnoccluding =
lumark588a3e82018-07-20 18:53:54 +08004406 mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
4407 && mUnknownAppVisibilityController.allResolved();
Jorim Jaggi1c530592018-04-06 15:11:47 +02004408 if (policy.isKeyguardShowingAndNotOccluded() || isUnoccluding) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004409 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004410 }
4411
4412 return SCREEN_ORIENTATION_UNSET;
4413 }
4414
4415 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07004416 String getName() {
4417 return mName;
4418 }
chaviwf29223e2018-01-31 13:23:51 -08004419
4420 @Override
4421 Dimmer getDimmer() {
4422 return mDimmer;
4423 }
4424
4425 @Override
4426 void prepareSurfaces() {
4427 mDimmer.resetDimStates();
4428 super.prepareSurfaces();
4429 getBounds(mTmpDimBoundsRect);
4430
4431 if (mDimmer.updateDims(getPendingTransaction(), mTmpDimBoundsRect)) {
4432 scheduleAnimation();
4433 }
4434 }
Robert Carr3b716242016-08-16 16:02:21 -07004435 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01004436
Robert Carr9034d962018-01-04 18:27:42 -08004437 private class NonMagnifiableWindowContainers extends NonAppWindowContainers {
4438 NonMagnifiableWindowContainers(String name, WindowManagerService service) {
4439 super(name, service);
4440 }
4441
4442 @Override
4443 void applyMagnificationSpec(Transaction t, MagnificationSpec spec) {
4444 }
4445 };
4446
Robert Carrb1579c82017-09-05 14:54:47 -07004447 SurfaceControl.Builder makeSurface(SurfaceSession s) {
4448 return mService.makeSurfaceBuilder(s)
4449 .setParent(mWindowingLayer);
4450 }
4451
4452 @Override
4453 SurfaceSession getSession() {
4454 return mSession;
4455 }
4456
4457 @Override
4458 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07004459 SurfaceSession s = child != null ? child.getSession() : getSession();
4460 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrf59b8dd2017-10-02 18:58:36 -07004461 if (child == null) {
4462 return b;
4463 }
4464
Robert Carree4d4b92017-11-22 12:21:46 -08004465 return b.setName(child.getName())
4466 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07004467 }
4468
4469 /**
4470 * The makeSurface variants are for use by the window-container
4471 * hierarchy. makeOverlay here is a function for various non windowing
4472 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
4473 * and other potpourii.
4474 */
4475 SurfaceControl.Builder makeOverlay() {
4476 return mService.makeSurfaceBuilder(mSession)
4477 .setParent(mOverlayLayer);
4478 }
4479
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004480 /**
4481 * Reparents the given surface to mOverlayLayer.
4482 */
4483 void reparentToOverlay(Transaction transaction, SurfaceControl surface) {
4484 transaction.reparent(surface, mOverlayLayer.getHandle());
4485 }
4486
Robert Carrb1579c82017-09-05 14:54:47 -07004487 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carr24be9ab2018-04-30 17:54:53 -07004488 if (spec.scale != 1.0) {
4489 mMagnificationSpec = spec;
4490 } else {
4491 mMagnificationSpec = null;
4492 }
4493
Robert Carrf59b8dd2017-10-02 18:58:36 -07004494 applyMagnificationSpec(getPendingTransaction(), spec);
4495 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07004496 }
4497
Robert Carr24be9ab2018-04-30 17:54:53 -07004498 void reapplyMagnificationSpec() {
4499 if (mMagnificationSpec != null) {
4500 applyMagnificationSpec(getPendingTransaction(), mMagnificationSpec);
4501 }
4502 }
4503
Robert Carrb1579c82017-09-05 14:54:47 -07004504 @Override
4505 void onParentSet() {
4506 // Since we are the top of the SurfaceControl hierarchy here
4507 // we create the root surfaces explicitly rather than chaining
4508 // up as the default implementation in onParentSet does. So we
4509 // explicitly do NOT call super here.
4510 }
4511
4512 @Override
4513 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07004514
4515 // These are layers as children of "mWindowingLayer"
4516 mBelowAppWindowsContainers.assignLayer(t, 0);
4517 mTaskStackContainers.assignLayer(t, 1);
4518 mAboveAppWindowsContainers.assignLayer(t, 2);
4519
lumarkff0ab692018-11-05 20:32:30 +08004520 final WindowState imeTarget = mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08004521 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08004522
Robert Carree4d4b92017-11-22 12:21:46 -08004523 // In the case where we have an IME target that is not in split-screen
4524 // mode IME assignment is easy. We just need the IME to go directly above
4525 // the target. This way children of the target will naturally go above the IME
4526 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08004527 //
Robert Carree4d4b92017-11-22 12:21:46 -08004528 // In the case of split-screen windowing mode, we need to elevate the IME above the
4529 // docked divider while keeping the app itself below the docked divider, so instead
4530 // we use relative layering of the IME targets child windows, and place the
4531 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08004532 //
Robert Carr234b6332018-03-20 13:38:16 -07004533 // In the case the IME target is animating, the animation Z order may be different
4534 // than the WindowContainer Z order, so it's difficult to be sure we have the correct
4535 // IME target. In this case we just layer the IME over all transitions by placing it in the
4536 // above applications layer.
4537 //
Robert Carree4d4b92017-11-22 12:21:46 -08004538 // In the case where we have no IME target we assign it where it's base layer would
4539 // place it in the AboveAppWindowContainers.
Robert Carr234b6332018-03-20 13:38:16 -07004540 if (imeTarget != null && !(imeTarget.inSplitScreenWindowingMode()
4541 || imeTarget.mToken.isAppAnimating())
Robert Carree4d4b92017-11-22 12:21:46 -08004542 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004543 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07004544 // TODO: We need to use an extra level on the app surface to ensure
4545 // this is always above SurfaceView but always below attached window.
4546 1);
Robert Carree4d4b92017-11-22 12:21:46 -08004547 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07004548 }
4549
4550 // Above we have assigned layers to our children, now we ask them to assign
4551 // layers to their children.
4552 mBelowAppWindowsContainers.assignChildLayers(t);
4553 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08004554 mAboveAppWindowsContainers.assignChildLayers(t,
4555 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07004556 mImeWindowsContainers.assignChildLayers(t);
4557 }
4558
4559 /**
4560 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
4561 * that the IME target is one of the docked applications. We'd like the docked divider to be
4562 * above both of the applications, and we'd like the IME to be above the docked divider.
4563 * However we need child windows of the applications to be above the IME (Text drag handles).
4564 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
4565 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
Tiger Huang7c610aa2018-10-27 00:01:01 +08004566 * with {@link WindowState#assignLayer}
Robert Carrb1579c82017-09-05 14:54:47 -07004567 */
4568 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
Robert Carrbb5de662018-04-26 13:29:51 -07004569 child.assignRelativeLayer(t, mImeWindowsContainers.getSurfaceControl(), 1);
Robert Carrb1579c82017-09-05 14:54:47 -07004570 }
4571
4572 @Override
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004573 void prepareSurfaces() {
4574 final ScreenRotationAnimation screenRotationAnimation =
4575 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
4576 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
4577 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
4578 mPendingTransaction.setMatrix(mWindowingLayer,
4579 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
4580 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
4581 mPendingTransaction.setPosition(mWindowingLayer,
4582 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
4583 mPendingTransaction.setAlpha(mWindowingLayer,
4584 screenRotationAnimation.getEnterTransformation().getAlpha());
4585 }
Chavi Weingartenb736e322018-02-23 00:27:54 +00004586
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004587 super.prepareSurfaces();
4588 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004589
Jorim Jaggibe418292018-03-26 16:14:12 +02004590 void assignStackOrdering() {
4591 mTaskStackContainers.assignStackOrdering(getPendingTransaction());
Robert Carr2f8aa392018-01-31 14:46:51 -08004592 }
Chavi Weingarten3a748552018-05-14 17:32:42 +00004593
4594 /**
4595 * Increment the deferral count to determine whether to update the IME target.
4596 */
4597 void deferUpdateImeTarget() {
4598 mDeferUpdateImeTargetCount++;
4599 }
4600
4601 /**
4602 * Decrement the deferral count to determine whether to update the IME target. If the count
4603 * reaches 0, a new ime target will get computed.
4604 */
4605 void continueUpdateImeTarget() {
4606 if (mDeferUpdateImeTargetCount == 0) {
4607 return;
4608 }
4609
4610 mDeferUpdateImeTargetCount--;
4611 if (mDeferUpdateImeTargetCount == 0) {
4612 computeImeTarget(true /* updateImeTarget */);
4613 }
4614 }
4615
4616 /**
4617 * @return Whether a new IME target should be computed.
4618 */
4619 private boolean canUpdateImeTarget() {
4620 return mDeferUpdateImeTargetCount == 0;
4621 }
Arthur Hung95b38a92018-07-20 18:56:12 +08004622
4623 InputMonitor getInputMonitor() {
4624 return mInputMonitor;
4625 }
Jorim Jaggif1292892018-09-10 11:58:13 +02004626
4627 /**
4628 * @return Cached value whether we told display manager that we have content.
4629 */
4630 boolean getLastHasContent() {
4631 return mLastHasContent;
4632 }
Arthur Hungbe5ce212018-09-13 18:41:56 +08004633
4634 void registerPointerEventListener(@NonNull PointerEventListener listener) {
4635 if (mPointerEventDispatcher != null) {
4636 mPointerEventDispatcher.registerInputEventListener(listener);
4637 }
4638 }
4639
4640 void unregisterPointerEventListener(@NonNull PointerEventListener listener) {
4641 if (mPointerEventDispatcher != null) {
4642 mPointerEventDispatcher.unregisterInputEventListener(listener);
4643 }
4644 }
lumark588a3e82018-07-20 18:53:54 +08004645
4646 void prepareAppTransition(@WindowManager.TransitionType int transit,
4647 boolean alwaysKeepCurrent, @WindowManager.TransitionFlags int flags,
4648 boolean forceOverride) {
4649 final boolean prepared = mAppTransition.prepareAppTransitionLocked(
4650 transit, alwaysKeepCurrent, flags, forceOverride);
4651 if (prepared && okToAnimate()) {
4652 mSkipAppTransitionAnimation = false;
4653 }
4654 }
4655
4656 void executeAppTransition() {
4657 if (mAppTransition.isTransitionSet()) {
4658 if (DEBUG_APP_TRANSITIONS) {
4659 Slog.w(TAG_WM, "Execute app transition: " + mAppTransition + ", displayId: "
4660 + mDisplayId + " Callers=" + Debug.getCallers(5));
4661 }
4662 mAppTransition.setReady();
4663 mService.mWindowPlacerLocked.requestTraversal();
4664 }
4665 }
4666
4667 /**
4668 * Update pendingLayoutChanges after app transition has finished.
4669 */
4670 void handleAnimatingStoppedAndTransition() {
4671 int changes = 0;
4672
4673 mAppTransition.setIdle();
4674
4675 for (int i = mNoAnimationNotifyOnTransitionFinished.size() - 1; i >= 0; i--) {
4676 final IBinder token = mNoAnimationNotifyOnTransitionFinished.get(i);
4677 mAppTransition.notifyAppTransitionFinishedLocked(token);
4678 }
4679 mNoAnimationNotifyOnTransitionFinished.clear();
4680
4681 mWallpaperController.hideDeferredWallpapersIfNeeded();
4682
4683 onAppTransitionDone();
4684
4685 changes |= FINISH_LAYOUT_REDO_LAYOUT;
4686 if (DEBUG_WALLPAPER_LIGHT) {
4687 Slog.v(TAG_WM, "Wallpaper layer changed: assigning layers + relayout");
4688 }
4689 computeImeTarget(true /* updateImeTarget */);
wilsonshihc32538e2018-11-07 17:27:34 +08004690 mWallpaperMayChange = true;
lumark588a3e82018-07-20 18:53:54 +08004691 // Since the window list has been rebuilt, focus might have to be recomputed since the
4692 // actual order of windows might have changed again.
4693 mService.mFocusMayChange = true;
4694
4695 pendingLayoutChanges |= changes;
4696 }
Andrii Kulian15cfb422018-11-07 13:38:49 -08004697
4698 /**
4699 * @see Display#FLAG_SHOULD_SHOW_SYSTEM_DECORATIONS
4700 */
4701 boolean supportsSystemDecorations() {
4702 // TODO(b/114338689): Read the setting from DisplaySettings.
4703 return mDisplay.supportsSystemDecorations()
4704 // TODO (b/111363427): Remove this and set the new FLAG_SHOULD_SHOW_LAUNCHER flag
4705 // (b/114338689) whenever vr 2d display id is set.
4706 || mDisplayId == mService.mVr2dDisplayId
4707 || mService.mForceDesktopModeOnExternalDisplays;
4708 }
Craig Mautner59c00972012-07-30 12:10:24 -07004709}