blob: 7d1c020d85f8e2fd6906c828583480f1dd56918f [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
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800404 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
405 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800406 final AppWindowToken atoken = w.mAppToken;
407 if (winAnimator.mDrawState == READY_TO_SHOW) {
Jorim Jaggiaf0d6d22018-06-08 15:25:35 +0200408 if (atoken == null || atoken.canShowWindows()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800409 if (w.performShowLocked()) {
410 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
411 if (DEBUG_LAYOUT_REPEATS) {
412 mService.mWindowPlacerLocked.debugLayoutRepeats(
413 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
414 }
415 }
416 }
417 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800418 };
419
420 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
421 final WindowStateAnimator winAnimator = w.mWinAnimator;
422 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
423 return;
424 }
425
426 final int flags = w.mAttrs.flags;
427
428 // If this window is animating, make a note that we have an animating window and take
429 // care of a request to run a detached wallpaper animation.
Jorim Jaggia5e10572017-11-15 14:36:26 +0100430 if (winAnimator.isAnimationSet()) {
431 final AnimationAdapter anim = w.getAnimation();
432 if (anim != null) {
433 if ((flags & FLAG_SHOW_WALLPAPER) != 0 && anim.getDetachWallpaper()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800434 mTmpWindow = w;
435 }
Jorim Jaggia5e10572017-11-15 14:36:26 +0100436 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800437 if (color != 0) {
438 final TaskStack stack = w.getStack();
439 if (stack != null) {
440 stack.setAnimationBackground(winAnimator, color);
441 }
442 }
443 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800444 }
445
446 // If this window's app token is running a detached wallpaper animation, make a note so
447 // we can ensure the wallpaper is displayed behind it.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200448 final AppWindowToken atoken = winAnimator.mWin.mAppToken;
449 final AnimationAdapter animation = atoken != null ? atoken.getAnimation() : null;
450 if (animation != null) {
451 if ((flags & FLAG_SHOW_WALLPAPER) != 0 && animation.getDetachWallpaper()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800452 mTmpWindow = w;
453 }
454
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200455 final int color = animation.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800456 if (color != 0) {
457 final TaskStack stack = w.getStack();
458 if (stack != null) {
459 stack.setAnimationBackground(winAnimator, color);
460 }
461 }
462 }
463 };
464
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800465 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
466 final int lostFocusUid = mTmpWindow.mOwnerUid;
467 final Handler handler = mService.mH;
468 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
469 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
470 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
471 w.mAttrs.hideTimeoutMilliseconds);
472 }
473 }
474 };
475
476 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
477 final AppWindowToken focusedApp = mService.mFocusedApp;
478 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
479 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
480
481 if (!w.canReceiveKeys()) {
482 return false;
483 }
484
485 final AppWindowToken wtoken = w.mAppToken;
486
487 // If this window's application has been removed, just skip it.
488 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
489 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
490 + (wtoken.removed ? "removed" : "sendingToBottom"));
491 return false;
492 }
493
494 if (focusedApp == null) {
495 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
496 + " using new focus @ " + w);
497 mTmpWindow = w;
498 return true;
499 }
500
501 if (!focusedApp.windowsAreFocusable()) {
502 // Current focused app windows aren't focusable...
503 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
504 + " focusable using new focus @ " + w);
505 mTmpWindow = w;
506 return true;
507 }
508
509 // Descend through all of the app tokens and find the first that either matches
510 // win.mAppToken (return win) or mFocusedApp (return null).
511 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
512 if (focusedApp.compareTo(wtoken) > 0) {
513 // App stack below focused app stack. No focus for you!!!
514 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
515 "findFocusedWindow: Reached focused app=" + focusedApp);
516 mTmpWindow = null;
517 return true;
518 }
519 }
520
521 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
522 mTmpWindow = w;
523 return true;
524 };
525
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800526 private final Consumer<WindowState> mPerformLayout = w -> {
527 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
528 // wasting time and funky changes while a window is animating away.
529 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
530 || w.isGoneForLayoutLw();
531
532 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
533 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
534 + " mLayoutAttached=" + w.mLayoutAttached
535 + " screen changed=" + w.isConfigChanged());
536 final AppWindowToken atoken = w.mAppToken;
537 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200538 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800539 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
540 + " parentHidden=" + w.isParentWindowHidden());
541 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200542 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800543 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
544 + " parentHidden=" + w.isParentWindowHidden());
545 }
546
547 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
548 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
549 // since that means "perform layout as normal, just don't display").
550 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
551 || ((w.isConfigChanged() || w.setReportResizeHints())
552 && !w.isGoneForLayoutLw() &&
553 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
554 (w.mHasSurface && w.mAppToken != null &&
555 w.mAppToken.layoutConfigChanges)))) {
556 if (!w.mLayoutAttached) {
557 if (mTmpInitial) {
558 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
559 w.mContentChanged = false;
560 }
561 if (w.mAttrs.type == TYPE_DREAM) {
562 // Don't layout windows behind a dream, so that if it does stuff like hide
563 // the status bar we won't get a bad transition when it goes away.
564 mTmpWindow = w;
565 }
566 w.mLayoutNeeded = false;
567 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200568 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000569 mService.mPolicy.layoutWindowLw(w, null, mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100570 w.mLayoutSeq = mLayoutSeq;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800571
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200572 // If this is the first layout, we need to initialize the last inset values as
573 // otherwise we'd immediately cause an unnecessary resize.
574 if (firstLayout) {
575 w.updateLastInsetValues();
576 }
577
Adrian Roos23df3a32018-03-15 15:41:13 +0100578 if (w.mAppToken != null) {
579 w.mAppToken.layoutLetterbox(w);
580 }
581
chaviw492139a2018-07-16 16:07:35 -0700582 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700583 + " mContainingFrame=" + w.getContainingFrame()
584 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800585 }
586 }
587 };
588
589 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
590 if (w.mLayoutAttached) {
591 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
592 + " mViewVisibility=" + w.mViewVisibility
593 + " mRelayoutCalled=" + w.mRelayoutCalled);
594 // If this view is GONE, then skip it -- keep the current frame, and let the caller
595 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
596 // windows, since that means "perform layout as normal, just don't display").
597 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
598 return;
599 }
600 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
601 || w.mLayoutNeeded) {
602 if (mTmpInitial) {
603 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
604 w.mContentChanged = false;
605 }
606 w.mLayoutNeeded = false;
607 w.prelayout();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000608 mService.mPolicy.layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100609 w.mLayoutSeq = mLayoutSeq;
chaviw492139a2018-07-16 16:07:35 -0700610 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700611 + " mContainingFrame=" + w.getContainingFrame()
612 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800613 }
614 } else if (w.mAttrs.type == TYPE_DREAM) {
615 // Don't layout windows behind a dream, so that if it does stuff like hide the
616 // status bar we won't get a bad transition when it goes away.
617 mTmpWindow = mTmpWindow2;
618 }
619 };
620
621 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
622 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
623 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
624 return w.canBeImeTarget();
625 };
626
627 private final Consumer<WindowState> mApplyPostLayoutPolicy =
628 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
629 mService.mInputMethodTarget);
630
631 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
632 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
633 final boolean obscuredChanged = w.mObscured !=
634 mTmpApplySurfaceChangesTransactionState.obscured;
635 final RootWindowContainer root = mService.mRoot;
636 // Only used if default window
637 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
638
639 // Update effect.
640 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
641 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
642 final boolean isDisplayed = w.isDisplayedLw();
643
644 if (isDisplayed && w.isObscuringDisplay()) {
645 // This window completely covers everything behind it, so we want to leave all
646 // of them as undimmed (for performance reasons).
647 root.mObscuringWindow = w;
648 mTmpApplySurfaceChangesTransactionState.obscured = true;
649 }
650
651 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
652 root.handleNotObscuredLocked(w,
653 mTmpApplySurfaceChangesTransactionState.obscured,
654 mTmpApplySurfaceChangesTransactionState.syswin);
655
656 if (w.mHasSurface && isDisplayed) {
657 final int type = w.mAttrs.type;
658 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
659 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
660 mTmpApplySurfaceChangesTransactionState.syswin = true;
661 }
662 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
663 && w.mAttrs.preferredRefreshRate != 0) {
664 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
665 = w.mAttrs.preferredRefreshRate;
666 }
667 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
668 && w.mAttrs.preferredDisplayModeId != 0) {
669 mTmpApplySurfaceChangesTransactionState.preferredModeId
670 = w.mAttrs.preferredDisplayModeId;
671 }
672 }
673 }
674
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800675 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
676 && mWallpaperController.isWallpaperTarget(w)) {
677 // This is the wallpaper target and its obscured state changed... make sure the
678 // current wallpaper's visibility has been updated accordingly.
679 mWallpaperController.updateWallpaperVisibility();
680 }
681
chaviw161ea3e2018-01-31 12:01:12 -0800682 w.handleWindowMovedIfNeeded();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800683
684 final WindowStateAnimator winAnimator = w.mWinAnimator;
685
686 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
687 w.mContentChanged = false;
688
689 // Moved from updateWindowsAndWallpaperLocked().
690 if (w.mHasSurface) {
691 // Take care of the window being ready to display.
692 final boolean committed = winAnimator.commitFinishDrawingLocked();
693 if (isDefaultDisplay && committed) {
694 if (w.mAttrs.type == TYPE_DREAM) {
695 // HACK: When a dream is shown, it may at that point hide the lock screen.
696 // So we need to redo the layout to let the phone window manager make this
697 // happen.
698 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
699 if (DEBUG_LAYOUT_REPEATS) {
700 surfacePlacer.debugLayoutRepeats(
701 "dream and commitFinishDrawingLocked true",
702 pendingLayoutChanges);
703 }
704 }
705 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
706 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
707 "First draw done in potential wallpaper target " + w);
708 root.mWallpaperMayChange = true;
709 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
710 if (DEBUG_LAYOUT_REPEATS) {
711 surfacePlacer.debugLayoutRepeats(
712 "wallpaper and commitFinishDrawingLocked true",
713 pendingLayoutChanges);
714 }
715 }
716 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800717 }
718
719 final AppWindowToken atoken = w.mAppToken;
720 if (atoken != null) {
Adrian Roos23df3a32018-03-15 15:41:13 +0100721 atoken.updateLetterboxSurface(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800722 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
723 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
724 mTmpUpdateAllDrawn.add(atoken);
725 }
726 }
727
728 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
729 && w.isDisplayedLw()) {
730 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
731 }
732
733 w.updateResizingWindowIfNeeded();
734 };
735
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800736 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800737 * Create new {@link DisplayContent} instance, add itself to the root window container and
738 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700739 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800740 * @param service You know.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700741 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
742 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700743 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700744 DisplayContent(Display display, WindowManagerService service,
Bryce Leef19cbe22018-02-02 15:09:21 -0800745 WallpaperController wallpaperController, DisplayWindowController controller) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100746 super(service);
Bryce Leef19cbe22018-02-02 15:09:21 -0800747 setController(controller);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800748 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
749 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
750 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
751 + " new=" + display);
752 }
753
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700754 mDisplay = display;
755 mDisplayId = display.getDisplayId();
Wale Ogunwale0303c572016-10-20 10:16:29 -0700756 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700757 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700758 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700759 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100760 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo,
761 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700762 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800763 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700764 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700765
Robert Carr74a66a22018-02-23 12:17:51 -0800766 // We use this as our arbitrary surface size for buffer-less parents
767 // that don't impose cropping on their children. It may need to be larger
768 // than the display size because fullscreen windows can be shifted offscreen
769 // due to surfaceInsets. 2 times the largest display dimension feels like an
770 // appropriately arbitrary number. Eventually we would like to give SurfaceFlinger
771 // layers the ability to match their parent sizes and be able to skip
772 // such arbitrary size settings.
773 mSurfaceSize = Math.max(mBaseDisplayHeight, mBaseDisplayWidth) * 2;
Robert Carrb1579c82017-09-05 14:54:47 -0700774
775 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession)
776 .setSize(mSurfaceSize, mSurfaceSize)
777 .setOpaque(true);
778 mWindowingLayer = b.setName("Display Root").build();
779 mOverlayLayer = b.setName("Display Overlays").build();
780
Robert Carrf59b8dd2017-10-02 18:58:36 -0700781 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700782 .setLayerStack(mWindowingLayer, mDisplayId)
783 .show(mWindowingLayer)
784 .setLayer(mOverlayLayer, 1)
785 .setLayerStack(mOverlayLayer, mDisplayId)
786 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700787 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700788
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700789 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700790 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700791 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700792 super.addChild(mAboveAppWindowsContainers, null);
793 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800794
795 // Add itself as a child to the root container.
796 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700797
798 // TODO(b/62541591): evaluate whether this is the best spot to declare the
799 // {@link DisplayContent} ready for use.
800 mDisplayReady = true;
801 }
802
803 boolean isReady() {
804 // The display is ready when the system and the individual display are both ready.
805 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700806 }
807
808 int getDisplayId() {
809 return mDisplayId;
810 }
811
Wale Ogunwale02319a62016-09-26 15:21:22 -0700812 WindowToken getWindowToken(IBinder binder) {
813 return mTokenMap.get(binder);
814 }
815
816 AppWindowToken getAppWindowToken(IBinder binder) {
817 final WindowToken token = getWindowToken(binder);
818 if (token == null) {
819 return null;
820 }
821 return token.asAppWindowToken();
822 }
823
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700824 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700825 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
826 if (dc != null) {
827 // We currently don't support adding a window token to the display if the display
828 // already has the binder mapped to another token. If there is a use case for supporting
829 // this moving forward we will either need to merge the WindowTokens some how or have
830 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700831 throw new IllegalArgumentException("Can't map token=" + token + " to display="
832 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700833 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700834 if (binder == null) {
835 throw new IllegalArgumentException("Can't map token=" + token + " to display="
836 + getName() + " binder is null");
837 }
838 if (token == null) {
839 throw new IllegalArgumentException("Can't map null token to display="
840 + getName() + " binder=" + binder);
841 }
842
Wale Ogunwale02319a62016-09-26 15:21:22 -0700843 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700844
845 if (token.asAppWindowToken() == null) {
846 // Add non-app token to container hierarchy on the display. App tokens are added through
847 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700848 switch (token.windowType) {
849 case TYPE_WALLPAPER:
850 mBelowAppWindowsContainers.addChild(token);
851 break;
852 case TYPE_INPUT_METHOD:
853 case TYPE_INPUT_METHOD_DIALOG:
854 mImeWindowsContainers.addChild(token);
855 break;
856 default:
857 mAboveAppWindowsContainers.addChild(token);
858 break;
859 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700860 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700861 }
862
863 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700864 final WindowToken token = mTokenMap.remove(binder);
865 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800866 token.setExiting();
867 }
868 return token;
869 }
870
871 /** Changes the display the input window token is housed on to this one. */
872 void reParentWindowToken(WindowToken token) {
873 final DisplayContent prevDc = token.getDisplayContent();
874 if (prevDc == this) {
875 return;
876 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800877 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
878 && token.asAppWindowToken() == null) {
879 // Removed the token from the map, but made sure it's not an app token before removing
880 // from parent.
881 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700882 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800883
884 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700885 }
886
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700887 void removeAppToken(IBinder binder) {
888 final WindowToken token = removeWindowToken(binder);
889 if (token == null) {
890 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
891 return;
892 }
893
894 final AppWindowToken appToken = token.asAppWindowToken();
895
896 if (appToken == null) {
897 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
898 return;
899 }
900
901 appToken.onRemovedFromDisplay();
902 }
903
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700904 Display getDisplay() {
905 return mDisplay;
906 }
907
Craig Mautner59c00972012-07-30 12:10:24 -0700908 DisplayInfo getDisplayInfo() {
909 return mDisplayInfo;
910 }
911
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700912 DisplayMetrics getDisplayMetrics() {
913 return mDisplayMetrics;
914 }
915
Andrii Kulian8ee72852017-03-10 10:36:45 -0800916 int getRotation() {
917 return mRotation;
918 }
919
Andrii Kulianef5ce1c2018-03-20 19:27:24 -0700920 @VisibleForTesting
Andrii Kulian8ee72852017-03-10 10:36:45 -0800921 void setRotation(int newRotation) {
922 mRotation = newRotation;
923 }
924
925 int getLastOrientation() {
926 return mLastOrientation;
927 }
928
929 void setLastOrientation(int orientation) {
930 mLastOrientation = orientation;
931 }
932
933 boolean getAltOrientation() {
934 return mAltOrientation;
935 }
936
937 void setAltOrientation(boolean altOrientation) {
938 mAltOrientation = altOrientation;
939 }
940
941 int getLastWindowForcedOrientation() {
942 return mLastWindowForcedOrientation;
943 }
944
Andrii Kulian06d07d62017-03-14 11:11:47 -0700945 /**
Riddle Hsu654a6f92018-07-13 22:59:36 +0800946 * Temporarily pauses rotation changes until resumed.
947 *
948 * This can be used to prevent rotation changes from occurring while the user is
949 * performing certain operations, such as drag and drop.
950 *
951 * This call nests and must be matched by an equal number of calls to
952 * {@link #resumeRotationLocked}.
953 */
954 void pauseRotationLocked() {
955 mDeferredRotationPauseCount++;
956 }
957
958 /**
959 * Resumes normal rotation changes after being paused.
960 */
961 void resumeRotationLocked() {
962 if (mDeferredRotationPauseCount <= 0) {
963 return;
964 }
965
966 mDeferredRotationPauseCount--;
967 if (mDeferredRotationPauseCount == 0) {
968 final boolean changed = updateRotationUnchecked();
969 if (changed) {
970 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
971 }
972 }
973 }
974
975 /**
Andrii Kulian06d07d62017-03-14 11:11:47 -0700976 * Update rotation of the display.
977 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -0700978 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
979 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
Andrii Kulian06d07d62017-03-14 11:11:47 -0700980 */
Robert Carrae606b42018-02-15 15:36:23 -0800981 boolean updateRotationUnchecked() {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -0700982 return updateRotationUnchecked(false /* forceUpdate */);
983 }
Andrii Kulian06d07d62017-03-14 11:11:47 -0700984
Andrii Kulian6cdcfe42018-05-23 17:59:43 -0700985 /**
986 * Update rotation of the display with an option to force the update.
987 * @param forceUpdate Force the rotation update. Sometimes in WM we might skip updating
988 * orientation because we're waiting for some rotation to finish or display
989 * to unfreeze, which results in configuration of the previously visible
990 * activity being applied to a newly visible one. Forcing the rotation
991 * update allows to workaround this issue.
992 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
993 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
994 */
995 boolean updateRotationUnchecked(boolean forceUpdate) {
996 ScreenRotationAnimation screenRotationAnimation;
997 if (!forceUpdate) {
Riddle Hsu654a6f92018-07-13 22:59:36 +0800998 if (mDeferredRotationPauseCount > 0) {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -0700999 // Rotation updates have been paused temporarily. Defer the update until
1000 // updates have been resumed.
1001 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
1002 return false;
1003 }
1004
1005 screenRotationAnimation =
1006 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
1007 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
1008 // Rotation updates cannot be performed while the previous rotation change
1009 // animation is still in progress. Skip this update. We will try updating
1010 // again after the animation is finished and the display is unfrozen.
1011 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
1012 return false;
1013 }
1014 if (mService.mDisplayFrozen) {
1015 // Even if the screen rotation animation has finished (e.g. isAnimating
1016 // returns false), there is still some time where we haven't yet unfrozen
1017 // the display. We also need to abort rotation here.
1018 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
1019 "Deferring rotation, still finishing previous rotation");
1020 return false;
1021 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001022 }
1023
1024 if (!mService.mDisplayEnabled) {
1025 // No point choosing a rotation if the display is not enabled.
1026 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
1027 return false;
1028 }
1029
1030 final int oldRotation = mRotation;
1031 final int lastOrientation = mLastOrientation;
1032 final boolean oldAltOrientation = mAltOrientation;
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001033 final int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation,
1034 isDefaultDisplay);
1035 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Computed rotation=" + rotation + " for display id="
1036 + mDisplayId + " based on lastOrientation=" + lastOrientation
1037 + " and oldRotation=" + oldRotation);
Robert Carr0e007272017-10-02 13:00:55 -07001038 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
Robert Carr897215d2017-03-29 12:25:50 -07001039 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001040
Robert Carr0e007272017-10-02 13:00:55 -07001041 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001042 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001043 if (seamlessRotated != null && !forceUpdate) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001044 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
1045 // to complete (that is, waiting for windows to redraw). It's tempting to check
1046 // w.mSeamlessRotationCount but that could be incorrect in the case of
1047 // window-removal.
1048 return false;
1049 }
Robert Carr0e007272017-10-02 13:00:55 -07001050
1051 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001052 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -07001053 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -07001054 mayRotateSeamlessly = false;
1055 }
1056 for (int i = 0; i < mService.mSessions.size(); i++) {
1057 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
1058 mayRotateSeamlessly = false;
1059 break;
1060 }
1061 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001062 }
Robert Carr0e007272017-10-02 13:00:55 -07001063 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001064
1065 // TODO: Implement forced rotation changes.
1066 // Set mAltOrientation to indicate that the application is receiving
1067 // an orientation that has different metrics than it expected.
1068 // eg. Portrait instead of Landscape.
1069
1070 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
1071 lastOrientation, rotation);
1072
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001073 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1074 + " selected orientation " + lastOrientation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001075 + ", got rotation " + rotation + " which has "
1076 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1077
1078 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1079 // No change.
1080 return false;
1081 }
1082
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001083 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1084 + " rotation changed to " + rotation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001085 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1086 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1087
1088 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1089 mService.mWaitingForConfig = true;
1090 }
1091
1092 mRotation = rotation;
1093 mAltOrientation = altOrientation;
1094 if (isDefaultDisplay) {
1095 mService.mPolicy.setRotationLw(rotation);
1096 }
1097
1098 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
Riddle Hsu654a6f92018-07-13 22:59:36 +08001099 mService.mH.sendNewMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1100 this, WINDOW_FREEZE_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001101
1102 setLayoutNeeded();
1103 final int[] anim = new int[2];
Robert Carrf59b8dd2017-10-02 18:58:36 -07001104 mService.mPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001105
1106 if (!rotateSeamlessly) {
Robert Carrae606b42018-02-15 15:36:23 -08001107 mService.startFreezingDisplayLocked(anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001108 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1109 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1110 mDisplayId);
1111 } else {
1112 // The screen rotation animation uses a screenshot to freeze the screen
1113 // while windows resize underneath.
1114 // When we are rotating seamlessly, we allow the elements to transition
1115 // to their rotated state independently and without a freeze required.
1116 screenRotationAnimation = null;
1117
Robert Carrefc75702018-02-16 11:46:16 -08001118 mService.startSeamlessRotation();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001119 }
1120
1121 // We need to update our screen size information to match the new rotation. If the rotation
1122 // has actually changed then this method will return true and, according to the comment at
1123 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1124 // By updating the Display info here it will be available to
1125 // #computeScreenConfiguration() later.
1126 updateDisplayAndOrientation(getConfiguration().uiMode);
1127
Robert Carrae606b42018-02-15 15:36:23 -08001128 // NOTE: We disable the rotation in the emulator because
1129 // it doesn't support hardware OpenGL emulation yet.
1130 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1131 && screenRotationAnimation.hasScreenshot()) {
1132 if (screenRotationAnimation.setRotation(getPendingTransaction(), rotation,
1133 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1134 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1135 mService.scheduleAnimationLocked();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001136 }
1137 }
1138
Vishnu Nair83537a72018-07-19 21:27:48 -07001139 forAllWindows(w -> {
1140 w.forceSeamlesslyRotateIfAllowed(oldRotation, rotation);
1141 }, true /* traverseTopToBottom */);
1142
1143 // TODO(b/111504081): Consolidate seamless rotation logic.
Robert Carrae606b42018-02-15 15:36:23 -08001144 if (rotateSeamlessly) {
Adrian Roos0e7b70a2018-06-07 15:29:34 +02001145 seamlesslyRotate(getPendingTransaction(), oldRotation, rotation);
Robert Carrae606b42018-02-15 15:36:23 -08001146 }
1147
1148 mService.mDisplayManagerInternal.performTraversal(getPendingTransaction());
1149 scheduleAnimation();
1150
Andrii Kulian06d07d62017-03-14 11:11:47 -07001151 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001152 if (w.mHasSurface && !rotateSeamlessly) {
1153 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001154 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001155 mService.mRoot.mOrientationChangeComplete = false;
1156 w.mLastFreezeDuration = 0;
1157 }
1158 w.mReportOrientationChanged = true;
1159 }, true /* traverseTopToBottom */);
1160
1161 if (rotateSeamlessly) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001162 mService.mH.sendNewMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1163 this, SEAMLESS_ROTATION_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001164 }
1165
1166 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1167 final WindowManagerService.RotationWatcher rotationWatcher
1168 = mService.mRotationWatchers.get(i);
1169 if (rotationWatcher.mDisplayId == mDisplayId) {
1170 try {
1171 rotationWatcher.mWatcher.onRotationChanged(rotation);
1172 } catch (RemoteException e) {
1173 // Ignore
1174 }
1175 }
1176 }
1177
1178 // TODO (multi-display): Magnification is supported only for the default display.
1179 // Announce rotation only if we will not animate as we already have the
1180 // windows in final state. Otherwise, we make this call at the rotation end.
1181 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1182 && isDefaultDisplay) {
1183 mService.mAccessibilityController.onRotationChangedLocked(this);
1184 }
1185
1186 return true;
1187 }
1188
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001189 void configureDisplayPolicy() {
1190 mService.mPolicy.setInitialDisplaySize(getDisplay(),
1191 mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1192
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001193 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
1194 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001195 }
1196
Andrii Kulian06d07d62017-03-14 11:11:47 -07001197 /**
1198 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1199 * changed.
1200 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1201 */
1202 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1203 // Use the effective "visual" dimensions based on current rotation
1204 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1205 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1206 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1207 int dw = realdw;
1208 int dh = realdh;
1209
1210 if (mAltOrientation) {
1211 if (realdw > realdh) {
1212 // Turn landscape into portrait.
1213 int maxw = (int)(realdh/1.3f);
1214 if (maxw < realdw) {
1215 dw = maxw;
1216 }
1217 } else {
1218 // Turn portrait into landscape.
1219 int maxh = (int)(realdw/1.3f);
1220 if (maxh < realdh) {
1221 dh = maxh;
1222 }
1223 }
1224 }
1225
1226 // Update application display metrics.
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001227 final WmDisplayCutout wmDisplayCutout = calculateDisplayCutoutForRotation(mRotation);
1228 final DisplayCutout displayCutout = wmDisplayCutout.getDisplayCutout();
1229
Andrii Kulian06d07d62017-03-14 11:11:47 -07001230 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001231 mDisplayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001232 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001233 mDisplayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001234 mDisplayInfo.rotation = mRotation;
1235 mDisplayInfo.logicalWidth = dw;
1236 mDisplayInfo.logicalHeight = dh;
1237 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1238 mDisplayInfo.appWidth = appWidth;
1239 mDisplayInfo.appHeight = appHeight;
1240 if (isDefaultDisplay) {
1241 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1242 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1243 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001244 mDisplayInfo.displayCutout = displayCutout.isEmpty() ? null : displayCutout;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001245 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1246 if (mDisplayScalingDisabled) {
1247 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1248 } else {
1249 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1250 }
1251
Andrii Kulianf0379de2018-03-14 16:24:07 -07001252 // We usually set the override info in DisplayManager so that we get consistent display
1253 // metrics values when displays are changing and don't send out new values until WM is aware
1254 // of them. However, we don't do this for displays that serve as containers for ActivityView
1255 // because we don't want letter-/pillar-boxing during resize.
1256 final DisplayInfo overrideDisplayInfo = mShouldOverrideDisplayConfiguration
1257 ? mDisplayInfo : null;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001258 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
Andrii Kulianf0379de2018-03-14 16:24:07 -07001259 overrideDisplayInfo);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001260
1261 mBaseDisplayRect.set(0, 0, dw, dh);
1262
1263 if (isDefaultDisplay) {
1264 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1265 mCompatDisplayMetrics);
1266 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001267
1268 updateBounds();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001269 return mDisplayInfo;
1270 }
1271
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001272 WmDisplayCutout calculateDisplayCutoutForRotation(int rotation) {
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001273 return mDisplayCutoutCache.getOrCompute(mInitialDisplayCutout, rotation);
1274 }
1275
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001276 private WmDisplayCutout calculateDisplayCutoutForRotationUncached(
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001277 DisplayCutout cutout, int rotation) {
Adrian Roos24264212018-02-19 16:26:15 +01001278 if (cutout == null || cutout == DisplayCutout.NO_CUTOUT) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001279 return WmDisplayCutout.NO_CUTOUT;
Adrian Roos1cf585052018-01-03 18:43:27 +01001280 }
Adrian Roos11c25582018-02-19 18:06:36 +01001281 if (rotation == ROTATION_0) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001282 return WmDisplayCutout.computeSafeInsets(
1283 cutout, mInitialDisplayWidth, mInitialDisplayHeight);
Adrian Roos24264212018-02-19 16:26:15 +01001284 }
Adrian Roosbed538e2018-02-21 17:50:07 +01001285 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Jorim Jaggi60640512018-06-29 01:14:31 +02001286 final List<Rect> bounds = WmDisplayCutout.computeSafeInsets(
1287 cutout, mInitialDisplayWidth, mInitialDisplayHeight)
1288 .getDisplayCutout().getBoundingRects();
Adrian Roos11c25582018-02-19 18:06:36 +01001289 transformPhysicalToLogicalCoordinates(rotation, mInitialDisplayWidth, mInitialDisplayHeight,
1290 mTmpMatrix);
Jorim Jaggi60640512018-06-29 01:14:31 +02001291 final Region region = Region.obtain();
1292 for (int i = 0; i < bounds.size(); i++) {
1293 final Rect rect = bounds.get(i);
1294 final RectF rectF = new RectF(bounds.get(i));
1295 mTmpMatrix.mapRect(rectF);
1296 rectF.round(rect);
1297 region.op(rect, Op.UNION);
1298 }
1299
1300 return WmDisplayCutout.computeSafeInsets(DisplayCutout.fromBounds(region),
Adrian Roos24264212018-02-19 16:26:15 +01001301 rotated ? mInitialDisplayHeight : mInitialDisplayWidth,
1302 rotated ? mInitialDisplayWidth : mInitialDisplayHeight);
Adrian Roos1cf585052018-01-03 18:43:27 +01001303 }
1304
Andrii Kulian06d07d62017-03-14 11:11:47 -07001305 /**
1306 * Compute display configuration based on display properties and policy settings.
1307 * Do not call if mDisplayReady == false.
1308 */
1309 void computeScreenConfiguration(Configuration config) {
1310 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1311
1312 final int dw = displayInfo.logicalWidth;
1313 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001314 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
1315 // TODO: Probably best to set this based on some setting in the display content object,
1316 // so the display can be configured for things like fullscreen.
1317 config.windowConfiguration.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001318
Adrian Roos11c25582018-02-19 18:06:36 +01001319 final float density = mDisplayMetrics.density;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001320 config.screenWidthDp =
1321 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001322 config.uiMode, mDisplayId, displayInfo.displayCutout) / density);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001323 config.screenHeightDp =
1324 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001325 config.uiMode, mDisplayId, displayInfo.displayCutout) / density);
Bryce Lee7566d762017-03-30 09:34:15 -07001326
Adrian Roos11c25582018-02-19 18:06:36 +01001327 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh,
1328 displayInfo.displayCutout, mTmpRect);
Bryce Lee7566d762017-03-30 09:34:15 -07001329 final int leftInset = mTmpRect.left;
1330 final int topInset = mTmpRect.top;
1331 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001332 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1333 leftInset + displayInfo.appWidth /* right */,
1334 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001335 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1336 || displayInfo.rotation == Surface.ROTATION_270);
1337
1338 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
Adrian Roos11c25582018-02-19 18:06:36 +01001339 density, config);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001340
1341 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1342 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1343 ? Configuration.SCREENLAYOUT_ROUND_YES
1344 : Configuration.SCREENLAYOUT_ROUND_NO);
1345
1346 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1347 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1348 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1349 dh, mDisplayId);
1350 config.densityDpi = displayInfo.logicalDensityDpi;
1351
1352 config.colorMode =
1353 (displayInfo.isHdr()
1354 ? Configuration.COLOR_MODE_HDR_YES
1355 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001356 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001357 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1358 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1359
1360 // Update the configuration based on available input devices, lid switch,
1361 // and platform configuration.
1362 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1363 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1364 config.navigation = Configuration.NAVIGATION_NONAV;
1365
1366 int keyboardPresence = 0;
1367 int navigationPresence = 0;
1368 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1369 final int len = devices != null ? devices.length : 0;
1370 for (int i = 0; i < len; i++) {
1371 InputDevice device = devices[i];
1372 if (!device.isVirtual()) {
1373 final int sources = device.getSources();
1374 final int presenceFlag = device.isExternal() ?
1375 WindowManagerPolicy.PRESENCE_EXTERNAL :
1376 WindowManagerPolicy.PRESENCE_INTERNAL;
1377
1378 // TODO(multi-display): Configure on per-display basis.
1379 if (mService.mIsTouchDevice) {
1380 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1381 InputDevice.SOURCE_TOUCHSCREEN) {
1382 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1383 }
1384 } else {
1385 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1386 }
1387
1388 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1389 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1390 navigationPresence |= presenceFlag;
1391 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1392 && config.navigation == Configuration.NAVIGATION_NONAV) {
1393 config.navigation = Configuration.NAVIGATION_DPAD;
1394 navigationPresence |= presenceFlag;
1395 }
1396
1397 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1398 config.keyboard = Configuration.KEYBOARD_QWERTY;
1399 keyboardPresence |= presenceFlag;
1400 }
1401 }
1402 }
1403
1404 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1405 config.navigation = Configuration.NAVIGATION_DPAD;
1406 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1407 }
1408
1409 // Determine whether a hard keyboard is available and enabled.
1410 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1411 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1412 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1413 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1414 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1415 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1416 }
1417
1418 // Let the policy update hidden states.
1419 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1420 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1421 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1422 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1423 }
1424
1425 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1426 int displayId) {
1427 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1428 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1429 final int unrotDw, unrotDh;
1430 if (rotated) {
1431 unrotDw = dh;
1432 unrotDh = dw;
1433 } else {
1434 unrotDw = dw;
1435 unrotDh = dh;
1436 }
1437 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1438 displayId);
1439 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1440 displayId);
1441 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1442 displayId);
1443 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1444 displayId);
1445 return sw;
1446 }
1447
1448 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1449 DisplayMetrics dm, int dw, int dh, int displayId) {
1450 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001451 displayId, mDisplayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001452 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001453 uiMode, displayId, mDisplayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001454 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1455 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1456 if (curSize == 0 || size < curSize) {
1457 curSize = size;
1458 }
1459 return curSize;
1460 }
1461
1462 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1463 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1464
1465 // We need to determine the smallest width that will occur under normal
1466 // operation. To this, start with the base screen size and compute the
1467 // width under the different possible rotations. We need to un-rotate
1468 // the current screen dimensions before doing this.
1469 int unrotDw, unrotDh;
1470 if (rotated) {
1471 unrotDw = dh;
1472 unrotDh = dw;
1473 } else {
1474 unrotDw = dw;
1475 unrotDh = dh;
1476 }
1477 displayInfo.smallestNominalAppWidth = 1<<30;
1478 displayInfo.smallestNominalAppHeight = 1<<30;
1479 displayInfo.largestNominalAppWidth = 0;
1480 displayInfo.largestNominalAppHeight = 0;
1481 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1482 unrotDh);
1483 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1484 unrotDw);
1485 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1486 unrotDh);
1487 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1488 unrotDw);
1489 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1490 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1491 displayId);
1492 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1493 displayId);
1494 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1495 displayId);
1496 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1497 displayId);
1498 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1499 outConfig.screenLayout = sl;
1500 }
1501
1502 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1503 int uiMode, int displayId) {
1504 // Get the app screen size at this rotation.
Adrian Roos11c25582018-02-19 18:06:36 +01001505 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId,
1506 mDisplayInfo.displayCutout);
1507 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId,
1508 mDisplayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001509
1510 // Compute the screen layout size class for this rotation.
1511 int longSize = w;
1512 int shortSize = h;
1513 if (longSize < shortSize) {
1514 int tmp = longSize;
1515 longSize = shortSize;
1516 shortSize = tmp;
1517 }
1518 longSize = (int)(longSize/density);
1519 shortSize = (int)(shortSize/density);
1520 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1521 }
1522
1523 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1524 int uiMode, int dw, int dh) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001525 final DisplayCutout displayCutout = calculateDisplayCutoutForRotation(
1526 rotation).getDisplayCutout();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001527 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001528 displayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001529 if (width < displayInfo.smallestNominalAppWidth) {
1530 displayInfo.smallestNominalAppWidth = width;
1531 }
1532 if (width > displayInfo.largestNominalAppWidth) {
1533 displayInfo.largestNominalAppWidth = width;
1534 }
1535 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001536 displayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001537 if (height < displayInfo.smallestNominalAppHeight) {
1538 displayInfo.smallestNominalAppHeight = height;
1539 }
1540 if (height > displayInfo.largestNominalAppHeight) {
1541 displayInfo.largestNominalAppHeight = height;
1542 }
1543 }
1544
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001545 DockedStackDividerController getDockedDividerController() {
1546 return mDividerControllerLocked;
1547 }
1548
Winson Chung655332c2016-10-31 13:14:28 -07001549 PinnedStackController getPinnedStackController() {
1550 return mPinnedStackControllerLocked;
1551 }
1552
Jeff Browna506a6e2013-06-04 00:02:38 -07001553 /**
1554 * Returns true if the specified UID has access to this display.
1555 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001556 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001557 return mDisplay.hasAccess(uid);
1558 }
1559
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001560 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001561 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001562 }
1563
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001564 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001565 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001566 }
1567
Wale Ogunwale61911492017-10-11 08:50:50 -07001568 /**
1569 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1570 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001571 TaskStack getSplitScreenPrimaryStack() {
1572 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001573 return (stack != null && stack.isVisible()) ? stack : null;
1574 }
1575
Robert Carr9785cf32018-04-25 15:06:07 -07001576 boolean hasSplitScreenPrimaryStack() {
1577 return getSplitScreenPrimaryStack() != null;
1578 }
1579
Wale Ogunwale61911492017-10-11 08:50:50 -07001580 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001581 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001582 * not visible.
1583 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001584 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1585 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001586 }
1587
1588 TaskStack getPinnedStack() {
1589 return mTaskStackContainers.getPinnedStack();
1590 }
1591
1592 private boolean hasPinnedStack() {
1593 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001594 }
1595
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001596 /**
1597 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1598 * Null is no compatible stack on the display.
1599 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001600 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001601 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1602 }
1603
1604 /**
1605 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1606 * activity type. Null is no compatible stack on the display.
1607 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001608 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001609 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001610 }
1611
Bryce Lee48f4b572017-04-10 10:54:15 -07001612 @VisibleForTesting
Kazuki Takise148d00a2018-05-31 15:32:19 +09001613 WindowList<TaskStack> getStacks() {
1614 return mTaskStackContainers.mChildren;
1615 }
1616
1617 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001618 TaskStack getTopStack() {
1619 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001620 }
1621
Winson Chunge2d72172018-01-25 17:46:20 +00001622 ArrayList<Task> getVisibleTasks() {
1623 return mTaskStackContainers.getVisibleTasks();
1624 }
1625
Wale Ogunwale61911492017-10-11 08:50:50 -07001626 void onStackWindowingModeChanged(TaskStack stack) {
1627 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001628 }
1629
Andrii Kulian441e4492016-09-29 15:25:00 -07001630 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001631 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001632 super.onConfigurationChanged(newParentConfig);
1633
Andrii Kulian3a507b52016-09-19 18:14:12 -07001634 // The display size information is heavily dependent on the resources in the current
1635 // configuration, so we need to reconfigure it every time the configuration changes.
1636 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1637 mService.reconfigureDisplayLocked(this);
1638
Bryce Leef3c6a472017-11-14 14:53:06 -08001639 final DockedStackDividerController dividerController = getDockedDividerController();
1640
1641 if (dividerController != null) {
1642 getDockedDividerController().onConfigurationChanged();
1643 }
1644
1645 final PinnedStackController pinnedStackController = getPinnedStackController();
1646
1647 if (pinnedStackController != null) {
1648 getPinnedStackController().onConfigurationChanged();
1649 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001650 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001651
Andrii Kulian441e4492016-09-29 15:25:00 -07001652 /**
1653 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1654 * bounds were updated.
1655 */
Bryce Leeaea60d22018-01-31 14:42:15 -08001656 void updateStackBoundsAfterConfigChange(@NonNull List<TaskStack> changedStackList) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001657 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1658 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001659 if (stack.updateBoundsAfterConfigChange()) {
Bryce Leeaea60d22018-01-31 14:42:15 -08001660 changedStackList.add(stack);
Andrii Kulian3a507b52016-09-19 18:14:12 -07001661 }
1662 }
Winson Chung32c566f2017-04-11 18:31:21 -07001663
1664 // If there was no pinned stack, we still need to notify the controller of the display info
1665 // update as a result of the config change. We do this here to consolidate the flow between
1666 // changes when there is and is not a stack.
Wale Ogunwale61911492017-10-11 08:50:50 -07001667 if (!hasPinnedStack()) {
Winson Chung32c566f2017-04-11 18:31:21 -07001668 mPinnedStackControllerLocked.onDisplayInfoChanged();
1669 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001670 }
1671
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001672 @Override
1673 boolean fillsParent() {
1674 return true;
1675 }
1676
1677 @Override
1678 boolean isVisible() {
1679 return true;
1680 }
1681
1682 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001683 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001684 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001685 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001686 }
1687
Robert Carr9785cf32018-04-25 15:06:07 -07001688 /**
1689 * In split-screen mode we process the IME containers above the docked divider
1690 * rather than directly above their target.
1691 */
1692 private boolean skipTraverseChild(WindowContainer child) {
1693 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null
1694 && !hasSplitScreenPrimaryStack()) {
1695 return true;
1696 }
1697 return false;
1698 }
1699
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001700 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001701 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1702 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1703 // target, or here in order if there isn't an IME target.
1704 if (traverseTopToBottom) {
1705 for (int i = mChildren.size() - 1; i >= 0; --i) {
1706 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001707 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001708 continue;
1709 }
Robert Carr9785cf32018-04-25 15:06:07 -07001710
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001711 if (child.forAllWindows(callback, traverseTopToBottom)) {
1712 return true;
1713 }
1714 }
1715 } else {
1716 final int count = mChildren.size();
1717 for (int i = 0; i < count; i++) {
1718 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001719 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001720 continue;
1721 }
Robert Carr9785cf32018-04-25 15:06:07 -07001722
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001723 if (child.forAllWindows(callback, traverseTopToBottom)) {
1724 return true;
1725 }
1726 }
1727 }
1728 return false;
1729 }
1730
1731 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1732 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1733 }
1734
Wale Ogunwale399c8692017-05-08 14:22:42 -07001735 @Override
1736 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001737 final WindowManagerPolicy policy = mService.mPolicy;
1738
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001739 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001740 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001741 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1742 + " is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001743 // If the display is frozen, some activities may be in the middle of restarting, and
1744 // thus have removed their old window. If the window has the flag to hide the lock
1745 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1746 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001747 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001748 } else if (policy.isKeyguardLocked()) {
1749 // Use the last orientation the while the display is frozen with the keyguard
1750 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1751 // window. We don't want to check the show when locked window directly though as
1752 // things aren't stable while the display is frozen, for example the window could be
1753 // momentarily unavailable due to activity relaunch.
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001754 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1755 + " is frozen while keyguard locked, return " + mLastOrientation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08001756 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001757 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001758 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001759 final int orientation = mAboveAppWindowsContainers.getOrientation();
1760 if (orientation != SCREEN_ORIENTATION_UNSET) {
1761 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001762 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001763 }
1764
Wale Ogunwale399c8692017-05-08 14:22:42 -07001765 // Top system windows are not requesting an orientation. Start searching from apps.
1766 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001767 }
1768
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001769 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001770 // Check if display metrics changed and update base values if needed.
1771 updateBaseDisplayMetricsIfNeeded();
1772
Craig Mautner722285e2012-09-07 13:55:58 -07001773 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001774 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001775
Wale Ogunwale61911492017-10-11 08:50:50 -07001776 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1777 mTaskStackContainers.getChildAt(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001778 }
Craig Mautner722285e2012-09-07 13:55:58 -07001779 }
1780
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001781 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001782 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1783 if (displayManagerInternal != null) {
1784 // Bootstrap the default logical display from the display manager.
1785 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1786 if (newDisplayInfo != null) {
1787 mDisplayInfo.copyFrom(newDisplayInfo);
1788 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001789 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001790
Andrii Kuliancd097992017-03-23 18:31:59 -07001791 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1792 mDisplayInfo.logicalDensityDpi);
1793 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1794 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1795 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01001796 mInitialDisplayCutout = mDisplayInfo.displayCutout;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001797 }
1798
Andrii Kuliancd097992017-03-23 18:31:59 -07001799 /**
1800 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1801 * values.
1802 */
1803 private void updateBaseDisplayMetricsIfNeeded() {
1804 // Get real display metrics without overrides from WM.
1805 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1806 final int orientation = mDisplayInfo.rotation;
1807 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1808 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1809 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1810 final int newDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01001811 final DisplayCutout newCutout = mDisplayInfo.displayCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07001812
1813 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1814 || mInitialDisplayHeight != newHeight
Adrian Roos1cf585052018-01-03 18:43:27 +01001815 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi
1816 || !Objects.equals(mInitialDisplayCutout, newCutout);
Andrii Kuliancd097992017-03-23 18:31:59 -07001817
1818 if (displayMetricsChanged) {
1819 // Check if display size or density is forced.
1820 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1821 || mBaseDisplayHeight != mInitialDisplayHeight;
1822 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1823
1824 // If there is an override set for base values - use it, otherwise use new values.
1825 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1826 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1827 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1828
1829 // Real display metrics changed, so we should also update initial values.
1830 mInitialDisplayWidth = newWidth;
1831 mInitialDisplayHeight = newHeight;
1832 mInitialDisplayDensity = newDensity;
Adrian Roos1cf585052018-01-03 18:43:27 +01001833 mInitialDisplayCutout = newCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07001834 mService.reconfigureDisplayLocked(this);
1835 }
1836 }
1837
Bryce Lee27cec322017-03-21 09:41:37 -07001838 /** Sets the maximum width the screen resolution can be */
1839 void setMaxUiWidth(int width) {
1840 if (DEBUG_DISPLAY) {
1841 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1842 }
1843
1844 mMaxUiWidth = width;
1845
1846 // Update existing metrics.
1847 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1848 }
1849
1850 /** Update base (override) display metrics. */
1851 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1852 mBaseDisplayWidth = baseWidth;
1853 mBaseDisplayHeight = baseHeight;
1854 mBaseDisplayDensity = baseDensity;
1855
1856 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1857 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1858 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1859 mBaseDisplayWidth = mMaxUiWidth;
1860
1861 if (DEBUG_DISPLAY) {
1862 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1863 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1864 + " on display:" + getDisplayId());
1865 }
1866 }
1867
1868 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08001869
1870 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07001871 }
1872
Wale Ogunwaledb506192017-12-08 10:57:32 -08001873 void getStableRect(Rect out) {
1874 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08001875 }
1876
Wale Ogunwale61911492017-10-11 08:50:50 -07001877 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001878 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1879 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001880
Wale Ogunwale61911492017-10-11 08:50:50 -07001881 final TaskStack stack = new TaskStack(mService, stackId, controller);
1882 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08001883 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001884 }
1885
Andrii Kulian51c1b672017-04-07 18:39:32 -07001886 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001887 final DisplayContent prevDc = stack.getDisplayContent();
1888 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001889 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1890 + " which is not currently attached to any display");
1891 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001892 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001893 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1894 + " to its current displayId=" + mDisplayId);
1895 }
1896
Wale Ogunwale61911492017-10-11 08:50:50 -07001897 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001898 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001899 }
1900
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001901 @Override
1902 protected void addChild(DisplayChildWindowContainer child,
1903 Comparator<DisplayChildWindowContainer> comparator) {
1904 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1905 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001906
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001907 @Override
1908 protected void addChild(DisplayChildWindowContainer child, int index) {
1909 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1910 }
1911
1912 @Override
1913 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001914 // Only allow removal of direct children from this display if the display is in the process
1915 // of been removed.
1916 if (mRemovingDisplay) {
1917 super.removeChild(child);
1918 return;
1919 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001920 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001921 }
1922
Andrii Kuliand2765632016-12-12 22:26:34 -08001923 @Override
1924 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1925 // Children of the display are statically ordered, so the real intention here is to perform
1926 // the operation on the display and not the static direct children.
1927 getParent().positionChildAt(position, this, includingParents);
1928 }
1929
Winson Chung59a47ded2018-01-25 17:46:06 +00001930 void positionStackAt(int position, TaskStack child) {
1931 mTaskStackContainers.positionChildAt(position, child, false /* includingParents */);
1932 layoutAndAssignWindowLayersIfNeeded();
1933 }
1934
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001935 int taskIdFromPoint(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001936 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1937 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001938 final int taskId = stack.taskIdFromPoint(x, y);
1939 if (taskId != -1) {
1940 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001941 }
1942 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001943 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001944 }
1945
Chong Zhang8e89b312015-09-09 15:09:30 -07001946 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001947 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001948 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001949 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001950 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001951 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001952 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07001953 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1954 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001955 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001956 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001957 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001958
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001959 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1960 if (mTmpTaskForResizePointSearchResult.searchDone) {
1961 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001962 }
1963 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001964 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001965 }
1966
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001967 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001968 // The provided task is the task on this display with focus, so if WindowManagerService's
1969 // focused app is not on this display, focusedTask will be null.
1970 if (focusedTask == null) {
1971 mTouchExcludeRegion.setEmpty();
1972 } else {
1973 mTouchExcludeRegion.set(mBaseDisplayRect);
1974 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1975 mTmpRect2.setEmpty();
Wale Ogunwale61911492017-10-11 08:50:50 -07001976 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1977 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001978 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
1979 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08001980 }
1981 // If we removed the focused task above, add it back and only leave its
1982 // outside touch area in the exclusion. TapDectector is not interested in
1983 // any touch inside the focused task itself.
1984 if (!mTmpRect2.isEmpty()) {
1985 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1986 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001987 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001988 final WindowState inputMethod = mService.mInputMethodWindow;
1989 if (inputMethod != null && inputMethod.isVisibleLw()) {
1990 // If the input method is visible and the user is typing, we don't want these touch
1991 // events to be intercepted and used to change focus. This would likely cause a
1992 // disappearance of the input method.
1993 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001994 if (inputMethod.getDisplayId() == mDisplayId) {
1995 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1996 } else {
1997 // IME is on a different display, so we need to update its tap detector.
1998 // TODO(multidisplay): Remove when IME will always appear on same display.
1999 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
2000 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002001 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002002 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002003 final WindowState win = mTapExcludedWindows.get(i);
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002004 win.getTouchableRegion(mTmpRegion);
2005 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
2006 }
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002007 for (int i = mTapExcludeProvidingWindows.size() - 1; i >= 0; i--) {
2008 final WindowState win = mTapExcludeProvidingWindows.valueAt(i);
2009 win.amendTapExcludeRegion(mTouchExcludeRegion);
2010 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08002011 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07002012 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002013 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07002014 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002015 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
2016 }
Craig Mautner1bef3892015-02-17 15:09:47 -08002017 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07002018 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08002019 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07002020 }
2021
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002022 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07002023 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002024 super.switchUser();
2025 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07002026 }
2027
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07002028 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002029 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2030 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07002031 }
2032 }
2033
Wale Ogunwale10124582016-09-15 20:25:50 -07002034 @Override
2035 void removeIfPossible() {
2036 if (isAnimating()) {
2037 mDeferredRemoval = true;
2038 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07002039 }
Wale Ogunwale10124582016-09-15 20:25:50 -07002040 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07002041 }
2042
Wale Ogunwale10124582016-09-15 20:25:50 -07002043 @Override
2044 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002045 mRemovingDisplay = true;
2046 try {
2047 super.removeImmediately();
2048 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -07002049 if (mService.canDispatchPointerEvents()) {
2050 if (mTapDetector != null) {
2051 mService.unregisterPointerEventListener(mTapDetector);
2052 }
2053 if (mDisplayId == DEFAULT_DISPLAY && mService.mMousePositionTracker != null) {
2054 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
2055 }
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002056 }
Jorim Jaggia3844032018-01-03 15:54:43 +01002057 mService.mAnimator.removeDisplayLocked(mDisplayId);
Louis Chang207f9112018-07-30 08:58:54 +08002058 mWindowingLayer.release();
2059 mOverlayLayer.release();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002060 } finally {
2061 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08002062 }
Bryce Leef19cbe22018-02-02 15:09:21 -08002063
2064 mService.onDisplayRemoved(mDisplayId);
Craig Mautner95da1082014-02-24 17:54:35 -08002065 }
2066
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002067 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07002068 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002069 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002070 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
2071
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002072 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002073 removeImmediately();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002074 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08002075 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002076 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08002077 }
2078
Andrii Kulian0214ed92017-05-16 13:44:05 -07002079 /** @return 'true' if removal of this display content is deferred due to active animation. */
2080 boolean isRemovalDeferred() {
2081 return mDeferredRemoval;
2082 }
2083
Wale Ogunwale10124582016-09-15 20:25:50 -07002084 boolean animateForIme(float interpolatedValue, float animationTarget,
2085 float dividerAnimationTarget) {
2086 boolean updated = false;
2087
Wale Ogunwale61911492017-10-11 08:50:50 -07002088 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2089 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002090 if (stack == null || !stack.isAdjustedForIme()) {
2091 continue;
2092 }
2093
2094 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
2095 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2096 updated = true;
2097 } else {
2098 mDividerControllerLocked.mLastAnimationProgress =
2099 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
2100 mDividerControllerLocked.mLastDividerProgress =
2101 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
2102 updated |= stack.updateAdjustForIme(
2103 mDividerControllerLocked.mLastAnimationProgress,
2104 mDividerControllerLocked.mLastDividerProgress,
2105 false /* force */);
2106 }
2107 if (interpolatedValue >= 1f) {
2108 stack.endImeAdjustAnimation();
2109 }
2110 }
2111
2112 return updated;
2113 }
2114
2115 boolean clearImeAdjustAnimation() {
2116 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002117 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2118 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002119 if (stack != null && stack.isAdjustedForIme()) {
2120 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2121 changed = true;
2122 }
2123 }
2124 return changed;
2125 }
2126
2127 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002128 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2129 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002130 if (stack.isVisible() && stack.isAdjustedForIme()) {
2131 stack.beginImeAdjustAnimation();
2132 }
2133 }
2134 }
2135
2136 void adjustForImeIfNeeded() {
2137 final WindowState imeWin = mService.mInputMethodWindow;
2138 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2139 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002140 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002141 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2142 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2143 imeTargetStack.getDockSide() : DOCKED_INVALID;
2144 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2145 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2146 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002147 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002148 final boolean imeHeightChanged = imeVisible &&
2149 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2150
2151 // The divider could be adjusted for IME position, or be thinner than usual,
2152 // or both. There are three possible cases:
2153 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2154 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2155 // - If IME is not visible, divider is not moved and is normal width.
2156
2157 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002158 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2159 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002160 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002161 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2162 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002163 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2164 } else {
2165 stack.resetAdjustedForIme(false);
2166 }
2167 }
2168 mDividerControllerLocked.setAdjustedForIme(
2169 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2170 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002171 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2172 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002173 stack.resetAdjustedForIme(!dockVisible);
2174 }
2175 mDividerControllerLocked.setAdjustedForIme(
2176 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2177 }
Winson Chung655332c2016-10-31 13:14:28 -07002178 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002179 }
2180
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002181 /**
2182 * If a window that has an animation specifying a colored background and the current wallpaper
2183 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2184 * suddenly disappear.
2185 */
2186 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002187 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2188 w -> w.mIsWallpaper && w.isVisibleNow());
2189
2190 if (visibleWallpaper != null) {
2191 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002192 }
2193 return winAnimator.mAnimLayer;
2194 }
2195
Wale Ogunwale10124582016-09-15 20:25:50 -07002196 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002197 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2198 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002199 stack.prepareFreezingTaskBounds();
2200 }
2201 }
2202
Wale Ogunwale94744212015-09-21 19:01:47 -07002203 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002204 getBounds(mTmpRect, newRotation);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002205
2206 // Compute a transform matrix to undo the coordinate space transformation,
2207 // and present the window at the same physical position it previously occupied.
2208 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2209 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2210
2211 mTmpRectF.set(bounds);
2212 mTmpMatrix.mapRect(mTmpRectF);
2213 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002214 }
2215
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002216 static int deltaRotation(int oldRotation, int newRotation) {
2217 int delta = newRotation - oldRotation;
2218 if (delta < 0) delta += 4;
2219 return delta;
2220 }
2221
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002222 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002223 Matrix outMatrix) {
2224 // For rotations without Z-ordering we don't need the target rectangle's position.
2225 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2226 displayHeight, outMatrix);
2227 }
2228
2229 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2230 float displayWidth, float displayHeight, Matrix outMatrix) {
2231 switch (rotation) {
2232 case ROTATION_0:
2233 outMatrix.reset();
2234 break;
2235 case ROTATION_270:
2236 outMatrix.setRotate(270, 0, 0);
2237 outMatrix.postTranslate(0, displayHeight);
2238 outMatrix.postTranslate(rectTop, 0);
2239 break;
2240 case ROTATION_180:
2241 outMatrix.reset();
2242 break;
2243 case ROTATION_90:
2244 outMatrix.setRotate(90, 0, 0);
2245 outMatrix.postTranslate(displayWidth, 0);
2246 outMatrix.postTranslate(-rectTop, rectLeft);
2247 break;
2248 }
2249 }
2250
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002251 @CallSuper
2252 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002253 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002254 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002255 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002256 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002257 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2258 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002259 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002260 }
2261 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2262 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002263 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2264 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002265 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002266 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002267 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2268 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002269 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002270 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002271 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2272 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002273 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002274 }
2275 proto.write(DPI, mBaseDisplayDensity);
2276 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002277 proto.write(ROTATION, mRotation);
2278 final ScreenRotationAnimation screenRotationAnimation =
2279 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2280 if (screenRotationAnimation != null) {
2281 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2282 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002283 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002284 proto.end(token);
2285 }
2286
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002287 @Override
2288 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2289 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002290 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2291 final String subPrefix = " " + prefix;
2292 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2293 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2294 pw.print("dpi");
2295 if (mInitialDisplayWidth != mBaseDisplayWidth
2296 || mInitialDisplayHeight != mBaseDisplayHeight
2297 || mInitialDisplayDensity != mBaseDisplayDensity) {
2298 pw.print(" base=");
2299 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2300 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2301 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002302 if (mDisplayScalingDisabled) {
2303 pw.println(" noscale");
2304 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002305 pw.print(" cur=");
2306 pw.print(mDisplayInfo.logicalWidth);
2307 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2308 pw.print(" app=");
2309 pw.print(mDisplayInfo.appWidth);
2310 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2311 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2312 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2313 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2314 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002315 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002316 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002317 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002318
Craig Mautnerdc548482014-02-05 13:35:24 -08002319 pw.println();
Adrian Roos5251b1d2018-03-23 18:57:43 +01002320 pw.print(prefix); pw.print("mLayoutSeq="); pw.println(mLayoutSeq);
Riddle Hsu654a6f92018-07-13 22:59:36 +08002321 pw.print(prefix);
2322 pw.print("mDeferredRotationPauseCount="); pw.println(mDeferredRotationPauseCount);
Adrian Roos5251b1d2018-03-23 18:57:43 +01002323
2324 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002325 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002326 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2327 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002328 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002329 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002330
Craig Mautnerdc548482014-02-05 13:35:24 -08002331 pw.println();
2332 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002333 pw.println();
2334 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002335 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002336 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002337 pw.print(" Exiting #"); pw.print(i);
2338 pw.print(' '); pw.print(token);
2339 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002340 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002341 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002342 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002343
Jorim Jaggi31f71702016-05-04 16:43:04 -07002344 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002345
2346 // Dump stack references
2347 final TaskStack homeStack = getHomeStack();
2348 if (homeStack != null) {
2349 pw.println(prefix + "homeStack=" + homeStack.getName());
2350 }
2351 final TaskStack pinnedStack = getPinnedStack();
2352 if (pinnedStack != null) {
2353 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2354 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002355 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002356 if (splitScreenPrimaryStack != null) {
2357 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2358 }
2359
2360 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002361 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002362 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002363 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002364
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002365 pw.println();
2366 mDisplayFrames.dump(prefix, pw);
Craig Mautner59c00972012-07-30 12:10:24 -07002367 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002368
2369 @Override
2370 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002371 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002372 }
2373
2374 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002375 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002376 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002377
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002378 /** Returns true if the stack in the windowing mode is visible. */
2379 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002380 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002381 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002382 }
2383
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002384 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002385 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002386 final int x = (int) xf;
2387 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002388 final WindowState touchedWin = getWindow(w -> {
2389 final int flags = w.mAttrs.flags;
2390 if (!w.isVisibleLw()) {
2391 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002392 }
2393 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002394 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002395 }
2396
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002397 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002398 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002399 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002400 }
2401
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002402 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002403
2404 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002405 return mTmpRegion.contains(x, y) || touchFlags == 0;
2406 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002407
2408 return touchedWin;
2409 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002410
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002411 boolean canAddToastWindowForUid(int uid) {
2412 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002413 // Also if the app is focused adding more than one toast at
2414 // a time for better backwards compatibility.
2415 final WindowState focusedWindowForUid = getWindow(w ->
2416 w.mOwnerUid == uid && w.isFocused());
2417 if (focusedWindowForUid != null) {
2418 return true;
2419 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002420 final WindowState win = getWindow(w ->
2421 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2422 && !w.mWindowRemovalAllowed);
2423 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002424 }
2425
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002426 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002427 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2428 return;
2429 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002430
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002431 // Used to communicate the old focus to the callback method.
2432 mTmpWindow = oldFocus;
2433
2434 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002435 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002436
2437 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002438 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002439
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002440 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002441
2442 if (mTmpWindow == null) {
2443 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2444 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002445 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002446 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002447 }
2448
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002449 /** Updates the layer assignment of windows on this display. */
2450 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002451 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07002452 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002453 if (setLayoutNeeded) {
2454 setLayoutNeeded();
2455 }
Robert Carrb1579c82017-09-05 14:54:47 -07002456
Robert Carrf59b8dd2017-10-02 18:58:36 -07002457 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002458 // the application of this transaction until the animation pass triggers
2459 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2460 // the hiding and showing of surfaces.
2461 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002462 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002463 }
2464
Wale Ogunwale1666e312016-12-16 11:27:18 -08002465 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2466 // moving containers or resizing them. Need to investigate the best way to have it automatically
2467 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002468 void layoutAndAssignWindowLayersIfNeeded() {
2469 mService.mWindowsChanged = true;
2470 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002471
2472 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2473 false /*updateInputWindows*/)) {
2474 assignWindowLayers(false /* setLayoutNeeded */);
2475 }
2476
2477 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2478 mService.mWindowPlacerLocked.performSurfacePlacement();
2479 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2480 }
2481
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002482 /** Returns true if a leaked surface was destroyed */
2483 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002484 // Used to indicate that a surface was leaked.
2485 mTmpWindow = null;
2486 forAllWindows(w -> {
2487 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002488 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002489 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002490 }
2491 if (!mService.mSessions.contains(wsa.mSession)) {
2492 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002493 + w + " surface=" + wsa.mSurfaceController
2494 + " token=" + w.mToken
2495 + " pid=" + w.mSession.mPid
2496 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002497 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002498 mService.mForceRemoves.add(w);
2499 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002500 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002501 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002502 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002503 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002504 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002505 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002506 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002507 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002508 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002509
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002510 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002511 }
2512
2513 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002514 * Determine and return the window that should be the IME target.
2515 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2516 * @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 +00002517 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002518 WindowState computeImeTarget(boolean updateImeTarget) {
2519 if (mService.mInputMethodWindow == null) {
2520 // There isn't an IME so there shouldn't be a target...That was easy!
2521 if (updateImeTarget) {
2522 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2523 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002524 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002525 }
2526 return null;
2527 }
2528
Chavi Weingarten3a748552018-05-14 17:32:42 +00002529 final WindowState curTarget = mService.mInputMethodTarget;
2530 if (!canUpdateImeTarget()) {
2531 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Defer updating IME target");
2532 return curTarget;
2533 }
2534
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002535 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2536 // same display. Or even when the current IME/target are not on the same screen as the next
2537 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002538 mUpdateImeTarget = updateImeTarget;
2539 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002540
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002541
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002542 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2543 // to be on top of it, but it is not -really- where input will go. So look down below
2544 // for a real window to target...
2545 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2546 final AppWindowToken token = target.mAppToken;
2547 if (token != null) {
2548 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2549 if (betterTarget != null) {
2550 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002551 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002552 }
2553 }
2554
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002555 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2556 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002557
chaviw1c13ad32018-06-12 16:44:23 -07002558 // Now, a special case -- if the last target's window is in the process of exiting, but
2559 // not removed, and the new target is home, keep on the last target to avoid flicker.
2560 // Home is a special case since its above other stacks in the ordering list, but layed
2561 // out below the others.
2562 if (curTarget != null && !curTarget.mRemoved && curTarget.isDisplayedLw()
2563 && curTarget.isClosing() && (target == null || target.isActivityTypeHome())) {
chaviw4ad54912018-05-30 11:05:44 -07002564 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "New target is home while current target is "
Chavi Weingarten3a748552018-05-14 17:32:42 +00002565 + "closing, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002566 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002567 }
2568
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002569 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2570 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002571
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002572 if (target == null) {
2573 if (updateImeTarget) {
2574 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2575 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2576 + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002577 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002578 }
2579
2580 return null;
2581 }
2582
2583 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002584 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2585 if (token != null) {
2586
2587 // Now some fun for dealing with window animations that modify the Z order. We need
2588 // to look at all windows below the current target that are in this app, finding the
2589 // highest visible one in layering.
2590 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002591 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002592 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002593 }
2594
2595 if (highestTarget != null) {
2596 final AppTransition appTransition = mService.mAppTransition;
2597 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2598 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2599 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002600 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002601
2602 if (appTransition.isTransitionSet()) {
2603 // If we are currently setting up for an animation, hold everything until we
2604 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002605 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002606 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002607 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002608 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002609 // If the window we are currently targeting is involved with an animation,
2610 // and it is on top of the next target we will be over, then hold off on
2611 // moving until that is done.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002612 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002613 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002614 }
2615 }
2616 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002617
2618 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2619 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002620 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002621 }
2622
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002623 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002624 }
2625
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002626 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002627 if (target == mService.mInputMethodTarget
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002628 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002629 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002630 }
2631
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002632 mService.mInputMethodTarget = target;
2633 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002634 assignWindowLayers(false /* setLayoutNeeded */);
2635 }
2636
2637 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2638 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2639 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2640 }
2641
2642 // Used to indicate we have reached the first window in the range we are interested in.
2643 mTmpWindow = null;
2644
2645 // TODO: Figure-out a more efficient way to do this.
2646 final WindowState candidate = getWindow(w -> {
2647 if (w == top) {
2648 // Reached the first window in the range we are interested in.
2649 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002650 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002651 if (mTmpWindow == null) {
2652 return false;
2653 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002654
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002655 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2656 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002657 }
2658 // If we reached the bottom of the range of windows we are considering,
2659 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002660 if (w == bottom) {
2661 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002662 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002663 return false;
2664 });
2665
2666 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002667 }
2668
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002669 void setLayoutNeeded() {
2670 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2671 mLayoutNeeded = true;
2672 }
2673
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002674 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002675 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2676 mLayoutNeeded = false;
2677 }
2678
2679 boolean isLayoutNeeded() {
2680 return mLayoutNeeded;
2681 }
2682
Wale Ogunwale02319a62016-09-26 15:21:22 -07002683 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2684 if (mTokenMap.isEmpty()) {
2685 return;
2686 }
2687 pw.println(" Display #" + mDisplayId);
2688 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2689 while (it.hasNext()) {
2690 final WindowToken token = it.next();
2691 pw.print(" ");
2692 pw.print(token);
2693 if (dumpAll) {
2694 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002695 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07002696 } else {
2697 pw.println();
2698 }
2699 }
2700 }
2701
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002702 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002703 final int[] index = new int[1];
2704 forAllWindows(w -> {
2705 final WindowStateAnimator wAnim = w.mWinAnimator;
2706 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2707 index[0] = index[0] + 1;
2708 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002709 }
2710
Jorim Jaggife762342016-10-13 14:33:27 +02002711 /**
2712 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2713 */
2714 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2715 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002716 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002717 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2718 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01002719 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02002720 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002721 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002722 }
2723
Wale Ogunwale494009b82016-10-21 09:01:38 -07002724 boolean checkWaitingForWindows() {
2725
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002726 mHaveBootMsg = false;
2727 mHaveApp = false;
2728 mHaveWallpaper = false;
2729 mHaveKeyguard = true;
2730
2731 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002732 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2733 return true;
2734 }
2735 if (w.isDrawnLw()) {
2736 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002737 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002738 } else if (w.mAttrs.type == TYPE_APPLICATION
2739 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002740 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002741 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002742 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002743 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002744 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002745 }
2746 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002747 return false;
2748 });
2749
2750 if (visibleWindow != null) {
2751 // We have a visible window.
2752 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002753 }
2754
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002755 // if the wallpaper service is disabled on the device, we're never going to have
2756 // wallpaper, don't bother waiting for it
2757 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2758 com.android.internal.R.bool.config_enableWallpaperService)
Daichi Hironoc1432222018-03-29 13:06:07 +09002759 && mService.mContext.getResources().getBoolean(
2760 com.android.internal.R.bool.config_checkWallpaperAtBoot)
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002761 && !mService.mOnlyCore;
2762
Wale Ogunwale494009b82016-10-21 09:01:38 -07002763 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2764 "******** booted=" + mService.mSystemBooted
2765 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002766 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2767 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2768 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002769
2770 // If we are turning on the screen to show the boot message, don't do it until the boot
2771 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002772 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002773 return true;
2774 }
2775
2776 // If we are turning on the screen after the boot is completed normally, don't do so until
2777 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002778 if (mService.mSystemBooted
2779 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002780 return true;
2781 }
2782
2783 return false;
2784 }
2785
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002786 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002787 mTmpWindowAnimator = animator;
2788 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002789 }
2790
2791 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002792 resetAnimationBackgroundAnimator();
2793
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002794 // Used to indicate a detached wallpaper.
2795 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002796 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002797
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002798 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002799
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002800 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002801 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002802 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2803 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002804 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2805 }
2806 }
2807
Wale Ogunwale494009b82016-10-21 09:01:38 -07002808 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002809 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002810 if (imFocus == null) {
2811 return false;
2812 }
2813
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002814 if (DEBUG_INPUT_METHOD) {
2815 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2816 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2817 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002818 }
2819
Wale Ogunwale494009b82016-10-21 09:01:38 -07002820 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2821
2822 if (DEBUG_INPUT_METHOD) {
2823 Slog.i(TAG_WM, "IM target client: " + imeClient);
2824 if (imeClient != null) {
2825 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2826 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2827 }
2828 }
2829
2830 return imeClient != null && imeClient.asBinder() == client.asBinder();
2831 }
2832
2833 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002834 final WindowState win = getWindow(
2835 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2836 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002837 }
2838
2839 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002840 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002841 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002842 final int curValue = w.mSystemUiVisibility;
2843 final int diff = (curValue ^ visibility) & globalDiff;
2844 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002845 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002846 w.mSeq++;
2847 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002848 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002849 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2850 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002851 visibility, newValue, diff);
2852 }
2853 } catch (RemoteException e) {
2854 // so sorry
2855 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002856 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002857 }
2858
2859 void onWindowFreezeTimeout() {
2860 Slog.w(TAG_WM, "Window freeze timeout expired.");
2861 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002862
2863 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002864 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002865 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002866 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002867 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002868 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2869 - mService.mDisplayFreezeTime);
2870 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002871 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002872 mService.mWindowPlacerLocked.performSurfacePlacement();
2873 }
2874
2875 void waitForAllWindowsDrawn() {
2876 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002877 forAllWindows(w -> {
2878 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2879 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2880 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002881 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002882 w.mLastContentInsets.set(-1, -1, -1, -1);
2883 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002884 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002885 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002886 }
2887
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002888 // TODO: Super crazy long method that should be broken down...
2889 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2890
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002891 final int dw = mDisplayInfo.logicalWidth;
2892 final int dh = mDisplayInfo.logicalHeight;
2893 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2894
2895 mTmpUpdateAllDrawn.clear();
2896
2897 int repeats = 0;
2898 do {
2899 repeats++;
2900 if (repeats > 6) {
2901 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2902 clearLayoutNeeded();
2903 break;
2904 }
2905
2906 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2907 pendingLayoutChanges);
2908
Andrii Kulian839def92016-11-02 10:58:58 -07002909 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2910 // the wallpaper window jumping across displays.
2911 // Remove check for default display when there will be support for multiple wallpaper
2912 // targets (on different displays).
2913 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002914 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002915 }
2916
Riddle Hsu654a6f92018-07-13 22:59:36 +08002917 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002918 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
Robert Carrae606b42018-02-15 15:36:23 -08002919 if (mService.updateOrientationFromAppTokensLocked(mDisplayId)) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002920 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002921 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002922 }
2923 }
2924
2925 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2926 setLayoutNeeded();
2927 }
2928
2929 // FIRST LOOP: Perform a layout, if needed.
2930 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2931 performLayout(repeats == 1, false /* updateInputWindows */);
2932 } else {
2933 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2934 }
2935
2936 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2937 pendingLayoutChanges = 0;
2938
2939 if (isDefaultDisplay) {
2940 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002941 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002942 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2943 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2944 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2945 }
2946 } while (pendingLayoutChanges != 0);
2947
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002948 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002949
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002950 mTmpRecoveringMemory = recoveringMemory;
2951 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01002952 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002953
2954 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002955 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2956 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2957 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002958 true /* inTraversal, must call performTraversalInTrans... below */);
2959
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002960 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2961 if (wallpaperVisible != mLastWallpaperVisible) {
2962 mLastWallpaperVisible = wallpaperVisible;
2963 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2964 }
2965
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002966 while (!mTmpUpdateAllDrawn.isEmpty()) {
2967 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2968 // See if any windows have been drawn, so they (and others associated with them)
2969 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002970 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002971 }
2972
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002973 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002974 }
2975
Bryce Leef3c6a472017-11-14 14:53:06 -08002976 private void updateBounds() {
2977 calculateBounds(mTmpBounds);
2978 setBounds(mTmpBounds);
2979 }
2980
2981 // Determines the current display bounds based on the current state
2982 private void calculateBounds(Rect out) {
2983 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
2984 final int orientation = mDisplayInfo.rotation;
2985 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2986 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
2987 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
2988 int width = mDisplayInfo.logicalWidth;
2989 int left = (physWidth - width) / 2;
2990 int height = mDisplayInfo.logicalHeight;
2991 int top = (physHeight - height) / 2;
2992 out.set(left, top, left + width, top + height);
2993 }
2994
2995 @Override
2996 public void getBounds(Rect out) {
2997 calculateBounds(out);
2998 }
2999
3000 private void getBounds(Rect out, int orientation) {
3001 getBounds(out);
3002
3003 // Rotate the Rect if needed.
3004 final int currentRotation = mDisplayInfo.rotation;
3005 final int rotationDelta = deltaRotation(currentRotation, orientation);
3006 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
3007 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
3008 mTmpRectF.set(out);
3009 mTmpMatrix.mapRect(mTmpRectF);
3010 mTmpRectF.round(out);
3011 }
3012 }
3013
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003014 void performLayout(boolean initial, boolean updateInputWindows) {
3015 if (!isLayoutNeeded()) {
3016 return;
3017 }
3018 clearLayoutNeeded();
3019
3020 final int dw = mDisplayInfo.logicalWidth;
3021 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003022 if (DEBUG_LAYOUT) {
3023 Slog.v(TAG, "-------------------------------------");
3024 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
3025 }
3026
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01003027 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
3028 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00003029 // TODO: Not sure if we really need to set the rotation here since we are updating from the
3030 // display info above...
3031 mDisplayFrames.mRotation = mRotation;
3032 mService.mPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003033 if (isDefaultDisplay) {
3034 // Not needed on non-default displays.
3035 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
3036 mService.mScreenRect.set(0, 0, dw, dh);
3037 }
3038
Adrian Roos5251b1d2018-03-23 18:57:43 +01003039 int seq = mLayoutSeq + 1;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003040 if (seq < 0) seq = 0;
Adrian Roos5251b1d2018-03-23 18:57:43 +01003041 mLayoutSeq = seq;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003042
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003043 // Used to indicate that we have processed the dream window and all additional windows are
3044 // behind it.
3045 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003046 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003047
3048 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003049 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003050
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003051 // Used to indicate that we have processed the dream window and all additional attached
3052 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003053 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003054 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003055
3056 // Now perform layout of attached windows, which usually depend on the position of the
3057 // window they are attached to. XXX does not deal with windows that are attached to windows
3058 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003059 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003060
3061 // Window frames may have changed. Tell the input dispatcher about it.
3062 mService.mInputMonitor.layoutInputConsumers(dw, dh);
3063 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
3064 if (updateInputWindows) {
3065 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
3066 }
3067
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003068 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
3069 }
3070
3071 /**
3072 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
3073 * In portrait mode, it grabs the full screenshot.
3074 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003075 * @param config of the output bitmap
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003076 */
chaviw0315a1a2018-03-05 15:28:35 -08003077 Bitmap screenshotDisplayLocked(Bitmap.Config config) {
3078 if (!mService.mPolicy.isScreenOn()) {
3079 if (DEBUG_SCREENSHOT) {
3080 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003081 }
chaviw0315a1a2018-03-05 15:28:35 -08003082 return null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003083 }
chaviwfbe47df2017-11-10 16:14:49 -08003084
chaviw0315a1a2018-03-05 15:28:35 -08003085 int dw = mDisplayInfo.logicalWidth;
3086 int dh = mDisplayInfo.logicalHeight;
chaviwfbe47df2017-11-10 16:14:49 -08003087
chaviw0315a1a2018-03-05 15:28:35 -08003088 if (dw <= 0 || dh <= 0) {
3089 return null;
3090 }
chaviwfbe47df2017-11-10 16:14:49 -08003091
chaviw0315a1a2018-03-05 15:28:35 -08003092 final Rect frame = new Rect(0, 0, dw, dh);
chaviwfbe47df2017-11-10 16:14:49 -08003093
chaviw0315a1a2018-03-05 15:28:35 -08003094 // The screenshot API does not apply the current screen rotation.
3095 int rot = mDisplay.getRotation();
chaviwfbe47df2017-11-10 16:14:49 -08003096
chaviw0315a1a2018-03-05 15:28:35 -08003097 if (rot == ROTATION_90 || rot == ROTATION_270) {
3098 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3099 }
chaviwfbe47df2017-11-10 16:14:49 -08003100
chaviw0315a1a2018-03-05 15:28:35 -08003101 // SurfaceFlinger is not aware of orientation, so convert our logical
3102 // crop to SurfaceFlinger's portrait orientation.
3103 convertCropForSurfaceFlinger(frame, rot, dw, dh);
3104
3105 final ScreenRotationAnimation screenRotationAnimation =
3106 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3107 final boolean inRotation = screenRotationAnimation != null &&
3108 screenRotationAnimation.isAnimating();
3109 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
3110
3111 // TODO(b/68392460): We should screenshot Task controls directly
3112 // but it's difficult at the moment as the Task doesn't have the
3113 // correct size set.
3114 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, 0, 1, inRotation, rot);
3115 if (bitmap == null) {
3116 Slog.w(TAG_WM, "Failed to take screenshot");
3117 return null;
3118 }
3119
3120 // Create a copy of the screenshot that is immutable and backed in ashmem.
3121 // This greatly reduces the overhead of passing the bitmap between processes.
3122 final Bitmap ret = bitmap.createAshmemBitmap(config);
3123 bitmap.recycle();
3124 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003125 }
3126
3127 // TODO: Can this use createRotationMatrix()?
3128 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3129 if (rot == Surface.ROTATION_90) {
3130 final int tmp = crop.top;
3131 crop.top = dw - crop.right;
3132 crop.right = crop.bottom;
3133 crop.bottom = dw - crop.left;
3134 crop.left = tmp;
3135 } else if (rot == Surface.ROTATION_180) {
3136 int tmp = crop.top;
3137 crop.top = dh - crop.bottom;
3138 crop.bottom = dh - tmp;
3139 tmp = crop.right;
3140 crop.right = dw - crop.left;
3141 crop.left = dw - tmp;
3142 } else if (rot == Surface.ROTATION_270) {
3143 final int tmp = crop.top;
3144 crop.top = crop.left;
3145 crop.left = dh - crop.bottom;
3146 crop.bottom = crop.right;
3147 crop.right = dh - tmp;
3148 }
3149 }
3150
3151 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003152 // Used to indicate the layout is needed.
3153 mTmpWindow = null;
3154
3155 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003156 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003157 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003158 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003159 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003160 w.setDisplayLayoutNeeded();
3161 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003162 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003163
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003164 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003165 mService.mWindowPlacerLocked.performSurfacePlacement();
3166 }
3167 }
3168
Wale Ogunwale1666e312016-12-16 11:27:18 -08003169 void setExitingTokensHasVisible(boolean hasVisible) {
3170 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3171 mExitingTokens.get(i).hasVisible = hasVisible;
3172 }
3173
3174 // Initialize state of exiting applications.
3175 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3176 }
3177
3178 void removeExistingTokensIfPossible() {
3179 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3180 final WindowToken token = mExitingTokens.get(i);
3181 if (!token.hasVisible) {
3182 mExitingTokens.remove(i);
3183 }
3184 }
3185
3186 // Time to remove any exiting applications?
3187 mTaskStackContainers.removeExistingAppTokensIfPossible();
3188 }
3189
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003190 @Override
3191 void onDescendantOverrideConfigurationChanged() {
3192 setLayoutNeeded();
3193 mService.requestTraversal();
3194 }
3195
David Stevens9440dc82017-03-16 19:00:20 -07003196 boolean okToDisplay() {
3197 if (mDisplayId == DEFAULT_DISPLAY) {
3198 return !mService.mDisplayFrozen
3199 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3200 }
3201 return mDisplayInfo.state == Display.STATE_ON;
3202 }
3203
3204 boolean okToAnimate() {
3205 return okToDisplay() &&
3206 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3207 }
3208
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003209 static final class TaskForResizePointSearchResult {
3210 boolean searchDone;
3211 Task taskForResize;
3212
3213 void reset() {
3214 searchDone = false;
3215 taskForResize = null;
3216 }
3217 }
Robert Carr3b716242016-08-16 16:02:21 -07003218
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003219 private static final class ApplySurfaceChangesTransactionState {
3220 boolean displayHasContent;
3221 boolean obscured;
3222 boolean syswin;
3223 boolean focusDisplayed;
3224 float preferredRefreshRate;
3225 int preferredModeId;
3226
3227 void reset() {
3228 displayHasContent = false;
3229 obscured = false;
3230 syswin = false;
3231 focusDisplayed = false;
3232 preferredRefreshRate = 0;
3233 preferredModeId = 0;
3234 }
3235 }
3236
3237 private static final class ScreenshotApplicationState {
3238 WindowState appWin;
3239 int maxLayer;
3240 int minLayer;
3241 boolean screenshotReady;
3242
3243 void reset(boolean screenshotReady) {
3244 appWin = null;
3245 maxLayer = 0;
3246 minLayer = 0;
3247 this.screenshotReady = screenshotReady;
3248 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3249 }
3250 }
3251
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003252 /**
3253 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3254 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3255 * homogeneous children type which is currently required by sub-classes of
3256 * {@link WindowContainer} class.
3257 */
3258 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3259
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003260 DisplayChildWindowContainer(WindowManagerService service) {
3261 super(service);
3262 }
3263
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003264 @Override
3265 boolean fillsParent() {
3266 return true;
3267 }
3268
3269 @Override
3270 boolean isVisible() {
3271 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003272 }
3273 }
3274
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003275 /**
3276 * Window container class that contains all containers on this display relating to Apps.
3277 * I.e Activities.
3278 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003279 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003280 /**
3281 * A control placed at the appropriate level for transitions to occur.
3282 */
chaviw23ee71c2017-12-18 11:29:41 -08003283 SurfaceControl mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02003284 SurfaceControl mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02003285 SurfaceControl mHomeAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003286
Robert Carrf7a7ca82017-12-06 13:17:07 -08003287 /**
3288 * Given that the split-screen divider does not have an AppWindowToken, it
3289 * will have to live inside of a "NonAppWindowContainer", in particular
3290 * {@link DisplayContent#mAboveAppWindowsContainers}. However, in visual Z order
3291 * it will need to be interleaved with some of our children, appearing on top of
3292 * both docked stacks but underneath any assistant stacks.
3293 *
3294 * To solve this problem we have this anchor control, which will always exist so
3295 * we can always assign it the correct value in our {@link #assignChildLayers}.
3296 * Likewise since it always exists, {@link AboveAppWindowContainers} can always
3297 * assign the divider a layer relative to it. This way we prevent linking lifecycle
3298 * events between the two containers.
3299 */
3300 SurfaceControl mSplitScreenDividerAnchor = null;
3301
Wale Ogunwale61911492017-10-11 08:50:50 -07003302 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3303 // through the list to find them.
3304 private TaskStack mHomeStack = null;
3305 private TaskStack mPinnedStack = null;
3306 private TaskStack mSplitScreenPrimaryStack = null;
3307
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003308 TaskStackContainers(WindowManagerService service) {
3309 super(service);
3310 }
3311
Wale Ogunwale61911492017-10-11 08:50:50 -07003312 /**
3313 * Returns the topmost stack on the display that is compatible with the input windowing mode
3314 * and activity type. Null is no compatible stack on the display.
3315 */
3316 TaskStack getStack(int windowingMode, int activityType) {
3317 if (activityType == ACTIVITY_TYPE_HOME) {
3318 return mHomeStack;
3319 }
3320 if (windowingMode == WINDOWING_MODE_PINNED) {
3321 return mPinnedStack;
3322 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3323 return mSplitScreenPrimaryStack;
3324 }
3325 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3326 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003327 if (activityType == ACTIVITY_TYPE_UNDEFINED
3328 && windowingMode == stack.getWindowingMode()) {
3329 // Passing in undefined type means we want to match the topmost stack with the
3330 // windowing mode.
3331 return stack;
3332 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003333 if (stack.isCompatible(windowingMode, activityType)) {
3334 return stack;
3335 }
3336 }
3337 return null;
3338 }
3339
3340 @VisibleForTesting
3341 TaskStack getTopStack() {
3342 return mTaskStackContainers.getChildCount() > 0
3343 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3344 }
3345
3346 TaskStack getHomeStack() {
3347 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3348 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3349 }
3350 return mHomeStack;
3351 }
3352
3353 TaskStack getPinnedStack() {
3354 return mPinnedStack;
3355 }
3356
Matthew Ng64e77cf2017-10-31 14:01:31 -07003357 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003358 return mSplitScreenPrimaryStack;
3359 }
3360
Winson Chunge2d72172018-01-25 17:46:20 +00003361 ArrayList<Task> getVisibleTasks() {
3362 final ArrayList<Task> visibleTasks = new ArrayList<>();
3363 forAllTasks(task -> {
3364 if (task.isVisible()) {
3365 visibleTasks.add(task);
3366 }
3367 });
3368 return visibleTasks;
3369 }
3370
Andrii Kulian839def92016-11-02 10:58:58 -07003371 /**
3372 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003373 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003374 */
3375 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003376 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003377 addChild(stack, onTop);
3378 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003379 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003380
Wale Ogunwale61911492017-10-11 08:50:50 -07003381 void onStackWindowingModeChanged(TaskStack stack) {
3382 removeStackReferenceIfNeeded(stack);
3383 addStackReferenceIfNeeded(stack);
3384 if (stack == mPinnedStack && getTopStack() != stack) {
3385 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3386 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3387 }
3388 }
3389
3390 private void addStackReferenceIfNeeded(TaskStack stack) {
3391 if (stack.isActivityTypeHome()) {
Louis Chang6322c422018-07-18 18:37:28 +08003392 // TODO(b/111363427) Rollback to throws exceptions once we figure out how to
3393 // properly deal with home type stack when external display removed
Wale Ogunwale61911492017-10-11 08:50:50 -07003394 if (mHomeStack != null) {
Louis Chang6322c422018-07-18 18:37:28 +08003395 // throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
3396 // + mHomeStack + " already exist on display=" + this + " stack=" + stack);
3397 Slog.e(TAG, "addStackReferenceIfNeeded: home stack="
Wale Ogunwale61911492017-10-11 08:50:50 -07003398 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
Louis Chang6322c422018-07-18 18:37:28 +08003399 } else {
3400 mHomeStack = stack;
Wale Ogunwale61911492017-10-11 08:50:50 -07003401 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003402 }
3403 final int windowingMode = stack.getWindowingMode();
3404 if (windowingMode == WINDOWING_MODE_PINNED) {
3405 if (mPinnedStack != null) {
3406 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3407 + mPinnedStack + " already exist on display=" + this
3408 + " stack=" + stack);
3409 }
3410 mPinnedStack = stack;
3411 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3412 if (mSplitScreenPrimaryStack != null) {
3413 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3414 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3415 + " already exist on display=" + this + " stack=" + stack);
3416 }
3417 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003418 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003419 }
3420 }
3421
3422 private void removeStackReferenceIfNeeded(TaskStack stack) {
3423 if (stack == mHomeStack) {
3424 mHomeStack = null;
3425 } else if (stack == mPinnedStack) {
3426 mPinnedStack = null;
3427 } else if (stack == mSplitScreenPrimaryStack) {
3428 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003429 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3430 mService.setDockedStackCreateStateLocked(
3431 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3432 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003433 }
Andrii Kulian839def92016-11-02 10:58:58 -07003434 }
3435
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003436 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003437 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3438 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003439 addChild(stack, addIndex);
3440 setLayoutNeeded();
3441 }
3442
Wale Ogunwale61911492017-10-11 08:50:50 -07003443 @Override
3444 protected void removeChild(TaskStack stack) {
3445 super.removeChild(stack);
3446 removeStackReferenceIfNeeded(stack);
3447 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003448
3449 @Override
3450 boolean isOnTop() {
3451 // Considered always on top
3452 return true;
3453 }
3454
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003455 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003456 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003457 if (child.getWindowConfiguration().isAlwaysOnTop()
3458 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003459 // This stack is always-on-top, override the default behavior.
3460 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3461
3462 // Moving to its current position, as we must call super but we don't want to
3463 // perform any meaningful action.
3464 final int currentPosition = mChildren.indexOf(child);
3465 super.positionChildAt(currentPosition, child, false /* includingParents */);
3466 return;
3467 }
3468
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003469 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3470 super.positionChildAt(targetPosition, child, includingParents);
3471
3472 setLayoutNeeded();
3473 }
3474
3475 /**
3476 * When stack is added or repositioned, find a proper position for it.
3477 * This will make sure that pinned stack always stays on top.
3478 * @param requestedPosition Position requested by caller.
3479 * @param stack Stack to be added or positioned.
3480 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3481 * @return The proper position for the stack.
3482 */
3483 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09003484 if (stack.inPinnedWindowingMode()) {
Kazuki Takisef85197b2018-06-18 18:18:36 +09003485 return POSITION_TOP;
Kazuki Takise148d00a2018-05-31 15:32:19 +09003486 }
3487
Kazuki Takisef85197b2018-06-18 18:18:36 +09003488 final int topChildPosition = mChildren.size() - 1;
3489 int belowAlwaysOnTopPosition = POSITION_BOTTOM;
3490 for (int i = topChildPosition; i >= 0; --i) {
3491 if (getStacks().get(i) != stack && !getStacks().get(i).isAlwaysOnTop()) {
3492 belowAlwaysOnTopPosition = i;
3493 break;
3494 }
3495 }
3496
3497 // The max possible position we can insert the stack at.
3498 int maxPosition = POSITION_TOP;
3499 // The min possible position we can insert the stack at.
3500 int minPosition = POSITION_BOTTOM;
3501
3502 if (stack.isAlwaysOnTop()) {
3503 if (hasPinnedStack()) {
3504 // Always-on-top stacks go below the pinned stack.
3505 maxPosition = getStacks().indexOf(mPinnedStack) - 1;
3506 }
3507 // Always-on-top stacks need to be above all other stacks.
3508 minPosition = belowAlwaysOnTopPosition !=
3509 POSITION_BOTTOM ? belowAlwaysOnTopPosition : topChildPosition;
3510 } else {
3511 // Other stacks need to be below the always-on-top stacks.
3512 maxPosition = belowAlwaysOnTopPosition !=
Arthur Hung928d8dc2018-07-18 15:38:05 +08003513 POSITION_BOTTOM ? belowAlwaysOnTopPosition : 0;
Kazuki Takisef85197b2018-06-18 18:18:36 +09003514 }
3515
3516 int targetPosition = requestedPosition;
3517 targetPosition = Math.min(targetPosition, maxPosition);
3518 targetPosition = Math.max(targetPosition, minPosition);
3519
3520 int prevPosition = getStacks().indexOf(stack);
3521 // The positions we calculated above (maxPosition, minPosition) do not take into
3522 // consideration the following edge cases.
3523 // 1) We need to adjust the position depending on the value "adding".
3524 // 2) When we are moving a stack to another position, we also need to adjust the
3525 // position depending on whether the stack is moving to a higher or lower position.
3526 if ((targetPosition != requestedPosition) &&
3527 (adding || targetPosition < prevPosition)) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09003528 targetPosition++;
3529 }
3530
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003531 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003532 }
3533
3534 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003535 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3536 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003537 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003538 if (super.forAllWindows(callback, traverseTopToBottom)) {
3539 return true;
3540 }
3541 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3542 return true;
3543 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003544 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003545 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3546 return true;
3547 }
3548 if (super.forAllWindows(callback, traverseTopToBottom)) {
3549 return true;
3550 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003551 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003552 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003553 }
3554
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003555 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003556 boolean traverseTopToBottom) {
3557 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3558 // app tokens.
3559 // TODO: Investigate if we need to continue to do this or if we can just process them
3560 // in-order.
3561 if (traverseTopToBottom) {
3562 for (int i = mChildren.size() - 1; i >= 0; --i) {
3563 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3564 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003565 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3566 traverseTopToBottom)) {
3567 return true;
3568 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003569 }
3570 }
3571 } else {
3572 final int count = mChildren.size();
3573 for (int i = 0; i < count; ++i) {
3574 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3575 final int appTokensCount = appTokens.size();
3576 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003577 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3578 traverseTopToBottom)) {
3579 return true;
3580 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003581 }
3582 }
3583 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003584 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003585 }
3586
Wale Ogunwale1666e312016-12-16 11:27:18 -08003587 void setExitingTokensHasVisible(boolean hasVisible) {
3588 for (int i = mChildren.size() - 1; i >= 0; --i) {
3589 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3590 for (int j = appTokens.size() - 1; j >= 0; --j) {
3591 appTokens.get(j).hasVisible = hasVisible;
3592 }
3593 }
3594 }
3595
3596 void removeExistingAppTokensIfPossible() {
3597 for (int i = mChildren.size() - 1; i >= 0; --i) {
3598 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3599 for (int j = appTokens.size() - 1; j >= 0; --j) {
3600 final AppWindowToken token = appTokens.get(j);
3601 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3602 && (!token.mIsExiting || token.isEmpty())) {
3603 // Make sure there is no animation running on this token, so any windows
3604 // associated with it will be removed as soon as their animations are
3605 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003606 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003607 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3608 "performLayout: App token exiting now removed" + token);
3609 token.removeIfPossible();
3610 }
3611 }
3612 }
3613 }
3614
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003615 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003616 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003617 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
3618 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003619 // Apps and their containers are not allowed to specify an orientation while the
Wale Ogunwaled0c225c2018-06-15 14:47:46 -07003620 // docked or freeform stack is visible...except for the home stack if the docked
3621 // stack is minimized and it actually set something and the bounds is different from
3622 // the display.
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003623 if (mHomeStack != null && mHomeStack.isVisible()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07003624 && mDividerControllerLocked.isMinimizedDock()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07003625 && !(mDividerControllerLocked.isHomeStackResizable()
3626 && mHomeStack.matchParentBounds())) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003627 final int orientation = mHomeStack.getOrientation();
3628 if (orientation != SCREEN_ORIENTATION_UNSET) {
3629 return orientation;
3630 }
3631 }
3632 return SCREEN_ORIENTATION_UNSPECIFIED;
3633 }
3634
3635 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07003636 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
3637 PackageManager.FEATURE_AUTOMOTIVE);
3638 if (isCar) {
3639 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
3640 // allow anything but the default orientation.
3641 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07003642 "Forcing UNSPECIFIED orientation in car for display id=" + mDisplayId
3643 + ". Ignoring " + orientation);
Dean Harding3e5a1522017-10-06 09:19:01 -07003644 return SCREEN_ORIENTATION_UNSPECIFIED;
3645 }
3646
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003647 if (orientation != SCREEN_ORIENTATION_UNSET
3648 && orientation != SCREEN_ORIENTATION_BEHIND) {
3649 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07003650 "App is requesting an orientation, return " + orientation
3651 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003652 return orientation;
3653 }
3654
3655 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07003656 "No app is requesting an orientation, return " + mLastOrientation
3657 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003658 // The next app has not been requested to be visible, so we keep the current orientation
3659 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003660 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003661 }
Robert Carrb1579c82017-09-05 14:54:47 -07003662
3663 @Override
3664 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carr2f8aa392018-01-31 14:46:51 -08003665 assignStackOrdering(t);
Robert Carrf7a7ca82017-12-06 13:17:07 -08003666
Robert Carr2f8aa392018-01-31 14:46:51 -08003667 for (int i = 0; i < mChildren.size(); i++) {
3668 final TaskStack s = mChildren.get(i);
3669 s.assignChildLayers(t);
3670 }
3671 }
3672
3673 void assignStackOrdering(SurfaceControl.Transaction t) {
Jorim Jaggibe418292018-03-26 16:14:12 +02003674
Robert Carrf7a7ca82017-12-06 13:17:07 -08003675 final int HOME_STACK_STATE = 0;
3676 final int NORMAL_STACK_STATE = 1;
3677 final int ALWAYS_ON_TOP_STATE = 2;
3678
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003679 int layer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08003680 int layerForAnimationLayer = 0;
Jorim Jaggibe418292018-03-26 16:14:12 +02003681 int layerForBoostedAnimationLayer = 0;
Jorim Jaggi391790622018-04-18 15:30:44 +02003682 int layerForHomeAnimationLayer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08003683
Robert Carrf7a7ca82017-12-06 13:17:07 -08003684 for (int state = 0; state <= ALWAYS_ON_TOP_STATE; state++) {
3685 for (int i = 0; i < mChildren.size(); i++) {
3686 final TaskStack s = mChildren.get(i);
Robert Carr2f8aa392018-01-31 14:46:51 -08003687 if (state == HOME_STACK_STATE && !s.isActivityTypeHome()) {
3688 continue;
3689 } else if (state == NORMAL_STACK_STATE && (s.isActivityTypeHome()
3690 || s.isAlwaysOnTop())) {
3691 continue;
3692 } else if (state == ALWAYS_ON_TOP_STATE && !s.isAlwaysOnTop()) {
3693 continue;
3694 }
3695 s.assignLayer(t, layer++);
3696 if (s.inSplitScreenWindowingMode() && mSplitScreenDividerAnchor != null) {
3697 t.setLayer(mSplitScreenDividerAnchor, layer++);
3698 }
Winson Chungd41f71d2018-03-16 15:26:07 -07003699 if ((s.isTaskAnimating() || s.isAppAnimating())
3700 && state != ALWAYS_ON_TOP_STATE) {
Robert Carr2f8aa392018-01-31 14:46:51 -08003701 // Ensure the animation layer ends up above the
3702 // highest animating stack and no higher.
3703 layerForAnimationLayer = layer++;
Robert Carrf7a7ca82017-12-06 13:17:07 -08003704 }
Jorim Jaggibe418292018-03-26 16:14:12 +02003705 if (state != ALWAYS_ON_TOP_STATE) {
3706 layerForBoostedAnimationLayer = layer++;
3707 }
Robert Carrf7a7ca82017-12-06 13:17:07 -08003708 }
Jorim Jaggi391790622018-04-18 15:30:44 +02003709 if (state == HOME_STACK_STATE) {
3710 layerForHomeAnimationLayer = layer++;
3711 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003712 }
Robert Carr2f8aa392018-01-31 14:46:51 -08003713 if (mAppAnimationLayer != null) {
3714 t.setLayer(mAppAnimationLayer, layerForAnimationLayer);
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003715 }
Jorim Jaggibe418292018-03-26 16:14:12 +02003716 if (mBoostedAppAnimationLayer != null) {
3717 t.setLayer(mBoostedAppAnimationLayer, layerForBoostedAnimationLayer);
3718 }
Jorim Jaggi391790622018-04-18 15:30:44 +02003719 if (mHomeAppAnimationLayer != null) {
3720 t.setLayer(mHomeAppAnimationLayer, layerForHomeAnimationLayer);
3721 }
Robert Carrb1579c82017-09-05 14:54:47 -07003722 }
3723
3724 @Override
Jorim Jaggi391790622018-04-18 15:30:44 +02003725 SurfaceControl getAppAnimationLayer(@AnimationLayer int animationLayer) {
3726 switch (animationLayer) {
3727 case ANIMATION_LAYER_BOOSTED:
3728 return mBoostedAppAnimationLayer;
3729 case ANIMATION_LAYER_HOME:
3730 return mHomeAppAnimationLayer;
3731 case ANIMATION_LAYER_STANDARD:
3732 default:
3733 return mAppAnimationLayer;
3734 }
chaviw23ee71c2017-12-18 11:29:41 -08003735 }
3736
Robert Carrf7a7ca82017-12-06 13:17:07 -08003737 SurfaceControl getSplitScreenDividerAnchor() {
3738 return mSplitScreenDividerAnchor;
3739 }
3740
chaviw23ee71c2017-12-18 11:29:41 -08003741 @Override
Robert Carrb1579c82017-09-05 14:54:47 -07003742 void onParentSet() {
3743 super.onParentSet();
3744 if (getParent() != null) {
chaviw23ee71c2017-12-18 11:29:41 -08003745 mAppAnimationLayer = makeChildSurface(null)
3746 .setName("animationLayer")
3747 .build();
Jorim Jaggibe418292018-03-26 16:14:12 +02003748 mBoostedAppAnimationLayer = makeChildSurface(null)
3749 .setName("boostedAnimationLayer")
3750 .build();
Jorim Jaggi391790622018-04-18 15:30:44 +02003751 mHomeAppAnimationLayer = makeChildSurface(null)
3752 .setName("homeAnimationLayer")
3753 .build();
Robert Carrf7a7ca82017-12-06 13:17:07 -08003754 mSplitScreenDividerAnchor = makeChildSurface(null)
3755 .setName("splitScreenDividerAnchor")
3756 .build();
3757 getPendingTransaction()
3758 .show(mAppAnimationLayer)
Jorim Jaggibe418292018-03-26 16:14:12 +02003759 .show(mBoostedAppAnimationLayer)
Jorim Jaggi391790622018-04-18 15:30:44 +02003760 .show(mHomeAppAnimationLayer)
Robert Carrf7a7ca82017-12-06 13:17:07 -08003761 .show(mSplitScreenDividerAnchor);
chaviw23ee71c2017-12-18 11:29:41 -08003762 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07003763 } else {
chaviw23ee71c2017-12-18 11:29:41 -08003764 mAppAnimationLayer.destroy();
3765 mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02003766 mBoostedAppAnimationLayer.destroy();
3767 mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02003768 mHomeAppAnimationLayer.destroy();
3769 mHomeAppAnimationLayer = null;
Robert Carrf7a7ca82017-12-06 13:17:07 -08003770 mSplitScreenDividerAnchor.destroy();
3771 mSplitScreenDividerAnchor = null;
Robert Carrb1579c82017-09-05 14:54:47 -07003772 }
3773 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003774 }
3775
Robert Carree4d4b92017-11-22 12:21:46 -08003776 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003777 AboveAppWindowContainers(String name, WindowManagerService service) {
3778 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08003779 }
3780
Robert Carrb9506032018-02-13 13:54:00 -08003781 @Override
Vishnu Nair83537a72018-07-19 21:27:48 -07003782 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
3783 final SurfaceControl.Builder builder = super.makeChildSurface(child);
3784 if (child instanceof WindowToken && ((WindowToken) child).mRoundedCornerOverlay) {
3785 // To draw above the ColorFade layer during the screen off transition, the
3786 // rounded corner overlays need to be at the root of the surface hierarchy.
3787 // TODO: move the ColorLayer into the display overlay layer such that this is not
3788 // necessary anymore.
3789 builder.setParent(null);
3790 }
3791 return builder;
3792 }
3793
3794 @Override
Robert Carrb9506032018-02-13 13:54:00 -08003795 void assignChildLayers(SurfaceControl.Transaction t) {
3796 assignChildLayers(t, null /* imeContainer */);
3797 }
3798
Robert Carree4d4b92017-11-22 12:21:46 -08003799 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
3800 boolean needAssignIme = imeContainer != null
3801 && imeContainer.getSurfaceControl() != null;
3802 for (int j = 0; j < mChildren.size(); ++j) {
3803 final WindowToken wt = mChildren.get(j);
Robert Carrf7a7ca82017-12-06 13:17:07 -08003804
3805 // See {@link mSplitScreenDividerAnchor}
3806 if (wt.windowType == TYPE_DOCK_DIVIDER) {
3807 wt.assignRelativeLayer(t, mTaskStackContainers.getSplitScreenDividerAnchor(), 1);
3808 continue;
3809 }
Vishnu Nair83537a72018-07-19 21:27:48 -07003810 if (wt.mRoundedCornerOverlay) {
3811 wt.assignLayer(t, WindowManagerPolicy.COLOR_FADE_LAYER + 1);
3812 continue;
3813 }
Robert Carree4d4b92017-11-22 12:21:46 -08003814 wt.assignLayer(t, j);
3815 wt.assignChildLayers(t);
3816
3817 int layer = mService.mPolicy.getWindowLayerFromTypeLw(
3818 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08003819
3820 if (needAssignIme && layer >= mService.mPolicy.getWindowLayerFromTypeLw(
3821 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003822 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08003823 needAssignIme = false;
3824 }
3825 }
3826 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003827 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08003828 }
3829 }
3830 }
3831
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003832 /**
3833 * Window container class that contains all containers on this display that are not related to
3834 * Apps. E.g. status bar.
3835 */
Robert Carree4d4b92017-11-22 12:21:46 -08003836 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07003837 /**
3838 * Compares two child window tokens returns -1 if the first is lesser than the second in
3839 * terms of z-order and 1 otherwise.
3840 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003841 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003842 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003843 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3844 token1.mOwnerCanManageAppTokens)
3845 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3846 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003847
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003848 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3849 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3850 return false;
3851 }
3852 final int req = w.mAttrs.screenOrientation;
3853 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3854 || req == SCREEN_ORIENTATION_UNSET) {
3855 return false;
3856 }
3857 return true;
3858 };
3859
Wale Ogunwale3a931692016-11-02 16:49:48 -07003860 private final String mName;
chaviwf29223e2018-01-31 13:23:51 -08003861 private final Dimmer mDimmer = new Dimmer(this);
3862 private final Rect mTmpDimBoundsRect = new Rect();
3863
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003864 NonAppWindowContainers(String name, WindowManagerService service) {
3865 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003866 mName = name;
3867 }
3868
3869 void addChild(WindowToken token) {
3870 addChild(token, mWindowComparator);
3871 }
3872
3873 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003874 int getOrientation() {
3875 final WindowManagerPolicy policy = mService.mPolicy;
3876 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003877 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003878
3879 if (win != null) {
3880 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003881 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003882 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003883 if (mService.mKeyguardGoingAway) {
3884 // Keyguard can't affect the orientation if it is going away...
3885 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3886 return SCREEN_ORIENTATION_UNSET;
3887 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003888 }
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07003889 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req
3890 + " for display id=" + mDisplayId);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003891 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003892 }
3893
Andrii Kulian8ee72852017-03-10 10:36:45 -08003894 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003895
Jorim Jaggi1c530592018-04-06 15:11:47 +02003896 // Only allow force setting the orientation when all unknown visibilities have been
3897 // resolved, as otherwise we just may be starting another occluding activity.
3898 final boolean isUnoccluding =
3899 mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
3900 && mService.mUnknownAppVisibilityController.allResolved();
3901 if (policy.isKeyguardShowingAndNotOccluded() || isUnoccluding) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003902 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003903 }
3904
3905 return SCREEN_ORIENTATION_UNSET;
3906 }
3907
3908 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003909 String getName() {
3910 return mName;
3911 }
chaviwf29223e2018-01-31 13:23:51 -08003912
3913 @Override
3914 Dimmer getDimmer() {
3915 return mDimmer;
3916 }
3917
3918 @Override
3919 void prepareSurfaces() {
3920 mDimmer.resetDimStates();
3921 super.prepareSurfaces();
3922 getBounds(mTmpDimBoundsRect);
3923
3924 if (mDimmer.updateDims(getPendingTransaction(), mTmpDimBoundsRect)) {
3925 scheduleAnimation();
3926 }
3927 }
Robert Carr3b716242016-08-16 16:02:21 -07003928 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003929
Robert Carr9034d962018-01-04 18:27:42 -08003930 private class NonMagnifiableWindowContainers extends NonAppWindowContainers {
3931 NonMagnifiableWindowContainers(String name, WindowManagerService service) {
3932 super(name, service);
3933 }
3934
3935 @Override
3936 void applyMagnificationSpec(Transaction t, MagnificationSpec spec) {
3937 }
3938 };
3939
Robert Carrb1579c82017-09-05 14:54:47 -07003940 SurfaceControl.Builder makeSurface(SurfaceSession s) {
3941 return mService.makeSurfaceBuilder(s)
3942 .setParent(mWindowingLayer);
3943 }
3944
3945 @Override
3946 SurfaceSession getSession() {
3947 return mSession;
3948 }
3949
3950 @Override
3951 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003952 SurfaceSession s = child != null ? child.getSession() : getSession();
3953 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrb1579c82017-09-05 14:54:47 -07003954 b.setSize(mSurfaceSize, mSurfaceSize);
Robert Carrf59b8dd2017-10-02 18:58:36 -07003955
3956 if (child == null) {
3957 return b;
3958 }
3959
Robert Carree4d4b92017-11-22 12:21:46 -08003960 return b.setName(child.getName())
3961 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07003962 }
3963
3964 /**
3965 * The makeSurface variants are for use by the window-container
3966 * hierarchy. makeOverlay here is a function for various non windowing
3967 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
3968 * and other potpourii.
3969 */
3970 SurfaceControl.Builder makeOverlay() {
3971 return mService.makeSurfaceBuilder(mSession)
3972 .setParent(mOverlayLayer);
3973 }
3974
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09003975 /**
3976 * Reparents the given surface to mOverlayLayer.
3977 */
3978 void reparentToOverlay(Transaction transaction, SurfaceControl surface) {
3979 transaction.reparent(surface, mOverlayLayer.getHandle());
3980 }
3981
Robert Carrb1579c82017-09-05 14:54:47 -07003982 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carr24be9ab2018-04-30 17:54:53 -07003983 if (spec.scale != 1.0) {
3984 mMagnificationSpec = spec;
3985 } else {
3986 mMagnificationSpec = null;
3987 }
3988
Robert Carrf59b8dd2017-10-02 18:58:36 -07003989 applyMagnificationSpec(getPendingTransaction(), spec);
3990 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07003991 }
3992
Robert Carr24be9ab2018-04-30 17:54:53 -07003993 void reapplyMagnificationSpec() {
3994 if (mMagnificationSpec != null) {
3995 applyMagnificationSpec(getPendingTransaction(), mMagnificationSpec);
3996 }
3997 }
3998
Robert Carrb1579c82017-09-05 14:54:47 -07003999 @Override
4000 void onParentSet() {
4001 // Since we are the top of the SurfaceControl hierarchy here
4002 // we create the root surfaces explicitly rather than chaining
4003 // up as the default implementation in onParentSet does. So we
4004 // explicitly do NOT call super here.
4005 }
4006
4007 @Override
4008 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07004009
4010 // These are layers as children of "mWindowingLayer"
4011 mBelowAppWindowsContainers.assignLayer(t, 0);
4012 mTaskStackContainers.assignLayer(t, 1);
4013 mAboveAppWindowsContainers.assignLayer(t, 2);
4014
4015 WindowState imeTarget = mService.mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08004016 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08004017
Robert Carree4d4b92017-11-22 12:21:46 -08004018 // In the case where we have an IME target that is not in split-screen
4019 // mode IME assignment is easy. We just need the IME to go directly above
4020 // the target. This way children of the target will naturally go above the IME
4021 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08004022 //
Robert Carree4d4b92017-11-22 12:21:46 -08004023 // In the case of split-screen windowing mode, we need to elevate the IME above the
4024 // docked divider while keeping the app itself below the docked divider, so instead
4025 // we use relative layering of the IME targets child windows, and place the
4026 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08004027 //
Robert Carr234b6332018-03-20 13:38:16 -07004028 // In the case the IME target is animating, the animation Z order may be different
4029 // than the WindowContainer Z order, so it's difficult to be sure we have the correct
4030 // IME target. In this case we just layer the IME over all transitions by placing it in the
4031 // above applications layer.
4032 //
Robert Carree4d4b92017-11-22 12:21:46 -08004033 // In the case where we have no IME target we assign it where it's base layer would
4034 // place it in the AboveAppWindowContainers.
Robert Carr234b6332018-03-20 13:38:16 -07004035 if (imeTarget != null && !(imeTarget.inSplitScreenWindowingMode()
4036 || imeTarget.mToken.isAppAnimating())
Robert Carree4d4b92017-11-22 12:21:46 -08004037 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004038 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07004039 // TODO: We need to use an extra level on the app surface to ensure
4040 // this is always above SurfaceView but always below attached window.
4041 1);
Robert Carree4d4b92017-11-22 12:21:46 -08004042 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07004043 }
4044
4045 // Above we have assigned layers to our children, now we ask them to assign
4046 // layers to their children.
4047 mBelowAppWindowsContainers.assignChildLayers(t);
4048 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08004049 mAboveAppWindowsContainers.assignChildLayers(t,
4050 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07004051 mImeWindowsContainers.assignChildLayers(t);
4052 }
4053
4054 /**
4055 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
4056 * that the IME target is one of the docked applications. We'd like the docked divider to be
4057 * above both of the applications, and we'd like the IME to be above the docked divider.
4058 * However we need child windows of the applications to be above the IME (Text drag handles).
4059 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
4060 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
4061 * with {@link #WindowState#assignLayer}
4062 */
4063 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
Robert Carrbb5de662018-04-26 13:29:51 -07004064 child.assignRelativeLayer(t, mImeWindowsContainers.getSurfaceControl(), 1);
Robert Carrb1579c82017-09-05 14:54:47 -07004065 }
4066
4067 @Override
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004068 void prepareSurfaces() {
4069 final ScreenRotationAnimation screenRotationAnimation =
4070 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
4071 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
4072 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
4073 mPendingTransaction.setMatrix(mWindowingLayer,
4074 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
4075 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
4076 mPendingTransaction.setPosition(mWindowingLayer,
4077 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
4078 mPendingTransaction.setAlpha(mWindowingLayer,
4079 screenRotationAnimation.getEnterTransformation().getAlpha());
4080 }
Chavi Weingartenb736e322018-02-23 00:27:54 +00004081
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004082 super.prepareSurfaces();
4083 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004084
Jorim Jaggibe418292018-03-26 16:14:12 +02004085 void assignStackOrdering() {
4086 mTaskStackContainers.assignStackOrdering(getPendingTransaction());
Robert Carr2f8aa392018-01-31 14:46:51 -08004087 }
Chavi Weingarten3a748552018-05-14 17:32:42 +00004088
4089 /**
4090 * Increment the deferral count to determine whether to update the IME target.
4091 */
4092 void deferUpdateImeTarget() {
4093 mDeferUpdateImeTargetCount++;
4094 }
4095
4096 /**
4097 * Decrement the deferral count to determine whether to update the IME target. If the count
4098 * reaches 0, a new ime target will get computed.
4099 */
4100 void continueUpdateImeTarget() {
4101 if (mDeferUpdateImeTargetCount == 0) {
4102 return;
4103 }
4104
4105 mDeferUpdateImeTargetCount--;
4106 if (mDeferUpdateImeTargetCount == 0) {
4107 computeImeTarget(true /* updateImeTarget */);
4108 }
4109 }
4110
4111 /**
4112 * @return Whether a new IME target should be computed.
4113 */
4114 private boolean canUpdateImeTarget() {
4115 return mDeferUpdateImeTargetCount == 0;
4116 }
Craig Mautner59c00972012-07-30 12:10:24 -07004117}