blob: 6286062c6c0a1f390e93e8bd1c26dd8b94cad257 [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 Ogunwale687b4272017-07-27 02:56:23 -070023import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070024import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
25import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwalea77e1462016-09-28 10:09:46 -070026import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070027import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
28import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale687b4272017-07-27 02:56:23 -070029import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
30import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070031import static android.view.Display.DEFAULT_DISPLAY;
32import static android.view.Display.FLAG_PRIVATE;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070033import static android.view.Surface.ROTATION_0;
34import static android.view.Surface.ROTATION_180;
35import static android.view.Surface.ROTATION_270;
36import static android.view.Surface.ROTATION_90;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070037import static android.view.View.GONE;
Wale Ogunwale10124582016-09-15 20:25:50 -070038import static android.view.WindowManager.DOCKED_BOTTOM;
39import static android.view.WindowManager.DOCKED_INVALID;
40import static android.view.WindowManager.DOCKED_TOP;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080041import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
42import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
43import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale494009b82016-10-21 09:01:38 -070044import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070045import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070046import static android.view.WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE;
47import static android.view.WindowManager.LayoutParams.NEEDS_MENU_UNSET;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070048import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale494009b82016-10-21 09:01:38 -070049import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070050import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale494009b82016-10-21 09:01:38 -070051import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
Robert Carrf7a7ca82017-12-06 13:17:07 -080052import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
Wale Ogunwale494009b82016-10-21 09:01:38 -070053import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070054import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070055import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
56import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Wale Ogunwale494009b82016-10-21 09:01:38 -070057import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070058import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
59import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070060import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070061import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Adrian Roose99bc052017-11-20 17:55:31 +010062import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
63import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
64import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
65import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Adrian Roos1cf585052018-01-03 18:43:27 +010066import static com.android.server.wm.utils.CoordinateTransforms.transformPhysicalToLogicalCoordinates;
Jorim Jaggif84e2f62018-01-16 14:17:59 +010067import static android.view.WindowManager.TRANSIT_KEYGUARD_UNOCCLUDE;
Wale Ogunwale1666e312016-12-16 11:27:18 -080068import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070069import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070070import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070071import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
72import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070073import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070074import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070075import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020076import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070077import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070078import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070079import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -080080import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070081import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070082import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070083import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070084import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070085import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080086import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -070087import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070088import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
89import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -070090import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070091import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -070092import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
93import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070094import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -070095import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070096import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070097import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070098import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070099import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700100import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700101import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700102import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700103import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Yi Jin6c6e9ca2018-03-20 16:53:35 -0700104import static com.android.server.wm.DisplayProto.ABOVE_APP_WINDOWS;
105import static com.android.server.wm.DisplayProto.BELOW_APP_WINDOWS;
106import static com.android.server.wm.DisplayProto.DISPLAY_FRAMES;
107import static com.android.server.wm.DisplayProto.DISPLAY_INFO;
108import static com.android.server.wm.DisplayProto.DOCKED_STACK_DIVIDER_CONTROLLER;
109import static com.android.server.wm.DisplayProto.DPI;
110import static com.android.server.wm.DisplayProto.ID;
111import static com.android.server.wm.DisplayProto.IME_WINDOWS;
112import static com.android.server.wm.DisplayProto.PINNED_STACK_CONTROLLER;
113import static com.android.server.wm.DisplayProto.ROTATION;
114import static com.android.server.wm.DisplayProto.SCREEN_ROTATION_ANIMATION;
115import static com.android.server.wm.DisplayProto.STACKS;
116import static com.android.server.wm.DisplayProto.WINDOW_CONTAINER;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700117
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700118import android.annotation.CallSuper;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700119import android.annotation.NonNull;
Dean Harding3e5a1522017-10-06 09:19:01 -0700120import android.content.pm.PackageManager;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700121import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700122import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700123import android.graphics.Bitmap;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700124import android.graphics.Matrix;
Adrian Roos1cf585052018-01-03 18:43:27 +0100125import android.graphics.Path;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800126import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700127import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700128import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100129import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700130import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700131import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700132import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700133import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700134import android.os.RemoteException;
135import android.os.SystemClock;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100136import android.os.Trace;
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800137import android.util.ArraySet;
Chong Zhang8e89b312015-09-09 15:09:30 -0700138import android.util.DisplayMetrics;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700139import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700140import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700141import android.view.Display;
Adrian Roos1cf585052018-01-03 18:43:27 +0100142import android.view.DisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700143import android.view.DisplayInfo;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700144import android.view.InputDevice;
Robert Carrb1579c82017-09-05 14:54:47 -0700145import android.view.MagnificationSpec;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700146import android.view.Surface;
147import android.view.SurfaceControl;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100148import android.view.SurfaceControl.Transaction;
Robert Carrb1579c82017-09-05 14:54:47 -0700149import android.view.SurfaceSession;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700150
Bryce Lee48f4b572017-04-10 10:54:15 -0700151import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800152import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700153import com.android.internal.view.IInputMethodClient;
Adrian Roose99bc052017-11-20 17:55:31 +0100154import com.android.server.policy.WindowManagerPolicy;
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100155import com.android.server.wm.utils.RotationCache;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100156import com.android.server.wm.utils.WmDisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700157
158import java.io.PrintWriter;
159import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700160import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700161import java.util.HashMap;
162import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700163import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700164import java.util.List;
Adrian Roos1cf585052018-01-03 18:43:27 +0100165import java.util.Objects;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800166import java.util.function.Consumer;
167import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700168
Craig Mautner59c00972012-07-30 12:10:24 -0700169/**
170 * Utility class for keeping track of the WindowStates and other pertinent contents of a
171 * particular Display.
172 *
173 * IMPORTANT: No method from this class should ever be used without holding
174 * WindowManagerService.mWindowMap.
175 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700176class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700177 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700178
179 /** Unique identifier of this stack. */
180 private final int mDisplayId;
181
Wale Ogunwale3a931692016-11-02 16:49:48 -0700182 /** The containers below are the only child containers the display can have. */
183 // Contains all window containers that are related to apps (Activities)
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100184 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers(mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700185 // Contains all non-app window containers that should be displayed above the app containers
186 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800187 private final AboveAppWindowContainers mAboveAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100188 new AboveAppWindowContainers("mAboveAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700189 // Contains all non-app window containers that should be displayed below the app containers
190 // (e.g. Wallpaper).
191 private final NonAppWindowContainers mBelowAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100192 new NonAppWindowContainers("mBelowAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700193 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
194 // 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 -0800195 // window containers together and move them in-sync if/when needed. We use a subclass of
196 // WindowContainer which is omitted from screen magnification, as the IME is never magnified.
197 private final NonMagnifiableWindowContainers mImeWindowsContainers =
198 new NonMagnifiableWindowContainers("mImeWindowsContainers", mService);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700199
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000200 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800201 private WindowState mTmpWindow2;
202 private WindowAnimator mTmpWindowAnimator;
203 private boolean mTmpRecoveringMemory;
204 private boolean mUpdateImeTarget;
205 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700206 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700207
Wale Ogunwale02319a62016-09-26 15:21:22 -0700208 // Mapping from a token IBinder to a WindowToken object on this display.
209 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
210
Andrii Kuliancd097992017-03-23 18:31:59 -0700211 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700212 int mInitialDisplayWidth = 0;
213 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700214 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700215
Adrian Roos1cf585052018-01-03 18:43:27 +0100216 DisplayCutout mInitialDisplayCutout;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100217 private final RotationCache<DisplayCutout, WmDisplayCutout> mDisplayCutoutCache
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100218 = new RotationCache<>(this::calculateDisplayCutoutForRotationUncached);
Adrian Roos1cf585052018-01-03 18:43:27 +0100219
Andrii Kuliancd097992017-03-23 18:31:59 -0700220 /**
221 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
222 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
223 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
224 */
Craig Mautner59c00972012-07-30 12:10:24 -0700225 int mBaseDisplayWidth = 0;
226 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700227 /**
228 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
229 * but can be set from Settings or via shell command "adb shell wm density".
230 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
231 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700232 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700233 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700234 private final DisplayInfo mDisplayInfo = new DisplayInfo();
235 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700236 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000237 DisplayFrames mDisplayFrames;
238
Andrii Kulian06d07d62017-03-14 11:11:47 -0700239 /**
240 * For default display it contains real metrics, empty for others.
241 * @see WindowManagerService#createWatermarkInTransaction()
242 */
243 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
244 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
245 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700246
Andrii Kulian06d07d62017-03-14 11:11:47 -0700247 /**
248 * Compat metrics computed based on {@link #mDisplayMetrics}.
249 * @see #updateDisplayAndOrientation(int)
250 */
251 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
252
253 /** The desired scaling factor for compatible apps. */
254 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700255
Andrii Kulian8ee72852017-03-10 10:36:45 -0800256 /**
257 * Current rotation of the display.
258 * Constants as per {@link android.view.Surface.Rotation}.
259 *
Robert Carrae606b42018-02-15 15:36:23 -0800260 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800261 */
262 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700263
Andrii Kulian8ee72852017-03-10 10:36:45 -0800264 /**
265 * Last applied orientation of the display.
266 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
267 *
268 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
269 */
270 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700271
Andrii Kulian8ee72852017-03-10 10:36:45 -0800272 /**
273 * Flag indicating that the application is receiving an orientation that has different metrics
274 * than it expected. E.g. Portrait instead of Landscape.
275 *
Robert Carrae606b42018-02-15 15:36:23 -0800276 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800277 */
278 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700279
Andrii Kulian8ee72852017-03-10 10:36:45 -0800280 /**
281 * Orientation forced by some window. If there is no visible window that specifies orientation
282 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
283 *
284 * @see NonAppWindowContainers#getOrientation()
285 */
286 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700287
Andrii Kulian8ee72852017-03-10 10:36:45 -0800288 /**
289 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
290 * occluded.
291 *
292 * @see NonAppWindowContainers#getOrientation()
293 */
294 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
295
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700296 /**
297 * Keep track of wallpaper visibility to notify changes.
298 */
299 private boolean mLastWallpaperVisible = false;
300
Andrii Kulian06d07d62017-03-14 11:11:47 -0700301 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700302
Craig Mautner39834192012-09-02 07:47:24 -0700303 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700304 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700305 int pendingLayoutChanges;
Riddle Hsu654a6f92018-07-13 22:59:36 +0800306 int mDeferredRotationPauseCount;
Andrii Kulian839def92016-11-02 10:58:58 -0700307 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800308 boolean isDefaultDisplay;
Andrii Kulianf0379de2018-03-14 16:24:07 -0700309 /**
310 * Flag indicating whether WindowManager should override info for this display in
311 * DisplayManager.
312 */
313 boolean mShouldOverrideDisplayConfiguration = true;
Craig Mautner39834192012-09-02 07:47:24 -0700314
Craig Mautnerdc548482014-02-05 13:35:24 -0800315 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700316 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800317
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700318 /** Detect user tapping outside of current focused task bounds .*/
319 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700320
Craig Mautner6601b7b2013-04-29 10:29:11 -0700321 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700322 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700323
Craig Mautner6601b7b2013-04-29 10:29:11 -0700324 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800325 private final Rect mTmpRect = new Rect();
326 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700327 private final RectF mTmpRectF = new RectF();
328 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800329 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700330
Bryce Leef3c6a472017-11-14 14:53:06 -0800331 /** Used for handing back size of display */
332 private final Rect mTmpBounds = new Rect();
333
Craig Mautner95da1082014-02-24 17:54:35 -0800334 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700335 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800336
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700337 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700338 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700339
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800340 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800341 /** A collection of windows that provide tap exclude regions inside of them. */
342 final ArraySet<WindowState> mTapExcludeProvidingWindows = new ArraySet<>();
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800343
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000344 private boolean mHaveBootMsg = false;
345 private boolean mHaveApp = false;
346 private boolean mHaveWallpaper = false;
347 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700348
349 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
350
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700351 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
352 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000353 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
354 new ApplySurfaceChangesTransactionState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700355
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700356 // True if this display is in the process of being removed. Used to determine if the removal of
357 // the display's direct children should be allowed.
358 private boolean mRemovingDisplay = false;
359
Bryce Leed1871262017-06-12 14:12:29 -0700360 // {@code false} if this display is in the processing of being created.
361 private boolean mDisplayReady = false;
362
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800363 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700364
Robert Carrb1579c82017-09-05 14:54:47 -0700365 private final SurfaceSession mSession = new SurfaceSession();
366
367 /**
368 * We organize all top-level Surfaces in to the following layers.
369 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800370 * and omitted from Screen-Magnification, for example the strict mode flash or
371 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700372 * {@link #mWindowingLayer} contains everything else.
373 */
374 private SurfaceControl mOverlayLayer;
375
376 /**
377 * See {@link #mOverlayLayer}
378 */
379 private SurfaceControl mWindowingLayer;
380
381 /**
382 * Specifies the size of the surfaces in {@link #mOverlayLayer} and {@link #mWindowingLayer}.
383 * <p>
384 * For these surfaces currently we use a surface based on the larger of width or height so we
385 * don't have to resize when rotating the display.
386 */
387 private int mSurfaceSize;
388
Adrian Roos5251b1d2018-03-23 18:57:43 +0100389 /**
390 * Sequence number for the current layout pass.
391 */
392 int mLayoutSeq = 0;
393
Chavi Weingarten3a748552018-05-14 17:32:42 +0000394 /**
395 * Specifies the count to determine whether to defer updating the IME target until ready.
396 */
397 private int mDeferUpdateImeTargetCount;
398
Jorim Jaggi9af095b2017-12-12 17:18:57 +0100399 /** Temporary float array to retrieve 3x3 matrix values. */
400 private final float[] mTmpFloats = new float[9];
401
Robert Carr24be9ab2018-04-30 17:54:53 -0700402 private MagnificationSpec mMagnificationSpec;
403
Arthur Hung95b38a92018-07-20 18:56:12 +0800404 private InputMonitor mInputMonitor;
405
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800406 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
407 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800408 final AppWindowToken atoken = w.mAppToken;
409 if (winAnimator.mDrawState == READY_TO_SHOW) {
Jorim Jaggiaf0d6d22018-06-08 15:25:35 +0200410 if (atoken == null || atoken.canShowWindows()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800411 if (w.performShowLocked()) {
412 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
413 if (DEBUG_LAYOUT_REPEATS) {
414 mService.mWindowPlacerLocked.debugLayoutRepeats(
415 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
416 }
417 }
418 }
419 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800420 };
421
422 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
423 final WindowStateAnimator winAnimator = w.mWinAnimator;
424 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
425 return;
426 }
427
428 final int flags = w.mAttrs.flags;
429
430 // If this window is animating, make a note that we have an animating window and take
431 // care of a request to run a detached wallpaper animation.
Jorim Jaggia5e10572017-11-15 14:36:26 +0100432 if (winAnimator.isAnimationSet()) {
433 final AnimationAdapter anim = w.getAnimation();
434 if (anim != null) {
435 if ((flags & FLAG_SHOW_WALLPAPER) != 0 && anim.getDetachWallpaper()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800436 mTmpWindow = w;
437 }
Jorim Jaggia5e10572017-11-15 14:36:26 +0100438 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800439 if (color != 0) {
440 final TaskStack stack = w.getStack();
441 if (stack != null) {
442 stack.setAnimationBackground(winAnimator, color);
443 }
444 }
445 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800446 }
447
448 // If this window's app token is running a detached wallpaper animation, make a note so
449 // we can ensure the wallpaper is displayed behind it.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200450 final AppWindowToken atoken = winAnimator.mWin.mAppToken;
451 final AnimationAdapter animation = atoken != null ? atoken.getAnimation() : null;
452 if (animation != null) {
453 if ((flags & FLAG_SHOW_WALLPAPER) != 0 && animation.getDetachWallpaper()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800454 mTmpWindow = w;
455 }
456
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200457 final int color = animation.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800458 if (color != 0) {
459 final TaskStack stack = w.getStack();
460 if (stack != null) {
461 stack.setAnimationBackground(winAnimator, color);
462 }
463 }
464 }
465 };
466
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800467 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
468 final int lostFocusUid = mTmpWindow.mOwnerUid;
469 final Handler handler = mService.mH;
470 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
471 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
472 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
473 w.mAttrs.hideTimeoutMilliseconds);
474 }
475 }
476 };
477
478 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
479 final AppWindowToken focusedApp = mService.mFocusedApp;
480 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
481 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
482
483 if (!w.canReceiveKeys()) {
484 return false;
485 }
486
487 final AppWindowToken wtoken = w.mAppToken;
488
489 // If this window's application has been removed, just skip it.
490 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
491 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
492 + (wtoken.removed ? "removed" : "sendingToBottom"));
493 return false;
494 }
495
496 if (focusedApp == null) {
497 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
498 + " using new focus @ " + w);
499 mTmpWindow = w;
500 return true;
501 }
502
503 if (!focusedApp.windowsAreFocusable()) {
504 // Current focused app windows aren't focusable...
505 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
506 + " focusable using new focus @ " + w);
507 mTmpWindow = w;
508 return true;
509 }
510
511 // Descend through all of the app tokens and find the first that either matches
512 // win.mAppToken (return win) or mFocusedApp (return null).
513 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
514 if (focusedApp.compareTo(wtoken) > 0) {
515 // App stack below focused app stack. No focus for you!!!
516 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
517 "findFocusedWindow: Reached focused app=" + focusedApp);
518 mTmpWindow = null;
519 return true;
520 }
521 }
522
523 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
524 mTmpWindow = w;
525 return true;
526 };
527
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800528 private final Consumer<WindowState> mPerformLayout = w -> {
529 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
530 // wasting time and funky changes while a window is animating away.
531 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
532 || w.isGoneForLayoutLw();
533
534 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
535 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
536 + " mLayoutAttached=" + w.mLayoutAttached
537 + " screen changed=" + w.isConfigChanged());
538 final AppWindowToken atoken = w.mAppToken;
539 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200540 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800541 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
542 + " parentHidden=" + w.isParentWindowHidden());
543 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200544 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800545 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
546 + " parentHidden=" + w.isParentWindowHidden());
547 }
548
549 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
550 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
551 // since that means "perform layout as normal, just don't display").
552 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
553 || ((w.isConfigChanged() || w.setReportResizeHints())
554 && !w.isGoneForLayoutLw() &&
555 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
556 (w.mHasSurface && w.mAppToken != null &&
557 w.mAppToken.layoutConfigChanges)))) {
558 if (!w.mLayoutAttached) {
559 if (mTmpInitial) {
560 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
561 w.mContentChanged = false;
562 }
563 if (w.mAttrs.type == TYPE_DREAM) {
564 // Don't layout windows behind a dream, so that if it does stuff like hide
565 // the status bar we won't get a bad transition when it goes away.
566 mTmpWindow = w;
567 }
568 w.mLayoutNeeded = false;
569 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200570 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000571 mService.mPolicy.layoutWindowLw(w, null, mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100572 w.mLayoutSeq = mLayoutSeq;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800573
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200574 // If this is the first layout, we need to initialize the last inset values as
575 // otherwise we'd immediately cause an unnecessary resize.
576 if (firstLayout) {
577 w.updateLastInsetValues();
578 }
579
Adrian Roos23df3a32018-03-15 15:41:13 +0100580 if (w.mAppToken != null) {
581 w.mAppToken.layoutLetterbox(w);
582 }
583
chaviw492139a2018-07-16 16:07:35 -0700584 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700585 + " mContainingFrame=" + w.getContainingFrame()
586 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800587 }
588 }
589 };
590
591 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
592 if (w.mLayoutAttached) {
593 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
594 + " mViewVisibility=" + w.mViewVisibility
595 + " mRelayoutCalled=" + w.mRelayoutCalled);
596 // If this view is GONE, then skip it -- keep the current frame, and let the caller
597 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
598 // windows, since that means "perform layout as normal, just don't display").
599 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
600 return;
601 }
602 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
603 || w.mLayoutNeeded) {
604 if (mTmpInitial) {
605 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
606 w.mContentChanged = false;
607 }
608 w.mLayoutNeeded = false;
609 w.prelayout();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000610 mService.mPolicy.layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100611 w.mLayoutSeq = mLayoutSeq;
chaviw492139a2018-07-16 16:07:35 -0700612 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700613 + " mContainingFrame=" + w.getContainingFrame()
614 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800615 }
616 } else if (w.mAttrs.type == TYPE_DREAM) {
617 // Don't layout windows behind a dream, so that if it does stuff like hide the
618 // status bar we won't get a bad transition when it goes away.
619 mTmpWindow = mTmpWindow2;
620 }
621 };
622
623 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
624 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
625 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
626 return w.canBeImeTarget();
627 };
628
629 private final Consumer<WindowState> mApplyPostLayoutPolicy =
630 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
631 mService.mInputMethodTarget);
632
633 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
634 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
635 final boolean obscuredChanged = w.mObscured !=
636 mTmpApplySurfaceChangesTransactionState.obscured;
637 final RootWindowContainer root = mService.mRoot;
638 // Only used if default window
639 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
640
641 // Update effect.
642 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
643 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
644 final boolean isDisplayed = w.isDisplayedLw();
645
646 if (isDisplayed && w.isObscuringDisplay()) {
647 // This window completely covers everything behind it, so we want to leave all
648 // of them as undimmed (for performance reasons).
649 root.mObscuringWindow = w;
650 mTmpApplySurfaceChangesTransactionState.obscured = true;
651 }
652
653 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
654 root.handleNotObscuredLocked(w,
655 mTmpApplySurfaceChangesTransactionState.obscured,
656 mTmpApplySurfaceChangesTransactionState.syswin);
657
658 if (w.mHasSurface && isDisplayed) {
659 final int type = w.mAttrs.type;
660 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
661 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
662 mTmpApplySurfaceChangesTransactionState.syswin = true;
663 }
664 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
665 && w.mAttrs.preferredRefreshRate != 0) {
666 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
667 = w.mAttrs.preferredRefreshRate;
668 }
669 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
670 && w.mAttrs.preferredDisplayModeId != 0) {
671 mTmpApplySurfaceChangesTransactionState.preferredModeId
672 = w.mAttrs.preferredDisplayModeId;
673 }
674 }
675 }
676
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800677 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
678 && mWallpaperController.isWallpaperTarget(w)) {
679 // This is the wallpaper target and its obscured state changed... make sure the
680 // current wallpaper's visibility has been updated accordingly.
681 mWallpaperController.updateWallpaperVisibility();
682 }
683
chaviw161ea3e2018-01-31 12:01:12 -0800684 w.handleWindowMovedIfNeeded();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800685
686 final WindowStateAnimator winAnimator = w.mWinAnimator;
687
688 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
689 w.mContentChanged = false;
690
691 // Moved from updateWindowsAndWallpaperLocked().
692 if (w.mHasSurface) {
693 // Take care of the window being ready to display.
694 final boolean committed = winAnimator.commitFinishDrawingLocked();
695 if (isDefaultDisplay && committed) {
696 if (w.mAttrs.type == TYPE_DREAM) {
697 // HACK: When a dream is shown, it may at that point hide the lock screen.
698 // So we need to redo the layout to let the phone window manager make this
699 // happen.
700 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
701 if (DEBUG_LAYOUT_REPEATS) {
702 surfacePlacer.debugLayoutRepeats(
703 "dream and commitFinishDrawingLocked true",
704 pendingLayoutChanges);
705 }
706 }
707 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
708 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
709 "First draw done in potential wallpaper target " + w);
710 root.mWallpaperMayChange = true;
711 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
712 if (DEBUG_LAYOUT_REPEATS) {
713 surfacePlacer.debugLayoutRepeats(
714 "wallpaper and commitFinishDrawingLocked true",
715 pendingLayoutChanges);
716 }
717 }
718 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800719 }
720
721 final AppWindowToken atoken = w.mAppToken;
722 if (atoken != null) {
Adrian Roos23df3a32018-03-15 15:41:13 +0100723 atoken.updateLetterboxSurface(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800724 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
725 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
726 mTmpUpdateAllDrawn.add(atoken);
727 }
728 }
729
730 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
731 && w.isDisplayedLw()) {
732 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
733 }
734
735 w.updateResizingWindowIfNeeded();
736 };
737
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800738 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800739 * Create new {@link DisplayContent} instance, add itself to the root window container and
740 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700741 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800742 * @param service You know.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700743 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
744 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700745 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700746 DisplayContent(Display display, WindowManagerService service,
Bryce Leef19cbe22018-02-02 15:09:21 -0800747 WallpaperController wallpaperController, DisplayWindowController controller) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100748 super(service);
Bryce Leef19cbe22018-02-02 15:09:21 -0800749 setController(controller);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800750 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
751 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
752 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
753 + " new=" + display);
754 }
755
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700756 mDisplay = display;
757 mDisplayId = display.getDisplayId();
Wale Ogunwale0303c572016-10-20 10:16:29 -0700758 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700759 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700760 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700761 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100762 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo,
763 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700764 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800765 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700766 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700767
Robert Carr74a66a22018-02-23 12:17:51 -0800768 // We use this as our arbitrary surface size for buffer-less parents
769 // that don't impose cropping on their children. It may need to be larger
770 // than the display size because fullscreen windows can be shifted offscreen
771 // due to surfaceInsets. 2 times the largest display dimension feels like an
772 // appropriately arbitrary number. Eventually we would like to give SurfaceFlinger
773 // layers the ability to match their parent sizes and be able to skip
774 // such arbitrary size settings.
775 mSurfaceSize = Math.max(mBaseDisplayHeight, mBaseDisplayWidth) * 2;
Robert Carrb1579c82017-09-05 14:54:47 -0700776
777 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession)
778 .setSize(mSurfaceSize, mSurfaceSize)
779 .setOpaque(true);
780 mWindowingLayer = b.setName("Display Root").build();
781 mOverlayLayer = b.setName("Display Overlays").build();
782
Robert Carrf59b8dd2017-10-02 18:58:36 -0700783 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700784 .setLayerStack(mWindowingLayer, mDisplayId)
785 .show(mWindowingLayer)
786 .setLayer(mOverlayLayer, 1)
787 .setLayerStack(mOverlayLayer, mDisplayId)
788 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700789 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700790
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700791 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700792 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700793 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700794 super.addChild(mAboveAppWindowsContainers, null);
795 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800796
797 // Add itself as a child to the root container.
798 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700799
800 // TODO(b/62541591): evaluate whether this is the best spot to declare the
801 // {@link DisplayContent} ready for use.
802 mDisplayReady = true;
Arthur Hung95b38a92018-07-20 18:56:12 +0800803
Garfield Tan3b388802018-07-31 15:24:35 -0700804 // TODO(b/112081256): Use independent InputMonitor.
805 mInputMonitor = isDefaultDisplay ? new InputMonitor(service, mDisplayId)
806 : mService.getDefaultDisplayContentLocked().mInputMonitor;
Bryce Leed1871262017-06-12 14:12:29 -0700807 }
808
809 boolean isReady() {
810 // The display is ready when the system and the individual display are both ready.
811 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700812 }
813
814 int getDisplayId() {
815 return mDisplayId;
816 }
817
Wale Ogunwale02319a62016-09-26 15:21:22 -0700818 WindowToken getWindowToken(IBinder binder) {
819 return mTokenMap.get(binder);
820 }
821
822 AppWindowToken getAppWindowToken(IBinder binder) {
823 final WindowToken token = getWindowToken(binder);
824 if (token == null) {
825 return null;
826 }
827 return token.asAppWindowToken();
828 }
829
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700830 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700831 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
832 if (dc != null) {
833 // We currently don't support adding a window token to the display if the display
834 // already has the binder mapped to another token. If there is a use case for supporting
835 // this moving forward we will either need to merge the WindowTokens some how or have
836 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700837 throw new IllegalArgumentException("Can't map token=" + token + " to display="
838 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700839 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700840 if (binder == null) {
841 throw new IllegalArgumentException("Can't map token=" + token + " to display="
842 + getName() + " binder is null");
843 }
844 if (token == null) {
845 throw new IllegalArgumentException("Can't map null token to display="
846 + getName() + " binder=" + binder);
847 }
848
Wale Ogunwale02319a62016-09-26 15:21:22 -0700849 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700850
851 if (token.asAppWindowToken() == null) {
852 // Add non-app token to container hierarchy on the display. App tokens are added through
853 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700854 switch (token.windowType) {
855 case TYPE_WALLPAPER:
856 mBelowAppWindowsContainers.addChild(token);
857 break;
858 case TYPE_INPUT_METHOD:
859 case TYPE_INPUT_METHOD_DIALOG:
860 mImeWindowsContainers.addChild(token);
861 break;
862 default:
863 mAboveAppWindowsContainers.addChild(token);
864 break;
865 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700866 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700867 }
868
869 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700870 final WindowToken token = mTokenMap.remove(binder);
871 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800872 token.setExiting();
873 }
874 return token;
875 }
876
877 /** Changes the display the input window token is housed on to this one. */
878 void reParentWindowToken(WindowToken token) {
879 final DisplayContent prevDc = token.getDisplayContent();
880 if (prevDc == this) {
881 return;
882 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800883 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
884 && token.asAppWindowToken() == null) {
885 // Removed the token from the map, but made sure it's not an app token before removing
886 // from parent.
887 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700888 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800889
890 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700891 }
892
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700893 void removeAppToken(IBinder binder) {
894 final WindowToken token = removeWindowToken(binder);
895 if (token == null) {
896 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
897 return;
898 }
899
900 final AppWindowToken appToken = token.asAppWindowToken();
901
902 if (appToken == null) {
903 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
904 return;
905 }
906
907 appToken.onRemovedFromDisplay();
908 }
909
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700910 Display getDisplay() {
911 return mDisplay;
912 }
913
Craig Mautner59c00972012-07-30 12:10:24 -0700914 DisplayInfo getDisplayInfo() {
915 return mDisplayInfo;
916 }
917
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700918 DisplayMetrics getDisplayMetrics() {
919 return mDisplayMetrics;
920 }
921
Andrii Kulian8ee72852017-03-10 10:36:45 -0800922 int getRotation() {
923 return mRotation;
924 }
925
Andrii Kulianef5ce1c2018-03-20 19:27:24 -0700926 @VisibleForTesting
Andrii Kulian8ee72852017-03-10 10:36:45 -0800927 void setRotation(int newRotation) {
928 mRotation = newRotation;
929 }
930
931 int getLastOrientation() {
932 return mLastOrientation;
933 }
934
935 void setLastOrientation(int orientation) {
936 mLastOrientation = orientation;
937 }
938
939 boolean getAltOrientation() {
940 return mAltOrientation;
941 }
942
943 void setAltOrientation(boolean altOrientation) {
944 mAltOrientation = altOrientation;
945 }
946
947 int getLastWindowForcedOrientation() {
948 return mLastWindowForcedOrientation;
949 }
950
Andrii Kulian06d07d62017-03-14 11:11:47 -0700951 /**
Riddle Hsu654a6f92018-07-13 22:59:36 +0800952 * Temporarily pauses rotation changes until resumed.
953 *
954 * This can be used to prevent rotation changes from occurring while the user is
955 * performing certain operations, such as drag and drop.
956 *
957 * This call nests and must be matched by an equal number of calls to
958 * {@link #resumeRotationLocked}.
959 */
960 void pauseRotationLocked() {
961 mDeferredRotationPauseCount++;
962 }
963
964 /**
965 * Resumes normal rotation changes after being paused.
966 */
967 void resumeRotationLocked() {
968 if (mDeferredRotationPauseCount <= 0) {
969 return;
970 }
971
972 mDeferredRotationPauseCount--;
973 if (mDeferredRotationPauseCount == 0) {
974 final boolean changed = updateRotationUnchecked();
975 if (changed) {
976 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
977 }
978 }
979 }
980
981 /**
Andrii Kulian06d07d62017-03-14 11:11:47 -0700982 * Update rotation of the display.
983 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -0700984 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
985 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
Andrii Kulian06d07d62017-03-14 11:11:47 -0700986 */
Robert Carrae606b42018-02-15 15:36:23 -0800987 boolean updateRotationUnchecked() {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -0700988 return updateRotationUnchecked(false /* forceUpdate */);
989 }
Andrii Kulian06d07d62017-03-14 11:11:47 -0700990
Andrii Kulian6cdcfe42018-05-23 17:59:43 -0700991 /**
992 * Update rotation of the display with an option to force the update.
993 * @param forceUpdate Force the rotation update. Sometimes in WM we might skip updating
994 * orientation because we're waiting for some rotation to finish or display
995 * to unfreeze, which results in configuration of the previously visible
996 * activity being applied to a newly visible one. Forcing the rotation
997 * update allows to workaround this issue.
998 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
999 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
1000 */
1001 boolean updateRotationUnchecked(boolean forceUpdate) {
1002 ScreenRotationAnimation screenRotationAnimation;
1003 if (!forceUpdate) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001004 if (mDeferredRotationPauseCount > 0) {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001005 // Rotation updates have been paused temporarily. Defer the update until
1006 // updates have been resumed.
1007 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
1008 return false;
1009 }
1010
1011 screenRotationAnimation =
1012 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
1013 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
1014 // Rotation updates cannot be performed while the previous rotation change
1015 // animation is still in progress. Skip this update. We will try updating
1016 // again after the animation is finished and the display is unfrozen.
1017 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
1018 return false;
1019 }
1020 if (mService.mDisplayFrozen) {
1021 // Even if the screen rotation animation has finished (e.g. isAnimating
1022 // returns false), there is still some time where we haven't yet unfrozen
1023 // the display. We also need to abort rotation here.
1024 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
1025 "Deferring rotation, still finishing previous rotation");
1026 return false;
1027 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001028 }
1029
1030 if (!mService.mDisplayEnabled) {
1031 // No point choosing a rotation if the display is not enabled.
1032 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
1033 return false;
1034 }
1035
1036 final int oldRotation = mRotation;
1037 final int lastOrientation = mLastOrientation;
1038 final boolean oldAltOrientation = mAltOrientation;
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001039 final int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation,
1040 isDefaultDisplay);
1041 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Computed rotation=" + rotation + " for display id="
1042 + mDisplayId + " based on lastOrientation=" + lastOrientation
1043 + " and oldRotation=" + oldRotation);
Robert Carr0e007272017-10-02 13:00:55 -07001044 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
Robert Carr897215d2017-03-29 12:25:50 -07001045 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001046
Robert Carr0e007272017-10-02 13:00:55 -07001047 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001048 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001049 if (seamlessRotated != null && !forceUpdate) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001050 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
1051 // to complete (that is, waiting for windows to redraw). It's tempting to check
1052 // w.mSeamlessRotationCount but that could be incorrect in the case of
1053 // window-removal.
1054 return false;
1055 }
Robert Carr0e007272017-10-02 13:00:55 -07001056
1057 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001058 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -07001059 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -07001060 mayRotateSeamlessly = false;
1061 }
1062 for (int i = 0; i < mService.mSessions.size(); i++) {
1063 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
1064 mayRotateSeamlessly = false;
1065 break;
1066 }
1067 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001068 }
Robert Carr0e007272017-10-02 13:00:55 -07001069 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001070
1071 // TODO: Implement forced rotation changes.
1072 // Set mAltOrientation to indicate that the application is receiving
1073 // an orientation that has different metrics than it expected.
1074 // eg. Portrait instead of Landscape.
1075
1076 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
1077 lastOrientation, rotation);
1078
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001079 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1080 + " selected orientation " + lastOrientation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001081 + ", got rotation " + rotation + " which has "
1082 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1083
1084 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1085 // No change.
1086 return false;
1087 }
1088
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001089 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1090 + " rotation changed to " + rotation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001091 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1092 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1093
1094 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1095 mService.mWaitingForConfig = true;
1096 }
1097
1098 mRotation = rotation;
1099 mAltOrientation = altOrientation;
1100 if (isDefaultDisplay) {
1101 mService.mPolicy.setRotationLw(rotation);
1102 }
1103
1104 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
Riddle Hsu654a6f92018-07-13 22:59:36 +08001105 mService.mH.sendNewMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1106 this, WINDOW_FREEZE_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001107
1108 setLayoutNeeded();
1109 final int[] anim = new int[2];
Robert Carrf59b8dd2017-10-02 18:58:36 -07001110 mService.mPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001111
1112 if (!rotateSeamlessly) {
Robert Carrae606b42018-02-15 15:36:23 -08001113 mService.startFreezingDisplayLocked(anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001114 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1115 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1116 mDisplayId);
1117 } else {
1118 // The screen rotation animation uses a screenshot to freeze the screen
1119 // while windows resize underneath.
1120 // When we are rotating seamlessly, we allow the elements to transition
1121 // to their rotated state independently and without a freeze required.
1122 screenRotationAnimation = null;
1123
Robert Carrefc75702018-02-16 11:46:16 -08001124 mService.startSeamlessRotation();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001125 }
1126
1127 // We need to update our screen size information to match the new rotation. If the rotation
1128 // has actually changed then this method will return true and, according to the comment at
1129 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1130 // By updating the Display info here it will be available to
1131 // #computeScreenConfiguration() later.
1132 updateDisplayAndOrientation(getConfiguration().uiMode);
1133
Robert Carrae606b42018-02-15 15:36:23 -08001134 // NOTE: We disable the rotation in the emulator because
1135 // it doesn't support hardware OpenGL emulation yet.
1136 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1137 && screenRotationAnimation.hasScreenshot()) {
1138 if (screenRotationAnimation.setRotation(getPendingTransaction(), rotation,
1139 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1140 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1141 mService.scheduleAnimationLocked();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001142 }
1143 }
1144
Vishnu Nair83537a72018-07-19 21:27:48 -07001145 forAllWindows(w -> {
1146 w.forceSeamlesslyRotateIfAllowed(oldRotation, rotation);
1147 }, true /* traverseTopToBottom */);
1148
1149 // TODO(b/111504081): Consolidate seamless rotation logic.
Robert Carrae606b42018-02-15 15:36:23 -08001150 if (rotateSeamlessly) {
Adrian Roos0e7b70a2018-06-07 15:29:34 +02001151 seamlesslyRotate(getPendingTransaction(), oldRotation, rotation);
Robert Carrae606b42018-02-15 15:36:23 -08001152 }
1153
1154 mService.mDisplayManagerInternal.performTraversal(getPendingTransaction());
1155 scheduleAnimation();
1156
Andrii Kulian06d07d62017-03-14 11:11:47 -07001157 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001158 if (w.mHasSurface && !rotateSeamlessly) {
1159 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001160 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001161 mService.mRoot.mOrientationChangeComplete = false;
1162 w.mLastFreezeDuration = 0;
1163 }
1164 w.mReportOrientationChanged = true;
1165 }, true /* traverseTopToBottom */);
1166
1167 if (rotateSeamlessly) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001168 mService.mH.sendNewMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1169 this, SEAMLESS_ROTATION_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001170 }
1171
1172 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1173 final WindowManagerService.RotationWatcher rotationWatcher
1174 = mService.mRotationWatchers.get(i);
1175 if (rotationWatcher.mDisplayId == mDisplayId) {
1176 try {
1177 rotationWatcher.mWatcher.onRotationChanged(rotation);
1178 } catch (RemoteException e) {
1179 // Ignore
1180 }
1181 }
1182 }
1183
1184 // TODO (multi-display): Magnification is supported only for the default display.
1185 // Announce rotation only if we will not animate as we already have the
1186 // windows in final state. Otherwise, we make this call at the rotation end.
1187 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1188 && isDefaultDisplay) {
1189 mService.mAccessibilityController.onRotationChangedLocked(this);
1190 }
1191
1192 return true;
1193 }
1194
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001195 void configureDisplayPolicy() {
1196 mService.mPolicy.setInitialDisplaySize(getDisplay(),
1197 mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1198
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001199 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
1200 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001201 }
1202
Andrii Kulian06d07d62017-03-14 11:11:47 -07001203 /**
1204 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1205 * changed.
1206 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1207 */
1208 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1209 // Use the effective "visual" dimensions based on current rotation
1210 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1211 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1212 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1213 int dw = realdw;
1214 int dh = realdh;
1215
1216 if (mAltOrientation) {
1217 if (realdw > realdh) {
1218 // Turn landscape into portrait.
1219 int maxw = (int)(realdh/1.3f);
1220 if (maxw < realdw) {
1221 dw = maxw;
1222 }
1223 } else {
1224 // Turn portrait into landscape.
1225 int maxh = (int)(realdw/1.3f);
1226 if (maxh < realdh) {
1227 dh = maxh;
1228 }
1229 }
1230 }
1231
1232 // Update application display metrics.
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001233 final WmDisplayCutout wmDisplayCutout = calculateDisplayCutoutForRotation(mRotation);
1234 final DisplayCutout displayCutout = wmDisplayCutout.getDisplayCutout();
1235
Andrii Kulian06d07d62017-03-14 11:11:47 -07001236 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001237 mDisplayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001238 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001239 mDisplayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001240 mDisplayInfo.rotation = mRotation;
1241 mDisplayInfo.logicalWidth = dw;
1242 mDisplayInfo.logicalHeight = dh;
1243 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1244 mDisplayInfo.appWidth = appWidth;
1245 mDisplayInfo.appHeight = appHeight;
1246 if (isDefaultDisplay) {
1247 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1248 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1249 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001250 mDisplayInfo.displayCutout = displayCutout.isEmpty() ? null : displayCutout;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001251 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1252 if (mDisplayScalingDisabled) {
1253 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1254 } else {
1255 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1256 }
1257
Andrii Kulianf0379de2018-03-14 16:24:07 -07001258 // We usually set the override info in DisplayManager so that we get consistent display
1259 // metrics values when displays are changing and don't send out new values until WM is aware
1260 // of them. However, we don't do this for displays that serve as containers for ActivityView
1261 // because we don't want letter-/pillar-boxing during resize.
1262 final DisplayInfo overrideDisplayInfo = mShouldOverrideDisplayConfiguration
1263 ? mDisplayInfo : null;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001264 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
Andrii Kulianf0379de2018-03-14 16:24:07 -07001265 overrideDisplayInfo);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001266
1267 mBaseDisplayRect.set(0, 0, dw, dh);
1268
1269 if (isDefaultDisplay) {
1270 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1271 mCompatDisplayMetrics);
1272 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001273
1274 updateBounds();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001275 return mDisplayInfo;
1276 }
1277
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001278 WmDisplayCutout calculateDisplayCutoutForRotation(int rotation) {
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001279 return mDisplayCutoutCache.getOrCompute(mInitialDisplayCutout, rotation);
1280 }
1281
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001282 private WmDisplayCutout calculateDisplayCutoutForRotationUncached(
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001283 DisplayCutout cutout, int rotation) {
Adrian Roos24264212018-02-19 16:26:15 +01001284 if (cutout == null || cutout == DisplayCutout.NO_CUTOUT) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001285 return WmDisplayCutout.NO_CUTOUT;
Adrian Roos1cf585052018-01-03 18:43:27 +01001286 }
Adrian Roos11c25582018-02-19 18:06:36 +01001287 if (rotation == ROTATION_0) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001288 return WmDisplayCutout.computeSafeInsets(
1289 cutout, mInitialDisplayWidth, mInitialDisplayHeight);
Adrian Roos24264212018-02-19 16:26:15 +01001290 }
Adrian Roosbed538e2018-02-21 17:50:07 +01001291 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Jorim Jaggi60640512018-06-29 01:14:31 +02001292 final List<Rect> bounds = WmDisplayCutout.computeSafeInsets(
1293 cutout, mInitialDisplayWidth, mInitialDisplayHeight)
1294 .getDisplayCutout().getBoundingRects();
Adrian Roos11c25582018-02-19 18:06:36 +01001295 transformPhysicalToLogicalCoordinates(rotation, mInitialDisplayWidth, mInitialDisplayHeight,
1296 mTmpMatrix);
Jorim Jaggi60640512018-06-29 01:14:31 +02001297 final Region region = Region.obtain();
1298 for (int i = 0; i < bounds.size(); i++) {
1299 final Rect rect = bounds.get(i);
1300 final RectF rectF = new RectF(bounds.get(i));
1301 mTmpMatrix.mapRect(rectF);
1302 rectF.round(rect);
1303 region.op(rect, Op.UNION);
1304 }
1305
1306 return WmDisplayCutout.computeSafeInsets(DisplayCutout.fromBounds(region),
Adrian Roos24264212018-02-19 16:26:15 +01001307 rotated ? mInitialDisplayHeight : mInitialDisplayWidth,
1308 rotated ? mInitialDisplayWidth : mInitialDisplayHeight);
Adrian Roos1cf585052018-01-03 18:43:27 +01001309 }
1310
Andrii Kulian06d07d62017-03-14 11:11:47 -07001311 /**
1312 * Compute display configuration based on display properties and policy settings.
1313 * Do not call if mDisplayReady == false.
1314 */
1315 void computeScreenConfiguration(Configuration config) {
1316 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1317
1318 final int dw = displayInfo.logicalWidth;
1319 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001320 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
1321 // TODO: Probably best to set this based on some setting in the display content object,
1322 // so the display can be configured for things like fullscreen.
1323 config.windowConfiguration.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001324
Adrian Roos11c25582018-02-19 18:06:36 +01001325 final float density = mDisplayMetrics.density;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001326 config.screenWidthDp =
1327 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001328 config.uiMode, mDisplayId, displayInfo.displayCutout) / density);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001329 config.screenHeightDp =
1330 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001331 config.uiMode, mDisplayId, displayInfo.displayCutout) / density);
Bryce Lee7566d762017-03-30 09:34:15 -07001332
Adrian Roos11c25582018-02-19 18:06:36 +01001333 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh,
1334 displayInfo.displayCutout, mTmpRect);
Bryce Lee7566d762017-03-30 09:34:15 -07001335 final int leftInset = mTmpRect.left;
1336 final int topInset = mTmpRect.top;
1337 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001338 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1339 leftInset + displayInfo.appWidth /* right */,
1340 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001341 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1342 || displayInfo.rotation == Surface.ROTATION_270);
1343
1344 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
Adrian Roos11c25582018-02-19 18:06:36 +01001345 density, config);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001346
1347 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1348 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1349 ? Configuration.SCREENLAYOUT_ROUND_YES
1350 : Configuration.SCREENLAYOUT_ROUND_NO);
1351
1352 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1353 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1354 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1355 dh, mDisplayId);
1356 config.densityDpi = displayInfo.logicalDensityDpi;
1357
1358 config.colorMode =
1359 (displayInfo.isHdr()
1360 ? Configuration.COLOR_MODE_HDR_YES
1361 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001362 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001363 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1364 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1365
1366 // Update the configuration based on available input devices, lid switch,
1367 // and platform configuration.
1368 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1369 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1370 config.navigation = Configuration.NAVIGATION_NONAV;
1371
1372 int keyboardPresence = 0;
1373 int navigationPresence = 0;
1374 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1375 final int len = devices != null ? devices.length : 0;
1376 for (int i = 0; i < len; i++) {
1377 InputDevice device = devices[i];
1378 if (!device.isVirtual()) {
1379 final int sources = device.getSources();
1380 final int presenceFlag = device.isExternal() ?
1381 WindowManagerPolicy.PRESENCE_EXTERNAL :
1382 WindowManagerPolicy.PRESENCE_INTERNAL;
1383
1384 // TODO(multi-display): Configure on per-display basis.
1385 if (mService.mIsTouchDevice) {
1386 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1387 InputDevice.SOURCE_TOUCHSCREEN) {
1388 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1389 }
1390 } else {
1391 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1392 }
1393
1394 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1395 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1396 navigationPresence |= presenceFlag;
1397 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1398 && config.navigation == Configuration.NAVIGATION_NONAV) {
1399 config.navigation = Configuration.NAVIGATION_DPAD;
1400 navigationPresence |= presenceFlag;
1401 }
1402
1403 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1404 config.keyboard = Configuration.KEYBOARD_QWERTY;
1405 keyboardPresence |= presenceFlag;
1406 }
1407 }
1408 }
1409
1410 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1411 config.navigation = Configuration.NAVIGATION_DPAD;
1412 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1413 }
1414
1415 // Determine whether a hard keyboard is available and enabled.
1416 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1417 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1418 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1419 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1420 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1421 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1422 }
1423
1424 // Let the policy update hidden states.
1425 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1426 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1427 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1428 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1429 }
1430
1431 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1432 int displayId) {
1433 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1434 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1435 final int unrotDw, unrotDh;
1436 if (rotated) {
1437 unrotDw = dh;
1438 unrotDh = dw;
1439 } else {
1440 unrotDw = dw;
1441 unrotDh = dh;
1442 }
1443 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1444 displayId);
1445 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1446 displayId);
1447 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1448 displayId);
1449 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1450 displayId);
1451 return sw;
1452 }
1453
1454 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1455 DisplayMetrics dm, int dw, int dh, int displayId) {
1456 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001457 displayId, mDisplayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001458 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001459 uiMode, displayId, mDisplayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001460 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1461 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1462 if (curSize == 0 || size < curSize) {
1463 curSize = size;
1464 }
1465 return curSize;
1466 }
1467
1468 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1469 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1470
1471 // We need to determine the smallest width that will occur under normal
1472 // operation. To this, start with the base screen size and compute the
1473 // width under the different possible rotations. We need to un-rotate
1474 // the current screen dimensions before doing this.
1475 int unrotDw, unrotDh;
1476 if (rotated) {
1477 unrotDw = dh;
1478 unrotDh = dw;
1479 } else {
1480 unrotDw = dw;
1481 unrotDh = dh;
1482 }
1483 displayInfo.smallestNominalAppWidth = 1<<30;
1484 displayInfo.smallestNominalAppHeight = 1<<30;
1485 displayInfo.largestNominalAppWidth = 0;
1486 displayInfo.largestNominalAppHeight = 0;
1487 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1488 unrotDh);
1489 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1490 unrotDw);
1491 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1492 unrotDh);
1493 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1494 unrotDw);
1495 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1496 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1497 displayId);
1498 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1499 displayId);
1500 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1501 displayId);
1502 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1503 displayId);
1504 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1505 outConfig.screenLayout = sl;
1506 }
1507
1508 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1509 int uiMode, int displayId) {
1510 // Get the app screen size at this rotation.
Adrian Roos11c25582018-02-19 18:06:36 +01001511 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId,
1512 mDisplayInfo.displayCutout);
1513 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId,
1514 mDisplayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001515
1516 // Compute the screen layout size class for this rotation.
1517 int longSize = w;
1518 int shortSize = h;
1519 if (longSize < shortSize) {
1520 int tmp = longSize;
1521 longSize = shortSize;
1522 shortSize = tmp;
1523 }
1524 longSize = (int)(longSize/density);
1525 shortSize = (int)(shortSize/density);
1526 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1527 }
1528
1529 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1530 int uiMode, int dw, int dh) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001531 final DisplayCutout displayCutout = calculateDisplayCutoutForRotation(
1532 rotation).getDisplayCutout();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001533 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001534 displayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001535 if (width < displayInfo.smallestNominalAppWidth) {
1536 displayInfo.smallestNominalAppWidth = width;
1537 }
1538 if (width > displayInfo.largestNominalAppWidth) {
1539 displayInfo.largestNominalAppWidth = width;
1540 }
1541 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001542 displayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001543 if (height < displayInfo.smallestNominalAppHeight) {
1544 displayInfo.smallestNominalAppHeight = height;
1545 }
1546 if (height > displayInfo.largestNominalAppHeight) {
1547 displayInfo.largestNominalAppHeight = height;
1548 }
1549 }
1550
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001551 DockedStackDividerController getDockedDividerController() {
1552 return mDividerControllerLocked;
1553 }
1554
Winson Chung655332c2016-10-31 13:14:28 -07001555 PinnedStackController getPinnedStackController() {
1556 return mPinnedStackControllerLocked;
1557 }
1558
Jeff Browna506a6e2013-06-04 00:02:38 -07001559 /**
1560 * Returns true if the specified UID has access to this display.
1561 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001562 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001563 return mDisplay.hasAccess(uid);
1564 }
1565
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001566 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001567 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001568 }
1569
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001570 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001571 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001572 }
1573
Wale Ogunwale61911492017-10-11 08:50:50 -07001574 /**
1575 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1576 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001577 TaskStack getSplitScreenPrimaryStack() {
1578 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001579 return (stack != null && stack.isVisible()) ? stack : null;
1580 }
1581
Robert Carr9785cf32018-04-25 15:06:07 -07001582 boolean hasSplitScreenPrimaryStack() {
1583 return getSplitScreenPrimaryStack() != null;
1584 }
1585
Wale Ogunwale61911492017-10-11 08:50:50 -07001586 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001587 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001588 * not visible.
1589 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001590 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1591 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001592 }
1593
1594 TaskStack getPinnedStack() {
1595 return mTaskStackContainers.getPinnedStack();
1596 }
1597
1598 private boolean hasPinnedStack() {
1599 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001600 }
1601
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001602 /**
1603 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1604 * Null is no compatible stack on the display.
1605 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001606 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001607 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1608 }
1609
1610 /**
1611 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1612 * activity type. Null is no compatible stack on the display.
1613 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001614 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001615 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001616 }
1617
Bryce Lee48f4b572017-04-10 10:54:15 -07001618 @VisibleForTesting
Kazuki Takise148d00a2018-05-31 15:32:19 +09001619 WindowList<TaskStack> getStacks() {
1620 return mTaskStackContainers.mChildren;
1621 }
1622
1623 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001624 TaskStack getTopStack() {
1625 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001626 }
1627
Winson Chunge2d72172018-01-25 17:46:20 +00001628 ArrayList<Task> getVisibleTasks() {
1629 return mTaskStackContainers.getVisibleTasks();
1630 }
1631
Wale Ogunwale61911492017-10-11 08:50:50 -07001632 void onStackWindowingModeChanged(TaskStack stack) {
1633 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001634 }
1635
Andrii Kulian441e4492016-09-29 15:25:00 -07001636 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001637 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001638 super.onConfigurationChanged(newParentConfig);
1639
Andrii Kulian3a507b52016-09-19 18:14:12 -07001640 // The display size information is heavily dependent on the resources in the current
1641 // configuration, so we need to reconfigure it every time the configuration changes.
1642 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1643 mService.reconfigureDisplayLocked(this);
1644
Bryce Leef3c6a472017-11-14 14:53:06 -08001645 final DockedStackDividerController dividerController = getDockedDividerController();
1646
1647 if (dividerController != null) {
1648 getDockedDividerController().onConfigurationChanged();
1649 }
1650
1651 final PinnedStackController pinnedStackController = getPinnedStackController();
1652
1653 if (pinnedStackController != null) {
1654 getPinnedStackController().onConfigurationChanged();
1655 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001656 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001657
Andrii Kulian441e4492016-09-29 15:25:00 -07001658 /**
1659 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1660 * bounds were updated.
1661 */
Bryce Leeaea60d22018-01-31 14:42:15 -08001662 void updateStackBoundsAfterConfigChange(@NonNull List<TaskStack> changedStackList) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001663 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1664 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001665 if (stack.updateBoundsAfterConfigChange()) {
Bryce Leeaea60d22018-01-31 14:42:15 -08001666 changedStackList.add(stack);
Andrii Kulian3a507b52016-09-19 18:14:12 -07001667 }
1668 }
Winson Chung32c566f2017-04-11 18:31:21 -07001669
1670 // If there was no pinned stack, we still need to notify the controller of the display info
1671 // update as a result of the config change. We do this here to consolidate the flow between
1672 // changes when there is and is not a stack.
Wale Ogunwale61911492017-10-11 08:50:50 -07001673 if (!hasPinnedStack()) {
Winson Chung32c566f2017-04-11 18:31:21 -07001674 mPinnedStackControllerLocked.onDisplayInfoChanged();
1675 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001676 }
1677
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001678 @Override
1679 boolean fillsParent() {
1680 return true;
1681 }
1682
1683 @Override
1684 boolean isVisible() {
1685 return true;
1686 }
1687
1688 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001689 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001690 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001691 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001692 }
1693
Robert Carr9785cf32018-04-25 15:06:07 -07001694 /**
1695 * In split-screen mode we process the IME containers above the docked divider
1696 * rather than directly above their target.
1697 */
1698 private boolean skipTraverseChild(WindowContainer child) {
1699 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null
1700 && !hasSplitScreenPrimaryStack()) {
1701 return true;
1702 }
1703 return false;
1704 }
1705
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001706 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001707 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1708 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1709 // target, or here in order if there isn't an IME target.
1710 if (traverseTopToBottom) {
1711 for (int i = mChildren.size() - 1; i >= 0; --i) {
1712 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001713 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001714 continue;
1715 }
Robert Carr9785cf32018-04-25 15:06:07 -07001716
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001717 if (child.forAllWindows(callback, traverseTopToBottom)) {
1718 return true;
1719 }
1720 }
1721 } else {
1722 final int count = mChildren.size();
1723 for (int i = 0; i < count; i++) {
1724 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001725 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001726 continue;
1727 }
Robert Carr9785cf32018-04-25 15:06:07 -07001728
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001729 if (child.forAllWindows(callback, traverseTopToBottom)) {
1730 return true;
1731 }
1732 }
1733 }
1734 return false;
1735 }
1736
1737 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1738 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1739 }
1740
Wale Ogunwale399c8692017-05-08 14:22:42 -07001741 @Override
1742 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001743 final WindowManagerPolicy policy = mService.mPolicy;
1744
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001745 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001746 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001747 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1748 + " is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001749 // If the display is frozen, some activities may be in the middle of restarting, and
1750 // thus have removed their old window. If the window has the flag to hide the lock
1751 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1752 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001753 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001754 } else if (policy.isKeyguardLocked()) {
1755 // Use the last orientation the while the display is frozen with the keyguard
1756 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1757 // window. We don't want to check the show when locked window directly though as
1758 // things aren't stable while the display is frozen, for example the window could be
1759 // momentarily unavailable due to activity relaunch.
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001760 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1761 + " is frozen while keyguard locked, return " + mLastOrientation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08001762 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001763 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001764 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001765 final int orientation = mAboveAppWindowsContainers.getOrientation();
1766 if (orientation != SCREEN_ORIENTATION_UNSET) {
1767 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001768 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001769 }
1770
Wale Ogunwale399c8692017-05-08 14:22:42 -07001771 // Top system windows are not requesting an orientation. Start searching from apps.
1772 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001773 }
1774
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001775 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001776 // Check if display metrics changed and update base values if needed.
1777 updateBaseDisplayMetricsIfNeeded();
1778
Craig Mautner722285e2012-09-07 13:55:58 -07001779 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001780 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001781
Wale Ogunwale61911492017-10-11 08:50:50 -07001782 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1783 mTaskStackContainers.getChildAt(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001784 }
Craig Mautner722285e2012-09-07 13:55:58 -07001785 }
1786
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001787 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001788 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1789 if (displayManagerInternal != null) {
1790 // Bootstrap the default logical display from the display manager.
1791 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1792 if (newDisplayInfo != null) {
1793 mDisplayInfo.copyFrom(newDisplayInfo);
1794 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001795 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001796
Andrii Kuliancd097992017-03-23 18:31:59 -07001797 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1798 mDisplayInfo.logicalDensityDpi);
1799 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1800 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1801 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01001802 mInitialDisplayCutout = mDisplayInfo.displayCutout;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001803 }
1804
Andrii Kuliancd097992017-03-23 18:31:59 -07001805 /**
1806 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1807 * values.
1808 */
1809 private void updateBaseDisplayMetricsIfNeeded() {
1810 // Get real display metrics without overrides from WM.
1811 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1812 final int orientation = mDisplayInfo.rotation;
1813 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1814 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1815 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1816 final int newDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01001817 final DisplayCutout newCutout = mDisplayInfo.displayCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07001818
1819 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1820 || mInitialDisplayHeight != newHeight
Adrian Roos1cf585052018-01-03 18:43:27 +01001821 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi
1822 || !Objects.equals(mInitialDisplayCutout, newCutout);
Andrii Kuliancd097992017-03-23 18:31:59 -07001823
1824 if (displayMetricsChanged) {
1825 // Check if display size or density is forced.
1826 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1827 || mBaseDisplayHeight != mInitialDisplayHeight;
1828 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1829
1830 // If there is an override set for base values - use it, otherwise use new values.
1831 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1832 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1833 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1834
1835 // Real display metrics changed, so we should also update initial values.
1836 mInitialDisplayWidth = newWidth;
1837 mInitialDisplayHeight = newHeight;
1838 mInitialDisplayDensity = newDensity;
Adrian Roos1cf585052018-01-03 18:43:27 +01001839 mInitialDisplayCutout = newCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07001840 mService.reconfigureDisplayLocked(this);
1841 }
1842 }
1843
Bryce Lee27cec322017-03-21 09:41:37 -07001844 /** Sets the maximum width the screen resolution can be */
1845 void setMaxUiWidth(int width) {
1846 if (DEBUG_DISPLAY) {
1847 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1848 }
1849
1850 mMaxUiWidth = width;
1851
1852 // Update existing metrics.
1853 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1854 }
1855
1856 /** Update base (override) display metrics. */
1857 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1858 mBaseDisplayWidth = baseWidth;
1859 mBaseDisplayHeight = baseHeight;
1860 mBaseDisplayDensity = baseDensity;
1861
1862 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1863 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1864 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1865 mBaseDisplayWidth = mMaxUiWidth;
1866
1867 if (DEBUG_DISPLAY) {
1868 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1869 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1870 + " on display:" + getDisplayId());
1871 }
1872 }
1873
1874 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08001875
1876 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07001877 }
1878
Wale Ogunwaledb506192017-12-08 10:57:32 -08001879 void getStableRect(Rect out) {
1880 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08001881 }
1882
Wale Ogunwale61911492017-10-11 08:50:50 -07001883 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001884 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1885 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001886
Wale Ogunwale61911492017-10-11 08:50:50 -07001887 final TaskStack stack = new TaskStack(mService, stackId, controller);
1888 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08001889 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001890 }
1891
Andrii Kulian51c1b672017-04-07 18:39:32 -07001892 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001893 final DisplayContent prevDc = stack.getDisplayContent();
1894 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001895 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1896 + " which is not currently attached to any display");
1897 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001898 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001899 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1900 + " to its current displayId=" + mDisplayId);
1901 }
1902
Wale Ogunwale61911492017-10-11 08:50:50 -07001903 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001904 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001905 }
1906
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001907 @Override
1908 protected void addChild(DisplayChildWindowContainer child,
1909 Comparator<DisplayChildWindowContainer> comparator) {
1910 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1911 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001912
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001913 @Override
1914 protected void addChild(DisplayChildWindowContainer child, int index) {
1915 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1916 }
1917
1918 @Override
1919 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001920 // Only allow removal of direct children from this display if the display is in the process
1921 // of been removed.
1922 if (mRemovingDisplay) {
1923 super.removeChild(child);
1924 return;
1925 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001926 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001927 }
1928
Andrii Kuliand2765632016-12-12 22:26:34 -08001929 @Override
1930 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1931 // Children of the display are statically ordered, so the real intention here is to perform
1932 // the operation on the display and not the static direct children.
1933 getParent().positionChildAt(position, this, includingParents);
1934 }
1935
Winson Chung59a47ded2018-01-25 17:46:06 +00001936 void positionStackAt(int position, TaskStack child) {
1937 mTaskStackContainers.positionChildAt(position, child, false /* includingParents */);
1938 layoutAndAssignWindowLayersIfNeeded();
1939 }
1940
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001941 int taskIdFromPoint(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001942 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1943 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001944 final int taskId = stack.taskIdFromPoint(x, y);
1945 if (taskId != -1) {
1946 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001947 }
1948 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001949 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001950 }
1951
Chong Zhang8e89b312015-09-09 15:09:30 -07001952 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001953 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001954 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001955 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001956 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001957 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001958 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07001959 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1960 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001961 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001962 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001963 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001964
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001965 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1966 if (mTmpTaskForResizePointSearchResult.searchDone) {
1967 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001968 }
1969 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001970 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001971 }
1972
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001973 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001974 // The provided task is the task on this display with focus, so if WindowManagerService's
1975 // focused app is not on this display, focusedTask will be null.
1976 if (focusedTask == null) {
1977 mTouchExcludeRegion.setEmpty();
1978 } else {
1979 mTouchExcludeRegion.set(mBaseDisplayRect);
1980 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1981 mTmpRect2.setEmpty();
Wale Ogunwale61911492017-10-11 08:50:50 -07001982 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1983 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001984 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
1985 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08001986 }
1987 // If we removed the focused task above, add it back and only leave its
1988 // outside touch area in the exclusion. TapDectector is not interested in
1989 // any touch inside the focused task itself.
1990 if (!mTmpRect2.isEmpty()) {
1991 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1992 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001993 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001994 final WindowState inputMethod = mService.mInputMethodWindow;
1995 if (inputMethod != null && inputMethod.isVisibleLw()) {
1996 // If the input method is visible and the user is typing, we don't want these touch
1997 // events to be intercepted and used to change focus. This would likely cause a
1998 // disappearance of the input method.
1999 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07002000 if (inputMethod.getDisplayId() == mDisplayId) {
2001 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
2002 } else {
2003 // IME is on a different display, so we need to update its tap detector.
2004 // TODO(multidisplay): Remove when IME will always appear on same display.
2005 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
2006 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002007 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002008 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002009 final WindowState win = mTapExcludedWindows.get(i);
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002010 win.getTouchableRegion(mTmpRegion);
2011 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
2012 }
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002013 for (int i = mTapExcludeProvidingWindows.size() - 1; i >= 0; i--) {
2014 final WindowState win = mTapExcludeProvidingWindows.valueAt(i);
2015 win.amendTapExcludeRegion(mTouchExcludeRegion);
2016 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08002017 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07002018 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002019 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07002020 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002021 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
2022 }
Craig Mautner1bef3892015-02-17 15:09:47 -08002023 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07002024 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08002025 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07002026 }
2027
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002028 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07002029 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002030 super.switchUser();
2031 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07002032 }
2033
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07002034 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002035 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2036 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07002037 }
2038 }
2039
Wale Ogunwale10124582016-09-15 20:25:50 -07002040 @Override
2041 void removeIfPossible() {
2042 if (isAnimating()) {
2043 mDeferredRemoval = true;
2044 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07002045 }
Wale Ogunwale10124582016-09-15 20:25:50 -07002046 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07002047 }
2048
Wale Ogunwale10124582016-09-15 20:25:50 -07002049 @Override
2050 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002051 mRemovingDisplay = true;
2052 try {
2053 super.removeImmediately();
2054 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -07002055 if (mService.canDispatchPointerEvents()) {
2056 if (mTapDetector != null) {
2057 mService.unregisterPointerEventListener(mTapDetector);
2058 }
2059 if (mDisplayId == DEFAULT_DISPLAY && mService.mMousePositionTracker != null) {
2060 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
2061 }
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002062 }
Jorim Jaggia3844032018-01-03 15:54:43 +01002063 mService.mAnimator.removeDisplayLocked(mDisplayId);
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002064 } finally {
2065 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08002066 }
Bryce Leef19cbe22018-02-02 15:09:21 -08002067
2068 mService.onDisplayRemoved(mDisplayId);
Craig Mautner95da1082014-02-24 17:54:35 -08002069 }
2070
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002071 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07002072 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002073 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002074 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
2075
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002076 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002077 removeImmediately();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002078 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08002079 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002080 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08002081 }
2082
Andrii Kulian0214ed92017-05-16 13:44:05 -07002083 /** @return 'true' if removal of this display content is deferred due to active animation. */
2084 boolean isRemovalDeferred() {
2085 return mDeferredRemoval;
2086 }
2087
Wale Ogunwale10124582016-09-15 20:25:50 -07002088 boolean animateForIme(float interpolatedValue, float animationTarget,
2089 float dividerAnimationTarget) {
2090 boolean updated = false;
2091
Wale Ogunwale61911492017-10-11 08:50:50 -07002092 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2093 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002094 if (stack == null || !stack.isAdjustedForIme()) {
2095 continue;
2096 }
2097
2098 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
2099 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2100 updated = true;
2101 } else {
2102 mDividerControllerLocked.mLastAnimationProgress =
2103 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
2104 mDividerControllerLocked.mLastDividerProgress =
2105 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
2106 updated |= stack.updateAdjustForIme(
2107 mDividerControllerLocked.mLastAnimationProgress,
2108 mDividerControllerLocked.mLastDividerProgress,
2109 false /* force */);
2110 }
2111 if (interpolatedValue >= 1f) {
2112 stack.endImeAdjustAnimation();
2113 }
2114 }
2115
2116 return updated;
2117 }
2118
2119 boolean clearImeAdjustAnimation() {
2120 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002121 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2122 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002123 if (stack != null && stack.isAdjustedForIme()) {
2124 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2125 changed = true;
2126 }
2127 }
2128 return changed;
2129 }
2130
2131 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002132 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2133 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002134 if (stack.isVisible() && stack.isAdjustedForIme()) {
2135 stack.beginImeAdjustAnimation();
2136 }
2137 }
2138 }
2139
2140 void adjustForImeIfNeeded() {
2141 final WindowState imeWin = mService.mInputMethodWindow;
2142 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2143 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002144 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002145 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2146 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2147 imeTargetStack.getDockSide() : DOCKED_INVALID;
2148 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2149 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2150 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002151 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002152 final boolean imeHeightChanged = imeVisible &&
2153 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2154
2155 // The divider could be adjusted for IME position, or be thinner than usual,
2156 // or both. There are three possible cases:
2157 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2158 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2159 // - If IME is not visible, divider is not moved and is normal width.
2160
2161 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002162 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2163 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002164 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002165 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2166 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002167 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2168 } else {
2169 stack.resetAdjustedForIme(false);
2170 }
2171 }
2172 mDividerControllerLocked.setAdjustedForIme(
2173 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2174 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002175 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2176 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002177 stack.resetAdjustedForIme(!dockVisible);
2178 }
2179 mDividerControllerLocked.setAdjustedForIme(
2180 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2181 }
Winson Chung655332c2016-10-31 13:14:28 -07002182 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002183 }
2184
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002185 /**
2186 * If a window that has an animation specifying a colored background and the current wallpaper
2187 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2188 * suddenly disappear.
2189 */
2190 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002191 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2192 w -> w.mIsWallpaper && w.isVisibleNow());
2193
2194 if (visibleWallpaper != null) {
2195 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002196 }
2197 return winAnimator.mAnimLayer;
2198 }
2199
Wale Ogunwale10124582016-09-15 20:25:50 -07002200 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002201 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2202 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002203 stack.prepareFreezingTaskBounds();
2204 }
2205 }
2206
Wale Ogunwale94744212015-09-21 19:01:47 -07002207 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002208 getBounds(mTmpRect, newRotation);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002209
2210 // Compute a transform matrix to undo the coordinate space transformation,
2211 // and present the window at the same physical position it previously occupied.
2212 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2213 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2214
2215 mTmpRectF.set(bounds);
2216 mTmpMatrix.mapRect(mTmpRectF);
2217 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002218 }
2219
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002220 static int deltaRotation(int oldRotation, int newRotation) {
2221 int delta = newRotation - oldRotation;
2222 if (delta < 0) delta += 4;
2223 return delta;
2224 }
2225
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002226 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002227 Matrix outMatrix) {
2228 // For rotations without Z-ordering we don't need the target rectangle's position.
2229 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2230 displayHeight, outMatrix);
2231 }
2232
2233 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2234 float displayWidth, float displayHeight, Matrix outMatrix) {
2235 switch (rotation) {
2236 case ROTATION_0:
2237 outMatrix.reset();
2238 break;
2239 case ROTATION_270:
2240 outMatrix.setRotate(270, 0, 0);
2241 outMatrix.postTranslate(0, displayHeight);
2242 outMatrix.postTranslate(rectTop, 0);
2243 break;
2244 case ROTATION_180:
2245 outMatrix.reset();
2246 break;
2247 case ROTATION_90:
2248 outMatrix.setRotate(90, 0, 0);
2249 outMatrix.postTranslate(displayWidth, 0);
2250 outMatrix.postTranslate(-rectTop, rectLeft);
2251 break;
2252 }
2253 }
2254
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002255 @CallSuper
2256 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002257 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002258 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002259 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002260 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002261 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2262 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002263 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002264 }
2265 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2266 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002267 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2268 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002269 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002270 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002271 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2272 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002273 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002274 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002275 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2276 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002277 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002278 }
2279 proto.write(DPI, mBaseDisplayDensity);
2280 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002281 proto.write(ROTATION, mRotation);
2282 final ScreenRotationAnimation screenRotationAnimation =
2283 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2284 if (screenRotationAnimation != null) {
2285 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2286 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002287 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002288 proto.end(token);
2289 }
2290
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002291 @Override
2292 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2293 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002294 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2295 final String subPrefix = " " + prefix;
2296 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2297 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2298 pw.print("dpi");
2299 if (mInitialDisplayWidth != mBaseDisplayWidth
2300 || mInitialDisplayHeight != mBaseDisplayHeight
2301 || mInitialDisplayDensity != mBaseDisplayDensity) {
2302 pw.print(" base=");
2303 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2304 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2305 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002306 if (mDisplayScalingDisabled) {
2307 pw.println(" noscale");
2308 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002309 pw.print(" cur=");
2310 pw.print(mDisplayInfo.logicalWidth);
2311 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2312 pw.print(" app=");
2313 pw.print(mDisplayInfo.appWidth);
2314 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2315 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2316 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2317 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2318 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002319 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002320 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002321 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002322
Craig Mautnerdc548482014-02-05 13:35:24 -08002323 pw.println();
Adrian Roos5251b1d2018-03-23 18:57:43 +01002324 pw.print(prefix); pw.print("mLayoutSeq="); pw.println(mLayoutSeq);
Riddle Hsu654a6f92018-07-13 22:59:36 +08002325 pw.print(prefix);
2326 pw.print("mDeferredRotationPauseCount="); pw.println(mDeferredRotationPauseCount);
Adrian Roos5251b1d2018-03-23 18:57:43 +01002327
2328 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002329 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002330 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2331 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002332 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002333 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002334
Craig Mautnerdc548482014-02-05 13:35:24 -08002335 pw.println();
2336 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002337 pw.println();
2338 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002339 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002340 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002341 pw.print(" Exiting #"); pw.print(i);
2342 pw.print(' '); pw.print(token);
2343 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002344 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002345 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002346 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002347
Jorim Jaggi31f71702016-05-04 16:43:04 -07002348 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002349
2350 // Dump stack references
2351 final TaskStack homeStack = getHomeStack();
2352 if (homeStack != null) {
2353 pw.println(prefix + "homeStack=" + homeStack.getName());
2354 }
2355 final TaskStack pinnedStack = getPinnedStack();
2356 if (pinnedStack != null) {
2357 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2358 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002359 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002360 if (splitScreenPrimaryStack != null) {
2361 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2362 }
2363
2364 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002365 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002366 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002367 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002368
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002369 pw.println();
2370 mDisplayFrames.dump(prefix, pw);
Arthur Hung95b38a92018-07-20 18:56:12 +08002371 pw.println();
2372 mInputMonitor.dump(pw, " ");
Craig Mautner59c00972012-07-30 12:10:24 -07002373 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002374
2375 @Override
2376 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002377 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002378 }
2379
2380 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002381 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002382 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002383
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002384 /** Returns true if the stack in the windowing mode is visible. */
2385 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002386 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002387 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002388 }
2389
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002390 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002391 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002392 final int x = (int) xf;
2393 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002394 final WindowState touchedWin = getWindow(w -> {
2395 final int flags = w.mAttrs.flags;
2396 if (!w.isVisibleLw()) {
2397 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002398 }
2399 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002400 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002401 }
2402
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002403 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002404 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002405 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002406 }
2407
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002408 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002409
2410 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002411 return mTmpRegion.contains(x, y) || touchFlags == 0;
2412 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002413
2414 return touchedWin;
2415 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002416
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002417 boolean canAddToastWindowForUid(int uid) {
2418 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002419 // Also if the app is focused adding more than one toast at
2420 // a time for better backwards compatibility.
2421 final WindowState focusedWindowForUid = getWindow(w ->
2422 w.mOwnerUid == uid && w.isFocused());
2423 if (focusedWindowForUid != null) {
2424 return true;
2425 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002426 final WindowState win = getWindow(w ->
2427 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2428 && !w.mWindowRemovalAllowed);
2429 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002430 }
2431
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002432 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002433 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2434 return;
2435 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002436
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002437 // Used to communicate the old focus to the callback method.
2438 mTmpWindow = oldFocus;
2439
2440 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002441 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002442
2443 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002444 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002445
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002446 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002447
2448 if (mTmpWindow == null) {
2449 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2450 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002451 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002452 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002453 }
2454
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002455 /** Updates the layer assignment of windows on this display. */
2456 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002457 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07002458 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002459 if (setLayoutNeeded) {
2460 setLayoutNeeded();
2461 }
Robert Carrb1579c82017-09-05 14:54:47 -07002462
Robert Carrf59b8dd2017-10-02 18:58:36 -07002463 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002464 // the application of this transaction until the animation pass triggers
2465 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2466 // the hiding and showing of surfaces.
2467 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002468 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002469 }
2470
Wale Ogunwale1666e312016-12-16 11:27:18 -08002471 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2472 // moving containers or resizing them. Need to investigate the best way to have it automatically
2473 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002474 void layoutAndAssignWindowLayersIfNeeded() {
2475 mService.mWindowsChanged = true;
2476 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002477
2478 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2479 false /*updateInputWindows*/)) {
2480 assignWindowLayers(false /* setLayoutNeeded */);
2481 }
2482
Arthur Hung95b38a92018-07-20 18:56:12 +08002483 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002484 mService.mWindowPlacerLocked.performSurfacePlacement();
Arthur Hung95b38a92018-07-20 18:56:12 +08002485 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002486 }
2487
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002488 /** Returns true if a leaked surface was destroyed */
2489 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002490 // Used to indicate that a surface was leaked.
2491 mTmpWindow = null;
2492 forAllWindows(w -> {
2493 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002494 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002495 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002496 }
2497 if (!mService.mSessions.contains(wsa.mSession)) {
2498 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002499 + w + " surface=" + wsa.mSurfaceController
2500 + " token=" + w.mToken
2501 + " pid=" + w.mSession.mPid
2502 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002503 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002504 mService.mForceRemoves.add(w);
2505 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002506 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002507 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002508 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002509 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002510 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002511 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002512 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002513 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002514 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002515
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002516 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002517 }
2518
2519 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002520 * Determine and return the window that should be the IME target.
2521 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2522 * @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 +00002523 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002524 WindowState computeImeTarget(boolean updateImeTarget) {
2525 if (mService.mInputMethodWindow == null) {
2526 // There isn't an IME so there shouldn't be a target...That was easy!
2527 if (updateImeTarget) {
2528 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2529 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002530 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002531 }
2532 return null;
2533 }
2534
Chavi Weingarten3a748552018-05-14 17:32:42 +00002535 final WindowState curTarget = mService.mInputMethodTarget;
2536 if (!canUpdateImeTarget()) {
2537 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Defer updating IME target");
2538 return curTarget;
2539 }
2540
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002541 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2542 // same display. Or even when the current IME/target are not on the same screen as the next
2543 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002544 mUpdateImeTarget = updateImeTarget;
2545 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002546
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002547
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002548 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2549 // to be on top of it, but it is not -really- where input will go. So look down below
2550 // for a real window to target...
2551 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2552 final AppWindowToken token = target.mAppToken;
2553 if (token != null) {
2554 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2555 if (betterTarget != null) {
2556 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002557 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002558 }
2559 }
2560
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002561 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2562 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002563
chaviw1c13ad32018-06-12 16:44:23 -07002564 // Now, a special case -- if the last target's window is in the process of exiting, but
2565 // not removed, and the new target is home, keep on the last target to avoid flicker.
2566 // Home is a special case since its above other stacks in the ordering list, but layed
2567 // out below the others.
2568 if (curTarget != null && !curTarget.mRemoved && curTarget.isDisplayedLw()
2569 && curTarget.isClosing() && (target == null || target.isActivityTypeHome())) {
chaviw4ad54912018-05-30 11:05:44 -07002570 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "New target is home while current target is "
Chavi Weingarten3a748552018-05-14 17:32:42 +00002571 + "closing, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002572 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002573 }
2574
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002575 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2576 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002577
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002578 if (target == null) {
2579 if (updateImeTarget) {
2580 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2581 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2582 + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002583 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002584 }
2585
2586 return null;
2587 }
2588
2589 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002590 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2591 if (token != null) {
2592
2593 // Now some fun for dealing with window animations that modify the Z order. We need
2594 // to look at all windows below the current target that are in this app, finding the
2595 // highest visible one in layering.
2596 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002597 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002598 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002599 }
2600
2601 if (highestTarget != null) {
2602 final AppTransition appTransition = mService.mAppTransition;
2603 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2604 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2605 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002606 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002607
2608 if (appTransition.isTransitionSet()) {
2609 // If we are currently setting up for an animation, hold everything until we
2610 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002611 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002612 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002613 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002614 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002615 // If the window we are currently targeting is involved with an animation,
2616 // and it is on top of the next target we will be over, then hold off on
2617 // moving until that is done.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002618 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002619 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002620 }
2621 }
2622 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002623
2624 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2625 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002626 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002627 }
2628
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002629 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002630 }
2631
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002632 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002633 if (target == mService.mInputMethodTarget
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002634 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002635 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002636 }
2637
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002638 mService.mInputMethodTarget = target;
2639 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002640 assignWindowLayers(false /* setLayoutNeeded */);
2641 }
2642
2643 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2644 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2645 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2646 }
2647
2648 // Used to indicate we have reached the first window in the range we are interested in.
2649 mTmpWindow = null;
2650
2651 // TODO: Figure-out a more efficient way to do this.
2652 final WindowState candidate = getWindow(w -> {
2653 if (w == top) {
2654 // Reached the first window in the range we are interested in.
2655 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002656 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002657 if (mTmpWindow == null) {
2658 return false;
2659 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002660
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002661 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2662 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002663 }
2664 // If we reached the bottom of the range of windows we are considering,
2665 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002666 if (w == bottom) {
2667 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002668 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002669 return false;
2670 });
2671
2672 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002673 }
2674
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002675 void setLayoutNeeded() {
2676 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2677 mLayoutNeeded = true;
2678 }
2679
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002680 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002681 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2682 mLayoutNeeded = false;
2683 }
2684
2685 boolean isLayoutNeeded() {
2686 return mLayoutNeeded;
2687 }
2688
Wale Ogunwale02319a62016-09-26 15:21:22 -07002689 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2690 if (mTokenMap.isEmpty()) {
2691 return;
2692 }
2693 pw.println(" Display #" + mDisplayId);
2694 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2695 while (it.hasNext()) {
2696 final WindowToken token = it.next();
2697 pw.print(" ");
2698 pw.print(token);
2699 if (dumpAll) {
2700 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002701 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07002702 } else {
2703 pw.println();
2704 }
2705 }
2706 }
2707
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002708 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002709 final int[] index = new int[1];
2710 forAllWindows(w -> {
2711 final WindowStateAnimator wAnim = w.mWinAnimator;
2712 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2713 index[0] = index[0] + 1;
2714 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002715 }
2716
Jorim Jaggife762342016-10-13 14:33:27 +02002717 /**
2718 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2719 */
2720 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2721 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002722 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002723 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2724 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01002725 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02002726 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002727 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002728 }
2729
Wale Ogunwale494009b82016-10-21 09:01:38 -07002730 boolean checkWaitingForWindows() {
2731
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002732 mHaveBootMsg = false;
2733 mHaveApp = false;
2734 mHaveWallpaper = false;
2735 mHaveKeyguard = true;
2736
2737 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002738 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2739 return true;
2740 }
2741 if (w.isDrawnLw()) {
2742 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002743 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002744 } else if (w.mAttrs.type == TYPE_APPLICATION
2745 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002746 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002747 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002748 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002749 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002750 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002751 }
2752 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002753 return false;
2754 });
2755
2756 if (visibleWindow != null) {
2757 // We have a visible window.
2758 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002759 }
2760
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002761 // if the wallpaper service is disabled on the device, we're never going to have
2762 // wallpaper, don't bother waiting for it
2763 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2764 com.android.internal.R.bool.config_enableWallpaperService)
Daichi Hironoc1432222018-03-29 13:06:07 +09002765 && mService.mContext.getResources().getBoolean(
2766 com.android.internal.R.bool.config_checkWallpaperAtBoot)
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002767 && !mService.mOnlyCore;
2768
Wale Ogunwale494009b82016-10-21 09:01:38 -07002769 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2770 "******** booted=" + mService.mSystemBooted
2771 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002772 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2773 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2774 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002775
2776 // If we are turning on the screen to show the boot message, don't do it until the boot
2777 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002778 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002779 return true;
2780 }
2781
2782 // If we are turning on the screen after the boot is completed normally, don't do so until
2783 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002784 if (mService.mSystemBooted
2785 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002786 return true;
2787 }
2788
2789 return false;
2790 }
2791
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002792 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002793 mTmpWindowAnimator = animator;
2794 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002795 }
2796
2797 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002798 resetAnimationBackgroundAnimator();
2799
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002800 // Used to indicate a detached wallpaper.
2801 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002802 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002803
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002804 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002805
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002806 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002807 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002808 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2809 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002810 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2811 }
2812 }
2813
Wale Ogunwale494009b82016-10-21 09:01:38 -07002814 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002815 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002816 if (imFocus == null) {
2817 return false;
2818 }
2819
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002820 if (DEBUG_INPUT_METHOD) {
2821 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2822 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2823 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002824 }
2825
Wale Ogunwale494009b82016-10-21 09:01:38 -07002826 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2827
2828 if (DEBUG_INPUT_METHOD) {
2829 Slog.i(TAG_WM, "IM target client: " + imeClient);
2830 if (imeClient != null) {
2831 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2832 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2833 }
2834 }
2835
2836 return imeClient != null && imeClient.asBinder() == client.asBinder();
2837 }
2838
2839 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002840 final WindowState win = getWindow(
2841 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2842 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002843 }
2844
2845 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002846 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002847 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002848 final int curValue = w.mSystemUiVisibility;
2849 final int diff = (curValue ^ visibility) & globalDiff;
2850 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002851 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002852 w.mSeq++;
2853 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002854 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002855 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2856 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002857 visibility, newValue, diff);
2858 }
2859 } catch (RemoteException e) {
2860 // so sorry
2861 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002862 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002863 }
2864
2865 void onWindowFreezeTimeout() {
2866 Slog.w(TAG_WM, "Window freeze timeout expired.");
2867 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002868
2869 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002870 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002871 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002872 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002873 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002874 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2875 - mService.mDisplayFreezeTime);
2876 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002877 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002878 mService.mWindowPlacerLocked.performSurfacePlacement();
2879 }
2880
2881 void waitForAllWindowsDrawn() {
2882 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002883 forAllWindows(w -> {
2884 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2885 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2886 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002887 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002888 w.mLastContentInsets.set(-1, -1, -1, -1);
2889 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002890 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002891 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002892 }
2893
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002894 // TODO: Super crazy long method that should be broken down...
2895 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2896
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002897 final int dw = mDisplayInfo.logicalWidth;
2898 final int dh = mDisplayInfo.logicalHeight;
2899 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2900
2901 mTmpUpdateAllDrawn.clear();
2902
2903 int repeats = 0;
2904 do {
2905 repeats++;
2906 if (repeats > 6) {
2907 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2908 clearLayoutNeeded();
2909 break;
2910 }
2911
2912 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2913 pendingLayoutChanges);
2914
Andrii Kulian839def92016-11-02 10:58:58 -07002915 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2916 // the wallpaper window jumping across displays.
2917 // Remove check for default display when there will be support for multiple wallpaper
2918 // targets (on different displays).
2919 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002920 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002921 }
2922
Riddle Hsu654a6f92018-07-13 22:59:36 +08002923 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002924 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
Robert Carrae606b42018-02-15 15:36:23 -08002925 if (mService.updateOrientationFromAppTokensLocked(mDisplayId)) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002926 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002927 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002928 }
2929 }
2930
2931 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2932 setLayoutNeeded();
2933 }
2934
2935 // FIRST LOOP: Perform a layout, if needed.
2936 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2937 performLayout(repeats == 1, false /* updateInputWindows */);
2938 } else {
2939 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2940 }
2941
2942 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2943 pendingLayoutChanges = 0;
2944
2945 if (isDefaultDisplay) {
2946 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002947 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002948 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2949 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2950 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2951 }
2952 } while (pendingLayoutChanges != 0);
2953
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002954 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002955
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002956 mTmpRecoveringMemory = recoveringMemory;
2957 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01002958 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002959
2960 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002961 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2962 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2963 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002964 true /* inTraversal, must call performTraversalInTrans... below */);
2965
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002966 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2967 if (wallpaperVisible != mLastWallpaperVisible) {
2968 mLastWallpaperVisible = wallpaperVisible;
2969 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2970 }
2971
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002972 while (!mTmpUpdateAllDrawn.isEmpty()) {
2973 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2974 // See if any windows have been drawn, so they (and others associated with them)
2975 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002976 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002977 }
2978
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002979 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002980 }
2981
Bryce Leef3c6a472017-11-14 14:53:06 -08002982 private void updateBounds() {
2983 calculateBounds(mTmpBounds);
2984 setBounds(mTmpBounds);
2985 }
2986
2987 // Determines the current display bounds based on the current state
2988 private void calculateBounds(Rect out) {
2989 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
2990 final int orientation = mDisplayInfo.rotation;
2991 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2992 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
2993 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
2994 int width = mDisplayInfo.logicalWidth;
2995 int left = (physWidth - width) / 2;
2996 int height = mDisplayInfo.logicalHeight;
2997 int top = (physHeight - height) / 2;
2998 out.set(left, top, left + width, top + height);
2999 }
3000
3001 @Override
3002 public void getBounds(Rect out) {
3003 calculateBounds(out);
3004 }
3005
3006 private void getBounds(Rect out, int orientation) {
3007 getBounds(out);
3008
3009 // Rotate the Rect if needed.
3010 final int currentRotation = mDisplayInfo.rotation;
3011 final int rotationDelta = deltaRotation(currentRotation, orientation);
3012 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
3013 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
3014 mTmpRectF.set(out);
3015 mTmpMatrix.mapRect(mTmpRectF);
3016 mTmpRectF.round(out);
3017 }
3018 }
3019
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003020 void performLayout(boolean initial, boolean updateInputWindows) {
3021 if (!isLayoutNeeded()) {
3022 return;
3023 }
3024 clearLayoutNeeded();
3025
3026 final int dw = mDisplayInfo.logicalWidth;
3027 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003028 if (DEBUG_LAYOUT) {
3029 Slog.v(TAG, "-------------------------------------");
3030 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
3031 }
3032
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01003033 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
3034 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00003035 // TODO: Not sure if we really need to set the rotation here since we are updating from the
3036 // display info above...
3037 mDisplayFrames.mRotation = mRotation;
3038 mService.mPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003039 if (isDefaultDisplay) {
3040 // Not needed on non-default displays.
3041 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
3042 mService.mScreenRect.set(0, 0, dw, dh);
3043 }
3044
Adrian Roos5251b1d2018-03-23 18:57:43 +01003045 int seq = mLayoutSeq + 1;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003046 if (seq < 0) seq = 0;
Adrian Roos5251b1d2018-03-23 18:57:43 +01003047 mLayoutSeq = seq;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003048
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003049 // Used to indicate that we have processed the dream window and all additional windows are
3050 // behind it.
3051 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003052 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003053
3054 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003055 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003056
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003057 // Used to indicate that we have processed the dream window and all additional attached
3058 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003059 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003060 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003061
3062 // Now perform layout of attached windows, which usually depend on the position of the
3063 // window they are attached to. XXX does not deal with windows that are attached to windows
3064 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003065 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003066
3067 // Window frames may have changed. Tell the input dispatcher about it.
Arthur Hung95b38a92018-07-20 18:56:12 +08003068 mInputMonitor.layoutInputConsumers(dw, dh);
3069 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003070 if (updateInputWindows) {
Arthur Hung95b38a92018-07-20 18:56:12 +08003071 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003072 }
3073
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003074 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
3075 }
3076
3077 /**
3078 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
3079 * In portrait mode, it grabs the full screenshot.
3080 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003081 * @param config of the output bitmap
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003082 */
chaviw0315a1a2018-03-05 15:28:35 -08003083 Bitmap screenshotDisplayLocked(Bitmap.Config config) {
3084 if (!mService.mPolicy.isScreenOn()) {
3085 if (DEBUG_SCREENSHOT) {
3086 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003087 }
chaviw0315a1a2018-03-05 15:28:35 -08003088 return null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003089 }
chaviwfbe47df2017-11-10 16:14:49 -08003090
chaviw0315a1a2018-03-05 15:28:35 -08003091 int dw = mDisplayInfo.logicalWidth;
3092 int dh = mDisplayInfo.logicalHeight;
chaviwfbe47df2017-11-10 16:14:49 -08003093
chaviw0315a1a2018-03-05 15:28:35 -08003094 if (dw <= 0 || dh <= 0) {
3095 return null;
3096 }
chaviwfbe47df2017-11-10 16:14:49 -08003097
chaviw0315a1a2018-03-05 15:28:35 -08003098 final Rect frame = new Rect(0, 0, dw, dh);
chaviwfbe47df2017-11-10 16:14:49 -08003099
chaviw0315a1a2018-03-05 15:28:35 -08003100 // The screenshot API does not apply the current screen rotation.
3101 int rot = mDisplay.getRotation();
chaviwfbe47df2017-11-10 16:14:49 -08003102
chaviw0315a1a2018-03-05 15:28:35 -08003103 if (rot == ROTATION_90 || rot == ROTATION_270) {
3104 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3105 }
chaviwfbe47df2017-11-10 16:14:49 -08003106
chaviw0315a1a2018-03-05 15:28:35 -08003107 // SurfaceFlinger is not aware of orientation, so convert our logical
3108 // crop to SurfaceFlinger's portrait orientation.
3109 convertCropForSurfaceFlinger(frame, rot, dw, dh);
3110
3111 final ScreenRotationAnimation screenRotationAnimation =
3112 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3113 final boolean inRotation = screenRotationAnimation != null &&
3114 screenRotationAnimation.isAnimating();
3115 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
3116
3117 // TODO(b/68392460): We should screenshot Task controls directly
3118 // but it's difficult at the moment as the Task doesn't have the
3119 // correct size set.
3120 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, 0, 1, inRotation, rot);
3121 if (bitmap == null) {
3122 Slog.w(TAG_WM, "Failed to take screenshot");
3123 return null;
3124 }
3125
3126 // Create a copy of the screenshot that is immutable and backed in ashmem.
3127 // This greatly reduces the overhead of passing the bitmap between processes.
3128 final Bitmap ret = bitmap.createAshmemBitmap(config);
3129 bitmap.recycle();
3130 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003131 }
3132
3133 // TODO: Can this use createRotationMatrix()?
3134 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3135 if (rot == Surface.ROTATION_90) {
3136 final int tmp = crop.top;
3137 crop.top = dw - crop.right;
3138 crop.right = crop.bottom;
3139 crop.bottom = dw - crop.left;
3140 crop.left = tmp;
3141 } else if (rot == Surface.ROTATION_180) {
3142 int tmp = crop.top;
3143 crop.top = dh - crop.bottom;
3144 crop.bottom = dh - tmp;
3145 tmp = crop.right;
3146 crop.right = dw - crop.left;
3147 crop.left = dw - tmp;
3148 } else if (rot == Surface.ROTATION_270) {
3149 final int tmp = crop.top;
3150 crop.top = crop.left;
3151 crop.left = dh - crop.bottom;
3152 crop.bottom = crop.right;
3153 crop.right = dh - tmp;
3154 }
3155 }
3156
3157 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003158 // Used to indicate the layout is needed.
3159 mTmpWindow = null;
3160
3161 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003162 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003163 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003164 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003165 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003166 w.setDisplayLayoutNeeded();
3167 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003168 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003169
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003170 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003171 mService.mWindowPlacerLocked.performSurfacePlacement();
3172 }
3173 }
3174
Wale Ogunwale1666e312016-12-16 11:27:18 -08003175 void setExitingTokensHasVisible(boolean hasVisible) {
3176 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3177 mExitingTokens.get(i).hasVisible = hasVisible;
3178 }
3179
3180 // Initialize state of exiting applications.
3181 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3182 }
3183
3184 void removeExistingTokensIfPossible() {
3185 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3186 final WindowToken token = mExitingTokens.get(i);
3187 if (!token.hasVisible) {
3188 mExitingTokens.remove(i);
3189 }
3190 }
3191
3192 // Time to remove any exiting applications?
3193 mTaskStackContainers.removeExistingAppTokensIfPossible();
3194 }
3195
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003196 @Override
3197 void onDescendantOverrideConfigurationChanged() {
3198 setLayoutNeeded();
3199 mService.requestTraversal();
3200 }
3201
David Stevens9440dc82017-03-16 19:00:20 -07003202 boolean okToDisplay() {
3203 if (mDisplayId == DEFAULT_DISPLAY) {
3204 return !mService.mDisplayFrozen
3205 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3206 }
3207 return mDisplayInfo.state == Display.STATE_ON;
3208 }
3209
3210 boolean okToAnimate() {
3211 return okToDisplay() &&
3212 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3213 }
3214
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003215 static final class TaskForResizePointSearchResult {
3216 boolean searchDone;
3217 Task taskForResize;
3218
3219 void reset() {
3220 searchDone = false;
3221 taskForResize = null;
3222 }
3223 }
Robert Carr3b716242016-08-16 16:02:21 -07003224
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003225 private static final class ApplySurfaceChangesTransactionState {
3226 boolean displayHasContent;
3227 boolean obscured;
3228 boolean syswin;
3229 boolean focusDisplayed;
3230 float preferredRefreshRate;
3231 int preferredModeId;
3232
3233 void reset() {
3234 displayHasContent = false;
3235 obscured = false;
3236 syswin = false;
3237 focusDisplayed = false;
3238 preferredRefreshRate = 0;
3239 preferredModeId = 0;
3240 }
3241 }
3242
3243 private static final class ScreenshotApplicationState {
3244 WindowState appWin;
3245 int maxLayer;
3246 int minLayer;
3247 boolean screenshotReady;
3248
3249 void reset(boolean screenshotReady) {
3250 appWin = null;
3251 maxLayer = 0;
3252 minLayer = 0;
3253 this.screenshotReady = screenshotReady;
3254 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3255 }
3256 }
3257
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003258 /**
3259 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3260 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3261 * homogeneous children type which is currently required by sub-classes of
3262 * {@link WindowContainer} class.
3263 */
3264 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3265
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003266 DisplayChildWindowContainer(WindowManagerService service) {
3267 super(service);
3268 }
3269
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003270 @Override
3271 boolean fillsParent() {
3272 return true;
3273 }
3274
3275 @Override
3276 boolean isVisible() {
3277 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003278 }
3279 }
3280
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003281 /**
3282 * Window container class that contains all containers on this display relating to Apps.
3283 * I.e Activities.
3284 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003285 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003286 /**
3287 * A control placed at the appropriate level for transitions to occur.
3288 */
chaviw23ee71c2017-12-18 11:29:41 -08003289 SurfaceControl mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02003290 SurfaceControl mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02003291 SurfaceControl mHomeAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003292
Robert Carrf7a7ca82017-12-06 13:17:07 -08003293 /**
3294 * Given that the split-screen divider does not have an AppWindowToken, it
3295 * will have to live inside of a "NonAppWindowContainer", in particular
3296 * {@link DisplayContent#mAboveAppWindowsContainers}. However, in visual Z order
3297 * it will need to be interleaved with some of our children, appearing on top of
3298 * both docked stacks but underneath any assistant stacks.
3299 *
3300 * To solve this problem we have this anchor control, which will always exist so
3301 * we can always assign it the correct value in our {@link #assignChildLayers}.
3302 * Likewise since it always exists, {@link AboveAppWindowContainers} can always
3303 * assign the divider a layer relative to it. This way we prevent linking lifecycle
3304 * events between the two containers.
3305 */
3306 SurfaceControl mSplitScreenDividerAnchor = null;
3307
Wale Ogunwale61911492017-10-11 08:50:50 -07003308 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3309 // through the list to find them.
3310 private TaskStack mHomeStack = null;
3311 private TaskStack mPinnedStack = null;
3312 private TaskStack mSplitScreenPrimaryStack = null;
3313
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003314 TaskStackContainers(WindowManagerService service) {
3315 super(service);
3316 }
3317
Wale Ogunwale61911492017-10-11 08:50:50 -07003318 /**
3319 * Returns the topmost stack on the display that is compatible with the input windowing mode
3320 * and activity type. Null is no compatible stack on the display.
3321 */
3322 TaskStack getStack(int windowingMode, int activityType) {
3323 if (activityType == ACTIVITY_TYPE_HOME) {
3324 return mHomeStack;
3325 }
3326 if (windowingMode == WINDOWING_MODE_PINNED) {
3327 return mPinnedStack;
3328 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3329 return mSplitScreenPrimaryStack;
3330 }
3331 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3332 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003333 if (activityType == ACTIVITY_TYPE_UNDEFINED
3334 && windowingMode == stack.getWindowingMode()) {
3335 // Passing in undefined type means we want to match the topmost stack with the
3336 // windowing mode.
3337 return stack;
3338 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003339 if (stack.isCompatible(windowingMode, activityType)) {
3340 return stack;
3341 }
3342 }
3343 return null;
3344 }
3345
3346 @VisibleForTesting
3347 TaskStack getTopStack() {
3348 return mTaskStackContainers.getChildCount() > 0
3349 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3350 }
3351
3352 TaskStack getHomeStack() {
3353 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3354 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3355 }
3356 return mHomeStack;
3357 }
3358
3359 TaskStack getPinnedStack() {
3360 return mPinnedStack;
3361 }
3362
Matthew Ng64e77cf2017-10-31 14:01:31 -07003363 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003364 return mSplitScreenPrimaryStack;
3365 }
3366
Winson Chunge2d72172018-01-25 17:46:20 +00003367 ArrayList<Task> getVisibleTasks() {
3368 final ArrayList<Task> visibleTasks = new ArrayList<>();
3369 forAllTasks(task -> {
3370 if (task.isVisible()) {
3371 visibleTasks.add(task);
3372 }
3373 });
3374 return visibleTasks;
3375 }
3376
Andrii Kulian839def92016-11-02 10:58:58 -07003377 /**
3378 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003379 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003380 */
3381 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003382 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003383 addChild(stack, onTop);
3384 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003385 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003386
Wale Ogunwale61911492017-10-11 08:50:50 -07003387 void onStackWindowingModeChanged(TaskStack stack) {
3388 removeStackReferenceIfNeeded(stack);
3389 addStackReferenceIfNeeded(stack);
3390 if (stack == mPinnedStack && getTopStack() != stack) {
3391 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3392 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3393 }
3394 }
3395
3396 private void addStackReferenceIfNeeded(TaskStack stack) {
3397 if (stack.isActivityTypeHome()) {
Louis Chang6322c422018-07-18 18:37:28 +08003398 // TODO(b/111363427) Rollback to throws exceptions once we figure out how to
3399 // properly deal with home type stack when external display removed
Wale Ogunwale61911492017-10-11 08:50:50 -07003400 if (mHomeStack != null) {
Louis Chang6322c422018-07-18 18:37:28 +08003401 // throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
3402 // + mHomeStack + " already exist on display=" + this + " stack=" + stack);
3403 Slog.e(TAG, "addStackReferenceIfNeeded: home stack="
Wale Ogunwale61911492017-10-11 08:50:50 -07003404 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
Louis Chang6322c422018-07-18 18:37:28 +08003405 } else {
3406 mHomeStack = stack;
Wale Ogunwale61911492017-10-11 08:50:50 -07003407 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003408 }
3409 final int windowingMode = stack.getWindowingMode();
3410 if (windowingMode == WINDOWING_MODE_PINNED) {
3411 if (mPinnedStack != null) {
3412 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3413 + mPinnedStack + " already exist on display=" + this
3414 + " stack=" + stack);
3415 }
3416 mPinnedStack = stack;
3417 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3418 if (mSplitScreenPrimaryStack != null) {
3419 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3420 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3421 + " already exist on display=" + this + " stack=" + stack);
3422 }
3423 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003424 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003425 }
3426 }
3427
3428 private void removeStackReferenceIfNeeded(TaskStack stack) {
3429 if (stack == mHomeStack) {
3430 mHomeStack = null;
3431 } else if (stack == mPinnedStack) {
3432 mPinnedStack = null;
3433 } else if (stack == mSplitScreenPrimaryStack) {
3434 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003435 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3436 mService.setDockedStackCreateStateLocked(
3437 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3438 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003439 }
Andrii Kulian839def92016-11-02 10:58:58 -07003440 }
3441
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003442 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003443 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3444 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003445 addChild(stack, addIndex);
3446 setLayoutNeeded();
3447 }
3448
Wale Ogunwale61911492017-10-11 08:50:50 -07003449 @Override
3450 protected void removeChild(TaskStack stack) {
3451 super.removeChild(stack);
3452 removeStackReferenceIfNeeded(stack);
3453 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003454
3455 @Override
3456 boolean isOnTop() {
3457 // Considered always on top
3458 return true;
3459 }
3460
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003461 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003462 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003463 if (child.getWindowConfiguration().isAlwaysOnTop()
3464 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003465 // This stack is always-on-top, override the default behavior.
3466 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3467
3468 // Moving to its current position, as we must call super but we don't want to
3469 // perform any meaningful action.
3470 final int currentPosition = mChildren.indexOf(child);
3471 super.positionChildAt(currentPosition, child, false /* includingParents */);
3472 return;
3473 }
3474
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003475 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3476 super.positionChildAt(targetPosition, child, includingParents);
3477
3478 setLayoutNeeded();
3479 }
3480
3481 /**
3482 * When stack is added or repositioned, find a proper position for it.
3483 * This will make sure that pinned stack always stays on top.
3484 * @param requestedPosition Position requested by caller.
3485 * @param stack Stack to be added or positioned.
3486 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3487 * @return The proper position for the stack.
3488 */
3489 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09003490 if (stack.inPinnedWindowingMode()) {
Kazuki Takisef85197b2018-06-18 18:18:36 +09003491 return POSITION_TOP;
Kazuki Takise148d00a2018-05-31 15:32:19 +09003492 }
3493
Kazuki Takisef85197b2018-06-18 18:18:36 +09003494 final int topChildPosition = mChildren.size() - 1;
3495 int belowAlwaysOnTopPosition = POSITION_BOTTOM;
3496 for (int i = topChildPosition; i >= 0; --i) {
3497 if (getStacks().get(i) != stack && !getStacks().get(i).isAlwaysOnTop()) {
3498 belowAlwaysOnTopPosition = i;
3499 break;
3500 }
3501 }
3502
3503 // The max possible position we can insert the stack at.
3504 int maxPosition = POSITION_TOP;
3505 // The min possible position we can insert the stack at.
3506 int minPosition = POSITION_BOTTOM;
3507
3508 if (stack.isAlwaysOnTop()) {
3509 if (hasPinnedStack()) {
3510 // Always-on-top stacks go below the pinned stack.
3511 maxPosition = getStacks().indexOf(mPinnedStack) - 1;
3512 }
3513 // Always-on-top stacks need to be above all other stacks.
3514 minPosition = belowAlwaysOnTopPosition !=
3515 POSITION_BOTTOM ? belowAlwaysOnTopPosition : topChildPosition;
3516 } else {
3517 // Other stacks need to be below the always-on-top stacks.
3518 maxPosition = belowAlwaysOnTopPosition !=
Arthur Hung928d8dc2018-07-18 15:38:05 +08003519 POSITION_BOTTOM ? belowAlwaysOnTopPosition : 0;
Kazuki Takisef85197b2018-06-18 18:18:36 +09003520 }
3521
3522 int targetPosition = requestedPosition;
3523 targetPosition = Math.min(targetPosition, maxPosition);
3524 targetPosition = Math.max(targetPosition, minPosition);
3525
3526 int prevPosition = getStacks().indexOf(stack);
3527 // The positions we calculated above (maxPosition, minPosition) do not take into
3528 // consideration the following edge cases.
3529 // 1) We need to adjust the position depending on the value "adding".
3530 // 2) When we are moving a stack to another position, we also need to adjust the
3531 // position depending on whether the stack is moving to a higher or lower position.
3532 if ((targetPosition != requestedPosition) &&
3533 (adding || targetPosition < prevPosition)) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09003534 targetPosition++;
3535 }
3536
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003537 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003538 }
3539
3540 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003541 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3542 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003543 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003544 if (super.forAllWindows(callback, traverseTopToBottom)) {
3545 return true;
3546 }
3547 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3548 return true;
3549 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003550 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003551 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3552 return true;
3553 }
3554 if (super.forAllWindows(callback, traverseTopToBottom)) {
3555 return true;
3556 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003557 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003558 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003559 }
3560
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003561 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003562 boolean traverseTopToBottom) {
3563 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3564 // app tokens.
3565 // TODO: Investigate if we need to continue to do this or if we can just process them
3566 // in-order.
3567 if (traverseTopToBottom) {
3568 for (int i = mChildren.size() - 1; i >= 0; --i) {
3569 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3570 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003571 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3572 traverseTopToBottom)) {
3573 return true;
3574 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003575 }
3576 }
3577 } else {
3578 final int count = mChildren.size();
3579 for (int i = 0; i < count; ++i) {
3580 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3581 final int appTokensCount = appTokens.size();
3582 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003583 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3584 traverseTopToBottom)) {
3585 return true;
3586 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003587 }
3588 }
3589 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003590 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003591 }
3592
Wale Ogunwale1666e312016-12-16 11:27:18 -08003593 void setExitingTokensHasVisible(boolean hasVisible) {
3594 for (int i = mChildren.size() - 1; i >= 0; --i) {
3595 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3596 for (int j = appTokens.size() - 1; j >= 0; --j) {
3597 appTokens.get(j).hasVisible = hasVisible;
3598 }
3599 }
3600 }
3601
3602 void removeExistingAppTokensIfPossible() {
3603 for (int i = mChildren.size() - 1; i >= 0; --i) {
3604 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3605 for (int j = appTokens.size() - 1; j >= 0; --j) {
3606 final AppWindowToken token = appTokens.get(j);
3607 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3608 && (!token.mIsExiting || token.isEmpty())) {
3609 // Make sure there is no animation running on this token, so any windows
3610 // associated with it will be removed as soon as their animations are
3611 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003612 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003613 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3614 "performLayout: App token exiting now removed" + token);
3615 token.removeIfPossible();
3616 }
3617 }
3618 }
3619 }
3620
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003621 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003622 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003623 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
3624 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003625 // Apps and their containers are not allowed to specify an orientation while the
Wale Ogunwaled0c225c2018-06-15 14:47:46 -07003626 // docked or freeform stack is visible...except for the home stack if the docked
3627 // stack is minimized and it actually set something and the bounds is different from
3628 // the display.
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003629 if (mHomeStack != null && mHomeStack.isVisible()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07003630 && mDividerControllerLocked.isMinimizedDock()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07003631 && !(mDividerControllerLocked.isHomeStackResizable()
3632 && mHomeStack.matchParentBounds())) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003633 final int orientation = mHomeStack.getOrientation();
3634 if (orientation != SCREEN_ORIENTATION_UNSET) {
3635 return orientation;
3636 }
3637 }
3638 return SCREEN_ORIENTATION_UNSPECIFIED;
3639 }
3640
3641 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07003642 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
3643 PackageManager.FEATURE_AUTOMOTIVE);
3644 if (isCar) {
3645 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
3646 // allow anything but the default orientation.
3647 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07003648 "Forcing UNSPECIFIED orientation in car for display id=" + mDisplayId
3649 + ". Ignoring " + orientation);
Dean Harding3e5a1522017-10-06 09:19:01 -07003650 return SCREEN_ORIENTATION_UNSPECIFIED;
3651 }
3652
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003653 if (orientation != SCREEN_ORIENTATION_UNSET
3654 && orientation != SCREEN_ORIENTATION_BEHIND) {
3655 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07003656 "App is requesting an orientation, return " + orientation
3657 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003658 return orientation;
3659 }
3660
3661 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07003662 "No app is requesting an orientation, return " + mLastOrientation
3663 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003664 // The next app has not been requested to be visible, so we keep the current orientation
3665 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003666 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003667 }
Robert Carrb1579c82017-09-05 14:54:47 -07003668
3669 @Override
3670 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carr2f8aa392018-01-31 14:46:51 -08003671 assignStackOrdering(t);
Robert Carrf7a7ca82017-12-06 13:17:07 -08003672
Robert Carr2f8aa392018-01-31 14:46:51 -08003673 for (int i = 0; i < mChildren.size(); i++) {
3674 final TaskStack s = mChildren.get(i);
3675 s.assignChildLayers(t);
3676 }
3677 }
3678
3679 void assignStackOrdering(SurfaceControl.Transaction t) {
Jorim Jaggibe418292018-03-26 16:14:12 +02003680
Robert Carrf7a7ca82017-12-06 13:17:07 -08003681 final int HOME_STACK_STATE = 0;
3682 final int NORMAL_STACK_STATE = 1;
3683 final int ALWAYS_ON_TOP_STATE = 2;
3684
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003685 int layer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08003686 int layerForAnimationLayer = 0;
Jorim Jaggibe418292018-03-26 16:14:12 +02003687 int layerForBoostedAnimationLayer = 0;
Jorim Jaggi391790622018-04-18 15:30:44 +02003688 int layerForHomeAnimationLayer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08003689
Robert Carrf7a7ca82017-12-06 13:17:07 -08003690 for (int state = 0; state <= ALWAYS_ON_TOP_STATE; state++) {
3691 for (int i = 0; i < mChildren.size(); i++) {
3692 final TaskStack s = mChildren.get(i);
Robert Carr2f8aa392018-01-31 14:46:51 -08003693 if (state == HOME_STACK_STATE && !s.isActivityTypeHome()) {
3694 continue;
3695 } else if (state == NORMAL_STACK_STATE && (s.isActivityTypeHome()
3696 || s.isAlwaysOnTop())) {
3697 continue;
3698 } else if (state == ALWAYS_ON_TOP_STATE && !s.isAlwaysOnTop()) {
3699 continue;
3700 }
3701 s.assignLayer(t, layer++);
3702 if (s.inSplitScreenWindowingMode() && mSplitScreenDividerAnchor != null) {
3703 t.setLayer(mSplitScreenDividerAnchor, layer++);
3704 }
Winson Chungd41f71d2018-03-16 15:26:07 -07003705 if ((s.isTaskAnimating() || s.isAppAnimating())
3706 && state != ALWAYS_ON_TOP_STATE) {
Robert Carr2f8aa392018-01-31 14:46:51 -08003707 // Ensure the animation layer ends up above the
3708 // highest animating stack and no higher.
3709 layerForAnimationLayer = layer++;
Robert Carrf7a7ca82017-12-06 13:17:07 -08003710 }
Jorim Jaggibe418292018-03-26 16:14:12 +02003711 if (state != ALWAYS_ON_TOP_STATE) {
3712 layerForBoostedAnimationLayer = layer++;
3713 }
Robert Carrf7a7ca82017-12-06 13:17:07 -08003714 }
Jorim Jaggi391790622018-04-18 15:30:44 +02003715 if (state == HOME_STACK_STATE) {
3716 layerForHomeAnimationLayer = layer++;
3717 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003718 }
Robert Carr2f8aa392018-01-31 14:46:51 -08003719 if (mAppAnimationLayer != null) {
3720 t.setLayer(mAppAnimationLayer, layerForAnimationLayer);
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003721 }
Jorim Jaggibe418292018-03-26 16:14:12 +02003722 if (mBoostedAppAnimationLayer != null) {
3723 t.setLayer(mBoostedAppAnimationLayer, layerForBoostedAnimationLayer);
3724 }
Jorim Jaggi391790622018-04-18 15:30:44 +02003725 if (mHomeAppAnimationLayer != null) {
3726 t.setLayer(mHomeAppAnimationLayer, layerForHomeAnimationLayer);
3727 }
Robert Carrb1579c82017-09-05 14:54:47 -07003728 }
3729
3730 @Override
Jorim Jaggi391790622018-04-18 15:30:44 +02003731 SurfaceControl getAppAnimationLayer(@AnimationLayer int animationLayer) {
3732 switch (animationLayer) {
3733 case ANIMATION_LAYER_BOOSTED:
3734 return mBoostedAppAnimationLayer;
3735 case ANIMATION_LAYER_HOME:
3736 return mHomeAppAnimationLayer;
3737 case ANIMATION_LAYER_STANDARD:
3738 default:
3739 return mAppAnimationLayer;
3740 }
chaviw23ee71c2017-12-18 11:29:41 -08003741 }
3742
Robert Carrf7a7ca82017-12-06 13:17:07 -08003743 SurfaceControl getSplitScreenDividerAnchor() {
3744 return mSplitScreenDividerAnchor;
3745 }
3746
chaviw23ee71c2017-12-18 11:29:41 -08003747 @Override
Robert Carrb1579c82017-09-05 14:54:47 -07003748 void onParentSet() {
3749 super.onParentSet();
3750 if (getParent() != null) {
chaviw23ee71c2017-12-18 11:29:41 -08003751 mAppAnimationLayer = makeChildSurface(null)
3752 .setName("animationLayer")
3753 .build();
Jorim Jaggibe418292018-03-26 16:14:12 +02003754 mBoostedAppAnimationLayer = makeChildSurface(null)
3755 .setName("boostedAnimationLayer")
3756 .build();
Jorim Jaggi391790622018-04-18 15:30:44 +02003757 mHomeAppAnimationLayer = makeChildSurface(null)
3758 .setName("homeAnimationLayer")
3759 .build();
Robert Carrf7a7ca82017-12-06 13:17:07 -08003760 mSplitScreenDividerAnchor = makeChildSurface(null)
3761 .setName("splitScreenDividerAnchor")
3762 .build();
3763 getPendingTransaction()
3764 .show(mAppAnimationLayer)
Jorim Jaggibe418292018-03-26 16:14:12 +02003765 .show(mBoostedAppAnimationLayer)
Jorim Jaggi391790622018-04-18 15:30:44 +02003766 .show(mHomeAppAnimationLayer)
Robert Carrf7a7ca82017-12-06 13:17:07 -08003767 .show(mSplitScreenDividerAnchor);
chaviw23ee71c2017-12-18 11:29:41 -08003768 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07003769 } else {
chaviw23ee71c2017-12-18 11:29:41 -08003770 mAppAnimationLayer.destroy();
3771 mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02003772 mBoostedAppAnimationLayer.destroy();
3773 mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02003774 mHomeAppAnimationLayer.destroy();
3775 mHomeAppAnimationLayer = null;
Robert Carrf7a7ca82017-12-06 13:17:07 -08003776 mSplitScreenDividerAnchor.destroy();
3777 mSplitScreenDividerAnchor = null;
Robert Carrb1579c82017-09-05 14:54:47 -07003778 }
3779 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003780 }
3781
Robert Carree4d4b92017-11-22 12:21:46 -08003782 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003783 AboveAppWindowContainers(String name, WindowManagerService service) {
3784 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08003785 }
3786
Robert Carrb9506032018-02-13 13:54:00 -08003787 @Override
Vishnu Nair83537a72018-07-19 21:27:48 -07003788 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
3789 final SurfaceControl.Builder builder = super.makeChildSurface(child);
3790 if (child instanceof WindowToken && ((WindowToken) child).mRoundedCornerOverlay) {
3791 // To draw above the ColorFade layer during the screen off transition, the
3792 // rounded corner overlays need to be at the root of the surface hierarchy.
3793 // TODO: move the ColorLayer into the display overlay layer such that this is not
3794 // necessary anymore.
3795 builder.setParent(null);
3796 }
3797 return builder;
3798 }
3799
3800 @Override
Robert Carrb9506032018-02-13 13:54:00 -08003801 void assignChildLayers(SurfaceControl.Transaction t) {
3802 assignChildLayers(t, null /* imeContainer */);
3803 }
3804
Robert Carree4d4b92017-11-22 12:21:46 -08003805 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
3806 boolean needAssignIme = imeContainer != null
3807 && imeContainer.getSurfaceControl() != null;
3808 for (int j = 0; j < mChildren.size(); ++j) {
3809 final WindowToken wt = mChildren.get(j);
Robert Carrf7a7ca82017-12-06 13:17:07 -08003810
3811 // See {@link mSplitScreenDividerAnchor}
3812 if (wt.windowType == TYPE_DOCK_DIVIDER) {
3813 wt.assignRelativeLayer(t, mTaskStackContainers.getSplitScreenDividerAnchor(), 1);
3814 continue;
3815 }
Vishnu Nair83537a72018-07-19 21:27:48 -07003816 if (wt.mRoundedCornerOverlay) {
3817 wt.assignLayer(t, WindowManagerPolicy.COLOR_FADE_LAYER + 1);
3818 continue;
3819 }
Robert Carree4d4b92017-11-22 12:21:46 -08003820 wt.assignLayer(t, j);
3821 wt.assignChildLayers(t);
3822
3823 int layer = mService.mPolicy.getWindowLayerFromTypeLw(
3824 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08003825
3826 if (needAssignIme && layer >= mService.mPolicy.getWindowLayerFromTypeLw(
3827 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003828 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08003829 needAssignIme = false;
3830 }
3831 }
3832 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003833 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08003834 }
3835 }
3836 }
3837
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003838 /**
3839 * Window container class that contains all containers on this display that are not related to
3840 * Apps. E.g. status bar.
3841 */
Robert Carree4d4b92017-11-22 12:21:46 -08003842 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07003843 /**
3844 * Compares two child window tokens returns -1 if the first is lesser than the second in
3845 * terms of z-order and 1 otherwise.
3846 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003847 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003848 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003849 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3850 token1.mOwnerCanManageAppTokens)
3851 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3852 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003853
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003854 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3855 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3856 return false;
3857 }
3858 final int req = w.mAttrs.screenOrientation;
3859 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3860 || req == SCREEN_ORIENTATION_UNSET) {
3861 return false;
3862 }
3863 return true;
3864 };
3865
Wale Ogunwale3a931692016-11-02 16:49:48 -07003866 private final String mName;
chaviwf29223e2018-01-31 13:23:51 -08003867 private final Dimmer mDimmer = new Dimmer(this);
3868 private final Rect mTmpDimBoundsRect = new Rect();
3869
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003870 NonAppWindowContainers(String name, WindowManagerService service) {
3871 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003872 mName = name;
3873 }
3874
3875 void addChild(WindowToken token) {
3876 addChild(token, mWindowComparator);
3877 }
3878
3879 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003880 int getOrientation() {
3881 final WindowManagerPolicy policy = mService.mPolicy;
3882 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003883 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003884
3885 if (win != null) {
3886 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003887 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003888 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003889 if (mService.mKeyguardGoingAway) {
3890 // Keyguard can't affect the orientation if it is going away...
3891 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3892 return SCREEN_ORIENTATION_UNSET;
3893 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003894 }
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07003895 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req
3896 + " for display id=" + mDisplayId);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003897 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003898 }
3899
Andrii Kulian8ee72852017-03-10 10:36:45 -08003900 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003901
Jorim Jaggi1c530592018-04-06 15:11:47 +02003902 // Only allow force setting the orientation when all unknown visibilities have been
3903 // resolved, as otherwise we just may be starting another occluding activity.
3904 final boolean isUnoccluding =
3905 mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
3906 && mService.mUnknownAppVisibilityController.allResolved();
3907 if (policy.isKeyguardShowingAndNotOccluded() || isUnoccluding) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003908 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003909 }
3910
3911 return SCREEN_ORIENTATION_UNSET;
3912 }
3913
3914 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003915 String getName() {
3916 return mName;
3917 }
chaviwf29223e2018-01-31 13:23:51 -08003918
3919 @Override
3920 Dimmer getDimmer() {
3921 return mDimmer;
3922 }
3923
3924 @Override
3925 void prepareSurfaces() {
3926 mDimmer.resetDimStates();
3927 super.prepareSurfaces();
3928 getBounds(mTmpDimBoundsRect);
3929
3930 if (mDimmer.updateDims(getPendingTransaction(), mTmpDimBoundsRect)) {
3931 scheduleAnimation();
3932 }
3933 }
Robert Carr3b716242016-08-16 16:02:21 -07003934 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003935
Robert Carr9034d962018-01-04 18:27:42 -08003936 private class NonMagnifiableWindowContainers extends NonAppWindowContainers {
3937 NonMagnifiableWindowContainers(String name, WindowManagerService service) {
3938 super(name, service);
3939 }
3940
3941 @Override
3942 void applyMagnificationSpec(Transaction t, MagnificationSpec spec) {
3943 }
3944 };
3945
Robert Carrb1579c82017-09-05 14:54:47 -07003946 SurfaceControl.Builder makeSurface(SurfaceSession s) {
3947 return mService.makeSurfaceBuilder(s)
3948 .setParent(mWindowingLayer);
3949 }
3950
3951 @Override
3952 SurfaceSession getSession() {
3953 return mSession;
3954 }
3955
3956 @Override
3957 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003958 SurfaceSession s = child != null ? child.getSession() : getSession();
3959 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrb1579c82017-09-05 14:54:47 -07003960 b.setSize(mSurfaceSize, mSurfaceSize);
Robert Carrf59b8dd2017-10-02 18:58:36 -07003961
3962 if (child == null) {
3963 return b;
3964 }
3965
Robert Carree4d4b92017-11-22 12:21:46 -08003966 return b.setName(child.getName())
3967 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07003968 }
3969
3970 /**
3971 * The makeSurface variants are for use by the window-container
3972 * hierarchy. makeOverlay here is a function for various non windowing
3973 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
3974 * and other potpourii.
3975 */
3976 SurfaceControl.Builder makeOverlay() {
3977 return mService.makeSurfaceBuilder(mSession)
3978 .setParent(mOverlayLayer);
3979 }
3980
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09003981 /**
3982 * Reparents the given surface to mOverlayLayer.
3983 */
3984 void reparentToOverlay(Transaction transaction, SurfaceControl surface) {
3985 transaction.reparent(surface, mOverlayLayer.getHandle());
3986 }
3987
Robert Carrb1579c82017-09-05 14:54:47 -07003988 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carr24be9ab2018-04-30 17:54:53 -07003989 if (spec.scale != 1.0) {
3990 mMagnificationSpec = spec;
3991 } else {
3992 mMagnificationSpec = null;
3993 }
3994
Robert Carrf59b8dd2017-10-02 18:58:36 -07003995 applyMagnificationSpec(getPendingTransaction(), spec);
3996 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07003997 }
3998
Robert Carr24be9ab2018-04-30 17:54:53 -07003999 void reapplyMagnificationSpec() {
4000 if (mMagnificationSpec != null) {
4001 applyMagnificationSpec(getPendingTransaction(), mMagnificationSpec);
4002 }
4003 }
4004
Robert Carrb1579c82017-09-05 14:54:47 -07004005 @Override
4006 void onParentSet() {
4007 // Since we are the top of the SurfaceControl hierarchy here
4008 // we create the root surfaces explicitly rather than chaining
4009 // up as the default implementation in onParentSet does. So we
4010 // explicitly do NOT call super here.
4011 }
4012
4013 @Override
4014 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07004015
4016 // These are layers as children of "mWindowingLayer"
4017 mBelowAppWindowsContainers.assignLayer(t, 0);
4018 mTaskStackContainers.assignLayer(t, 1);
4019 mAboveAppWindowsContainers.assignLayer(t, 2);
4020
4021 WindowState imeTarget = mService.mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08004022 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08004023
Robert Carree4d4b92017-11-22 12:21:46 -08004024 // In the case where we have an IME target that is not in split-screen
4025 // mode IME assignment is easy. We just need the IME to go directly above
4026 // the target. This way children of the target will naturally go above the IME
4027 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08004028 //
Robert Carree4d4b92017-11-22 12:21:46 -08004029 // In the case of split-screen windowing mode, we need to elevate the IME above the
4030 // docked divider while keeping the app itself below the docked divider, so instead
4031 // we use relative layering of the IME targets child windows, and place the
4032 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08004033 //
Robert Carr234b6332018-03-20 13:38:16 -07004034 // In the case the IME target is animating, the animation Z order may be different
4035 // than the WindowContainer Z order, so it's difficult to be sure we have the correct
4036 // IME target. In this case we just layer the IME over all transitions by placing it in the
4037 // above applications layer.
4038 //
Robert Carree4d4b92017-11-22 12:21:46 -08004039 // In the case where we have no IME target we assign it where it's base layer would
4040 // place it in the AboveAppWindowContainers.
Robert Carr234b6332018-03-20 13:38:16 -07004041 if (imeTarget != null && !(imeTarget.inSplitScreenWindowingMode()
4042 || imeTarget.mToken.isAppAnimating())
Robert Carree4d4b92017-11-22 12:21:46 -08004043 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004044 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07004045 // TODO: We need to use an extra level on the app surface to ensure
4046 // this is always above SurfaceView but always below attached window.
4047 1);
Robert Carree4d4b92017-11-22 12:21:46 -08004048 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07004049 }
4050
4051 // Above we have assigned layers to our children, now we ask them to assign
4052 // layers to their children.
4053 mBelowAppWindowsContainers.assignChildLayers(t);
4054 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08004055 mAboveAppWindowsContainers.assignChildLayers(t,
4056 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07004057 mImeWindowsContainers.assignChildLayers(t);
4058 }
4059
4060 /**
4061 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
4062 * that the IME target is one of the docked applications. We'd like the docked divider to be
4063 * above both of the applications, and we'd like the IME to be above the docked divider.
4064 * However we need child windows of the applications to be above the IME (Text drag handles).
4065 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
4066 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
4067 * with {@link #WindowState#assignLayer}
4068 */
4069 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
Robert Carrbb5de662018-04-26 13:29:51 -07004070 child.assignRelativeLayer(t, mImeWindowsContainers.getSurfaceControl(), 1);
Robert Carrb1579c82017-09-05 14:54:47 -07004071 }
4072
4073 @Override
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004074 void prepareSurfaces() {
4075 final ScreenRotationAnimation screenRotationAnimation =
4076 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
4077 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
4078 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
4079 mPendingTransaction.setMatrix(mWindowingLayer,
4080 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
4081 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
4082 mPendingTransaction.setPosition(mWindowingLayer,
4083 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
4084 mPendingTransaction.setAlpha(mWindowingLayer,
4085 screenRotationAnimation.getEnterTransformation().getAlpha());
4086 }
Chavi Weingartenb736e322018-02-23 00:27:54 +00004087
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004088 super.prepareSurfaces();
4089 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004090
Jorim Jaggibe418292018-03-26 16:14:12 +02004091 void assignStackOrdering() {
4092 mTaskStackContainers.assignStackOrdering(getPendingTransaction());
Robert Carr2f8aa392018-01-31 14:46:51 -08004093 }
Chavi Weingarten3a748552018-05-14 17:32:42 +00004094
4095 /**
4096 * Increment the deferral count to determine whether to update the IME target.
4097 */
4098 void deferUpdateImeTarget() {
4099 mDeferUpdateImeTargetCount++;
4100 }
4101
4102 /**
4103 * Decrement the deferral count to determine whether to update the IME target. If the count
4104 * reaches 0, a new ime target will get computed.
4105 */
4106 void continueUpdateImeTarget() {
4107 if (mDeferUpdateImeTargetCount == 0) {
4108 return;
4109 }
4110
4111 mDeferUpdateImeTargetCount--;
4112 if (mDeferUpdateImeTargetCount == 0) {
4113 computeImeTarget(true /* updateImeTarget */);
4114 }
4115 }
4116
4117 /**
4118 * @return Whether a new IME target should be computed.
4119 */
4120 private boolean canUpdateImeTarget() {
4121 return mDeferUpdateImeTargetCount == 0;
4122 }
Arthur Hung95b38a92018-07-20 18:56:12 +08004123
4124 InputMonitor getInputMonitor() {
4125 return mInputMonitor;
4126 }
Craig Mautner59c00972012-07-30 12:10:24 -07004127}