blob: 605705e9f18ffc29479341fe67313d53af2efe8f [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 */
Riddle Hsuad256a12018-07-18 16:11:30 +0800176class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer>
177 implements WindowManagerPolicy.DisplayContentInfo {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700178 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700179
180 /** Unique identifier of this stack. */
181 private final int mDisplayId;
182
Wale Ogunwale3a931692016-11-02 16:49:48 -0700183 /** The containers below are the only child containers the display can have. */
184 // Contains all window containers that are related to apps (Activities)
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100185 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers(mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700186 // Contains all non-app window containers that should be displayed above the app containers
187 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800188 private final AboveAppWindowContainers mAboveAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100189 new AboveAppWindowContainers("mAboveAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700190 // Contains all non-app window containers that should be displayed below the app containers
191 // (e.g. Wallpaper).
192 private final NonAppWindowContainers mBelowAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100193 new NonAppWindowContainers("mBelowAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700194 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
195 // 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 -0800196 // window containers together and move them in-sync if/when needed. We use a subclass of
197 // WindowContainer which is omitted from screen magnification, as the IME is never magnified.
198 private final NonMagnifiableWindowContainers mImeWindowsContainers =
199 new NonMagnifiableWindowContainers("mImeWindowsContainers", mService);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700200
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000201 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800202 private WindowState mTmpWindow2;
203 private WindowAnimator mTmpWindowAnimator;
204 private boolean mTmpRecoveringMemory;
205 private boolean mUpdateImeTarget;
206 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700207 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700208
Wale Ogunwale02319a62016-09-26 15:21:22 -0700209 // Mapping from a token IBinder to a WindowToken object on this display.
210 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
211
Andrii Kuliancd097992017-03-23 18:31:59 -0700212 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700213 int mInitialDisplayWidth = 0;
214 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700215 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700216
Adrian Roos1cf585052018-01-03 18:43:27 +0100217 DisplayCutout mInitialDisplayCutout;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100218 private final RotationCache<DisplayCutout, WmDisplayCutout> mDisplayCutoutCache
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100219 = new RotationCache<>(this::calculateDisplayCutoutForRotationUncached);
Adrian Roos1cf585052018-01-03 18:43:27 +0100220
Andrii Kuliancd097992017-03-23 18:31:59 -0700221 /**
222 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
223 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
224 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
225 */
Craig Mautner59c00972012-07-30 12:10:24 -0700226 int mBaseDisplayWidth = 0;
227 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700228 /**
229 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
230 * but can be set from Settings or via shell command "adb shell wm density".
231 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
232 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700233 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700234 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700235 private final DisplayInfo mDisplayInfo = new DisplayInfo();
236 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700237 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Riddle Hsuad256a12018-07-18 16:11:30 +0800238 final DisplayRotation mDisplayRotation;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000239 DisplayFrames mDisplayFrames;
240
Andrii Kulian06d07d62017-03-14 11:11:47 -0700241 /**
242 * For default display it contains real metrics, empty for others.
243 * @see WindowManagerService#createWatermarkInTransaction()
244 */
245 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
246 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
247 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700248
Andrii Kulian06d07d62017-03-14 11:11:47 -0700249 /**
250 * Compat metrics computed based on {@link #mDisplayMetrics}.
251 * @see #updateDisplayAndOrientation(int)
252 */
253 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
254
255 /** The desired scaling factor for compatible apps. */
256 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700257
Andrii Kulian8ee72852017-03-10 10:36:45 -0800258 /**
259 * Current rotation of the display.
260 * Constants as per {@link android.view.Surface.Rotation}.
261 *
Robert Carrae606b42018-02-15 15:36:23 -0800262 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800263 */
264 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700265
Andrii Kulian8ee72852017-03-10 10:36:45 -0800266 /**
267 * Last applied orientation of the display.
268 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
269 *
270 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
271 */
272 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700273
Andrii Kulian8ee72852017-03-10 10:36:45 -0800274 /**
275 * Flag indicating that the application is receiving an orientation that has different metrics
276 * than it expected. E.g. Portrait instead of Landscape.
277 *
Robert Carrae606b42018-02-15 15:36:23 -0800278 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800279 */
280 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700281
Andrii Kulian8ee72852017-03-10 10:36:45 -0800282 /**
283 * Orientation forced by some window. If there is no visible window that specifies orientation
284 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
285 *
286 * @see NonAppWindowContainers#getOrientation()
287 */
288 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700289
Andrii Kulian8ee72852017-03-10 10:36:45 -0800290 /**
291 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
292 * occluded.
293 *
294 * @see NonAppWindowContainers#getOrientation()
295 */
296 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
297
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700298 /**
299 * Keep track of wallpaper visibility to notify changes.
300 */
301 private boolean mLastWallpaperVisible = false;
302
Andrii Kulian06d07d62017-03-14 11:11:47 -0700303 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700304
Craig Mautner39834192012-09-02 07:47:24 -0700305 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700306 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700307 int pendingLayoutChanges;
Riddle Hsu654a6f92018-07-13 22:59:36 +0800308 int mDeferredRotationPauseCount;
Andrii Kulian839def92016-11-02 10:58:58 -0700309 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800310 boolean isDefaultDisplay;
Andrii Kulianf0379de2018-03-14 16:24:07 -0700311 /**
312 * Flag indicating whether WindowManager should override info for this display in
313 * DisplayManager.
314 */
315 boolean mShouldOverrideDisplayConfiguration = true;
Craig Mautner39834192012-09-02 07:47:24 -0700316
Craig Mautnerdc548482014-02-05 13:35:24 -0800317 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700318 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800319
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700320 /** Detect user tapping outside of current focused task bounds .*/
321 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700322
Craig Mautner6601b7b2013-04-29 10:29:11 -0700323 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700324 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700325
Craig Mautner6601b7b2013-04-29 10:29:11 -0700326 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800327 private final Rect mTmpRect = new Rect();
328 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700329 private final RectF mTmpRectF = new RectF();
330 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800331 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700332
Bryce Leef3c6a472017-11-14 14:53:06 -0800333 /** Used for handing back size of display */
334 private final Rect mTmpBounds = new Rect();
335
Craig Mautner95da1082014-02-24 17:54:35 -0800336 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700337 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800338
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700339 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700340 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700341
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800342 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800343 /** A collection of windows that provide tap exclude regions inside of them. */
344 final ArraySet<WindowState> mTapExcludeProvidingWindows = new ArraySet<>();
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800345
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000346 private boolean mHaveBootMsg = false;
347 private boolean mHaveApp = false;
348 private boolean mHaveWallpaper = false;
349 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700350
351 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
352
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700353 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
354 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000355 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
356 new ApplySurfaceChangesTransactionState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700357
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700358 // True if this display is in the process of being removed. Used to determine if the removal of
359 // the display's direct children should be allowed.
360 private boolean mRemovingDisplay = false;
361
Bryce Leed1871262017-06-12 14:12:29 -0700362 // {@code false} if this display is in the processing of being created.
363 private boolean mDisplayReady = false;
364
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800365 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700366
Robert Carrb1579c82017-09-05 14:54:47 -0700367 private final SurfaceSession mSession = new SurfaceSession();
368
369 /**
370 * We organize all top-level Surfaces in to the following layers.
371 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800372 * and omitted from Screen-Magnification, for example the strict mode flash or
373 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700374 * {@link #mWindowingLayer} contains everything else.
375 */
376 private SurfaceControl mOverlayLayer;
377
378 /**
379 * See {@link #mOverlayLayer}
380 */
381 private SurfaceControl mWindowingLayer;
382
383 /**
384 * Specifies the size of the surfaces in {@link #mOverlayLayer} and {@link #mWindowingLayer}.
385 * <p>
386 * For these surfaces currently we use a surface based on the larger of width or height so we
387 * don't have to resize when rotating the display.
388 */
389 private int mSurfaceSize;
390
Adrian Roos5251b1d2018-03-23 18:57:43 +0100391 /**
392 * Sequence number for the current layout pass.
393 */
394 int mLayoutSeq = 0;
395
Chavi Weingarten3a748552018-05-14 17:32:42 +0000396 /**
397 * Specifies the count to determine whether to defer updating the IME target until ready.
398 */
399 private int mDeferUpdateImeTargetCount;
400
Jorim Jaggi9af095b2017-12-12 17:18:57 +0100401 /** Temporary float array to retrieve 3x3 matrix values. */
402 private final float[] mTmpFloats = new float[9];
403
Robert Carr24be9ab2018-04-30 17:54:53 -0700404 private MagnificationSpec mMagnificationSpec;
405
Arthur Hung95b38a92018-07-20 18:56:12 +0800406 private InputMonitor mInputMonitor;
407
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800408 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
409 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800410 final AppWindowToken atoken = w.mAppToken;
411 if (winAnimator.mDrawState == READY_TO_SHOW) {
Jorim Jaggiaf0d6d22018-06-08 15:25:35 +0200412 if (atoken == null || atoken.canShowWindows()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800413 if (w.performShowLocked()) {
414 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
415 if (DEBUG_LAYOUT_REPEATS) {
416 mService.mWindowPlacerLocked.debugLayoutRepeats(
417 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
418 }
419 }
420 }
421 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800422 };
423
424 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
425 final WindowStateAnimator winAnimator = w.mWinAnimator;
426 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
427 return;
428 }
429
430 final int flags = w.mAttrs.flags;
431
432 // If this window is animating, make a note that we have an animating window and take
433 // care of a request to run a detached wallpaper animation.
Jorim Jaggia5e10572017-11-15 14:36:26 +0100434 if (winAnimator.isAnimationSet()) {
435 final AnimationAdapter anim = w.getAnimation();
436 if (anim != null) {
437 if ((flags & FLAG_SHOW_WALLPAPER) != 0 && anim.getDetachWallpaper()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800438 mTmpWindow = w;
439 }
Jorim Jaggia5e10572017-11-15 14:36:26 +0100440 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800441 if (color != 0) {
442 final TaskStack stack = w.getStack();
443 if (stack != null) {
444 stack.setAnimationBackground(winAnimator, color);
445 }
446 }
447 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800448 }
449
450 // If this window's app token is running a detached wallpaper animation, make a note so
451 // we can ensure the wallpaper is displayed behind it.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200452 final AppWindowToken atoken = winAnimator.mWin.mAppToken;
453 final AnimationAdapter animation = atoken != null ? atoken.getAnimation() : null;
454 if (animation != null) {
455 if ((flags & FLAG_SHOW_WALLPAPER) != 0 && animation.getDetachWallpaper()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800456 mTmpWindow = w;
457 }
458
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200459 final int color = animation.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800460 if (color != 0) {
461 final TaskStack stack = w.getStack();
462 if (stack != null) {
463 stack.setAnimationBackground(winAnimator, color);
464 }
465 }
466 }
467 };
468
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800469 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
470 final int lostFocusUid = mTmpWindow.mOwnerUid;
471 final Handler handler = mService.mH;
472 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
473 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
474 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
475 w.mAttrs.hideTimeoutMilliseconds);
476 }
477 }
478 };
479
480 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
481 final AppWindowToken focusedApp = mService.mFocusedApp;
482 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
483 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
484
485 if (!w.canReceiveKeys()) {
486 return false;
487 }
488
489 final AppWindowToken wtoken = w.mAppToken;
490
491 // If this window's application has been removed, just skip it.
492 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
493 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
494 + (wtoken.removed ? "removed" : "sendingToBottom"));
495 return false;
496 }
497
498 if (focusedApp == null) {
499 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
500 + " using new focus @ " + w);
501 mTmpWindow = w;
502 return true;
503 }
504
505 if (!focusedApp.windowsAreFocusable()) {
506 // Current focused app windows aren't focusable...
507 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
508 + " focusable using new focus @ " + w);
509 mTmpWindow = w;
510 return true;
511 }
512
513 // Descend through all of the app tokens and find the first that either matches
514 // win.mAppToken (return win) or mFocusedApp (return null).
515 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
516 if (focusedApp.compareTo(wtoken) > 0) {
517 // App stack below focused app stack. No focus for you!!!
518 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
519 "findFocusedWindow: Reached focused app=" + focusedApp);
520 mTmpWindow = null;
521 return true;
522 }
523 }
524
525 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
526 mTmpWindow = w;
527 return true;
528 };
529
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800530 private final Consumer<WindowState> mPerformLayout = w -> {
531 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
532 // wasting time and funky changes while a window is animating away.
533 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
534 || w.isGoneForLayoutLw();
535
536 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
537 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
538 + " mLayoutAttached=" + w.mLayoutAttached
539 + " screen changed=" + w.isConfigChanged());
540 final AppWindowToken atoken = w.mAppToken;
541 if (gone) Slog.v(TAG, " GONE: 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 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200546 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800547 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
548 + " parentHidden=" + w.isParentWindowHidden());
549 }
550
551 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
552 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
553 // since that means "perform layout as normal, just don't display").
554 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
555 || ((w.isConfigChanged() || w.setReportResizeHints())
556 && !w.isGoneForLayoutLw() &&
557 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
558 (w.mHasSurface && w.mAppToken != null &&
559 w.mAppToken.layoutConfigChanges)))) {
560 if (!w.mLayoutAttached) {
561 if (mTmpInitial) {
562 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
563 w.mContentChanged = false;
564 }
565 if (w.mAttrs.type == TYPE_DREAM) {
566 // Don't layout windows behind a dream, so that if it does stuff like hide
567 // the status bar we won't get a bad transition when it goes away.
568 mTmpWindow = w;
569 }
570 w.mLayoutNeeded = false;
571 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200572 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000573 mService.mPolicy.layoutWindowLw(w, null, mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100574 w.mLayoutSeq = mLayoutSeq;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800575
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200576 // If this is the first layout, we need to initialize the last inset values as
577 // otherwise we'd immediately cause an unnecessary resize.
578 if (firstLayout) {
579 w.updateLastInsetValues();
580 }
581
Adrian Roos23df3a32018-03-15 15:41:13 +0100582 if (w.mAppToken != null) {
583 w.mAppToken.layoutLetterbox(w);
584 }
585
chaviw492139a2018-07-16 16:07:35 -0700586 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700587 + " mContainingFrame=" + w.getContainingFrame()
588 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800589 }
590 }
591 };
592
593 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
594 if (w.mLayoutAttached) {
595 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
596 + " mViewVisibility=" + w.mViewVisibility
597 + " mRelayoutCalled=" + w.mRelayoutCalled);
598 // If this view is GONE, then skip it -- keep the current frame, and let the caller
599 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
600 // windows, since that means "perform layout as normal, just don't display").
601 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
602 return;
603 }
604 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
605 || w.mLayoutNeeded) {
606 if (mTmpInitial) {
607 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
608 w.mContentChanged = false;
609 }
610 w.mLayoutNeeded = false;
611 w.prelayout();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000612 mService.mPolicy.layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100613 w.mLayoutSeq = mLayoutSeq;
chaviw492139a2018-07-16 16:07:35 -0700614 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700615 + " mContainingFrame=" + w.getContainingFrame()
616 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800617 }
618 } else if (w.mAttrs.type == TYPE_DREAM) {
619 // Don't layout windows behind a dream, so that if it does stuff like hide the
620 // status bar we won't get a bad transition when it goes away.
621 mTmpWindow = mTmpWindow2;
622 }
623 };
624
625 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
626 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
627 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
628 return w.canBeImeTarget();
629 };
630
631 private final Consumer<WindowState> mApplyPostLayoutPolicy =
632 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
633 mService.mInputMethodTarget);
634
635 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
636 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
637 final boolean obscuredChanged = w.mObscured !=
638 mTmpApplySurfaceChangesTransactionState.obscured;
639 final RootWindowContainer root = mService.mRoot;
640 // Only used if default window
641 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
642
643 // Update effect.
644 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
645 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
646 final boolean isDisplayed = w.isDisplayedLw();
647
648 if (isDisplayed && w.isObscuringDisplay()) {
649 // This window completely covers everything behind it, so we want to leave all
650 // of them as undimmed (for performance reasons).
651 root.mObscuringWindow = w;
652 mTmpApplySurfaceChangesTransactionState.obscured = true;
653 }
654
655 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
656 root.handleNotObscuredLocked(w,
657 mTmpApplySurfaceChangesTransactionState.obscured,
658 mTmpApplySurfaceChangesTransactionState.syswin);
659
660 if (w.mHasSurface && isDisplayed) {
661 final int type = w.mAttrs.type;
662 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
663 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
664 mTmpApplySurfaceChangesTransactionState.syswin = true;
665 }
666 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
667 && w.mAttrs.preferredRefreshRate != 0) {
668 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
669 = w.mAttrs.preferredRefreshRate;
670 }
671 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
672 && w.mAttrs.preferredDisplayModeId != 0) {
673 mTmpApplySurfaceChangesTransactionState.preferredModeId
674 = w.mAttrs.preferredDisplayModeId;
675 }
676 }
677 }
678
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800679 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
680 && mWallpaperController.isWallpaperTarget(w)) {
681 // This is the wallpaper target and its obscured state changed... make sure the
682 // current wallpaper's visibility has been updated accordingly.
683 mWallpaperController.updateWallpaperVisibility();
684 }
685
chaviw161ea3e2018-01-31 12:01:12 -0800686 w.handleWindowMovedIfNeeded();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800687
688 final WindowStateAnimator winAnimator = w.mWinAnimator;
689
690 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
691 w.mContentChanged = false;
692
693 // Moved from updateWindowsAndWallpaperLocked().
694 if (w.mHasSurface) {
695 // Take care of the window being ready to display.
696 final boolean committed = winAnimator.commitFinishDrawingLocked();
697 if (isDefaultDisplay && committed) {
698 if (w.mAttrs.type == TYPE_DREAM) {
699 // HACK: When a dream is shown, it may at that point hide the lock screen.
700 // So we need to redo the layout to let the phone window manager make this
701 // happen.
702 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
703 if (DEBUG_LAYOUT_REPEATS) {
704 surfacePlacer.debugLayoutRepeats(
705 "dream and commitFinishDrawingLocked true",
706 pendingLayoutChanges);
707 }
708 }
709 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
710 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
711 "First draw done in potential wallpaper target " + w);
712 root.mWallpaperMayChange = true;
713 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
714 if (DEBUG_LAYOUT_REPEATS) {
715 surfacePlacer.debugLayoutRepeats(
716 "wallpaper and commitFinishDrawingLocked true",
717 pendingLayoutChanges);
718 }
719 }
720 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800721 }
722
723 final AppWindowToken atoken = w.mAppToken;
724 if (atoken != null) {
Adrian Roos23df3a32018-03-15 15:41:13 +0100725 atoken.updateLetterboxSurface(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800726 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
727 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
728 mTmpUpdateAllDrawn.add(atoken);
729 }
730 }
731
732 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
733 && w.isDisplayedLw()) {
734 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
735 }
736
737 w.updateResizingWindowIfNeeded();
738 };
739
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800740 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800741 * Create new {@link DisplayContent} instance, add itself to the root window container and
742 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700743 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800744 * @param service You know.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700745 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
746 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700747 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700748 DisplayContent(Display display, WindowManagerService service,
Bryce Leef19cbe22018-02-02 15:09:21 -0800749 WallpaperController wallpaperController, DisplayWindowController controller) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100750 super(service);
Bryce Leef19cbe22018-02-02 15:09:21 -0800751 setController(controller);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800752 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
753 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
754 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
755 + " new=" + display);
756 }
757
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700758 mDisplay = display;
759 mDisplayId = display.getDisplayId();
Wale Ogunwale0303c572016-10-20 10:16:29 -0700760 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700761 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700762 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700763 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100764 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo,
765 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700766 initializeDisplayBaseInfo();
Riddle Hsuad256a12018-07-18 16:11:30 +0800767 mDisplayRotation = new DisplayRotation(this, mService.mPolicy, mService.mContext);
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800768 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700769 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700770
Robert Carr74a66a22018-02-23 12:17:51 -0800771 // We use this as our arbitrary surface size for buffer-less parents
772 // that don't impose cropping on their children. It may need to be larger
773 // than the display size because fullscreen windows can be shifted offscreen
774 // due to surfaceInsets. 2 times the largest display dimension feels like an
775 // appropriately arbitrary number. Eventually we would like to give SurfaceFlinger
776 // layers the ability to match their parent sizes and be able to skip
777 // such arbitrary size settings.
778 mSurfaceSize = Math.max(mBaseDisplayHeight, mBaseDisplayWidth) * 2;
Robert Carrb1579c82017-09-05 14:54:47 -0700779
780 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession)
781 .setSize(mSurfaceSize, mSurfaceSize)
782 .setOpaque(true);
783 mWindowingLayer = b.setName("Display Root").build();
784 mOverlayLayer = b.setName("Display Overlays").build();
785
Robert Carrf59b8dd2017-10-02 18:58:36 -0700786 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700787 .setLayerStack(mWindowingLayer, mDisplayId)
788 .show(mWindowingLayer)
789 .setLayer(mOverlayLayer, 1)
790 .setLayerStack(mOverlayLayer, mDisplayId)
791 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700792 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700793
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700794 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700795 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700796 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700797 super.addChild(mAboveAppWindowsContainers, null);
798 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800799
800 // Add itself as a child to the root container.
801 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700802
803 // TODO(b/62541591): evaluate whether this is the best spot to declare the
804 // {@link DisplayContent} ready for use.
805 mDisplayReady = true;
Arthur Hung95b38a92018-07-20 18:56:12 +0800806
807 mInputMonitor = new InputMonitor(service, mDisplayId);
Bryce Leed1871262017-06-12 14:12:29 -0700808 }
809
810 boolean isReady() {
811 // The display is ready when the system and the individual display are both ready.
812 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700813 }
814
815 int getDisplayId() {
816 return mDisplayId;
817 }
818
Wale Ogunwale02319a62016-09-26 15:21:22 -0700819 WindowToken getWindowToken(IBinder binder) {
820 return mTokenMap.get(binder);
821 }
822
823 AppWindowToken getAppWindowToken(IBinder binder) {
824 final WindowToken token = getWindowToken(binder);
825 if (token == null) {
826 return null;
827 }
828 return token.asAppWindowToken();
829 }
830
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700831 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700832 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
833 if (dc != null) {
834 // We currently don't support adding a window token to the display if the display
835 // already has the binder mapped to another token. If there is a use case for supporting
836 // this moving forward we will either need to merge the WindowTokens some how or have
837 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700838 throw new IllegalArgumentException("Can't map token=" + token + " to display="
839 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700840 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700841 if (binder == null) {
842 throw new IllegalArgumentException("Can't map token=" + token + " to display="
843 + getName() + " binder is null");
844 }
845 if (token == null) {
846 throw new IllegalArgumentException("Can't map null token to display="
847 + getName() + " binder=" + binder);
848 }
849
Wale Ogunwale02319a62016-09-26 15:21:22 -0700850 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700851
852 if (token.asAppWindowToken() == null) {
853 // Add non-app token to container hierarchy on the display. App tokens are added through
854 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700855 switch (token.windowType) {
856 case TYPE_WALLPAPER:
857 mBelowAppWindowsContainers.addChild(token);
858 break;
859 case TYPE_INPUT_METHOD:
860 case TYPE_INPUT_METHOD_DIALOG:
861 mImeWindowsContainers.addChild(token);
862 break;
863 default:
864 mAboveAppWindowsContainers.addChild(token);
865 break;
866 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700867 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700868 }
869
870 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700871 final WindowToken token = mTokenMap.remove(binder);
872 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800873 token.setExiting();
874 }
875 return token;
876 }
877
878 /** Changes the display the input window token is housed on to this one. */
879 void reParentWindowToken(WindowToken token) {
880 final DisplayContent prevDc = token.getDisplayContent();
881 if (prevDc == this) {
882 return;
883 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800884 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
885 && token.asAppWindowToken() == null) {
886 // Removed the token from the map, but made sure it's not an app token before removing
887 // from parent.
888 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700889 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800890
891 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700892 }
893
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700894 void removeAppToken(IBinder binder) {
895 final WindowToken token = removeWindowToken(binder);
896 if (token == null) {
897 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
898 return;
899 }
900
901 final AppWindowToken appToken = token.asAppWindowToken();
902
903 if (appToken == null) {
904 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
905 return;
906 }
907
908 appToken.onRemovedFromDisplay();
909 }
910
Riddle Hsuad256a12018-07-18 16:11:30 +0800911 @Override
912 public Display getDisplay() {
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700913 return mDisplay;
914 }
915
Craig Mautner59c00972012-07-30 12:10:24 -0700916 DisplayInfo getDisplayInfo() {
917 return mDisplayInfo;
918 }
919
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700920 DisplayMetrics getDisplayMetrics() {
921 return mDisplayMetrics;
922 }
923
Riddle Hsuad256a12018-07-18 16:11:30 +0800924 @Override
925 public DisplayRotation getDisplayRotation() {
926 return mDisplayRotation;
927 }
928
Andrii Kulian8ee72852017-03-10 10:36:45 -0800929 int getRotation() {
930 return mRotation;
931 }
932
Andrii Kulianef5ce1c2018-03-20 19:27:24 -0700933 @VisibleForTesting
Andrii Kulian8ee72852017-03-10 10:36:45 -0800934 void setRotation(int newRotation) {
935 mRotation = newRotation;
Riddle Hsuad256a12018-07-18 16:11:30 +0800936 mDisplayRotation.setRotation(newRotation);
Andrii Kulian8ee72852017-03-10 10:36:45 -0800937 }
938
939 int getLastOrientation() {
940 return mLastOrientation;
941 }
942
943 void setLastOrientation(int orientation) {
944 mLastOrientation = orientation;
945 }
946
947 boolean getAltOrientation() {
948 return mAltOrientation;
949 }
950
951 void setAltOrientation(boolean altOrientation) {
952 mAltOrientation = altOrientation;
953 }
954
955 int getLastWindowForcedOrientation() {
956 return mLastWindowForcedOrientation;
957 }
958
Andrii Kulian06d07d62017-03-14 11:11:47 -0700959 /**
Riddle Hsu654a6f92018-07-13 22:59:36 +0800960 * Temporarily pauses rotation changes until resumed.
961 *
962 * This can be used to prevent rotation changes from occurring while the user is
963 * performing certain operations, such as drag and drop.
964 *
965 * This call nests and must be matched by an equal number of calls to
966 * {@link #resumeRotationLocked}.
967 */
968 void pauseRotationLocked() {
969 mDeferredRotationPauseCount++;
970 }
971
972 /**
973 * Resumes normal rotation changes after being paused.
974 */
975 void resumeRotationLocked() {
976 if (mDeferredRotationPauseCount <= 0) {
977 return;
978 }
979
980 mDeferredRotationPauseCount--;
981 if (mDeferredRotationPauseCount == 0) {
982 final boolean changed = updateRotationUnchecked();
983 if (changed) {
984 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
985 }
986 }
987 }
988
989 /**
Riddle Hsuad256a12018-07-18 16:11:30 +0800990 * If this is true we have updated our desired orientation, but not yet changed the real
991 * orientation our applied our screen rotation animation. For example, because a previous
992 * screen rotation was in progress.
993 *
994 * @return {@code true} if the there is an ongoing rotation change.
995 */
996 boolean rotationNeedsUpdate() {
997 final int lastOrientation = getLastOrientation();
998 final int oldRotation = getRotation();
999 final boolean oldAltOrientation = getAltOrientation();
1000
1001 final int rotation = mService.mPolicy.rotationForOrientationLw(
1002 mDisplayRotation, lastOrientation, oldRotation);
1003 final boolean altOrientation = !mDisplayRotation.rotationHasCompatibleMetrics(
1004 lastOrientation, rotation);
1005 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1006 return false;
1007 }
1008 return true;
1009 }
1010
1011 /**
Andrii Kulian06d07d62017-03-14 11:11:47 -07001012 * Update rotation of the display.
1013 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001014 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
1015 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001016 */
Robert Carrae606b42018-02-15 15:36:23 -08001017 boolean updateRotationUnchecked() {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001018 return updateRotationUnchecked(false /* forceUpdate */);
1019 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001020
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001021 /**
1022 * Update rotation of the display with an option to force the update.
1023 * @param forceUpdate Force the rotation update. Sometimes in WM we might skip updating
1024 * orientation because we're waiting for some rotation to finish or display
1025 * to unfreeze, which results in configuration of the previously visible
1026 * activity being applied to a newly visible one. Forcing the rotation
1027 * update allows to workaround this issue.
1028 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
1029 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
1030 */
1031 boolean updateRotationUnchecked(boolean forceUpdate) {
1032 ScreenRotationAnimation screenRotationAnimation;
1033 if (!forceUpdate) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001034 if (mDeferredRotationPauseCount > 0) {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001035 // Rotation updates have been paused temporarily. Defer the update until
1036 // updates have been resumed.
1037 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
1038 return false;
1039 }
1040
1041 screenRotationAnimation =
1042 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
1043 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
1044 // Rotation updates cannot be performed while the previous rotation change
1045 // animation is still in progress. Skip this update. We will try updating
1046 // again after the animation is finished and the display is unfrozen.
1047 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
1048 return false;
1049 }
1050 if (mService.mDisplayFrozen) {
1051 // Even if the screen rotation animation has finished (e.g. isAnimating
1052 // returns false), there is still some time where we haven't yet unfrozen
1053 // the display. We also need to abort rotation here.
1054 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
1055 "Deferring rotation, still finishing previous rotation");
1056 return false;
1057 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001058 }
1059
1060 if (!mService.mDisplayEnabled) {
1061 // No point choosing a rotation if the display is not enabled.
1062 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
1063 return false;
1064 }
1065
1066 final int oldRotation = mRotation;
1067 final int lastOrientation = mLastOrientation;
1068 final boolean oldAltOrientation = mAltOrientation;
Riddle Hsuad256a12018-07-18 16:11:30 +08001069 final int rotation = mService.mPolicy.rotationForOrientationLw(mDisplayRotation,
1070 lastOrientation, oldRotation);
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001071 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Computed rotation=" + rotation + " for display id="
1072 + mDisplayId + " based on lastOrientation=" + lastOrientation
1073 + " and oldRotation=" + oldRotation);
Riddle Hsuad256a12018-07-18 16:11:30 +08001074 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(mDisplayRotation,
1075 oldRotation, rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001076
Robert Carr0e007272017-10-02 13:00:55 -07001077 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001078 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001079 if (seamlessRotated != null && !forceUpdate) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001080 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
1081 // to complete (that is, waiting for windows to redraw). It's tempting to check
1082 // w.mSeamlessRotationCount but that could be incorrect in the case of
1083 // window-removal.
1084 return false;
1085 }
Robert Carr0e007272017-10-02 13:00:55 -07001086
1087 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001088 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -07001089 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -07001090 mayRotateSeamlessly = false;
1091 }
1092 for (int i = 0; i < mService.mSessions.size(); i++) {
1093 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
1094 mayRotateSeamlessly = false;
1095 break;
1096 }
1097 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001098 }
Robert Carr0e007272017-10-02 13:00:55 -07001099 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001100
1101 // TODO: Implement forced rotation changes.
1102 // Set mAltOrientation to indicate that the application is receiving
1103 // an orientation that has different metrics than it expected.
1104 // eg. Portrait instead of Landscape.
1105
Riddle Hsuad256a12018-07-18 16:11:30 +08001106 final boolean altOrientation = !mDisplayRotation.rotationHasCompatibleMetrics(
Andrii Kulian06d07d62017-03-14 11:11:47 -07001107 lastOrientation, rotation);
1108
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001109 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1110 + " selected orientation " + lastOrientation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001111 + ", got rotation " + rotation + " which has "
1112 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1113
1114 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1115 // No change.
1116 return false;
1117 }
1118
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001119 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1120 + " rotation changed to " + rotation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001121 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1122 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1123
1124 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1125 mService.mWaitingForConfig = true;
1126 }
1127
Riddle Hsuad256a12018-07-18 16:11:30 +08001128 setRotation(rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001129 mAltOrientation = altOrientation;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001130
1131 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
Riddle Hsu654a6f92018-07-13 22:59:36 +08001132 mService.mH.sendNewMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1133 this, WINDOW_FREEZE_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001134
1135 setLayoutNeeded();
1136 final int[] anim = new int[2];
Robert Carrf59b8dd2017-10-02 18:58:36 -07001137 mService.mPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001138
1139 if (!rotateSeamlessly) {
Robert Carrae606b42018-02-15 15:36:23 -08001140 mService.startFreezingDisplayLocked(anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001141 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1142 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1143 mDisplayId);
1144 } else {
1145 // The screen rotation animation uses a screenshot to freeze the screen
1146 // while windows resize underneath.
1147 // When we are rotating seamlessly, we allow the elements to transition
1148 // to their rotated state independently and without a freeze required.
1149 screenRotationAnimation = null;
1150
Robert Carrefc75702018-02-16 11:46:16 -08001151 mService.startSeamlessRotation();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001152 }
1153
1154 // We need to update our screen size information to match the new rotation. If the rotation
1155 // has actually changed then this method will return true and, according to the comment at
1156 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1157 // By updating the Display info here it will be available to
1158 // #computeScreenConfiguration() later.
1159 updateDisplayAndOrientation(getConfiguration().uiMode);
1160
Robert Carrae606b42018-02-15 15:36:23 -08001161 // NOTE: We disable the rotation in the emulator because
1162 // it doesn't support hardware OpenGL emulation yet.
1163 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1164 && screenRotationAnimation.hasScreenshot()) {
1165 if (screenRotationAnimation.setRotation(getPendingTransaction(), rotation,
1166 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1167 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1168 mService.scheduleAnimationLocked();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001169 }
1170 }
1171
Vishnu Nair83537a72018-07-19 21:27:48 -07001172 forAllWindows(w -> {
1173 w.forceSeamlesslyRotateIfAllowed(oldRotation, rotation);
1174 }, true /* traverseTopToBottom */);
1175
1176 // TODO(b/111504081): Consolidate seamless rotation logic.
Robert Carrae606b42018-02-15 15:36:23 -08001177 if (rotateSeamlessly) {
Adrian Roos0e7b70a2018-06-07 15:29:34 +02001178 seamlesslyRotate(getPendingTransaction(), oldRotation, rotation);
Robert Carrae606b42018-02-15 15:36:23 -08001179 }
1180
1181 mService.mDisplayManagerInternal.performTraversal(getPendingTransaction());
1182 scheduleAnimation();
1183
Andrii Kulian06d07d62017-03-14 11:11:47 -07001184 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001185 if (w.mHasSurface && !rotateSeamlessly) {
1186 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001187 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001188 mService.mRoot.mOrientationChangeComplete = false;
1189 w.mLastFreezeDuration = 0;
1190 }
1191 w.mReportOrientationChanged = true;
1192 }, true /* traverseTopToBottom */);
1193
1194 if (rotateSeamlessly) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001195 mService.mH.sendNewMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1196 this, SEAMLESS_ROTATION_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001197 }
1198
1199 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1200 final WindowManagerService.RotationWatcher rotationWatcher
1201 = mService.mRotationWatchers.get(i);
1202 if (rotationWatcher.mDisplayId == mDisplayId) {
1203 try {
1204 rotationWatcher.mWatcher.onRotationChanged(rotation);
1205 } catch (RemoteException e) {
1206 // Ignore
1207 }
1208 }
1209 }
1210
1211 // TODO (multi-display): Magnification is supported only for the default display.
1212 // Announce rotation only if we will not animate as we already have the
1213 // windows in final state. Otherwise, we make this call at the rotation end.
1214 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1215 && isDefaultDisplay) {
1216 mService.mAccessibilityController.onRotationChangedLocked(this);
1217 }
1218
1219 return true;
1220 }
1221
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001222 void configureDisplayPolicy() {
Riddle Hsuad256a12018-07-18 16:11:30 +08001223 mDisplayRotation.configure();
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001224 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
1225 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001226 }
1227
Andrii Kulian06d07d62017-03-14 11:11:47 -07001228 /**
1229 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1230 * changed.
1231 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1232 */
1233 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1234 // Use the effective "visual" dimensions based on current rotation
1235 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1236 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1237 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1238 int dw = realdw;
1239 int dh = realdh;
1240
1241 if (mAltOrientation) {
1242 if (realdw > realdh) {
1243 // Turn landscape into portrait.
1244 int maxw = (int)(realdh/1.3f);
1245 if (maxw < realdw) {
1246 dw = maxw;
1247 }
1248 } else {
1249 // Turn portrait into landscape.
1250 int maxh = (int)(realdw/1.3f);
1251 if (maxh < realdh) {
1252 dh = maxh;
1253 }
1254 }
1255 }
1256
1257 // Update application display metrics.
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001258 final WmDisplayCutout wmDisplayCutout = calculateDisplayCutoutForRotation(mRotation);
1259 final DisplayCutout displayCutout = wmDisplayCutout.getDisplayCutout();
1260
Andrii Kulian06d07d62017-03-14 11:11:47 -07001261 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001262 mDisplayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001263 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001264 mDisplayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001265 mDisplayInfo.rotation = mRotation;
1266 mDisplayInfo.logicalWidth = dw;
1267 mDisplayInfo.logicalHeight = dh;
1268 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1269 mDisplayInfo.appWidth = appWidth;
1270 mDisplayInfo.appHeight = appHeight;
1271 if (isDefaultDisplay) {
1272 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1273 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1274 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001275 mDisplayInfo.displayCutout = displayCutout.isEmpty() ? null : displayCutout;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001276 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1277 if (mDisplayScalingDisabled) {
1278 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1279 } else {
1280 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1281 }
1282
Andrii Kulianf0379de2018-03-14 16:24:07 -07001283 // We usually set the override info in DisplayManager so that we get consistent display
1284 // metrics values when displays are changing and don't send out new values until WM is aware
1285 // of them. However, we don't do this for displays that serve as containers for ActivityView
1286 // because we don't want letter-/pillar-boxing during resize.
1287 final DisplayInfo overrideDisplayInfo = mShouldOverrideDisplayConfiguration
1288 ? mDisplayInfo : null;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001289 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
Andrii Kulianf0379de2018-03-14 16:24:07 -07001290 overrideDisplayInfo);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001291
1292 mBaseDisplayRect.set(0, 0, dw, dh);
1293
1294 if (isDefaultDisplay) {
1295 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1296 mCompatDisplayMetrics);
1297 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001298
1299 updateBounds();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001300 return mDisplayInfo;
1301 }
1302
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001303 WmDisplayCutout calculateDisplayCutoutForRotation(int rotation) {
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001304 return mDisplayCutoutCache.getOrCompute(mInitialDisplayCutout, rotation);
1305 }
1306
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001307 private WmDisplayCutout calculateDisplayCutoutForRotationUncached(
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001308 DisplayCutout cutout, int rotation) {
Adrian Roos24264212018-02-19 16:26:15 +01001309 if (cutout == null || cutout == DisplayCutout.NO_CUTOUT) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001310 return WmDisplayCutout.NO_CUTOUT;
Adrian Roos1cf585052018-01-03 18:43:27 +01001311 }
Adrian Roos11c25582018-02-19 18:06:36 +01001312 if (rotation == ROTATION_0) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001313 return WmDisplayCutout.computeSafeInsets(
1314 cutout, mInitialDisplayWidth, mInitialDisplayHeight);
Adrian Roos24264212018-02-19 16:26:15 +01001315 }
Adrian Roosbed538e2018-02-21 17:50:07 +01001316 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Jorim Jaggi60640512018-06-29 01:14:31 +02001317 final List<Rect> bounds = WmDisplayCutout.computeSafeInsets(
1318 cutout, mInitialDisplayWidth, mInitialDisplayHeight)
1319 .getDisplayCutout().getBoundingRects();
Adrian Roos11c25582018-02-19 18:06:36 +01001320 transformPhysicalToLogicalCoordinates(rotation, mInitialDisplayWidth, mInitialDisplayHeight,
1321 mTmpMatrix);
Jorim Jaggi60640512018-06-29 01:14:31 +02001322 final Region region = Region.obtain();
1323 for (int i = 0; i < bounds.size(); i++) {
1324 final Rect rect = bounds.get(i);
1325 final RectF rectF = new RectF(bounds.get(i));
1326 mTmpMatrix.mapRect(rectF);
1327 rectF.round(rect);
1328 region.op(rect, Op.UNION);
1329 }
1330
1331 return WmDisplayCutout.computeSafeInsets(DisplayCutout.fromBounds(region),
Adrian Roos24264212018-02-19 16:26:15 +01001332 rotated ? mInitialDisplayHeight : mInitialDisplayWidth,
1333 rotated ? mInitialDisplayWidth : mInitialDisplayHeight);
Adrian Roos1cf585052018-01-03 18:43:27 +01001334 }
1335
Andrii Kulian06d07d62017-03-14 11:11:47 -07001336 /**
1337 * Compute display configuration based on display properties and policy settings.
1338 * Do not call if mDisplayReady == false.
1339 */
1340 void computeScreenConfiguration(Configuration config) {
1341 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1342
1343 final int dw = displayInfo.logicalWidth;
1344 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001345 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
1346 // TODO: Probably best to set this based on some setting in the display content object,
1347 // so the display can be configured for things like fullscreen.
1348 config.windowConfiguration.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001349
Adrian Roos11c25582018-02-19 18:06:36 +01001350 final float density = mDisplayMetrics.density;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001351 config.screenWidthDp =
1352 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001353 config.uiMode, mDisplayId, displayInfo.displayCutout) / density);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001354 config.screenHeightDp =
1355 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001356 config.uiMode, mDisplayId, displayInfo.displayCutout) / density);
Bryce Lee7566d762017-03-30 09:34:15 -07001357
Adrian Roos11c25582018-02-19 18:06:36 +01001358 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh,
1359 displayInfo.displayCutout, mTmpRect);
Bryce Lee7566d762017-03-30 09:34:15 -07001360 final int leftInset = mTmpRect.left;
1361 final int topInset = mTmpRect.top;
1362 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001363 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1364 leftInset + displayInfo.appWidth /* right */,
1365 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001366 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1367 || displayInfo.rotation == Surface.ROTATION_270);
1368
1369 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
Adrian Roos11c25582018-02-19 18:06:36 +01001370 density, config);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001371
1372 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1373 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1374 ? Configuration.SCREENLAYOUT_ROUND_YES
1375 : Configuration.SCREENLAYOUT_ROUND_NO);
1376
1377 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1378 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1379 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1380 dh, mDisplayId);
1381 config.densityDpi = displayInfo.logicalDensityDpi;
1382
1383 config.colorMode =
1384 (displayInfo.isHdr()
1385 ? Configuration.COLOR_MODE_HDR_YES
1386 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001387 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001388 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1389 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1390
1391 // Update the configuration based on available input devices, lid switch,
1392 // and platform configuration.
1393 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1394 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1395 config.navigation = Configuration.NAVIGATION_NONAV;
1396
1397 int keyboardPresence = 0;
1398 int navigationPresence = 0;
1399 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1400 final int len = devices != null ? devices.length : 0;
1401 for (int i = 0; i < len; i++) {
1402 InputDevice device = devices[i];
1403 if (!device.isVirtual()) {
1404 final int sources = device.getSources();
1405 final int presenceFlag = device.isExternal() ?
1406 WindowManagerPolicy.PRESENCE_EXTERNAL :
1407 WindowManagerPolicy.PRESENCE_INTERNAL;
1408
1409 // TODO(multi-display): Configure on per-display basis.
1410 if (mService.mIsTouchDevice) {
1411 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1412 InputDevice.SOURCE_TOUCHSCREEN) {
1413 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1414 }
1415 } else {
1416 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1417 }
1418
1419 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1420 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1421 navigationPresence |= presenceFlag;
1422 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1423 && config.navigation == Configuration.NAVIGATION_NONAV) {
1424 config.navigation = Configuration.NAVIGATION_DPAD;
1425 navigationPresence |= presenceFlag;
1426 }
1427
1428 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1429 config.keyboard = Configuration.KEYBOARD_QWERTY;
1430 keyboardPresence |= presenceFlag;
1431 }
1432 }
1433 }
1434
1435 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1436 config.navigation = Configuration.NAVIGATION_DPAD;
1437 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1438 }
1439
1440 // Determine whether a hard keyboard is available and enabled.
1441 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1442 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1443 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1444 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1445 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1446 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1447 }
1448
1449 // Let the policy update hidden states.
1450 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1451 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1452 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1453 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1454 }
1455
1456 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1457 int displayId) {
1458 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1459 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1460 final int unrotDw, unrotDh;
1461 if (rotated) {
1462 unrotDw = dh;
1463 unrotDh = dw;
1464 } else {
1465 unrotDw = dw;
1466 unrotDh = dh;
1467 }
1468 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1469 displayId);
1470 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1471 displayId);
1472 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1473 displayId);
1474 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1475 displayId);
1476 return sw;
1477 }
1478
1479 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1480 DisplayMetrics dm, int dw, int dh, int displayId) {
1481 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001482 displayId, mDisplayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001483 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001484 uiMode, displayId, mDisplayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001485 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1486 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1487 if (curSize == 0 || size < curSize) {
1488 curSize = size;
1489 }
1490 return curSize;
1491 }
1492
1493 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1494 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1495
1496 // We need to determine the smallest width that will occur under normal
1497 // operation. To this, start with the base screen size and compute the
1498 // width under the different possible rotations. We need to un-rotate
1499 // the current screen dimensions before doing this.
1500 int unrotDw, unrotDh;
1501 if (rotated) {
1502 unrotDw = dh;
1503 unrotDh = dw;
1504 } else {
1505 unrotDw = dw;
1506 unrotDh = dh;
1507 }
1508 displayInfo.smallestNominalAppWidth = 1<<30;
1509 displayInfo.smallestNominalAppHeight = 1<<30;
1510 displayInfo.largestNominalAppWidth = 0;
1511 displayInfo.largestNominalAppHeight = 0;
1512 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1513 unrotDh);
1514 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1515 unrotDw);
1516 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1517 unrotDh);
1518 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1519 unrotDw);
1520 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1521 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1522 displayId);
1523 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1524 displayId);
1525 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1526 displayId);
1527 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1528 displayId);
1529 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1530 outConfig.screenLayout = sl;
1531 }
1532
1533 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1534 int uiMode, int displayId) {
1535 // Get the app screen size at this rotation.
Adrian Roos11c25582018-02-19 18:06:36 +01001536 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId,
1537 mDisplayInfo.displayCutout);
1538 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId,
1539 mDisplayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001540
1541 // Compute the screen layout size class for this rotation.
1542 int longSize = w;
1543 int shortSize = h;
1544 if (longSize < shortSize) {
1545 int tmp = longSize;
1546 longSize = shortSize;
1547 shortSize = tmp;
1548 }
1549 longSize = (int)(longSize/density);
1550 shortSize = (int)(shortSize/density);
1551 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1552 }
1553
1554 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1555 int uiMode, int dw, int dh) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001556 final DisplayCutout displayCutout = calculateDisplayCutoutForRotation(
1557 rotation).getDisplayCutout();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001558 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001559 displayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001560 if (width < displayInfo.smallestNominalAppWidth) {
1561 displayInfo.smallestNominalAppWidth = width;
1562 }
1563 if (width > displayInfo.largestNominalAppWidth) {
1564 displayInfo.largestNominalAppWidth = width;
1565 }
1566 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001567 displayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001568 if (height < displayInfo.smallestNominalAppHeight) {
1569 displayInfo.smallestNominalAppHeight = height;
1570 }
1571 if (height > displayInfo.largestNominalAppHeight) {
1572 displayInfo.largestNominalAppHeight = height;
1573 }
1574 }
1575
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001576 DockedStackDividerController getDockedDividerController() {
1577 return mDividerControllerLocked;
1578 }
1579
Winson Chung655332c2016-10-31 13:14:28 -07001580 PinnedStackController getPinnedStackController() {
1581 return mPinnedStackControllerLocked;
1582 }
1583
Jeff Browna506a6e2013-06-04 00:02:38 -07001584 /**
1585 * Returns true if the specified UID has access to this display.
1586 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001587 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001588 return mDisplay.hasAccess(uid);
1589 }
1590
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001591 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001592 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001593 }
1594
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001595 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001596 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001597 }
1598
Wale Ogunwale61911492017-10-11 08:50:50 -07001599 /**
1600 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1601 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001602 TaskStack getSplitScreenPrimaryStack() {
1603 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001604 return (stack != null && stack.isVisible()) ? stack : null;
1605 }
1606
Robert Carr9785cf32018-04-25 15:06:07 -07001607 boolean hasSplitScreenPrimaryStack() {
1608 return getSplitScreenPrimaryStack() != null;
1609 }
1610
Wale Ogunwale61911492017-10-11 08:50:50 -07001611 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001612 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001613 * not visible.
1614 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001615 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1616 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001617 }
1618
1619 TaskStack getPinnedStack() {
1620 return mTaskStackContainers.getPinnedStack();
1621 }
1622
1623 private boolean hasPinnedStack() {
1624 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001625 }
1626
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001627 /**
1628 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1629 * Null is no compatible stack on the display.
1630 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001631 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001632 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1633 }
1634
1635 /**
1636 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1637 * activity type. Null is no compatible stack on the display.
1638 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001639 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001640 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001641 }
1642
Bryce Lee48f4b572017-04-10 10:54:15 -07001643 @VisibleForTesting
Kazuki Takise148d00a2018-05-31 15:32:19 +09001644 WindowList<TaskStack> getStacks() {
1645 return mTaskStackContainers.mChildren;
1646 }
1647
1648 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001649 TaskStack getTopStack() {
1650 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001651 }
1652
Winson Chunge2d72172018-01-25 17:46:20 +00001653 ArrayList<Task> getVisibleTasks() {
1654 return mTaskStackContainers.getVisibleTasks();
1655 }
1656
Wale Ogunwale61911492017-10-11 08:50:50 -07001657 void onStackWindowingModeChanged(TaskStack stack) {
1658 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001659 }
1660
Andrii Kulian441e4492016-09-29 15:25:00 -07001661 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001662 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001663 super.onConfigurationChanged(newParentConfig);
1664
Andrii Kulian3a507b52016-09-19 18:14:12 -07001665 // The display size information is heavily dependent on the resources in the current
1666 // configuration, so we need to reconfigure it every time the configuration changes.
1667 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1668 mService.reconfigureDisplayLocked(this);
1669
Bryce Leef3c6a472017-11-14 14:53:06 -08001670 final DockedStackDividerController dividerController = getDockedDividerController();
1671
1672 if (dividerController != null) {
1673 getDockedDividerController().onConfigurationChanged();
1674 }
1675
1676 final PinnedStackController pinnedStackController = getPinnedStackController();
1677
1678 if (pinnedStackController != null) {
1679 getPinnedStackController().onConfigurationChanged();
1680 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001681 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001682
Andrii Kulian441e4492016-09-29 15:25:00 -07001683 /**
1684 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1685 * bounds were updated.
1686 */
Bryce Leeaea60d22018-01-31 14:42:15 -08001687 void updateStackBoundsAfterConfigChange(@NonNull List<TaskStack> changedStackList) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001688 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1689 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001690 if (stack.updateBoundsAfterConfigChange()) {
Bryce Leeaea60d22018-01-31 14:42:15 -08001691 changedStackList.add(stack);
Andrii Kulian3a507b52016-09-19 18:14:12 -07001692 }
1693 }
Winson Chung32c566f2017-04-11 18:31:21 -07001694
1695 // If there was no pinned stack, we still need to notify the controller of the display info
1696 // update as a result of the config change. We do this here to consolidate the flow between
1697 // changes when there is and is not a stack.
Wale Ogunwale61911492017-10-11 08:50:50 -07001698 if (!hasPinnedStack()) {
Winson Chung32c566f2017-04-11 18:31:21 -07001699 mPinnedStackControllerLocked.onDisplayInfoChanged();
1700 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001701 }
1702
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001703 @Override
1704 boolean fillsParent() {
1705 return true;
1706 }
1707
1708 @Override
1709 boolean isVisible() {
1710 return true;
1711 }
1712
1713 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001714 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001715 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001716 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001717 }
1718
Robert Carr9785cf32018-04-25 15:06:07 -07001719 /**
1720 * In split-screen mode we process the IME containers above the docked divider
1721 * rather than directly above their target.
1722 */
1723 private boolean skipTraverseChild(WindowContainer child) {
1724 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null
1725 && !hasSplitScreenPrimaryStack()) {
1726 return true;
1727 }
1728 return false;
1729 }
1730
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001731 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001732 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1733 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1734 // target, or here in order if there isn't an IME target.
1735 if (traverseTopToBottom) {
1736 for (int i = mChildren.size() - 1; i >= 0; --i) {
1737 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001738 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001739 continue;
1740 }
Robert Carr9785cf32018-04-25 15:06:07 -07001741
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001742 if (child.forAllWindows(callback, traverseTopToBottom)) {
1743 return true;
1744 }
1745 }
1746 } else {
1747 final int count = mChildren.size();
1748 for (int i = 0; i < count; i++) {
1749 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001750 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001751 continue;
1752 }
Robert Carr9785cf32018-04-25 15:06:07 -07001753
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001754 if (child.forAllWindows(callback, traverseTopToBottom)) {
1755 return true;
1756 }
1757 }
1758 }
1759 return false;
1760 }
1761
1762 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1763 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1764 }
1765
Wale Ogunwale399c8692017-05-08 14:22:42 -07001766 @Override
1767 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001768 final WindowManagerPolicy policy = mService.mPolicy;
1769
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001770 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001771 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001772 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1773 + " is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001774 // If the display is frozen, some activities may be in the middle of restarting, and
1775 // thus have removed their old window. If the window has the flag to hide the lock
1776 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1777 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001778 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001779 } else if (policy.isKeyguardLocked()) {
1780 // Use the last orientation the while the display is frozen with the keyguard
1781 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1782 // window. We don't want to check the show when locked window directly though as
1783 // things aren't stable while the display is frozen, for example the window could be
1784 // momentarily unavailable due to activity relaunch.
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001785 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1786 + " is frozen while keyguard locked, return " + mLastOrientation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08001787 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001788 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001789 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001790 final int orientation = mAboveAppWindowsContainers.getOrientation();
1791 if (orientation != SCREEN_ORIENTATION_UNSET) {
1792 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001793 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001794 }
1795
Wale Ogunwale399c8692017-05-08 14:22:42 -07001796 // Top system windows are not requesting an orientation. Start searching from apps.
1797 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001798 }
1799
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001800 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001801 // Check if display metrics changed and update base values if needed.
1802 updateBaseDisplayMetricsIfNeeded();
1803
Craig Mautner722285e2012-09-07 13:55:58 -07001804 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001805 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001806
Wale Ogunwale61911492017-10-11 08:50:50 -07001807 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1808 mTaskStackContainers.getChildAt(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001809 }
Craig Mautner722285e2012-09-07 13:55:58 -07001810 }
1811
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001812 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001813 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1814 if (displayManagerInternal != null) {
1815 // Bootstrap the default logical display from the display manager.
1816 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1817 if (newDisplayInfo != null) {
1818 mDisplayInfo.copyFrom(newDisplayInfo);
1819 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001820 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001821
Andrii Kuliancd097992017-03-23 18:31:59 -07001822 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1823 mDisplayInfo.logicalDensityDpi);
1824 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1825 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1826 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01001827 mInitialDisplayCutout = mDisplayInfo.displayCutout;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001828 }
1829
Andrii Kuliancd097992017-03-23 18:31:59 -07001830 /**
1831 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1832 * values.
1833 */
1834 private void updateBaseDisplayMetricsIfNeeded() {
1835 // Get real display metrics without overrides from WM.
1836 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1837 final int orientation = mDisplayInfo.rotation;
1838 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1839 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1840 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1841 final int newDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01001842 final DisplayCutout newCutout = mDisplayInfo.displayCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07001843
1844 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1845 || mInitialDisplayHeight != newHeight
Adrian Roos1cf585052018-01-03 18:43:27 +01001846 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi
1847 || !Objects.equals(mInitialDisplayCutout, newCutout);
Andrii Kuliancd097992017-03-23 18:31:59 -07001848
1849 if (displayMetricsChanged) {
1850 // Check if display size or density is forced.
1851 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1852 || mBaseDisplayHeight != mInitialDisplayHeight;
1853 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1854
1855 // If there is an override set for base values - use it, otherwise use new values.
1856 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1857 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1858 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1859
1860 // Real display metrics changed, so we should also update initial values.
1861 mInitialDisplayWidth = newWidth;
1862 mInitialDisplayHeight = newHeight;
1863 mInitialDisplayDensity = newDensity;
Adrian Roos1cf585052018-01-03 18:43:27 +01001864 mInitialDisplayCutout = newCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07001865 mService.reconfigureDisplayLocked(this);
1866 }
1867 }
1868
Bryce Lee27cec322017-03-21 09:41:37 -07001869 /** Sets the maximum width the screen resolution can be */
1870 void setMaxUiWidth(int width) {
1871 if (DEBUG_DISPLAY) {
1872 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1873 }
1874
1875 mMaxUiWidth = width;
1876
1877 // Update existing metrics.
1878 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1879 }
1880
1881 /** Update base (override) display metrics. */
1882 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1883 mBaseDisplayWidth = baseWidth;
1884 mBaseDisplayHeight = baseHeight;
1885 mBaseDisplayDensity = baseDensity;
1886
1887 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1888 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1889 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1890 mBaseDisplayWidth = mMaxUiWidth;
1891
1892 if (DEBUG_DISPLAY) {
1893 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1894 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1895 + " on display:" + getDisplayId());
1896 }
1897 }
1898
1899 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08001900
1901 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07001902 }
1903
Wale Ogunwaledb506192017-12-08 10:57:32 -08001904 void getStableRect(Rect out) {
1905 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08001906 }
1907
Wale Ogunwale61911492017-10-11 08:50:50 -07001908 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001909 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1910 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001911
Wale Ogunwale61911492017-10-11 08:50:50 -07001912 final TaskStack stack = new TaskStack(mService, stackId, controller);
1913 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08001914 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001915 }
1916
Andrii Kulian51c1b672017-04-07 18:39:32 -07001917 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001918 final DisplayContent prevDc = stack.getDisplayContent();
1919 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001920 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1921 + " which is not currently attached to any display");
1922 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001923 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001924 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1925 + " to its current displayId=" + mDisplayId);
1926 }
1927
Wale Ogunwale61911492017-10-11 08:50:50 -07001928 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001929 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001930 }
1931
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001932 @Override
1933 protected void addChild(DisplayChildWindowContainer child,
1934 Comparator<DisplayChildWindowContainer> comparator) {
1935 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1936 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001937
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001938 @Override
1939 protected void addChild(DisplayChildWindowContainer child, int index) {
1940 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1941 }
1942
1943 @Override
1944 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001945 // Only allow removal of direct children from this display if the display is in the process
1946 // of been removed.
1947 if (mRemovingDisplay) {
1948 super.removeChild(child);
1949 return;
1950 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001951 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001952 }
1953
Andrii Kuliand2765632016-12-12 22:26:34 -08001954 @Override
1955 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1956 // Children of the display are statically ordered, so the real intention here is to perform
1957 // the operation on the display and not the static direct children.
1958 getParent().positionChildAt(position, this, includingParents);
1959 }
1960
Winson Chung59a47ded2018-01-25 17:46:06 +00001961 void positionStackAt(int position, TaskStack child) {
1962 mTaskStackContainers.positionChildAt(position, child, false /* includingParents */);
1963 layoutAndAssignWindowLayersIfNeeded();
1964 }
1965
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001966 int taskIdFromPoint(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001967 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1968 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001969 final int taskId = stack.taskIdFromPoint(x, y);
1970 if (taskId != -1) {
1971 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001972 }
1973 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001974 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001975 }
1976
Chong Zhang8e89b312015-09-09 15:09:30 -07001977 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001978 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001979 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001980 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001981 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001982 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001983 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07001984 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1985 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001986 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001987 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001988 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001989
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001990 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1991 if (mTmpTaskForResizePointSearchResult.searchDone) {
1992 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001993 }
1994 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001995 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001996 }
1997
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001998 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001999 // The provided task is the task on this display with focus, so if WindowManagerService's
2000 // focused app is not on this display, focusedTask will be null.
2001 if (focusedTask == null) {
2002 mTouchExcludeRegion.setEmpty();
2003 } else {
2004 mTouchExcludeRegion.set(mBaseDisplayRect);
2005 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
2006 mTmpRect2.setEmpty();
Wale Ogunwale61911492017-10-11 08:50:50 -07002007 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2008 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002009 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
2010 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08002011 }
2012 // If we removed the focused task above, add it back and only leave its
2013 // outside touch area in the exclusion. TapDectector is not interested in
2014 // any touch inside the focused task itself.
2015 if (!mTmpRect2.isEmpty()) {
2016 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
2017 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08002018 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002019 final WindowState inputMethod = mService.mInputMethodWindow;
2020 if (inputMethod != null && inputMethod.isVisibleLw()) {
2021 // If the input method is visible and the user is typing, we don't want these touch
2022 // events to be intercepted and used to change focus. This would likely cause a
2023 // disappearance of the input method.
2024 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07002025 if (inputMethod.getDisplayId() == mDisplayId) {
2026 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
2027 } else {
2028 // IME is on a different display, so we need to update its tap detector.
2029 // TODO(multidisplay): Remove when IME will always appear on same display.
2030 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
2031 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002032 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002033 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002034 final WindowState win = mTapExcludedWindows.get(i);
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002035 win.getTouchableRegion(mTmpRegion);
2036 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
2037 }
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002038 for (int i = mTapExcludeProvidingWindows.size() - 1; i >= 0; i--) {
2039 final WindowState win = mTapExcludeProvidingWindows.valueAt(i);
2040 win.amendTapExcludeRegion(mTouchExcludeRegion);
2041 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08002042 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07002043 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002044 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07002045 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002046 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
2047 }
Craig Mautner1bef3892015-02-17 15:09:47 -08002048 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07002049 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08002050 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07002051 }
2052
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002053 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07002054 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002055 super.switchUser();
2056 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07002057 }
2058
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07002059 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002060 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2061 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07002062 }
2063 }
2064
Wale Ogunwale10124582016-09-15 20:25:50 -07002065 @Override
2066 void removeIfPossible() {
2067 if (isAnimating()) {
2068 mDeferredRemoval = true;
2069 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07002070 }
Wale Ogunwale10124582016-09-15 20:25:50 -07002071 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07002072 }
2073
Wale Ogunwale10124582016-09-15 20:25:50 -07002074 @Override
2075 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002076 mRemovingDisplay = true;
2077 try {
2078 super.removeImmediately();
2079 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -07002080 if (mService.canDispatchPointerEvents()) {
2081 if (mTapDetector != null) {
2082 mService.unregisterPointerEventListener(mTapDetector);
2083 }
2084 if (mDisplayId == DEFAULT_DISPLAY && mService.mMousePositionTracker != null) {
2085 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
2086 }
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002087 }
Jorim Jaggia3844032018-01-03 15:54:43 +01002088 mService.mAnimator.removeDisplayLocked(mDisplayId);
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002089 } finally {
2090 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08002091 }
Bryce Leef19cbe22018-02-02 15:09:21 -08002092
2093 mService.onDisplayRemoved(mDisplayId);
Craig Mautner95da1082014-02-24 17:54:35 -08002094 }
2095
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002096 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07002097 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002098 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002099 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
2100
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002101 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002102 removeImmediately();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002103 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08002104 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002105 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08002106 }
2107
Andrii Kulian0214ed92017-05-16 13:44:05 -07002108 /** @return 'true' if removal of this display content is deferred due to active animation. */
2109 boolean isRemovalDeferred() {
2110 return mDeferredRemoval;
2111 }
2112
Wale Ogunwale10124582016-09-15 20:25:50 -07002113 boolean animateForIme(float interpolatedValue, float animationTarget,
2114 float dividerAnimationTarget) {
2115 boolean updated = false;
2116
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 continue;
2121 }
2122
2123 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
2124 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2125 updated = true;
2126 } else {
2127 mDividerControllerLocked.mLastAnimationProgress =
2128 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
2129 mDividerControllerLocked.mLastDividerProgress =
2130 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
2131 updated |= stack.updateAdjustForIme(
2132 mDividerControllerLocked.mLastAnimationProgress,
2133 mDividerControllerLocked.mLastDividerProgress,
2134 false /* force */);
2135 }
2136 if (interpolatedValue >= 1f) {
2137 stack.endImeAdjustAnimation();
2138 }
2139 }
2140
2141 return updated;
2142 }
2143
2144 boolean clearImeAdjustAnimation() {
2145 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002146 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2147 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002148 if (stack != null && stack.isAdjustedForIme()) {
2149 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2150 changed = true;
2151 }
2152 }
2153 return changed;
2154 }
2155
2156 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002157 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2158 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002159 if (stack.isVisible() && stack.isAdjustedForIme()) {
2160 stack.beginImeAdjustAnimation();
2161 }
2162 }
2163 }
2164
2165 void adjustForImeIfNeeded() {
2166 final WindowState imeWin = mService.mInputMethodWindow;
2167 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2168 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002169 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002170 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2171 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2172 imeTargetStack.getDockSide() : DOCKED_INVALID;
2173 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2174 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2175 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002176 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002177 final boolean imeHeightChanged = imeVisible &&
2178 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2179
2180 // The divider could be adjusted for IME position, or be thinner than usual,
2181 // or both. There are three possible cases:
2182 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2183 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2184 // - If IME is not visible, divider is not moved and is normal width.
2185
2186 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002187 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2188 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002189 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002190 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2191 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002192 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2193 } else {
2194 stack.resetAdjustedForIme(false);
2195 }
2196 }
2197 mDividerControllerLocked.setAdjustedForIme(
2198 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2199 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002200 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2201 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002202 stack.resetAdjustedForIme(!dockVisible);
2203 }
2204 mDividerControllerLocked.setAdjustedForIme(
2205 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2206 }
Winson Chung655332c2016-10-31 13:14:28 -07002207 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002208 }
2209
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002210 /**
2211 * If a window that has an animation specifying a colored background and the current wallpaper
2212 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2213 * suddenly disappear.
2214 */
2215 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002216 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2217 w -> w.mIsWallpaper && w.isVisibleNow());
2218
2219 if (visibleWallpaper != null) {
2220 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002221 }
2222 return winAnimator.mAnimLayer;
2223 }
2224
Wale Ogunwale10124582016-09-15 20:25:50 -07002225 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002226 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2227 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002228 stack.prepareFreezingTaskBounds();
2229 }
2230 }
2231
Wale Ogunwale94744212015-09-21 19:01:47 -07002232 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002233 getBounds(mTmpRect, newRotation);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002234
2235 // Compute a transform matrix to undo the coordinate space transformation,
2236 // and present the window at the same physical position it previously occupied.
2237 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2238 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2239
2240 mTmpRectF.set(bounds);
2241 mTmpMatrix.mapRect(mTmpRectF);
2242 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002243 }
2244
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002245 static int deltaRotation(int oldRotation, int newRotation) {
2246 int delta = newRotation - oldRotation;
2247 if (delta < 0) delta += 4;
2248 return delta;
2249 }
2250
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002251 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002252 Matrix outMatrix) {
2253 // For rotations without Z-ordering we don't need the target rectangle's position.
2254 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2255 displayHeight, outMatrix);
2256 }
2257
2258 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2259 float displayWidth, float displayHeight, Matrix outMatrix) {
2260 switch (rotation) {
2261 case ROTATION_0:
2262 outMatrix.reset();
2263 break;
2264 case ROTATION_270:
2265 outMatrix.setRotate(270, 0, 0);
2266 outMatrix.postTranslate(0, displayHeight);
2267 outMatrix.postTranslate(rectTop, 0);
2268 break;
2269 case ROTATION_180:
2270 outMatrix.reset();
2271 break;
2272 case ROTATION_90:
2273 outMatrix.setRotate(90, 0, 0);
2274 outMatrix.postTranslate(displayWidth, 0);
2275 outMatrix.postTranslate(-rectTop, rectLeft);
2276 break;
2277 }
2278 }
2279
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002280 @CallSuper
2281 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002282 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002283 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002284 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002285 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002286 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2287 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002288 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002289 }
2290 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2291 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002292 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2293 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002294 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002295 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002296 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2297 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002298 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002299 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002300 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2301 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002302 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002303 }
2304 proto.write(DPI, mBaseDisplayDensity);
2305 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002306 proto.write(ROTATION, mRotation);
2307 final ScreenRotationAnimation screenRotationAnimation =
2308 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2309 if (screenRotationAnimation != null) {
2310 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2311 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002312 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002313 proto.end(token);
2314 }
2315
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002316 @Override
2317 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2318 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002319 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2320 final String subPrefix = " " + prefix;
2321 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2322 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2323 pw.print("dpi");
2324 if (mInitialDisplayWidth != mBaseDisplayWidth
2325 || mInitialDisplayHeight != mBaseDisplayHeight
2326 || mInitialDisplayDensity != mBaseDisplayDensity) {
2327 pw.print(" base=");
2328 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2329 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2330 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002331 if (mDisplayScalingDisabled) {
2332 pw.println(" noscale");
2333 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002334 pw.print(" cur=");
2335 pw.print(mDisplayInfo.logicalWidth);
2336 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2337 pw.print(" app=");
2338 pw.print(mDisplayInfo.appWidth);
2339 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2340 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2341 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2342 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2343 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002344 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002345 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002346 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002347
Craig Mautnerdc548482014-02-05 13:35:24 -08002348 pw.println();
Adrian Roos5251b1d2018-03-23 18:57:43 +01002349 pw.print(prefix); pw.print("mLayoutSeq="); pw.println(mLayoutSeq);
Riddle Hsu654a6f92018-07-13 22:59:36 +08002350 pw.print(prefix);
2351 pw.print("mDeferredRotationPauseCount="); pw.println(mDeferredRotationPauseCount);
Adrian Roos5251b1d2018-03-23 18:57:43 +01002352
2353 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002354 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002355 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2356 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002357 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002358 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002359
Craig Mautnerdc548482014-02-05 13:35:24 -08002360 pw.println();
2361 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002362 pw.println();
2363 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002364 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002365 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002366 pw.print(" Exiting #"); pw.print(i);
2367 pw.print(' '); pw.print(token);
2368 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002369 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002370 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002371 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002372
Jorim Jaggi31f71702016-05-04 16:43:04 -07002373 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002374
2375 // Dump stack references
2376 final TaskStack homeStack = getHomeStack();
2377 if (homeStack != null) {
2378 pw.println(prefix + "homeStack=" + homeStack.getName());
2379 }
2380 final TaskStack pinnedStack = getPinnedStack();
2381 if (pinnedStack != null) {
2382 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2383 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002384 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002385 if (splitScreenPrimaryStack != null) {
2386 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2387 }
2388
2389 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002390 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002391 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002392 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002393
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002394 pw.println();
2395 mDisplayFrames.dump(prefix, pw);
Arthur Hung95b38a92018-07-20 18:56:12 +08002396 pw.println();
Riddle Hsuad256a12018-07-18 16:11:30 +08002397 mDisplayRotation.dump(prefix, pw);
2398 pw.println();
Arthur Hung95b38a92018-07-20 18:56:12 +08002399 mInputMonitor.dump(pw, " ");
Craig Mautner59c00972012-07-30 12:10:24 -07002400 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002401
2402 @Override
2403 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002404 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002405 }
2406
2407 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002408 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002409 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002410
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002411 /** Returns true if the stack in the windowing mode is visible. */
2412 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002413 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002414 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002415 }
2416
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002417 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002418 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002419 final int x = (int) xf;
2420 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002421 final WindowState touchedWin = getWindow(w -> {
2422 final int flags = w.mAttrs.flags;
2423 if (!w.isVisibleLw()) {
2424 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002425 }
2426 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002427 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002428 }
2429
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002430 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002431 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002432 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002433 }
2434
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002435 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002436
2437 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002438 return mTmpRegion.contains(x, y) || touchFlags == 0;
2439 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002440
2441 return touchedWin;
2442 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002443
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002444 boolean canAddToastWindowForUid(int uid) {
2445 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002446 // Also if the app is focused adding more than one toast at
2447 // a time for better backwards compatibility.
2448 final WindowState focusedWindowForUid = getWindow(w ->
2449 w.mOwnerUid == uid && w.isFocused());
2450 if (focusedWindowForUid != null) {
2451 return true;
2452 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002453 final WindowState win = getWindow(w ->
2454 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2455 && !w.mWindowRemovalAllowed);
2456 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002457 }
2458
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002459 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002460 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2461 return;
2462 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002463
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002464 // Used to communicate the old focus to the callback method.
2465 mTmpWindow = oldFocus;
2466
2467 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002468 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002469
2470 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002471 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002472
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002473 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002474
2475 if (mTmpWindow == null) {
2476 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2477 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002478 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002479 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002480 }
2481
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002482 /** Updates the layer assignment of windows on this display. */
2483 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002484 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07002485 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002486 if (setLayoutNeeded) {
2487 setLayoutNeeded();
2488 }
Robert Carrb1579c82017-09-05 14:54:47 -07002489
Robert Carrf59b8dd2017-10-02 18:58:36 -07002490 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002491 // the application of this transaction until the animation pass triggers
2492 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2493 // the hiding and showing of surfaces.
2494 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002495 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002496 }
2497
Wale Ogunwale1666e312016-12-16 11:27:18 -08002498 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2499 // moving containers or resizing them. Need to investigate the best way to have it automatically
2500 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002501 void layoutAndAssignWindowLayersIfNeeded() {
2502 mService.mWindowsChanged = true;
2503 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002504
2505 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2506 false /*updateInputWindows*/)) {
2507 assignWindowLayers(false /* setLayoutNeeded */);
2508 }
2509
Arthur Hung95b38a92018-07-20 18:56:12 +08002510 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002511 mService.mWindowPlacerLocked.performSurfacePlacement();
Arthur Hung95b38a92018-07-20 18:56:12 +08002512 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002513 }
2514
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002515 /** Returns true if a leaked surface was destroyed */
2516 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002517 // Used to indicate that a surface was leaked.
2518 mTmpWindow = null;
2519 forAllWindows(w -> {
2520 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002521 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002522 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002523 }
2524 if (!mService.mSessions.contains(wsa.mSession)) {
2525 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002526 + w + " surface=" + wsa.mSurfaceController
2527 + " token=" + w.mToken
2528 + " pid=" + w.mSession.mPid
2529 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002530 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002531 mService.mForceRemoves.add(w);
2532 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002533 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002534 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002535 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002536 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002537 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002538 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002539 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002540 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002541 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002542
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002543 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002544 }
2545
2546 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002547 * Determine and return the window that should be the IME target.
2548 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2549 * @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 +00002550 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002551 WindowState computeImeTarget(boolean updateImeTarget) {
2552 if (mService.mInputMethodWindow == null) {
2553 // There isn't an IME so there shouldn't be a target...That was easy!
2554 if (updateImeTarget) {
2555 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2556 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002557 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002558 }
2559 return null;
2560 }
2561
Chavi Weingarten3a748552018-05-14 17:32:42 +00002562 final WindowState curTarget = mService.mInputMethodTarget;
2563 if (!canUpdateImeTarget()) {
2564 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Defer updating IME target");
2565 return curTarget;
2566 }
2567
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002568 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2569 // same display. Or even when the current IME/target are not on the same screen as the next
2570 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002571 mUpdateImeTarget = updateImeTarget;
2572 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002573
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002574
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002575 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2576 // to be on top of it, but it is not -really- where input will go. So look down below
2577 // for a real window to target...
2578 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2579 final AppWindowToken token = target.mAppToken;
2580 if (token != null) {
2581 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2582 if (betterTarget != null) {
2583 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002584 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002585 }
2586 }
2587
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002588 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2589 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002590
chaviw1c13ad32018-06-12 16:44:23 -07002591 // Now, a special case -- if the last target's window is in the process of exiting, but
2592 // not removed, and the new target is home, keep on the last target to avoid flicker.
2593 // Home is a special case since its above other stacks in the ordering list, but layed
2594 // out below the others.
2595 if (curTarget != null && !curTarget.mRemoved && curTarget.isDisplayedLw()
2596 && curTarget.isClosing() && (target == null || target.isActivityTypeHome())) {
chaviw4ad54912018-05-30 11:05:44 -07002597 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "New target is home while current target is "
Chavi Weingarten3a748552018-05-14 17:32:42 +00002598 + "closing, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002599 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002600 }
2601
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002602 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2603 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002604
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002605 if (target == null) {
2606 if (updateImeTarget) {
2607 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2608 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2609 + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002610 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002611 }
2612
2613 return null;
2614 }
2615
2616 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002617 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2618 if (token != null) {
2619
2620 // Now some fun for dealing with window animations that modify the Z order. We need
2621 // to look at all windows below the current target that are in this app, finding the
2622 // highest visible one in layering.
2623 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002624 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002625 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002626 }
2627
2628 if (highestTarget != null) {
2629 final AppTransition appTransition = mService.mAppTransition;
2630 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2631 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2632 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002633 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002634
2635 if (appTransition.isTransitionSet()) {
2636 // If we are currently setting up for an animation, hold everything until we
2637 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002638 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002639 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002640 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002641 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002642 // If the window we are currently targeting is involved with an animation,
2643 // and it is on top of the next target we will be over, then hold off on
2644 // moving until that is done.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002645 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002646 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002647 }
2648 }
2649 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002650
2651 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2652 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002653 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002654 }
2655
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002656 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002657 }
2658
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002659 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002660 if (target == mService.mInputMethodTarget
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002661 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002662 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002663 }
2664
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002665 mService.mInputMethodTarget = target;
2666 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002667 assignWindowLayers(false /* setLayoutNeeded */);
2668 }
2669
2670 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2671 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2672 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2673 }
2674
2675 // Used to indicate we have reached the first window in the range we are interested in.
2676 mTmpWindow = null;
2677
2678 // TODO: Figure-out a more efficient way to do this.
2679 final WindowState candidate = getWindow(w -> {
2680 if (w == top) {
2681 // Reached the first window in the range we are interested in.
2682 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002683 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002684 if (mTmpWindow == null) {
2685 return false;
2686 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002687
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002688 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2689 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002690 }
2691 // If we reached the bottom of the range of windows we are considering,
2692 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002693 if (w == bottom) {
2694 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002695 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002696 return false;
2697 });
2698
2699 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002700 }
2701
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002702 void setLayoutNeeded() {
2703 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2704 mLayoutNeeded = true;
2705 }
2706
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002707 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002708 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2709 mLayoutNeeded = false;
2710 }
2711
2712 boolean isLayoutNeeded() {
2713 return mLayoutNeeded;
2714 }
2715
Wale Ogunwale02319a62016-09-26 15:21:22 -07002716 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2717 if (mTokenMap.isEmpty()) {
2718 return;
2719 }
2720 pw.println(" Display #" + mDisplayId);
2721 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2722 while (it.hasNext()) {
2723 final WindowToken token = it.next();
2724 pw.print(" ");
2725 pw.print(token);
2726 if (dumpAll) {
2727 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002728 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07002729 } else {
2730 pw.println();
2731 }
2732 }
2733 }
2734
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002735 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002736 final int[] index = new int[1];
2737 forAllWindows(w -> {
2738 final WindowStateAnimator wAnim = w.mWinAnimator;
2739 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2740 index[0] = index[0] + 1;
2741 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002742 }
2743
Jorim Jaggife762342016-10-13 14:33:27 +02002744 /**
2745 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2746 */
2747 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2748 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002749 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002750 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2751 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01002752 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02002753 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002754 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002755 }
2756
Wale Ogunwale494009b82016-10-21 09:01:38 -07002757 boolean checkWaitingForWindows() {
2758
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002759 mHaveBootMsg = false;
2760 mHaveApp = false;
2761 mHaveWallpaper = false;
2762 mHaveKeyguard = true;
2763
2764 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002765 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2766 return true;
2767 }
2768 if (w.isDrawnLw()) {
2769 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002770 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002771 } else if (w.mAttrs.type == TYPE_APPLICATION
2772 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002773 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002774 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002775 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002776 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002777 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002778 }
2779 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002780 return false;
2781 });
2782
2783 if (visibleWindow != null) {
2784 // We have a visible window.
2785 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002786 }
2787
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002788 // if the wallpaper service is disabled on the device, we're never going to have
2789 // wallpaper, don't bother waiting for it
2790 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2791 com.android.internal.R.bool.config_enableWallpaperService)
Daichi Hironoc1432222018-03-29 13:06:07 +09002792 && mService.mContext.getResources().getBoolean(
2793 com.android.internal.R.bool.config_checkWallpaperAtBoot)
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002794 && !mService.mOnlyCore;
2795
Wale Ogunwale494009b82016-10-21 09:01:38 -07002796 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2797 "******** booted=" + mService.mSystemBooted
2798 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002799 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2800 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2801 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002802
2803 // If we are turning on the screen to show the boot message, don't do it until the boot
2804 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002805 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002806 return true;
2807 }
2808
2809 // If we are turning on the screen after the boot is completed normally, don't do so until
2810 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002811 if (mService.mSystemBooted
2812 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002813 return true;
2814 }
2815
2816 return false;
2817 }
2818
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002819 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002820 mTmpWindowAnimator = animator;
2821 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002822 }
2823
2824 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002825 resetAnimationBackgroundAnimator();
2826
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002827 // Used to indicate a detached wallpaper.
2828 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002829 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002830
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002831 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002832
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002833 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002834 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002835 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2836 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002837 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2838 }
2839 }
2840
Wale Ogunwale494009b82016-10-21 09:01:38 -07002841 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002842 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002843 if (imFocus == null) {
2844 return false;
2845 }
2846
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002847 if (DEBUG_INPUT_METHOD) {
2848 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2849 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2850 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002851 }
2852
Wale Ogunwale494009b82016-10-21 09:01:38 -07002853 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2854
2855 if (DEBUG_INPUT_METHOD) {
2856 Slog.i(TAG_WM, "IM target client: " + imeClient);
2857 if (imeClient != null) {
2858 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2859 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2860 }
2861 }
2862
2863 return imeClient != null && imeClient.asBinder() == client.asBinder();
2864 }
2865
2866 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002867 final WindowState win = getWindow(
2868 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2869 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002870 }
2871
2872 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002873 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002874 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002875 final int curValue = w.mSystemUiVisibility;
2876 final int diff = (curValue ^ visibility) & globalDiff;
2877 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002878 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002879 w.mSeq++;
2880 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002881 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002882 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2883 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002884 visibility, newValue, diff);
2885 }
2886 } catch (RemoteException e) {
2887 // so sorry
2888 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002889 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002890 }
2891
2892 void onWindowFreezeTimeout() {
2893 Slog.w(TAG_WM, "Window freeze timeout expired.");
2894 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002895
2896 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002897 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002898 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002899 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002900 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002901 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2902 - mService.mDisplayFreezeTime);
2903 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002904 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002905 mService.mWindowPlacerLocked.performSurfacePlacement();
2906 }
2907
2908 void waitForAllWindowsDrawn() {
2909 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002910 forAllWindows(w -> {
2911 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2912 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2913 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002914 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002915 w.mLastContentInsets.set(-1, -1, -1, -1);
2916 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002917 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002918 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002919 }
2920
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002921 // TODO: Super crazy long method that should be broken down...
2922 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2923
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002924 final int dw = mDisplayInfo.logicalWidth;
2925 final int dh = mDisplayInfo.logicalHeight;
2926 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2927
2928 mTmpUpdateAllDrawn.clear();
2929
2930 int repeats = 0;
2931 do {
2932 repeats++;
2933 if (repeats > 6) {
2934 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2935 clearLayoutNeeded();
2936 break;
2937 }
2938
2939 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2940 pendingLayoutChanges);
2941
Andrii Kulian839def92016-11-02 10:58:58 -07002942 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2943 // the wallpaper window jumping across displays.
2944 // Remove check for default display when there will be support for multiple wallpaper
2945 // targets (on different displays).
2946 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002947 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002948 }
2949
Riddle Hsu654a6f92018-07-13 22:59:36 +08002950 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002951 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
Robert Carrae606b42018-02-15 15:36:23 -08002952 if (mService.updateOrientationFromAppTokensLocked(mDisplayId)) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002953 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002954 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002955 }
2956 }
2957
2958 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2959 setLayoutNeeded();
2960 }
2961
2962 // FIRST LOOP: Perform a layout, if needed.
2963 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2964 performLayout(repeats == 1, false /* updateInputWindows */);
2965 } else {
2966 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2967 }
2968
2969 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2970 pendingLayoutChanges = 0;
2971
2972 if (isDefaultDisplay) {
2973 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002974 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002975 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2976 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2977 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2978 }
2979 } while (pendingLayoutChanges != 0);
2980
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002981 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002982
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002983 mTmpRecoveringMemory = recoveringMemory;
2984 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01002985 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002986
2987 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002988 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2989 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2990 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002991 true /* inTraversal, must call performTraversalInTrans... below */);
2992
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002993 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2994 if (wallpaperVisible != mLastWallpaperVisible) {
2995 mLastWallpaperVisible = wallpaperVisible;
2996 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2997 }
2998
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002999 while (!mTmpUpdateAllDrawn.isEmpty()) {
3000 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
3001 // See if any windows have been drawn, so they (and others associated with them)
3002 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07003003 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003004 }
3005
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003006 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003007 }
3008
Bryce Leef3c6a472017-11-14 14:53:06 -08003009 private void updateBounds() {
3010 calculateBounds(mTmpBounds);
3011 setBounds(mTmpBounds);
3012 }
3013
3014 // Determines the current display bounds based on the current state
3015 private void calculateBounds(Rect out) {
3016 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
3017 final int orientation = mDisplayInfo.rotation;
3018 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
3019 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
3020 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
3021 int width = mDisplayInfo.logicalWidth;
3022 int left = (physWidth - width) / 2;
3023 int height = mDisplayInfo.logicalHeight;
3024 int top = (physHeight - height) / 2;
3025 out.set(left, top, left + width, top + height);
3026 }
3027
3028 @Override
3029 public void getBounds(Rect out) {
3030 calculateBounds(out);
3031 }
3032
3033 private void getBounds(Rect out, int orientation) {
3034 getBounds(out);
3035
3036 // Rotate the Rect if needed.
3037 final int currentRotation = mDisplayInfo.rotation;
3038 final int rotationDelta = deltaRotation(currentRotation, orientation);
3039 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
3040 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
3041 mTmpRectF.set(out);
3042 mTmpMatrix.mapRect(mTmpRectF);
3043 mTmpRectF.round(out);
3044 }
3045 }
3046
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003047 void performLayout(boolean initial, boolean updateInputWindows) {
3048 if (!isLayoutNeeded()) {
3049 return;
3050 }
3051 clearLayoutNeeded();
3052
3053 final int dw = mDisplayInfo.logicalWidth;
3054 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003055 if (DEBUG_LAYOUT) {
3056 Slog.v(TAG, "-------------------------------------");
3057 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
3058 }
3059
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01003060 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
3061 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00003062 // TODO: Not sure if we really need to set the rotation here since we are updating from the
3063 // display info above...
3064 mDisplayFrames.mRotation = mRotation;
3065 mService.mPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003066 if (isDefaultDisplay) {
3067 // Not needed on non-default displays.
3068 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
3069 mService.mScreenRect.set(0, 0, dw, dh);
3070 }
3071
Adrian Roos5251b1d2018-03-23 18:57:43 +01003072 int seq = mLayoutSeq + 1;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003073 if (seq < 0) seq = 0;
Adrian Roos5251b1d2018-03-23 18:57:43 +01003074 mLayoutSeq = seq;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003075
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003076 // Used to indicate that we have processed the dream window and all additional windows are
3077 // behind it.
3078 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003079 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003080
3081 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003082 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003083
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003084 // Used to indicate that we have processed the dream window and all additional attached
3085 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003086 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003087 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003088
3089 // Now perform layout of attached windows, which usually depend on the position of the
3090 // window they are attached to. XXX does not deal with windows that are attached to windows
3091 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003092 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003093
3094 // Window frames may have changed. Tell the input dispatcher about it.
Arthur Hung95b38a92018-07-20 18:56:12 +08003095 mInputMonitor.layoutInputConsumers(dw, dh);
3096 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003097 if (updateInputWindows) {
Arthur Hung95b38a92018-07-20 18:56:12 +08003098 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003099 }
3100
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003101 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
3102 }
3103
3104 /**
3105 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
3106 * In portrait mode, it grabs the full screenshot.
3107 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003108 * @param config of the output bitmap
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003109 */
chaviw0315a1a2018-03-05 15:28:35 -08003110 Bitmap screenshotDisplayLocked(Bitmap.Config config) {
3111 if (!mService.mPolicy.isScreenOn()) {
3112 if (DEBUG_SCREENSHOT) {
3113 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003114 }
chaviw0315a1a2018-03-05 15:28:35 -08003115 return null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003116 }
chaviwfbe47df2017-11-10 16:14:49 -08003117
chaviw0315a1a2018-03-05 15:28:35 -08003118 int dw = mDisplayInfo.logicalWidth;
3119 int dh = mDisplayInfo.logicalHeight;
chaviwfbe47df2017-11-10 16:14:49 -08003120
chaviw0315a1a2018-03-05 15:28:35 -08003121 if (dw <= 0 || dh <= 0) {
3122 return null;
3123 }
chaviwfbe47df2017-11-10 16:14:49 -08003124
chaviw0315a1a2018-03-05 15:28:35 -08003125 final Rect frame = new Rect(0, 0, dw, dh);
chaviwfbe47df2017-11-10 16:14:49 -08003126
chaviw0315a1a2018-03-05 15:28:35 -08003127 // The screenshot API does not apply the current screen rotation.
3128 int rot = mDisplay.getRotation();
chaviwfbe47df2017-11-10 16:14:49 -08003129
chaviw0315a1a2018-03-05 15:28:35 -08003130 if (rot == ROTATION_90 || rot == ROTATION_270) {
3131 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3132 }
chaviwfbe47df2017-11-10 16:14:49 -08003133
chaviw0315a1a2018-03-05 15:28:35 -08003134 // SurfaceFlinger is not aware of orientation, so convert our logical
3135 // crop to SurfaceFlinger's portrait orientation.
3136 convertCropForSurfaceFlinger(frame, rot, dw, dh);
3137
3138 final ScreenRotationAnimation screenRotationAnimation =
3139 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3140 final boolean inRotation = screenRotationAnimation != null &&
3141 screenRotationAnimation.isAnimating();
3142 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
3143
3144 // TODO(b/68392460): We should screenshot Task controls directly
3145 // but it's difficult at the moment as the Task doesn't have the
3146 // correct size set.
3147 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, 0, 1, inRotation, rot);
3148 if (bitmap == null) {
3149 Slog.w(TAG_WM, "Failed to take screenshot");
3150 return null;
3151 }
3152
3153 // Create a copy of the screenshot that is immutable and backed in ashmem.
3154 // This greatly reduces the overhead of passing the bitmap between processes.
3155 final Bitmap ret = bitmap.createAshmemBitmap(config);
3156 bitmap.recycle();
3157 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003158 }
3159
3160 // TODO: Can this use createRotationMatrix()?
3161 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3162 if (rot == Surface.ROTATION_90) {
3163 final int tmp = crop.top;
3164 crop.top = dw - crop.right;
3165 crop.right = crop.bottom;
3166 crop.bottom = dw - crop.left;
3167 crop.left = tmp;
3168 } else if (rot == Surface.ROTATION_180) {
3169 int tmp = crop.top;
3170 crop.top = dh - crop.bottom;
3171 crop.bottom = dh - tmp;
3172 tmp = crop.right;
3173 crop.right = dw - crop.left;
3174 crop.left = dw - tmp;
3175 } else if (rot == Surface.ROTATION_270) {
3176 final int tmp = crop.top;
3177 crop.top = crop.left;
3178 crop.left = dh - crop.bottom;
3179 crop.bottom = crop.right;
3180 crop.right = dh - tmp;
3181 }
3182 }
3183
3184 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003185 // Used to indicate the layout is needed.
3186 mTmpWindow = null;
3187
3188 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003189 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003190 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003191 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003192 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003193 w.setDisplayLayoutNeeded();
3194 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003195 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003196
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003197 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003198 mService.mWindowPlacerLocked.performSurfacePlacement();
3199 }
3200 }
3201
Wale Ogunwale1666e312016-12-16 11:27:18 -08003202 void setExitingTokensHasVisible(boolean hasVisible) {
3203 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3204 mExitingTokens.get(i).hasVisible = hasVisible;
3205 }
3206
3207 // Initialize state of exiting applications.
3208 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3209 }
3210
3211 void removeExistingTokensIfPossible() {
3212 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3213 final WindowToken token = mExitingTokens.get(i);
3214 if (!token.hasVisible) {
3215 mExitingTokens.remove(i);
3216 }
3217 }
3218
3219 // Time to remove any exiting applications?
3220 mTaskStackContainers.removeExistingAppTokensIfPossible();
3221 }
3222
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003223 @Override
3224 void onDescendantOverrideConfigurationChanged() {
3225 setLayoutNeeded();
3226 mService.requestTraversal();
3227 }
3228
David Stevens9440dc82017-03-16 19:00:20 -07003229 boolean okToDisplay() {
3230 if (mDisplayId == DEFAULT_DISPLAY) {
3231 return !mService.mDisplayFrozen
3232 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3233 }
3234 return mDisplayInfo.state == Display.STATE_ON;
3235 }
3236
3237 boolean okToAnimate() {
3238 return okToDisplay() &&
3239 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3240 }
3241
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003242 static final class TaskForResizePointSearchResult {
3243 boolean searchDone;
3244 Task taskForResize;
3245
3246 void reset() {
3247 searchDone = false;
3248 taskForResize = null;
3249 }
3250 }
Robert Carr3b716242016-08-16 16:02:21 -07003251
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003252 private static final class ApplySurfaceChangesTransactionState {
3253 boolean displayHasContent;
3254 boolean obscured;
3255 boolean syswin;
3256 boolean focusDisplayed;
3257 float preferredRefreshRate;
3258 int preferredModeId;
3259
3260 void reset() {
3261 displayHasContent = false;
3262 obscured = false;
3263 syswin = false;
3264 focusDisplayed = false;
3265 preferredRefreshRate = 0;
3266 preferredModeId = 0;
3267 }
3268 }
3269
3270 private static final class ScreenshotApplicationState {
3271 WindowState appWin;
3272 int maxLayer;
3273 int minLayer;
3274 boolean screenshotReady;
3275
3276 void reset(boolean screenshotReady) {
3277 appWin = null;
3278 maxLayer = 0;
3279 minLayer = 0;
3280 this.screenshotReady = screenshotReady;
3281 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3282 }
3283 }
3284
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003285 /**
3286 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3287 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3288 * homogeneous children type which is currently required by sub-classes of
3289 * {@link WindowContainer} class.
3290 */
3291 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3292
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003293 DisplayChildWindowContainer(WindowManagerService service) {
3294 super(service);
3295 }
3296
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003297 @Override
3298 boolean fillsParent() {
3299 return true;
3300 }
3301
3302 @Override
3303 boolean isVisible() {
3304 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003305 }
3306 }
3307
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003308 /**
3309 * Window container class that contains all containers on this display relating to Apps.
3310 * I.e Activities.
3311 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003312 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003313 /**
3314 * A control placed at the appropriate level for transitions to occur.
3315 */
chaviw23ee71c2017-12-18 11:29:41 -08003316 SurfaceControl mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02003317 SurfaceControl mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02003318 SurfaceControl mHomeAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003319
Robert Carrf7a7ca82017-12-06 13:17:07 -08003320 /**
3321 * Given that the split-screen divider does not have an AppWindowToken, it
3322 * will have to live inside of a "NonAppWindowContainer", in particular
3323 * {@link DisplayContent#mAboveAppWindowsContainers}. However, in visual Z order
3324 * it will need to be interleaved with some of our children, appearing on top of
3325 * both docked stacks but underneath any assistant stacks.
3326 *
3327 * To solve this problem we have this anchor control, which will always exist so
3328 * we can always assign it the correct value in our {@link #assignChildLayers}.
3329 * Likewise since it always exists, {@link AboveAppWindowContainers} can always
3330 * assign the divider a layer relative to it. This way we prevent linking lifecycle
3331 * events between the two containers.
3332 */
3333 SurfaceControl mSplitScreenDividerAnchor = null;
3334
Wale Ogunwale61911492017-10-11 08:50:50 -07003335 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3336 // through the list to find them.
3337 private TaskStack mHomeStack = null;
3338 private TaskStack mPinnedStack = null;
3339 private TaskStack mSplitScreenPrimaryStack = null;
3340
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003341 TaskStackContainers(WindowManagerService service) {
3342 super(service);
3343 }
3344
Wale Ogunwale61911492017-10-11 08:50:50 -07003345 /**
3346 * Returns the topmost stack on the display that is compatible with the input windowing mode
3347 * and activity type. Null is no compatible stack on the display.
3348 */
3349 TaskStack getStack(int windowingMode, int activityType) {
3350 if (activityType == ACTIVITY_TYPE_HOME) {
3351 return mHomeStack;
3352 }
3353 if (windowingMode == WINDOWING_MODE_PINNED) {
3354 return mPinnedStack;
3355 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3356 return mSplitScreenPrimaryStack;
3357 }
3358 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3359 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003360 if (activityType == ACTIVITY_TYPE_UNDEFINED
3361 && windowingMode == stack.getWindowingMode()) {
3362 // Passing in undefined type means we want to match the topmost stack with the
3363 // windowing mode.
3364 return stack;
3365 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003366 if (stack.isCompatible(windowingMode, activityType)) {
3367 return stack;
3368 }
3369 }
3370 return null;
3371 }
3372
3373 @VisibleForTesting
3374 TaskStack getTopStack() {
3375 return mTaskStackContainers.getChildCount() > 0
3376 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3377 }
3378
3379 TaskStack getHomeStack() {
3380 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3381 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3382 }
3383 return mHomeStack;
3384 }
3385
3386 TaskStack getPinnedStack() {
3387 return mPinnedStack;
3388 }
3389
Matthew Ng64e77cf2017-10-31 14:01:31 -07003390 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003391 return mSplitScreenPrimaryStack;
3392 }
3393
Winson Chunge2d72172018-01-25 17:46:20 +00003394 ArrayList<Task> getVisibleTasks() {
3395 final ArrayList<Task> visibleTasks = new ArrayList<>();
3396 forAllTasks(task -> {
3397 if (task.isVisible()) {
3398 visibleTasks.add(task);
3399 }
3400 });
3401 return visibleTasks;
3402 }
3403
Andrii Kulian839def92016-11-02 10:58:58 -07003404 /**
3405 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003406 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003407 */
3408 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003409 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003410 addChild(stack, onTop);
3411 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003412 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003413
Wale Ogunwale61911492017-10-11 08:50:50 -07003414 void onStackWindowingModeChanged(TaskStack stack) {
3415 removeStackReferenceIfNeeded(stack);
3416 addStackReferenceIfNeeded(stack);
3417 if (stack == mPinnedStack && getTopStack() != stack) {
3418 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3419 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3420 }
3421 }
3422
3423 private void addStackReferenceIfNeeded(TaskStack stack) {
3424 if (stack.isActivityTypeHome()) {
Louis Chang6322c422018-07-18 18:37:28 +08003425 // TODO(b/111363427) Rollback to throws exceptions once we figure out how to
3426 // properly deal with home type stack when external display removed
Wale Ogunwale61911492017-10-11 08:50:50 -07003427 if (mHomeStack != null) {
Louis Chang6322c422018-07-18 18:37:28 +08003428 // throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
3429 // + mHomeStack + " already exist on display=" + this + " stack=" + stack);
3430 Slog.e(TAG, "addStackReferenceIfNeeded: home stack="
Wale Ogunwale61911492017-10-11 08:50:50 -07003431 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
Louis Chang6322c422018-07-18 18:37:28 +08003432 } else {
3433 mHomeStack = stack;
Wale Ogunwale61911492017-10-11 08:50:50 -07003434 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003435 }
3436 final int windowingMode = stack.getWindowingMode();
3437 if (windowingMode == WINDOWING_MODE_PINNED) {
3438 if (mPinnedStack != null) {
3439 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3440 + mPinnedStack + " already exist on display=" + this
3441 + " stack=" + stack);
3442 }
3443 mPinnedStack = stack;
3444 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3445 if (mSplitScreenPrimaryStack != null) {
3446 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3447 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3448 + " already exist on display=" + this + " stack=" + stack);
3449 }
3450 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003451 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003452 }
3453 }
3454
3455 private void removeStackReferenceIfNeeded(TaskStack stack) {
3456 if (stack == mHomeStack) {
3457 mHomeStack = null;
3458 } else if (stack == mPinnedStack) {
3459 mPinnedStack = null;
3460 } else if (stack == mSplitScreenPrimaryStack) {
3461 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003462 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3463 mService.setDockedStackCreateStateLocked(
3464 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3465 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003466 }
Andrii Kulian839def92016-11-02 10:58:58 -07003467 }
3468
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003469 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003470 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3471 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003472 addChild(stack, addIndex);
3473 setLayoutNeeded();
3474 }
3475
Wale Ogunwale61911492017-10-11 08:50:50 -07003476 @Override
3477 protected void removeChild(TaskStack stack) {
3478 super.removeChild(stack);
3479 removeStackReferenceIfNeeded(stack);
3480 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003481
3482 @Override
3483 boolean isOnTop() {
3484 // Considered always on top
3485 return true;
3486 }
3487
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003488 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003489 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003490 if (child.getWindowConfiguration().isAlwaysOnTop()
3491 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003492 // This stack is always-on-top, override the default behavior.
3493 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3494
3495 // Moving to its current position, as we must call super but we don't want to
3496 // perform any meaningful action.
3497 final int currentPosition = mChildren.indexOf(child);
3498 super.positionChildAt(currentPosition, child, false /* includingParents */);
3499 return;
3500 }
3501
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003502 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3503 super.positionChildAt(targetPosition, child, includingParents);
3504
3505 setLayoutNeeded();
3506 }
3507
3508 /**
3509 * When stack is added or repositioned, find a proper position for it.
3510 * This will make sure that pinned stack always stays on top.
3511 * @param requestedPosition Position requested by caller.
3512 * @param stack Stack to be added or positioned.
3513 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3514 * @return The proper position for the stack.
3515 */
3516 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09003517 if (stack.inPinnedWindowingMode()) {
Kazuki Takisef85197b2018-06-18 18:18:36 +09003518 return POSITION_TOP;
Kazuki Takise148d00a2018-05-31 15:32:19 +09003519 }
3520
Kazuki Takisef85197b2018-06-18 18:18:36 +09003521 final int topChildPosition = mChildren.size() - 1;
3522 int belowAlwaysOnTopPosition = POSITION_BOTTOM;
3523 for (int i = topChildPosition; i >= 0; --i) {
3524 if (getStacks().get(i) != stack && !getStacks().get(i).isAlwaysOnTop()) {
3525 belowAlwaysOnTopPosition = i;
3526 break;
3527 }
3528 }
3529
3530 // The max possible position we can insert the stack at.
3531 int maxPosition = POSITION_TOP;
3532 // The min possible position we can insert the stack at.
3533 int minPosition = POSITION_BOTTOM;
3534
3535 if (stack.isAlwaysOnTop()) {
3536 if (hasPinnedStack()) {
3537 // Always-on-top stacks go below the pinned stack.
3538 maxPosition = getStacks().indexOf(mPinnedStack) - 1;
3539 }
3540 // Always-on-top stacks need to be above all other stacks.
3541 minPosition = belowAlwaysOnTopPosition !=
3542 POSITION_BOTTOM ? belowAlwaysOnTopPosition : topChildPosition;
3543 } else {
3544 // Other stacks need to be below the always-on-top stacks.
3545 maxPosition = belowAlwaysOnTopPosition !=
Arthur Hung928d8dc2018-07-18 15:38:05 +08003546 POSITION_BOTTOM ? belowAlwaysOnTopPosition : 0;
Kazuki Takisef85197b2018-06-18 18:18:36 +09003547 }
3548
3549 int targetPosition = requestedPosition;
3550 targetPosition = Math.min(targetPosition, maxPosition);
3551 targetPosition = Math.max(targetPosition, minPosition);
3552
3553 int prevPosition = getStacks().indexOf(stack);
3554 // The positions we calculated above (maxPosition, minPosition) do not take into
3555 // consideration the following edge cases.
3556 // 1) We need to adjust the position depending on the value "adding".
3557 // 2) When we are moving a stack to another position, we also need to adjust the
3558 // position depending on whether the stack is moving to a higher or lower position.
3559 if ((targetPosition != requestedPosition) &&
3560 (adding || targetPosition < prevPosition)) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09003561 targetPosition++;
3562 }
3563
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003564 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003565 }
3566
3567 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003568 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3569 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003570 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003571 if (super.forAllWindows(callback, traverseTopToBottom)) {
3572 return true;
3573 }
3574 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3575 return true;
3576 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003577 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003578 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3579 return true;
3580 }
3581 if (super.forAllWindows(callback, traverseTopToBottom)) {
3582 return true;
3583 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003584 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003585 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003586 }
3587
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003588 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003589 boolean traverseTopToBottom) {
3590 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3591 // app tokens.
3592 // TODO: Investigate if we need to continue to do this or if we can just process them
3593 // in-order.
3594 if (traverseTopToBottom) {
3595 for (int i = mChildren.size() - 1; i >= 0; --i) {
3596 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3597 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003598 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3599 traverseTopToBottom)) {
3600 return true;
3601 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003602 }
3603 }
3604 } else {
3605 final int count = mChildren.size();
3606 for (int i = 0; i < count; ++i) {
3607 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3608 final int appTokensCount = appTokens.size();
3609 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003610 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3611 traverseTopToBottom)) {
3612 return true;
3613 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003614 }
3615 }
3616 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003617 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003618 }
3619
Wale Ogunwale1666e312016-12-16 11:27:18 -08003620 void setExitingTokensHasVisible(boolean hasVisible) {
3621 for (int i = mChildren.size() - 1; i >= 0; --i) {
3622 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3623 for (int j = appTokens.size() - 1; j >= 0; --j) {
3624 appTokens.get(j).hasVisible = hasVisible;
3625 }
3626 }
3627 }
3628
3629 void removeExistingAppTokensIfPossible() {
3630 for (int i = mChildren.size() - 1; i >= 0; --i) {
3631 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3632 for (int j = appTokens.size() - 1; j >= 0; --j) {
3633 final AppWindowToken token = appTokens.get(j);
3634 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3635 && (!token.mIsExiting || token.isEmpty())) {
3636 // Make sure there is no animation running on this token, so any windows
3637 // associated with it will be removed as soon as their animations are
3638 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003639 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003640 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3641 "performLayout: App token exiting now removed" + token);
3642 token.removeIfPossible();
3643 }
3644 }
3645 }
3646 }
3647
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003648 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003649 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003650 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
3651 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003652 // Apps and their containers are not allowed to specify an orientation while the
Wale Ogunwaled0c225c2018-06-15 14:47:46 -07003653 // docked or freeform stack is visible...except for the home stack if the docked
3654 // stack is minimized and it actually set something and the bounds is different from
3655 // the display.
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003656 if (mHomeStack != null && mHomeStack.isVisible()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07003657 && mDividerControllerLocked.isMinimizedDock()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07003658 && !(mDividerControllerLocked.isHomeStackResizable()
3659 && mHomeStack.matchParentBounds())) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003660 final int orientation = mHomeStack.getOrientation();
3661 if (orientation != SCREEN_ORIENTATION_UNSET) {
3662 return orientation;
3663 }
3664 }
3665 return SCREEN_ORIENTATION_UNSPECIFIED;
3666 }
3667
3668 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07003669 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
3670 PackageManager.FEATURE_AUTOMOTIVE);
3671 if (isCar) {
3672 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
3673 // allow anything but the default orientation.
3674 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07003675 "Forcing UNSPECIFIED orientation in car for display id=" + mDisplayId
3676 + ". Ignoring " + orientation);
Dean Harding3e5a1522017-10-06 09:19:01 -07003677 return SCREEN_ORIENTATION_UNSPECIFIED;
3678 }
3679
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003680 if (orientation != SCREEN_ORIENTATION_UNSET
3681 && orientation != SCREEN_ORIENTATION_BEHIND) {
3682 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07003683 "App is requesting an orientation, return " + orientation
3684 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003685 return orientation;
3686 }
3687
3688 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07003689 "No app is requesting an orientation, return " + mLastOrientation
3690 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003691 // The next app has not been requested to be visible, so we keep the current orientation
3692 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003693 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003694 }
Robert Carrb1579c82017-09-05 14:54:47 -07003695
3696 @Override
3697 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carr2f8aa392018-01-31 14:46:51 -08003698 assignStackOrdering(t);
Robert Carrf7a7ca82017-12-06 13:17:07 -08003699
Robert Carr2f8aa392018-01-31 14:46:51 -08003700 for (int i = 0; i < mChildren.size(); i++) {
3701 final TaskStack s = mChildren.get(i);
3702 s.assignChildLayers(t);
3703 }
3704 }
3705
3706 void assignStackOrdering(SurfaceControl.Transaction t) {
Jorim Jaggibe418292018-03-26 16:14:12 +02003707
Robert Carrf7a7ca82017-12-06 13:17:07 -08003708 final int HOME_STACK_STATE = 0;
3709 final int NORMAL_STACK_STATE = 1;
3710 final int ALWAYS_ON_TOP_STATE = 2;
3711
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003712 int layer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08003713 int layerForAnimationLayer = 0;
Jorim Jaggibe418292018-03-26 16:14:12 +02003714 int layerForBoostedAnimationLayer = 0;
Jorim Jaggi391790622018-04-18 15:30:44 +02003715 int layerForHomeAnimationLayer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08003716
Robert Carrf7a7ca82017-12-06 13:17:07 -08003717 for (int state = 0; state <= ALWAYS_ON_TOP_STATE; state++) {
3718 for (int i = 0; i < mChildren.size(); i++) {
3719 final TaskStack s = mChildren.get(i);
Robert Carr2f8aa392018-01-31 14:46:51 -08003720 if (state == HOME_STACK_STATE && !s.isActivityTypeHome()) {
3721 continue;
3722 } else if (state == NORMAL_STACK_STATE && (s.isActivityTypeHome()
3723 || s.isAlwaysOnTop())) {
3724 continue;
3725 } else if (state == ALWAYS_ON_TOP_STATE && !s.isAlwaysOnTop()) {
3726 continue;
3727 }
3728 s.assignLayer(t, layer++);
3729 if (s.inSplitScreenWindowingMode() && mSplitScreenDividerAnchor != null) {
3730 t.setLayer(mSplitScreenDividerAnchor, layer++);
3731 }
Winson Chungd41f71d2018-03-16 15:26:07 -07003732 if ((s.isTaskAnimating() || s.isAppAnimating())
3733 && state != ALWAYS_ON_TOP_STATE) {
Robert Carr2f8aa392018-01-31 14:46:51 -08003734 // Ensure the animation layer ends up above the
3735 // highest animating stack and no higher.
3736 layerForAnimationLayer = layer++;
Robert Carrf7a7ca82017-12-06 13:17:07 -08003737 }
Jorim Jaggibe418292018-03-26 16:14:12 +02003738 if (state != ALWAYS_ON_TOP_STATE) {
3739 layerForBoostedAnimationLayer = layer++;
3740 }
Robert Carrf7a7ca82017-12-06 13:17:07 -08003741 }
Jorim Jaggi391790622018-04-18 15:30:44 +02003742 if (state == HOME_STACK_STATE) {
3743 layerForHomeAnimationLayer = layer++;
3744 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003745 }
Robert Carr2f8aa392018-01-31 14:46:51 -08003746 if (mAppAnimationLayer != null) {
3747 t.setLayer(mAppAnimationLayer, layerForAnimationLayer);
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003748 }
Jorim Jaggibe418292018-03-26 16:14:12 +02003749 if (mBoostedAppAnimationLayer != null) {
3750 t.setLayer(mBoostedAppAnimationLayer, layerForBoostedAnimationLayer);
3751 }
Jorim Jaggi391790622018-04-18 15:30:44 +02003752 if (mHomeAppAnimationLayer != null) {
3753 t.setLayer(mHomeAppAnimationLayer, layerForHomeAnimationLayer);
3754 }
Robert Carrb1579c82017-09-05 14:54:47 -07003755 }
3756
3757 @Override
Jorim Jaggi391790622018-04-18 15:30:44 +02003758 SurfaceControl getAppAnimationLayer(@AnimationLayer int animationLayer) {
3759 switch (animationLayer) {
3760 case ANIMATION_LAYER_BOOSTED:
3761 return mBoostedAppAnimationLayer;
3762 case ANIMATION_LAYER_HOME:
3763 return mHomeAppAnimationLayer;
3764 case ANIMATION_LAYER_STANDARD:
3765 default:
3766 return mAppAnimationLayer;
3767 }
chaviw23ee71c2017-12-18 11:29:41 -08003768 }
3769
Robert Carrf7a7ca82017-12-06 13:17:07 -08003770 SurfaceControl getSplitScreenDividerAnchor() {
3771 return mSplitScreenDividerAnchor;
3772 }
3773
chaviw23ee71c2017-12-18 11:29:41 -08003774 @Override
Robert Carrb1579c82017-09-05 14:54:47 -07003775 void onParentSet() {
3776 super.onParentSet();
3777 if (getParent() != null) {
chaviw23ee71c2017-12-18 11:29:41 -08003778 mAppAnimationLayer = makeChildSurface(null)
3779 .setName("animationLayer")
3780 .build();
Jorim Jaggibe418292018-03-26 16:14:12 +02003781 mBoostedAppAnimationLayer = makeChildSurface(null)
3782 .setName("boostedAnimationLayer")
3783 .build();
Jorim Jaggi391790622018-04-18 15:30:44 +02003784 mHomeAppAnimationLayer = makeChildSurface(null)
3785 .setName("homeAnimationLayer")
3786 .build();
Robert Carrf7a7ca82017-12-06 13:17:07 -08003787 mSplitScreenDividerAnchor = makeChildSurface(null)
3788 .setName("splitScreenDividerAnchor")
3789 .build();
3790 getPendingTransaction()
3791 .show(mAppAnimationLayer)
Jorim Jaggibe418292018-03-26 16:14:12 +02003792 .show(mBoostedAppAnimationLayer)
Jorim Jaggi391790622018-04-18 15:30:44 +02003793 .show(mHomeAppAnimationLayer)
Robert Carrf7a7ca82017-12-06 13:17:07 -08003794 .show(mSplitScreenDividerAnchor);
chaviw23ee71c2017-12-18 11:29:41 -08003795 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07003796 } else {
chaviw23ee71c2017-12-18 11:29:41 -08003797 mAppAnimationLayer.destroy();
3798 mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02003799 mBoostedAppAnimationLayer.destroy();
3800 mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02003801 mHomeAppAnimationLayer.destroy();
3802 mHomeAppAnimationLayer = null;
Robert Carrf7a7ca82017-12-06 13:17:07 -08003803 mSplitScreenDividerAnchor.destroy();
3804 mSplitScreenDividerAnchor = null;
Robert Carrb1579c82017-09-05 14:54:47 -07003805 }
3806 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003807 }
3808
Robert Carree4d4b92017-11-22 12:21:46 -08003809 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003810 AboveAppWindowContainers(String name, WindowManagerService service) {
3811 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08003812 }
3813
Robert Carrb9506032018-02-13 13:54:00 -08003814 @Override
Vishnu Nair83537a72018-07-19 21:27:48 -07003815 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
3816 final SurfaceControl.Builder builder = super.makeChildSurface(child);
3817 if (child instanceof WindowToken && ((WindowToken) child).mRoundedCornerOverlay) {
3818 // To draw above the ColorFade layer during the screen off transition, the
3819 // rounded corner overlays need to be at the root of the surface hierarchy.
3820 // TODO: move the ColorLayer into the display overlay layer such that this is not
3821 // necessary anymore.
3822 builder.setParent(null);
3823 }
3824 return builder;
3825 }
3826
3827 @Override
Robert Carrb9506032018-02-13 13:54:00 -08003828 void assignChildLayers(SurfaceControl.Transaction t) {
3829 assignChildLayers(t, null /* imeContainer */);
3830 }
3831
Robert Carree4d4b92017-11-22 12:21:46 -08003832 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
3833 boolean needAssignIme = imeContainer != null
3834 && imeContainer.getSurfaceControl() != null;
3835 for (int j = 0; j < mChildren.size(); ++j) {
3836 final WindowToken wt = mChildren.get(j);
Robert Carrf7a7ca82017-12-06 13:17:07 -08003837
3838 // See {@link mSplitScreenDividerAnchor}
3839 if (wt.windowType == TYPE_DOCK_DIVIDER) {
3840 wt.assignRelativeLayer(t, mTaskStackContainers.getSplitScreenDividerAnchor(), 1);
3841 continue;
3842 }
Vishnu Nair83537a72018-07-19 21:27:48 -07003843 if (wt.mRoundedCornerOverlay) {
3844 wt.assignLayer(t, WindowManagerPolicy.COLOR_FADE_LAYER + 1);
3845 continue;
3846 }
Robert Carree4d4b92017-11-22 12:21:46 -08003847 wt.assignLayer(t, j);
3848 wt.assignChildLayers(t);
3849
3850 int layer = mService.mPolicy.getWindowLayerFromTypeLw(
3851 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08003852
3853 if (needAssignIme && layer >= mService.mPolicy.getWindowLayerFromTypeLw(
3854 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003855 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08003856 needAssignIme = false;
3857 }
3858 }
3859 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003860 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08003861 }
3862 }
3863 }
3864
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003865 /**
3866 * Window container class that contains all containers on this display that are not related to
3867 * Apps. E.g. status bar.
3868 */
Robert Carree4d4b92017-11-22 12:21:46 -08003869 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07003870 /**
3871 * Compares two child window tokens returns -1 if the first is lesser than the second in
3872 * terms of z-order and 1 otherwise.
3873 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003874 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003875 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003876 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3877 token1.mOwnerCanManageAppTokens)
3878 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3879 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003880
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003881 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3882 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3883 return false;
3884 }
3885 final int req = w.mAttrs.screenOrientation;
3886 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3887 || req == SCREEN_ORIENTATION_UNSET) {
3888 return false;
3889 }
3890 return true;
3891 };
3892
Wale Ogunwale3a931692016-11-02 16:49:48 -07003893 private final String mName;
chaviwf29223e2018-01-31 13:23:51 -08003894 private final Dimmer mDimmer = new Dimmer(this);
3895 private final Rect mTmpDimBoundsRect = new Rect();
3896
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003897 NonAppWindowContainers(String name, WindowManagerService service) {
3898 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003899 mName = name;
3900 }
3901
3902 void addChild(WindowToken token) {
3903 addChild(token, mWindowComparator);
3904 }
3905
3906 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003907 int getOrientation() {
3908 final WindowManagerPolicy policy = mService.mPolicy;
3909 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003910 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003911
3912 if (win != null) {
3913 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003914 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003915 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003916 if (mService.mKeyguardGoingAway) {
3917 // Keyguard can't affect the orientation if it is going away...
3918 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3919 return SCREEN_ORIENTATION_UNSET;
3920 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003921 }
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07003922 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req
3923 + " for display id=" + mDisplayId);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003924 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003925 }
3926
Andrii Kulian8ee72852017-03-10 10:36:45 -08003927 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003928
Jorim Jaggi1c530592018-04-06 15:11:47 +02003929 // Only allow force setting the orientation when all unknown visibilities have been
3930 // resolved, as otherwise we just may be starting another occluding activity.
3931 final boolean isUnoccluding =
3932 mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
3933 && mService.mUnknownAppVisibilityController.allResolved();
3934 if (policy.isKeyguardShowingAndNotOccluded() || isUnoccluding) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003935 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003936 }
3937
3938 return SCREEN_ORIENTATION_UNSET;
3939 }
3940
3941 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003942 String getName() {
3943 return mName;
3944 }
chaviwf29223e2018-01-31 13:23:51 -08003945
3946 @Override
3947 Dimmer getDimmer() {
3948 return mDimmer;
3949 }
3950
3951 @Override
3952 void prepareSurfaces() {
3953 mDimmer.resetDimStates();
3954 super.prepareSurfaces();
3955 getBounds(mTmpDimBoundsRect);
3956
3957 if (mDimmer.updateDims(getPendingTransaction(), mTmpDimBoundsRect)) {
3958 scheduleAnimation();
3959 }
3960 }
Robert Carr3b716242016-08-16 16:02:21 -07003961 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003962
Robert Carr9034d962018-01-04 18:27:42 -08003963 private class NonMagnifiableWindowContainers extends NonAppWindowContainers {
3964 NonMagnifiableWindowContainers(String name, WindowManagerService service) {
3965 super(name, service);
3966 }
3967
3968 @Override
3969 void applyMagnificationSpec(Transaction t, MagnificationSpec spec) {
3970 }
3971 };
3972
Robert Carrb1579c82017-09-05 14:54:47 -07003973 SurfaceControl.Builder makeSurface(SurfaceSession s) {
3974 return mService.makeSurfaceBuilder(s)
3975 .setParent(mWindowingLayer);
3976 }
3977
3978 @Override
3979 SurfaceSession getSession() {
3980 return mSession;
3981 }
3982
3983 @Override
3984 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003985 SurfaceSession s = child != null ? child.getSession() : getSession();
3986 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrb1579c82017-09-05 14:54:47 -07003987 b.setSize(mSurfaceSize, mSurfaceSize);
Robert Carrf59b8dd2017-10-02 18:58:36 -07003988
3989 if (child == null) {
3990 return b;
3991 }
3992
Robert Carree4d4b92017-11-22 12:21:46 -08003993 return b.setName(child.getName())
3994 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07003995 }
3996
3997 /**
3998 * The makeSurface variants are for use by the window-container
3999 * hierarchy. makeOverlay here is a function for various non windowing
4000 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
4001 * and other potpourii.
4002 */
4003 SurfaceControl.Builder makeOverlay() {
4004 return mService.makeSurfaceBuilder(mSession)
4005 .setParent(mOverlayLayer);
4006 }
4007
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004008 /**
4009 * Reparents the given surface to mOverlayLayer.
4010 */
4011 void reparentToOverlay(Transaction transaction, SurfaceControl surface) {
4012 transaction.reparent(surface, mOverlayLayer.getHandle());
4013 }
4014
Robert Carrb1579c82017-09-05 14:54:47 -07004015 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carr24be9ab2018-04-30 17:54:53 -07004016 if (spec.scale != 1.0) {
4017 mMagnificationSpec = spec;
4018 } else {
4019 mMagnificationSpec = null;
4020 }
4021
Robert Carrf59b8dd2017-10-02 18:58:36 -07004022 applyMagnificationSpec(getPendingTransaction(), spec);
4023 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07004024 }
4025
Robert Carr24be9ab2018-04-30 17:54:53 -07004026 void reapplyMagnificationSpec() {
4027 if (mMagnificationSpec != null) {
4028 applyMagnificationSpec(getPendingTransaction(), mMagnificationSpec);
4029 }
4030 }
4031
Robert Carrb1579c82017-09-05 14:54:47 -07004032 @Override
4033 void onParentSet() {
4034 // Since we are the top of the SurfaceControl hierarchy here
4035 // we create the root surfaces explicitly rather than chaining
4036 // up as the default implementation in onParentSet does. So we
4037 // explicitly do NOT call super here.
4038 }
4039
4040 @Override
4041 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07004042
4043 // These are layers as children of "mWindowingLayer"
4044 mBelowAppWindowsContainers.assignLayer(t, 0);
4045 mTaskStackContainers.assignLayer(t, 1);
4046 mAboveAppWindowsContainers.assignLayer(t, 2);
4047
4048 WindowState imeTarget = mService.mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08004049 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08004050
Robert Carree4d4b92017-11-22 12:21:46 -08004051 // In the case where we have an IME target that is not in split-screen
4052 // mode IME assignment is easy. We just need the IME to go directly above
4053 // the target. This way children of the target will naturally go above the IME
4054 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08004055 //
Robert Carree4d4b92017-11-22 12:21:46 -08004056 // In the case of split-screen windowing mode, we need to elevate the IME above the
4057 // docked divider while keeping the app itself below the docked divider, so instead
4058 // we use relative layering of the IME targets child windows, and place the
4059 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08004060 //
Robert Carr234b6332018-03-20 13:38:16 -07004061 // In the case the IME target is animating, the animation Z order may be different
4062 // than the WindowContainer Z order, so it's difficult to be sure we have the correct
4063 // IME target. In this case we just layer the IME over all transitions by placing it in the
4064 // above applications layer.
4065 //
Robert Carree4d4b92017-11-22 12:21:46 -08004066 // In the case where we have no IME target we assign it where it's base layer would
4067 // place it in the AboveAppWindowContainers.
Robert Carr234b6332018-03-20 13:38:16 -07004068 if (imeTarget != null && !(imeTarget.inSplitScreenWindowingMode()
4069 || imeTarget.mToken.isAppAnimating())
Robert Carree4d4b92017-11-22 12:21:46 -08004070 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004071 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07004072 // TODO: We need to use an extra level on the app surface to ensure
4073 // this is always above SurfaceView but always below attached window.
4074 1);
Robert Carree4d4b92017-11-22 12:21:46 -08004075 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07004076 }
4077
4078 // Above we have assigned layers to our children, now we ask them to assign
4079 // layers to their children.
4080 mBelowAppWindowsContainers.assignChildLayers(t);
4081 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08004082 mAboveAppWindowsContainers.assignChildLayers(t,
4083 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07004084 mImeWindowsContainers.assignChildLayers(t);
4085 }
4086
4087 /**
4088 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
4089 * that the IME target is one of the docked applications. We'd like the docked divider to be
4090 * above both of the applications, and we'd like the IME to be above the docked divider.
4091 * However we need child windows of the applications to be above the IME (Text drag handles).
4092 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
4093 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
4094 * with {@link #WindowState#assignLayer}
4095 */
4096 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
Robert Carrbb5de662018-04-26 13:29:51 -07004097 child.assignRelativeLayer(t, mImeWindowsContainers.getSurfaceControl(), 1);
Robert Carrb1579c82017-09-05 14:54:47 -07004098 }
4099
4100 @Override
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004101 void prepareSurfaces() {
4102 final ScreenRotationAnimation screenRotationAnimation =
4103 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
4104 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
4105 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
4106 mPendingTransaction.setMatrix(mWindowingLayer,
4107 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
4108 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
4109 mPendingTransaction.setPosition(mWindowingLayer,
4110 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
4111 mPendingTransaction.setAlpha(mWindowingLayer,
4112 screenRotationAnimation.getEnterTransformation().getAlpha());
4113 }
Chavi Weingartenb736e322018-02-23 00:27:54 +00004114
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004115 super.prepareSurfaces();
4116 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004117
Jorim Jaggibe418292018-03-26 16:14:12 +02004118 void assignStackOrdering() {
4119 mTaskStackContainers.assignStackOrdering(getPendingTransaction());
Robert Carr2f8aa392018-01-31 14:46:51 -08004120 }
Chavi Weingarten3a748552018-05-14 17:32:42 +00004121
4122 /**
4123 * Increment the deferral count to determine whether to update the IME target.
4124 */
4125 void deferUpdateImeTarget() {
4126 mDeferUpdateImeTargetCount++;
4127 }
4128
4129 /**
4130 * Decrement the deferral count to determine whether to update the IME target. If the count
4131 * reaches 0, a new ime target will get computed.
4132 */
4133 void continueUpdateImeTarget() {
4134 if (mDeferUpdateImeTargetCount == 0) {
4135 return;
4136 }
4137
4138 mDeferUpdateImeTargetCount--;
4139 if (mDeferUpdateImeTargetCount == 0) {
4140 computeImeTarget(true /* updateImeTarget */);
4141 }
4142 }
4143
4144 /**
4145 * @return Whether a new IME target should be computed.
4146 */
4147 private boolean canUpdateImeTarget() {
4148 return mDeferUpdateImeTargetCount == 0;
4149 }
Arthur Hung95b38a92018-07-20 18:56:12 +08004150
4151 InputMonitor getInputMonitor() {
4152 return mInputMonitor;
4153 }
Craig Mautner59c00972012-07-30 12:10:24 -07004154}