blob: 47dbccb9a20286040e62b44408ab50d5fec478f4 [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 Ogunwale30e441d2017-11-09 08:28:45 -080019import static android.app.ActivityManager.SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT;
Wale Ogunwale61911492017-10-11 08:50:50 -070020import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070021import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070022import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
Wale Ogunwale687b4272017-07-27 02:56:23 -070023import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070024import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
25import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwalea77e1462016-09-28 10:09:46 -070026import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070027import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
28import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale687b4272017-07-27 02:56:23 -070029import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
30import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070031import static android.view.Display.DEFAULT_DISPLAY;
32import static android.view.Display.FLAG_PRIVATE;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070033import static android.view.Surface.ROTATION_0;
34import static android.view.Surface.ROTATION_180;
35import static android.view.Surface.ROTATION_270;
36import static android.view.Surface.ROTATION_90;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070037import static android.view.View.GONE;
Wale Ogunwale10124582016-09-15 20:25:50 -070038import static android.view.WindowManager.DOCKED_BOTTOM;
39import static android.view.WindowManager.DOCKED_INVALID;
40import static android.view.WindowManager.DOCKED_TOP;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080041import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
42import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
43import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale494009b82016-10-21 09:01:38 -070044import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070045import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070046import static android.view.WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE;
47import static android.view.WindowManager.LayoutParams.NEEDS_MENU_UNSET;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070048import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale494009b82016-10-21 09:01:38 -070049import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070050import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale494009b82016-10-21 09:01:38 -070051import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
Robert Carrf7a7ca82017-12-06 13:17:07 -080052import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
Wale Ogunwale494009b82016-10-21 09:01:38 -070053import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070054import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070055import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
56import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Wale Ogunwale494009b82016-10-21 09:01:38 -070057import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070058import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
59import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070060import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070061import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Adrian Roose99bc052017-11-20 17:55:31 +010062import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
63import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
64import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
65import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Adrian Roos1cf585052018-01-03 18:43:27 +010066import static com.android.server.wm.utils.CoordinateTransforms.transformPhysicalToLogicalCoordinates;
Jorim Jaggif84e2f62018-01-16 14:17:59 +010067import static android.view.WindowManager.TRANSIT_KEYGUARD_UNOCCLUDE;
Wale Ogunwale1666e312016-12-16 11:27:18 -080068import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070069import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070070import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070071import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
72import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070073import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070074import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070075import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020076import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070077import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070078import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070079import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -080080import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070081import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070082import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070083import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070084import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070085import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -080086import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -070087import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070088import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
89import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -070090import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070091import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -070092import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
93import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070094import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -070095import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070096import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -070097import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070098import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070099import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700100import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700101import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700102import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700103import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Yi Jin6c6e9ca2018-03-20 16:53:35 -0700104import static com.android.server.wm.DisplayProto.ABOVE_APP_WINDOWS;
105import static com.android.server.wm.DisplayProto.BELOW_APP_WINDOWS;
106import static com.android.server.wm.DisplayProto.DISPLAY_FRAMES;
107import static com.android.server.wm.DisplayProto.DISPLAY_INFO;
108import static com.android.server.wm.DisplayProto.DOCKED_STACK_DIVIDER_CONTROLLER;
109import static com.android.server.wm.DisplayProto.DPI;
110import static com.android.server.wm.DisplayProto.ID;
111import static com.android.server.wm.DisplayProto.IME_WINDOWS;
112import static com.android.server.wm.DisplayProto.PINNED_STACK_CONTROLLER;
113import static com.android.server.wm.DisplayProto.ROTATION;
114import static com.android.server.wm.DisplayProto.SCREEN_ROTATION_ANIMATION;
115import static com.android.server.wm.DisplayProto.STACKS;
116import static com.android.server.wm.DisplayProto.WINDOW_CONTAINER;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700117
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700118import android.annotation.CallSuper;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700119import android.annotation.NonNull;
Dean Harding3e5a1522017-10-06 09:19:01 -0700120import android.content.pm.PackageManager;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700121import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700122import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700123import android.graphics.Bitmap;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700124import android.graphics.Matrix;
Adrian Roos1cf585052018-01-03 18:43:27 +0100125import android.graphics.Path;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800126import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700127import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700128import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100129import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700130import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700131import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700132import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700133import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700134import android.os.RemoteException;
135import android.os.SystemClock;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100136import android.os.Trace;
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800137import android.util.ArraySet;
Chong Zhang8e89b312015-09-09 15:09:30 -0700138import android.util.DisplayMetrics;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700139import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700140import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700141import android.view.Display;
Adrian Roos1cf585052018-01-03 18:43:27 +0100142import android.view.DisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700143import android.view.DisplayInfo;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700144import android.view.InputDevice;
Robert Carrb1579c82017-09-05 14:54:47 -0700145import android.view.MagnificationSpec;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700146import android.view.Surface;
147import android.view.SurfaceControl;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100148import android.view.SurfaceControl.Transaction;
Robert Carrb1579c82017-09-05 14:54:47 -0700149import android.view.SurfaceSession;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700150
Bryce Lee48f4b572017-04-10 10:54:15 -0700151import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800152import com.android.internal.util.ToBooleanFunction;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700153import com.android.internal.view.IInputMethodClient;
Adrian Roose99bc052017-11-20 17:55:31 +0100154import com.android.server.policy.WindowManagerPolicy;
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100155import com.android.server.wm.utils.RotationCache;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100156import com.android.server.wm.utils.WmDisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700157
158import java.io.PrintWriter;
159import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700160import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700161import java.util.HashMap;
162import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700163import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700164import java.util.List;
Adrian Roos1cf585052018-01-03 18:43:27 +0100165import java.util.Objects;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800166import java.util.function.Consumer;
167import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700168
Craig Mautner59c00972012-07-30 12:10:24 -0700169/**
170 * Utility class for keeping track of the WindowStates and other pertinent contents of a
171 * particular Display.
172 *
173 * IMPORTANT: No method from this class should ever be used without holding
174 * WindowManagerService.mWindowMap.
175 */
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700176class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer> {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700177 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700178
179 /** Unique identifier of this stack. */
180 private final int mDisplayId;
181
Wale Ogunwale3a931692016-11-02 16:49:48 -0700182 /** The containers below are the only child containers the display can have. */
183 // Contains all window containers that are related to apps (Activities)
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100184 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers(mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700185 // Contains all non-app window containers that should be displayed above the app containers
186 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800187 private final AboveAppWindowContainers mAboveAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100188 new AboveAppWindowContainers("mAboveAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700189 // Contains all non-app window containers that should be displayed below the app containers
190 // (e.g. Wallpaper).
191 private final NonAppWindowContainers mBelowAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100192 new NonAppWindowContainers("mBelowAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700193 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
194 // on the IME target. We mainly have this container grouping so we can keep track of all the IME
Robert Carr9034d962018-01-04 18:27:42 -0800195 // window containers together and move them in-sync if/when needed. We use a subclass of
196 // WindowContainer which is omitted from screen magnification, as the IME is never magnified.
197 private final NonMagnifiableWindowContainers mImeWindowsContainers =
198 new NonMagnifiableWindowContainers("mImeWindowsContainers", mService);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700199
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000200 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800201 private WindowState mTmpWindow2;
202 private WindowAnimator mTmpWindowAnimator;
203 private boolean mTmpRecoveringMemory;
204 private boolean mUpdateImeTarget;
205 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700206 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700207
Wale Ogunwale02319a62016-09-26 15:21:22 -0700208 // Mapping from a token IBinder to a WindowToken object on this display.
209 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
210
Andrii Kuliancd097992017-03-23 18:31:59 -0700211 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700212 int mInitialDisplayWidth = 0;
213 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700214 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700215
Adrian Roos1cf585052018-01-03 18:43:27 +0100216 DisplayCutout mInitialDisplayCutout;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100217 private final RotationCache<DisplayCutout, WmDisplayCutout> mDisplayCutoutCache
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100218 = new RotationCache<>(this::calculateDisplayCutoutForRotationUncached);
Adrian Roos1cf585052018-01-03 18:43:27 +0100219
Andrii Kuliancd097992017-03-23 18:31:59 -0700220 /**
221 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
222 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
223 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
224 */
Craig Mautner59c00972012-07-30 12:10:24 -0700225 int mBaseDisplayWidth = 0;
226 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700227 /**
228 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
229 * but can be set from Settings or via shell command "adb shell wm density".
230 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
231 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700232 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700233 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700234 private final DisplayInfo mDisplayInfo = new DisplayInfo();
235 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700236 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000237 DisplayFrames mDisplayFrames;
238
Andrii Kulian06d07d62017-03-14 11:11:47 -0700239 /**
240 * For default display it contains real metrics, empty for others.
241 * @see WindowManagerService#createWatermarkInTransaction()
242 */
243 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
244 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
245 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700246
Andrii Kulian06d07d62017-03-14 11:11:47 -0700247 /**
248 * Compat metrics computed based on {@link #mDisplayMetrics}.
249 * @see #updateDisplayAndOrientation(int)
250 */
251 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
252
253 /** The desired scaling factor for compatible apps. */
254 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700255
Andrii Kulian8ee72852017-03-10 10:36:45 -0800256 /**
257 * Current rotation of the display.
258 * Constants as per {@link android.view.Surface.Rotation}.
259 *
Robert Carrae606b42018-02-15 15:36:23 -0800260 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800261 */
262 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700263
Andrii Kulian8ee72852017-03-10 10:36:45 -0800264 /**
265 * Last applied orientation of the display.
266 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
267 *
268 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
269 */
270 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700271
Andrii Kulian8ee72852017-03-10 10:36:45 -0800272 /**
273 * Flag indicating that the application is receiving an orientation that has different metrics
274 * than it expected. E.g. Portrait instead of Landscape.
275 *
Robert Carrae606b42018-02-15 15:36:23 -0800276 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800277 */
278 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700279
Andrii Kulian8ee72852017-03-10 10:36:45 -0800280 /**
281 * Orientation forced by some window. If there is no visible window that specifies orientation
282 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
283 *
284 * @see NonAppWindowContainers#getOrientation()
285 */
286 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700287
Andrii Kulian8ee72852017-03-10 10:36:45 -0800288 /**
289 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
290 * occluded.
291 *
292 * @see NonAppWindowContainers#getOrientation()
293 */
294 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
295
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700296 /**
297 * Keep track of wallpaper visibility to notify changes.
298 */
299 private boolean mLastWallpaperVisible = false;
300
Andrii Kulian06d07d62017-03-14 11:11:47 -0700301 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700302
Craig Mautner39834192012-09-02 07:47:24 -0700303 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700304 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700305 int pendingLayoutChanges;
Andrii Kulian839def92016-11-02 10:58:58 -0700306 // TODO(multi-display): remove some of the usages.
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800307 boolean isDefaultDisplay;
Andrii Kulianf0379de2018-03-14 16:24:07 -0700308 /**
309 * Flag indicating whether WindowManager should override info for this display in
310 * DisplayManager.
311 */
312 boolean mShouldOverrideDisplayConfiguration = true;
Craig Mautner39834192012-09-02 07:47:24 -0700313
Craig Mautnerdc548482014-02-05 13:35:24 -0800314 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700315 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800316
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700317 /** Detect user tapping outside of current focused task bounds .*/
318 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700319
Craig Mautner6601b7b2013-04-29 10:29:11 -0700320 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700321 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700322
Craig Mautner6601b7b2013-04-29 10:29:11 -0700323 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800324 private final Rect mTmpRect = new Rect();
325 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700326 private final RectF mTmpRectF = new RectF();
327 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800328 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700329
Bryce Leef3c6a472017-11-14 14:53:06 -0800330 /** Used for handing back size of display */
331 private final Rect mTmpBounds = new Rect();
332
Craig Mautner95da1082014-02-24 17:54:35 -0800333 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700334 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800335
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700336 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700337 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700338
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800339 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800340 /** A collection of windows that provide tap exclude regions inside of them. */
341 final ArraySet<WindowState> mTapExcludeProvidingWindows = new ArraySet<>();
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800342
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000343 private boolean mHaveBootMsg = false;
344 private boolean mHaveApp = false;
345 private boolean mHaveWallpaper = false;
346 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700347
348 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
349
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700350 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
351 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000352 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
353 new ApplySurfaceChangesTransactionState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700354
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700355 // True if this display is in the process of being removed. Used to determine if the removal of
356 // the display's direct children should be allowed.
357 private boolean mRemovingDisplay = false;
358
Bryce Leed1871262017-06-12 14:12:29 -0700359 // {@code false} if this display is in the processing of being created.
360 private boolean mDisplayReady = false;
361
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800362 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700363
Robert Carrb1579c82017-09-05 14:54:47 -0700364 private final SurfaceSession mSession = new SurfaceSession();
365
366 /**
367 * We organize all top-level Surfaces in to the following layers.
368 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800369 * and omitted from Screen-Magnification, for example the strict mode flash or
370 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700371 * {@link #mWindowingLayer} contains everything else.
372 */
373 private SurfaceControl mOverlayLayer;
374
375 /**
376 * See {@link #mOverlayLayer}
377 */
378 private SurfaceControl mWindowingLayer;
379
380 /**
381 * Specifies the size of the surfaces in {@link #mOverlayLayer} and {@link #mWindowingLayer}.
382 * <p>
383 * For these surfaces currently we use a surface based on the larger of width or height so we
384 * don't have to resize when rotating the display.
385 */
386 private int mSurfaceSize;
387
Adrian Roos5251b1d2018-03-23 18:57:43 +0100388 /**
389 * Sequence number for the current layout pass.
390 */
391 int mLayoutSeq = 0;
392
Chavi Weingarten3a748552018-05-14 17:32:42 +0000393 /**
394 * Specifies the count to determine whether to defer updating the IME target until ready.
395 */
396 private int mDeferUpdateImeTargetCount;
397
Jorim Jaggi9af095b2017-12-12 17:18:57 +0100398 /** Temporary float array to retrieve 3x3 matrix values. */
399 private final float[] mTmpFloats = new float[9];
400
Robert Carr24be9ab2018-04-30 17:54:53 -0700401 private MagnificationSpec mMagnificationSpec;
402
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800403 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
404 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800405 final AppWindowToken atoken = w.mAppToken;
406 if (winAnimator.mDrawState == READY_TO_SHOW) {
Jorim Jaggiaf0d6d22018-06-08 15:25:35 +0200407 if (atoken == null || atoken.canShowWindows()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800408 if (w.performShowLocked()) {
409 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
410 if (DEBUG_LAYOUT_REPEATS) {
411 mService.mWindowPlacerLocked.debugLayoutRepeats(
412 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
413 }
414 }
415 }
416 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800417 };
418
419 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
420 final WindowStateAnimator winAnimator = w.mWinAnimator;
421 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
422 return;
423 }
424
425 final int flags = w.mAttrs.flags;
426
427 // If this window is animating, make a note that we have an animating window and take
428 // care of a request to run a detached wallpaper animation.
Jorim Jaggia5e10572017-11-15 14:36:26 +0100429 if (winAnimator.isAnimationSet()) {
430 final AnimationAdapter anim = w.getAnimation();
431 if (anim != null) {
432 if ((flags & FLAG_SHOW_WALLPAPER) != 0 && anim.getDetachWallpaper()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800433 mTmpWindow = w;
434 }
Jorim Jaggia5e10572017-11-15 14:36:26 +0100435 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800436 if (color != 0) {
437 final TaskStack stack = w.getStack();
438 if (stack != null) {
439 stack.setAnimationBackground(winAnimator, color);
440 }
441 }
442 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800443 }
444
445 // If this window's app token is running a detached wallpaper animation, make a note so
446 // we can ensure the wallpaper is displayed behind it.
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200447 final AppWindowToken atoken = winAnimator.mWin.mAppToken;
448 final AnimationAdapter animation = atoken != null ? atoken.getAnimation() : null;
449 if (animation != null) {
450 if ((flags & FLAG_SHOW_WALLPAPER) != 0 && animation.getDetachWallpaper()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800451 mTmpWindow = w;
452 }
453
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200454 final int color = animation.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800455 if (color != 0) {
456 final TaskStack stack = w.getStack();
457 if (stack != null) {
458 stack.setAnimationBackground(winAnimator, color);
459 }
460 }
461 }
462 };
463
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800464 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
465 final int lostFocusUid = mTmpWindow.mOwnerUid;
466 final Handler handler = mService.mH;
467 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
468 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
469 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
470 w.mAttrs.hideTimeoutMilliseconds);
471 }
472 }
473 };
474
475 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
476 final AppWindowToken focusedApp = mService.mFocusedApp;
477 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
478 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
479
480 if (!w.canReceiveKeys()) {
481 return false;
482 }
483
484 final AppWindowToken wtoken = w.mAppToken;
485
486 // If this window's application has been removed, just skip it.
487 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
488 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
489 + (wtoken.removed ? "removed" : "sendingToBottom"));
490 return false;
491 }
492
493 if (focusedApp == null) {
494 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
495 + " using new focus @ " + w);
496 mTmpWindow = w;
497 return true;
498 }
499
500 if (!focusedApp.windowsAreFocusable()) {
501 // Current focused app windows aren't focusable...
502 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
503 + " focusable using new focus @ " + w);
504 mTmpWindow = w;
505 return true;
506 }
507
508 // Descend through all of the app tokens and find the first that either matches
509 // win.mAppToken (return win) or mFocusedApp (return null).
510 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
511 if (focusedApp.compareTo(wtoken) > 0) {
512 // App stack below focused app stack. No focus for you!!!
513 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
514 "findFocusedWindow: Reached focused app=" + focusedApp);
515 mTmpWindow = null;
516 return true;
517 }
518 }
519
520 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
521 mTmpWindow = w;
522 return true;
523 };
524
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800525 private final Consumer<WindowState> mPerformLayout = w -> {
526 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
527 // wasting time and funky changes while a window is animating away.
528 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
529 || w.isGoneForLayoutLw();
530
531 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
532 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
533 + " mLayoutAttached=" + w.mLayoutAttached
534 + " screen changed=" + w.isConfigChanged());
535 final AppWindowToken atoken = w.mAppToken;
536 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200537 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800538 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
539 + " parentHidden=" + w.isParentWindowHidden());
540 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200541 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800542 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
543 + " parentHidden=" + w.isParentWindowHidden());
544 }
545
546 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
547 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
548 // since that means "perform layout as normal, just don't display").
549 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
550 || ((w.isConfigChanged() || w.setReportResizeHints())
551 && !w.isGoneForLayoutLw() &&
552 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
553 (w.mHasSurface && w.mAppToken != null &&
554 w.mAppToken.layoutConfigChanges)))) {
555 if (!w.mLayoutAttached) {
556 if (mTmpInitial) {
557 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
558 w.mContentChanged = false;
559 }
560 if (w.mAttrs.type == TYPE_DREAM) {
561 // Don't layout windows behind a dream, so that if it does stuff like hide
562 // the status bar we won't get a bad transition when it goes away.
563 mTmpWindow = w;
564 }
565 w.mLayoutNeeded = false;
566 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200567 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000568 mService.mPolicy.layoutWindowLw(w, null, mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100569 w.mLayoutSeq = mLayoutSeq;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800570
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200571 // If this is the first layout, we need to initialize the last inset values as
572 // otherwise we'd immediately cause an unnecessary resize.
573 if (firstLayout) {
574 w.updateLastInsetValues();
575 }
576
Adrian Roos23df3a32018-03-15 15:41:13 +0100577 if (w.mAppToken != null) {
578 w.mAppToken.layoutLetterbox(w);
579 }
580
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800581 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
582 + " mContainingFrame=" + w.mContainingFrame
583 + " mDisplayFrame=" + w.mDisplayFrame);
584 }
585 }
586 };
587
588 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
589 if (w.mLayoutAttached) {
590 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
591 + " mViewVisibility=" + w.mViewVisibility
592 + " mRelayoutCalled=" + w.mRelayoutCalled);
593 // If this view is GONE, then skip it -- keep the current frame, and let the caller
594 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
595 // windows, since that means "perform layout as normal, just don't display").
596 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
597 return;
598 }
599 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
600 || w.mLayoutNeeded) {
601 if (mTmpInitial) {
602 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
603 w.mContentChanged = false;
604 }
605 w.mLayoutNeeded = false;
606 w.prelayout();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000607 mService.mPolicy.layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100608 w.mLayoutSeq = mLayoutSeq;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800609 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.mFrame
610 + " mContainingFrame=" + w.mContainingFrame
611 + " mDisplayFrame=" + w.mDisplayFrame);
612 }
613 } else if (w.mAttrs.type == TYPE_DREAM) {
614 // Don't layout windows behind a dream, so that if it does stuff like hide the
615 // status bar we won't get a bad transition when it goes away.
616 mTmpWindow = mTmpWindow2;
617 }
618 };
619
620 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
621 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
622 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
623 return w.canBeImeTarget();
624 };
625
626 private final Consumer<WindowState> mApplyPostLayoutPolicy =
627 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
628 mService.mInputMethodTarget);
629
630 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
631 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
632 final boolean obscuredChanged = w.mObscured !=
633 mTmpApplySurfaceChangesTransactionState.obscured;
634 final RootWindowContainer root = mService.mRoot;
635 // Only used if default window
636 final boolean someoneLosingFocus = !mService.mLosingFocus.isEmpty();
637
638 // Update effect.
639 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
640 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
641 final boolean isDisplayed = w.isDisplayedLw();
642
643 if (isDisplayed && w.isObscuringDisplay()) {
644 // This window completely covers everything behind it, so we want to leave all
645 // of them as undimmed (for performance reasons).
646 root.mObscuringWindow = w;
647 mTmpApplySurfaceChangesTransactionState.obscured = true;
648 }
649
650 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
651 root.handleNotObscuredLocked(w,
652 mTmpApplySurfaceChangesTransactionState.obscured,
653 mTmpApplySurfaceChangesTransactionState.syswin);
654
655 if (w.mHasSurface && isDisplayed) {
656 final int type = w.mAttrs.type;
657 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
658 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
659 mTmpApplySurfaceChangesTransactionState.syswin = true;
660 }
661 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
662 && w.mAttrs.preferredRefreshRate != 0) {
663 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
664 = w.mAttrs.preferredRefreshRate;
665 }
666 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
667 && w.mAttrs.preferredDisplayModeId != 0) {
668 mTmpApplySurfaceChangesTransactionState.preferredModeId
669 = w.mAttrs.preferredDisplayModeId;
670 }
671 }
672 }
673
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800674 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
675 && mWallpaperController.isWallpaperTarget(w)) {
676 // This is the wallpaper target and its obscured state changed... make sure the
677 // current wallpaper's visibility has been updated accordingly.
678 mWallpaperController.updateWallpaperVisibility();
679 }
680
chaviw161ea3e2018-01-31 12:01:12 -0800681 w.handleWindowMovedIfNeeded();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800682
683 final WindowStateAnimator winAnimator = w.mWinAnimator;
684
685 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
686 w.mContentChanged = false;
687
688 // Moved from updateWindowsAndWallpaperLocked().
689 if (w.mHasSurface) {
690 // Take care of the window being ready to display.
691 final boolean committed = winAnimator.commitFinishDrawingLocked();
692 if (isDefaultDisplay && committed) {
693 if (w.mAttrs.type == TYPE_DREAM) {
694 // HACK: When a dream is shown, it may at that point hide the lock screen.
695 // So we need to redo the layout to let the phone window manager make this
696 // happen.
697 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
698 if (DEBUG_LAYOUT_REPEATS) {
699 surfacePlacer.debugLayoutRepeats(
700 "dream and commitFinishDrawingLocked true",
701 pendingLayoutChanges);
702 }
703 }
704 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
705 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
706 "First draw done in potential wallpaper target " + w);
707 root.mWallpaperMayChange = true;
708 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
709 if (DEBUG_LAYOUT_REPEATS) {
710 surfacePlacer.debugLayoutRepeats(
711 "wallpaper and commitFinishDrawingLocked true",
712 pendingLayoutChanges);
713 }
714 }
715 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800716 }
717
718 final AppWindowToken atoken = w.mAppToken;
719 if (atoken != null) {
Adrian Roos23df3a32018-03-15 15:41:13 +0100720 atoken.updateLetterboxSurface(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800721 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
722 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
723 mTmpUpdateAllDrawn.add(atoken);
724 }
725 }
726
727 if (isDefaultDisplay && someoneLosingFocus && w == mService.mCurrentFocus
728 && w.isDisplayedLw()) {
729 mTmpApplySurfaceChangesTransactionState.focusDisplayed = true;
730 }
731
732 w.updateResizingWindowIfNeeded();
733 };
734
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800735 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800736 * Create new {@link DisplayContent} instance, add itself to the root window container and
737 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700738 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800739 * @param service You know.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700740 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
741 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700742 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700743 DisplayContent(Display display, WindowManagerService service,
Bryce Leef19cbe22018-02-02 15:09:21 -0800744 WallpaperController wallpaperController, DisplayWindowController controller) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100745 super(service);
Bryce Leef19cbe22018-02-02 15:09:21 -0800746 setController(controller);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800747 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
748 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
749 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
750 + " new=" + display);
751 }
752
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700753 mDisplay = display;
754 mDisplayId = display.getDisplayId();
Wale Ogunwale0303c572016-10-20 10:16:29 -0700755 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700756 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700757 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700758 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100759 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo,
760 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700761 initializeDisplayBaseInfo();
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800762 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700763 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700764
Robert Carr74a66a22018-02-23 12:17:51 -0800765 // We use this as our arbitrary surface size for buffer-less parents
766 // that don't impose cropping on their children. It may need to be larger
767 // than the display size because fullscreen windows can be shifted offscreen
768 // due to surfaceInsets. 2 times the largest display dimension feels like an
769 // appropriately arbitrary number. Eventually we would like to give SurfaceFlinger
770 // layers the ability to match their parent sizes and be able to skip
771 // such arbitrary size settings.
772 mSurfaceSize = Math.max(mBaseDisplayHeight, mBaseDisplayWidth) * 2;
Robert Carrb1579c82017-09-05 14:54:47 -0700773
774 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession)
775 .setSize(mSurfaceSize, mSurfaceSize)
776 .setOpaque(true);
777 mWindowingLayer = b.setName("Display Root").build();
778 mOverlayLayer = b.setName("Display Overlays").build();
779
Robert Carrf59b8dd2017-10-02 18:58:36 -0700780 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700781 .setLayerStack(mWindowingLayer, mDisplayId)
782 .show(mWindowingLayer)
783 .setLayer(mOverlayLayer, 1)
784 .setLayerStack(mOverlayLayer, mDisplayId)
785 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700786 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700787
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700788 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700789 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700790 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700791 super.addChild(mAboveAppWindowsContainers, null);
792 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800793
794 // Add itself as a child to the root container.
795 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700796
797 // TODO(b/62541591): evaluate whether this is the best spot to declare the
798 // {@link DisplayContent} ready for use.
799 mDisplayReady = true;
800 }
801
802 boolean isReady() {
803 // The display is ready when the system and the individual display are both ready.
804 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700805 }
806
807 int getDisplayId() {
808 return mDisplayId;
809 }
810
Wale Ogunwale02319a62016-09-26 15:21:22 -0700811 WindowToken getWindowToken(IBinder binder) {
812 return mTokenMap.get(binder);
813 }
814
815 AppWindowToken getAppWindowToken(IBinder binder) {
816 final WindowToken token = getWindowToken(binder);
817 if (token == null) {
818 return null;
819 }
820 return token.asAppWindowToken();
821 }
822
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700823 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700824 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
825 if (dc != null) {
826 // We currently don't support adding a window token to the display if the display
827 // already has the binder mapped to another token. If there is a use case for supporting
828 // this moving forward we will either need to merge the WindowTokens some how or have
829 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700830 throw new IllegalArgumentException("Can't map token=" + token + " to display="
831 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700832 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700833 if (binder == null) {
834 throw new IllegalArgumentException("Can't map token=" + token + " to display="
835 + getName() + " binder is null");
836 }
837 if (token == null) {
838 throw new IllegalArgumentException("Can't map null token to display="
839 + getName() + " binder=" + binder);
840 }
841
Wale Ogunwale02319a62016-09-26 15:21:22 -0700842 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700843
844 if (token.asAppWindowToken() == null) {
845 // Add non-app token to container hierarchy on the display. App tokens are added through
846 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700847 switch (token.windowType) {
848 case TYPE_WALLPAPER:
849 mBelowAppWindowsContainers.addChild(token);
850 break;
851 case TYPE_INPUT_METHOD:
852 case TYPE_INPUT_METHOD_DIALOG:
853 mImeWindowsContainers.addChild(token);
854 break;
855 default:
856 mAboveAppWindowsContainers.addChild(token);
857 break;
858 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700859 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700860 }
861
862 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700863 final WindowToken token = mTokenMap.remove(binder);
864 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800865 token.setExiting();
866 }
867 return token;
868 }
869
870 /** Changes the display the input window token is housed on to this one. */
871 void reParentWindowToken(WindowToken token) {
872 final DisplayContent prevDc = token.getDisplayContent();
873 if (prevDc == this) {
874 return;
875 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800876 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
877 && token.asAppWindowToken() == null) {
878 // Removed the token from the map, but made sure it's not an app token before removing
879 // from parent.
880 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700881 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800882
883 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700884 }
885
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700886 void removeAppToken(IBinder binder) {
887 final WindowToken token = removeWindowToken(binder);
888 if (token == null) {
889 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
890 return;
891 }
892
893 final AppWindowToken appToken = token.asAppWindowToken();
894
895 if (appToken == null) {
896 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
897 return;
898 }
899
900 appToken.onRemovedFromDisplay();
901 }
902
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700903 Display getDisplay() {
904 return mDisplay;
905 }
906
Craig Mautner59c00972012-07-30 12:10:24 -0700907 DisplayInfo getDisplayInfo() {
908 return mDisplayInfo;
909 }
910
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700911 DisplayMetrics getDisplayMetrics() {
912 return mDisplayMetrics;
913 }
914
Andrii Kulian8ee72852017-03-10 10:36:45 -0800915 int getRotation() {
916 return mRotation;
917 }
918
Andrii Kulianef5ce1c2018-03-20 19:27:24 -0700919 @VisibleForTesting
Andrii Kulian8ee72852017-03-10 10:36:45 -0800920 void setRotation(int newRotation) {
921 mRotation = newRotation;
922 }
923
924 int getLastOrientation() {
925 return mLastOrientation;
926 }
927
928 void setLastOrientation(int orientation) {
929 mLastOrientation = orientation;
930 }
931
932 boolean getAltOrientation() {
933 return mAltOrientation;
934 }
935
936 void setAltOrientation(boolean altOrientation) {
937 mAltOrientation = altOrientation;
938 }
939
940 int getLastWindowForcedOrientation() {
941 return mLastWindowForcedOrientation;
942 }
943
Andrii Kulian06d07d62017-03-14 11:11:47 -0700944 /**
945 * Update rotation of the display.
946 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -0700947 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
948 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
Andrii Kulian06d07d62017-03-14 11:11:47 -0700949 */
Robert Carrae606b42018-02-15 15:36:23 -0800950 boolean updateRotationUnchecked() {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -0700951 return updateRotationUnchecked(false /* forceUpdate */);
952 }
Andrii Kulian06d07d62017-03-14 11:11:47 -0700953
Andrii Kulian6cdcfe42018-05-23 17:59:43 -0700954 /**
955 * Update rotation of the display with an option to force the update.
956 * @param forceUpdate Force the rotation update. Sometimes in WM we might skip updating
957 * orientation because we're waiting for some rotation to finish or display
958 * to unfreeze, which results in configuration of the previously visible
959 * activity being applied to a newly visible one. Forcing the rotation
960 * update allows to workaround this issue.
961 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
962 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
963 */
964 boolean updateRotationUnchecked(boolean forceUpdate) {
965 ScreenRotationAnimation screenRotationAnimation;
966 if (!forceUpdate) {
967 if (mService.mDeferredRotationPauseCount > 0) {
968 // Rotation updates have been paused temporarily. Defer the update until
969 // updates have been resumed.
970 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
971 return false;
972 }
973
974 screenRotationAnimation =
975 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
976 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
977 // Rotation updates cannot be performed while the previous rotation change
978 // animation is still in progress. Skip this update. We will try updating
979 // again after the animation is finished and the display is unfrozen.
980 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
981 return false;
982 }
983 if (mService.mDisplayFrozen) {
984 // Even if the screen rotation animation has finished (e.g. isAnimating
985 // returns false), there is still some time where we haven't yet unfrozen
986 // the display. We also need to abort rotation here.
987 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
988 "Deferring rotation, still finishing previous rotation");
989 return false;
990 }
Andrii Kulian06d07d62017-03-14 11:11:47 -0700991 }
992
993 if (!mService.mDisplayEnabled) {
994 // No point choosing a rotation if the display is not enabled.
995 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
996 return false;
997 }
998
999 final int oldRotation = mRotation;
1000 final int lastOrientation = mLastOrientation;
1001 final boolean oldAltOrientation = mAltOrientation;
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001002 final int rotation = mService.mPolicy.rotationForOrientationLw(lastOrientation, oldRotation,
1003 isDefaultDisplay);
1004 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Computed rotation=" + rotation + " for display id="
1005 + mDisplayId + " based on lastOrientation=" + lastOrientation
1006 + " and oldRotation=" + oldRotation);
Robert Carr0e007272017-10-02 13:00:55 -07001007 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(oldRotation,
Robert Carr897215d2017-03-29 12:25:50 -07001008 rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001009
Robert Carr0e007272017-10-02 13:00:55 -07001010 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001011 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001012 if (seamlessRotated != null && !forceUpdate) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001013 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
1014 // to complete (that is, waiting for windows to redraw). It's tempting to check
1015 // w.mSeamlessRotationCount but that could be incorrect in the case of
1016 // window-removal.
1017 return false;
1018 }
Robert Carr0e007272017-10-02 13:00:55 -07001019
1020 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001021 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -07001022 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -07001023 mayRotateSeamlessly = false;
1024 }
1025 for (int i = 0; i < mService.mSessions.size(); i++) {
1026 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
1027 mayRotateSeamlessly = false;
1028 break;
1029 }
1030 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001031 }
Robert Carr0e007272017-10-02 13:00:55 -07001032 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001033
1034 // TODO: Implement forced rotation changes.
1035 // Set mAltOrientation to indicate that the application is receiving
1036 // an orientation that has different metrics than it expected.
1037 // eg. Portrait instead of Landscape.
1038
1039 final boolean altOrientation = !mService.mPolicy.rotationHasCompatibleMetricsLw(
1040 lastOrientation, rotation);
1041
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001042 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1043 + " selected orientation " + lastOrientation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001044 + ", got rotation " + rotation + " which has "
1045 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1046
1047 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1048 // No change.
1049 return false;
1050 }
1051
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001052 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1053 + " rotation changed to " + rotation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001054 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1055 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1056
1057 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1058 mService.mWaitingForConfig = true;
1059 }
1060
1061 mRotation = rotation;
1062 mAltOrientation = altOrientation;
1063 if (isDefaultDisplay) {
1064 mService.mPolicy.setRotationLw(rotation);
1065 }
1066
1067 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
1068 mService.mH.removeMessages(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT);
1069 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1070 WINDOW_FREEZE_TIMEOUT_DURATION);
1071
1072 setLayoutNeeded();
1073 final int[] anim = new int[2];
Robert Carrf59b8dd2017-10-02 18:58:36 -07001074 mService.mPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001075
1076 if (!rotateSeamlessly) {
Robert Carrae606b42018-02-15 15:36:23 -08001077 mService.startFreezingDisplayLocked(anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001078 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1079 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1080 mDisplayId);
1081 } else {
1082 // The screen rotation animation uses a screenshot to freeze the screen
1083 // while windows resize underneath.
1084 // When we are rotating seamlessly, we allow the elements to transition
1085 // to their rotated state independently and without a freeze required.
1086 screenRotationAnimation = null;
1087
Robert Carrefc75702018-02-16 11:46:16 -08001088 mService.startSeamlessRotation();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001089 }
1090
1091 // We need to update our screen size information to match the new rotation. If the rotation
1092 // has actually changed then this method will return true and, according to the comment at
1093 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1094 // By updating the Display info here it will be available to
1095 // #computeScreenConfiguration() later.
1096 updateDisplayAndOrientation(getConfiguration().uiMode);
1097
Robert Carrae606b42018-02-15 15:36:23 -08001098 // NOTE: We disable the rotation in the emulator because
1099 // it doesn't support hardware OpenGL emulation yet.
1100 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1101 && screenRotationAnimation.hasScreenshot()) {
1102 if (screenRotationAnimation.setRotation(getPendingTransaction(), rotation,
1103 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1104 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1105 mService.scheduleAnimationLocked();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001106 }
1107 }
1108
Adrian Roos28c25e22018-05-31 18:07:28 +02001109 forAllWindows(w -> {
1110 w.forceSeamlesslyRotateIfAllowed(oldRotation, rotation);
1111 }, true /* traverseTopToBottom */);
1112
Robert Carrae606b42018-02-15 15:36:23 -08001113 if (rotateSeamlessly) {
Adrian Roos41f7e9d2018-06-07 15:29:34 +02001114 seamlesslyRotate(getPendingTransaction(), oldRotation, rotation);
Robert Carrae606b42018-02-15 15:36:23 -08001115 }
1116
1117 mService.mDisplayManagerInternal.performTraversal(getPendingTransaction());
1118 scheduleAnimation();
1119
Andrii Kulian06d07d62017-03-14 11:11:47 -07001120 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001121 if (w.mHasSurface && !rotateSeamlessly) {
1122 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001123 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001124 mService.mRoot.mOrientationChangeComplete = false;
1125 w.mLastFreezeDuration = 0;
1126 }
1127 w.mReportOrientationChanged = true;
1128 }, true /* traverseTopToBottom */);
1129
1130 if (rotateSeamlessly) {
1131 mService.mH.removeMessages(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT);
1132 mService.mH.sendEmptyMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1133 SEAMLESS_ROTATION_TIMEOUT_DURATION);
1134 }
1135
1136 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1137 final WindowManagerService.RotationWatcher rotationWatcher
1138 = mService.mRotationWatchers.get(i);
1139 if (rotationWatcher.mDisplayId == mDisplayId) {
1140 try {
1141 rotationWatcher.mWatcher.onRotationChanged(rotation);
1142 } catch (RemoteException e) {
1143 // Ignore
1144 }
1145 }
1146 }
1147
1148 // TODO (multi-display): Magnification is supported only for the default display.
1149 // Announce rotation only if we will not animate as we already have the
1150 // windows in final state. Otherwise, we make this call at the rotation end.
1151 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1152 && isDefaultDisplay) {
1153 mService.mAccessibilityController.onRotationChangedLocked(this);
1154 }
1155
1156 return true;
1157 }
1158
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001159 void configureDisplayPolicy() {
1160 mService.mPolicy.setInitialDisplaySize(getDisplay(),
1161 mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1162
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001163 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
1164 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001165 }
1166
Andrii Kulian06d07d62017-03-14 11:11:47 -07001167 /**
1168 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1169 * changed.
1170 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1171 */
1172 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1173 // Use the effective "visual" dimensions based on current rotation
1174 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1175 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1176 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1177 int dw = realdw;
1178 int dh = realdh;
1179
1180 if (mAltOrientation) {
1181 if (realdw > realdh) {
1182 // Turn landscape into portrait.
1183 int maxw = (int)(realdh/1.3f);
1184 if (maxw < realdw) {
1185 dw = maxw;
1186 }
1187 } else {
1188 // Turn portrait into landscape.
1189 int maxh = (int)(realdw/1.3f);
1190 if (maxh < realdh) {
1191 dh = maxh;
1192 }
1193 }
1194 }
1195
1196 // Update application display metrics.
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001197 final WmDisplayCutout wmDisplayCutout = calculateDisplayCutoutForRotation(mRotation);
1198 final DisplayCutout displayCutout = wmDisplayCutout.getDisplayCutout();
1199
Andrii Kulian06d07d62017-03-14 11:11:47 -07001200 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001201 mDisplayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001202 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001203 mDisplayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001204 mDisplayInfo.rotation = mRotation;
1205 mDisplayInfo.logicalWidth = dw;
1206 mDisplayInfo.logicalHeight = dh;
1207 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1208 mDisplayInfo.appWidth = appWidth;
1209 mDisplayInfo.appHeight = appHeight;
1210 if (isDefaultDisplay) {
1211 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1212 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1213 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001214 mDisplayInfo.displayCutout = displayCutout.isEmpty() ? null : displayCutout;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001215 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1216 if (mDisplayScalingDisabled) {
1217 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1218 } else {
1219 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1220 }
1221
Andrii Kulianf0379de2018-03-14 16:24:07 -07001222 // We usually set the override info in DisplayManager so that we get consistent display
1223 // metrics values when displays are changing and don't send out new values until WM is aware
1224 // of them. However, we don't do this for displays that serve as containers for ActivityView
1225 // because we don't want letter-/pillar-boxing during resize.
1226 final DisplayInfo overrideDisplayInfo = mShouldOverrideDisplayConfiguration
1227 ? mDisplayInfo : null;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001228 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
Andrii Kulianf0379de2018-03-14 16:24:07 -07001229 overrideDisplayInfo);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001230
1231 mBaseDisplayRect.set(0, 0, dw, dh);
1232
1233 if (isDefaultDisplay) {
1234 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1235 mCompatDisplayMetrics);
1236 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001237
1238 updateBounds();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001239 return mDisplayInfo;
1240 }
1241
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001242 WmDisplayCutout calculateDisplayCutoutForRotation(int rotation) {
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001243 return mDisplayCutoutCache.getOrCompute(mInitialDisplayCutout, rotation);
1244 }
1245
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001246 private WmDisplayCutout calculateDisplayCutoutForRotationUncached(
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001247 DisplayCutout cutout, int rotation) {
Adrian Roos24264212018-02-19 16:26:15 +01001248 if (cutout == null || cutout == DisplayCutout.NO_CUTOUT) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001249 return WmDisplayCutout.NO_CUTOUT;
Adrian Roos1cf585052018-01-03 18:43:27 +01001250 }
Adrian Roos11c25582018-02-19 18:06:36 +01001251 if (rotation == ROTATION_0) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001252 return WmDisplayCutout.computeSafeInsets(
1253 cutout, mInitialDisplayWidth, mInitialDisplayHeight);
Adrian Roos24264212018-02-19 16:26:15 +01001254 }
Adrian Roosbed538e2018-02-21 17:50:07 +01001255 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Adrian Roos1cf585052018-01-03 18:43:27 +01001256 final Path bounds = cutout.getBounds().getBoundaryPath();
Adrian Roos11c25582018-02-19 18:06:36 +01001257 transformPhysicalToLogicalCoordinates(rotation, mInitialDisplayWidth, mInitialDisplayHeight,
1258 mTmpMatrix);
Adrian Roos1cf585052018-01-03 18:43:27 +01001259 bounds.transform(mTmpMatrix);
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001260 return WmDisplayCutout.computeSafeInsets(DisplayCutout.fromBounds(bounds),
Adrian Roos24264212018-02-19 16:26:15 +01001261 rotated ? mInitialDisplayHeight : mInitialDisplayWidth,
1262 rotated ? mInitialDisplayWidth : mInitialDisplayHeight);
Adrian Roos1cf585052018-01-03 18:43:27 +01001263 }
1264
Andrii Kulian06d07d62017-03-14 11:11:47 -07001265 /**
1266 * Compute display configuration based on display properties and policy settings.
1267 * Do not call if mDisplayReady == false.
1268 */
1269 void computeScreenConfiguration(Configuration config) {
1270 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1271
1272 final int dw = displayInfo.logicalWidth;
1273 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001274 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
1275 // TODO: Probably best to set this based on some setting in the display content object,
1276 // so the display can be configured for things like fullscreen.
1277 config.windowConfiguration.setWindowingMode(WINDOWING_MODE_FULLSCREEN);
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001278
Adrian Roos11c25582018-02-19 18:06:36 +01001279 final float density = mDisplayMetrics.density;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001280 config.screenWidthDp =
1281 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001282 config.uiMode, mDisplayId, displayInfo.displayCutout) / density);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001283 config.screenHeightDp =
1284 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001285 config.uiMode, mDisplayId, displayInfo.displayCutout) / density);
Bryce Lee7566d762017-03-30 09:34:15 -07001286
Adrian Roos11c25582018-02-19 18:06:36 +01001287 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh,
1288 displayInfo.displayCutout, mTmpRect);
Bryce Lee7566d762017-03-30 09:34:15 -07001289 final int leftInset = mTmpRect.left;
1290 final int topInset = mTmpRect.top;
1291 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001292 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1293 leftInset + displayInfo.appWidth /* right */,
1294 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001295 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1296 || displayInfo.rotation == Surface.ROTATION_270);
1297
1298 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
Adrian Roos11c25582018-02-19 18:06:36 +01001299 density, config);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001300
1301 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1302 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1303 ? Configuration.SCREENLAYOUT_ROUND_YES
1304 : Configuration.SCREENLAYOUT_ROUND_NO);
1305
1306 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1307 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1308 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1309 dh, mDisplayId);
1310 config.densityDpi = displayInfo.logicalDensityDpi;
1311
1312 config.colorMode =
1313 (displayInfo.isHdr()
1314 ? Configuration.COLOR_MODE_HDR_YES
1315 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001316 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001317 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1318 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1319
1320 // Update the configuration based on available input devices, lid switch,
1321 // and platform configuration.
1322 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1323 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1324 config.navigation = Configuration.NAVIGATION_NONAV;
1325
1326 int keyboardPresence = 0;
1327 int navigationPresence = 0;
1328 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1329 final int len = devices != null ? devices.length : 0;
1330 for (int i = 0; i < len; i++) {
1331 InputDevice device = devices[i];
1332 if (!device.isVirtual()) {
1333 final int sources = device.getSources();
1334 final int presenceFlag = device.isExternal() ?
1335 WindowManagerPolicy.PRESENCE_EXTERNAL :
1336 WindowManagerPolicy.PRESENCE_INTERNAL;
1337
1338 // TODO(multi-display): Configure on per-display basis.
1339 if (mService.mIsTouchDevice) {
1340 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1341 InputDevice.SOURCE_TOUCHSCREEN) {
1342 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1343 }
1344 } else {
1345 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1346 }
1347
1348 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1349 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1350 navigationPresence |= presenceFlag;
1351 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1352 && config.navigation == Configuration.NAVIGATION_NONAV) {
1353 config.navigation = Configuration.NAVIGATION_DPAD;
1354 navigationPresence |= presenceFlag;
1355 }
1356
1357 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1358 config.keyboard = Configuration.KEYBOARD_QWERTY;
1359 keyboardPresence |= presenceFlag;
1360 }
1361 }
1362 }
1363
1364 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1365 config.navigation = Configuration.NAVIGATION_DPAD;
1366 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1367 }
1368
1369 // Determine whether a hard keyboard is available and enabled.
1370 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1371 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1372 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1373 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1374 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1375 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1376 }
1377
1378 // Let the policy update hidden states.
1379 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1380 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1381 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1382 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1383 }
1384
1385 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1386 int displayId) {
1387 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1388 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1389 final int unrotDw, unrotDh;
1390 if (rotated) {
1391 unrotDw = dh;
1392 unrotDh = dw;
1393 } else {
1394 unrotDw = dw;
1395 unrotDh = dh;
1396 }
1397 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1398 displayId);
1399 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1400 displayId);
1401 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1402 displayId);
1403 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1404 displayId);
1405 return sw;
1406 }
1407
1408 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1409 DisplayMetrics dm, int dw, int dh, int displayId) {
1410 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001411 displayId, mDisplayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001412 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001413 uiMode, displayId, mDisplayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001414 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1415 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1416 if (curSize == 0 || size < curSize) {
1417 curSize = size;
1418 }
1419 return curSize;
1420 }
1421
1422 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1423 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1424
1425 // We need to determine the smallest width that will occur under normal
1426 // operation. To this, start with the base screen size and compute the
1427 // width under the different possible rotations. We need to un-rotate
1428 // the current screen dimensions before doing this.
1429 int unrotDw, unrotDh;
1430 if (rotated) {
1431 unrotDw = dh;
1432 unrotDh = dw;
1433 } else {
1434 unrotDw = dw;
1435 unrotDh = dh;
1436 }
1437 displayInfo.smallestNominalAppWidth = 1<<30;
1438 displayInfo.smallestNominalAppHeight = 1<<30;
1439 displayInfo.largestNominalAppWidth = 0;
1440 displayInfo.largestNominalAppHeight = 0;
1441 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1442 unrotDh);
1443 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1444 unrotDw);
1445 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1446 unrotDh);
1447 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1448 unrotDw);
1449 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1450 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1451 displayId);
1452 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1453 displayId);
1454 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1455 displayId);
1456 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1457 displayId);
1458 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1459 outConfig.screenLayout = sl;
1460 }
1461
1462 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1463 int uiMode, int displayId) {
1464 // Get the app screen size at this rotation.
Adrian Roos11c25582018-02-19 18:06:36 +01001465 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId,
1466 mDisplayInfo.displayCutout);
1467 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId,
1468 mDisplayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001469
1470 // Compute the screen layout size class for this rotation.
1471 int longSize = w;
1472 int shortSize = h;
1473 if (longSize < shortSize) {
1474 int tmp = longSize;
1475 longSize = shortSize;
1476 shortSize = tmp;
1477 }
1478 longSize = (int)(longSize/density);
1479 shortSize = (int)(shortSize/density);
1480 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1481 }
1482
1483 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1484 int uiMode, int dw, int dh) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001485 final DisplayCutout displayCutout = calculateDisplayCutoutForRotation(
1486 rotation).getDisplayCutout();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001487 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001488 displayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001489 if (width < displayInfo.smallestNominalAppWidth) {
1490 displayInfo.smallestNominalAppWidth = width;
1491 }
1492 if (width > displayInfo.largestNominalAppWidth) {
1493 displayInfo.largestNominalAppWidth = width;
1494 }
1495 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001496 displayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001497 if (height < displayInfo.smallestNominalAppHeight) {
1498 displayInfo.smallestNominalAppHeight = height;
1499 }
1500 if (height > displayInfo.largestNominalAppHeight) {
1501 displayInfo.largestNominalAppHeight = height;
1502 }
1503 }
1504
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001505 DockedStackDividerController getDockedDividerController() {
1506 return mDividerControllerLocked;
1507 }
1508
Winson Chung655332c2016-10-31 13:14:28 -07001509 PinnedStackController getPinnedStackController() {
1510 return mPinnedStackControllerLocked;
1511 }
1512
Jeff Browna506a6e2013-06-04 00:02:38 -07001513 /**
1514 * Returns true if the specified UID has access to this display.
1515 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001516 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001517 return mDisplay.hasAccess(uid);
1518 }
1519
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001520 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001521 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001522 }
1523
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001524 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001525 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001526 }
1527
Wale Ogunwale61911492017-10-11 08:50:50 -07001528 /**
1529 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1530 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001531 TaskStack getSplitScreenPrimaryStack() {
1532 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001533 return (stack != null && stack.isVisible()) ? stack : null;
1534 }
1535
Robert Carr9785cf32018-04-25 15:06:07 -07001536 boolean hasSplitScreenPrimaryStack() {
1537 return getSplitScreenPrimaryStack() != null;
1538 }
1539
Wale Ogunwale61911492017-10-11 08:50:50 -07001540 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001541 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001542 * not visible.
1543 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001544 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1545 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001546 }
1547
1548 TaskStack getPinnedStack() {
1549 return mTaskStackContainers.getPinnedStack();
1550 }
1551
1552 private boolean hasPinnedStack() {
1553 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001554 }
1555
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001556 /**
1557 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1558 * Null is no compatible stack on the display.
1559 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001560 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001561 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1562 }
1563
1564 /**
1565 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1566 * activity type. Null is no compatible stack on the display.
1567 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001568 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001569 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001570 }
1571
Bryce Lee48f4b572017-04-10 10:54:15 -07001572 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001573 TaskStack getTopStack() {
1574 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001575 }
1576
Winson Chunge2d72172018-01-25 17:46:20 +00001577 ArrayList<Task> getVisibleTasks() {
1578 return mTaskStackContainers.getVisibleTasks();
1579 }
1580
Wale Ogunwale61911492017-10-11 08:50:50 -07001581 void onStackWindowingModeChanged(TaskStack stack) {
1582 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001583 }
1584
Andrii Kulian441e4492016-09-29 15:25:00 -07001585 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001586 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001587 super.onConfigurationChanged(newParentConfig);
1588
Andrii Kulian3a507b52016-09-19 18:14:12 -07001589 // The display size information is heavily dependent on the resources in the current
1590 // configuration, so we need to reconfigure it every time the configuration changes.
1591 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1592 mService.reconfigureDisplayLocked(this);
1593
Bryce Leef3c6a472017-11-14 14:53:06 -08001594 final DockedStackDividerController dividerController = getDockedDividerController();
1595
1596 if (dividerController != null) {
1597 getDockedDividerController().onConfigurationChanged();
1598 }
1599
1600 final PinnedStackController pinnedStackController = getPinnedStackController();
1601
1602 if (pinnedStackController != null) {
1603 getPinnedStackController().onConfigurationChanged();
1604 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001605 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001606
Andrii Kulian441e4492016-09-29 15:25:00 -07001607 /**
1608 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1609 * bounds were updated.
1610 */
Bryce Leeaea60d22018-01-31 14:42:15 -08001611 void updateStackBoundsAfterConfigChange(@NonNull List<TaskStack> changedStackList) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001612 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1613 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001614 if (stack.updateBoundsAfterConfigChange()) {
Bryce Leeaea60d22018-01-31 14:42:15 -08001615 changedStackList.add(stack);
Andrii Kulian3a507b52016-09-19 18:14:12 -07001616 }
1617 }
Winson Chung32c566f2017-04-11 18:31:21 -07001618
1619 // If there was no pinned stack, we still need to notify the controller of the display info
1620 // update as a result of the config change. We do this here to consolidate the flow between
1621 // changes when there is and is not a stack.
Wale Ogunwale61911492017-10-11 08:50:50 -07001622 if (!hasPinnedStack()) {
Winson Chung32c566f2017-04-11 18:31:21 -07001623 mPinnedStackControllerLocked.onDisplayInfoChanged();
1624 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001625 }
1626
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001627 @Override
1628 boolean fillsParent() {
1629 return true;
1630 }
1631
1632 @Override
1633 boolean isVisible() {
1634 return true;
1635 }
1636
1637 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001638 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001639 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001640 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001641 }
1642
Robert Carr9785cf32018-04-25 15:06:07 -07001643 /**
1644 * In split-screen mode we process the IME containers above the docked divider
1645 * rather than directly above their target.
1646 */
1647 private boolean skipTraverseChild(WindowContainer child) {
1648 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null
1649 && !hasSplitScreenPrimaryStack()) {
1650 return true;
1651 }
1652 return false;
1653 }
1654
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001655 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001656 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1657 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1658 // target, or here in order if there isn't an IME target.
1659 if (traverseTopToBottom) {
1660 for (int i = mChildren.size() - 1; i >= 0; --i) {
1661 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001662 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001663 continue;
1664 }
Robert Carr9785cf32018-04-25 15:06:07 -07001665
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001666 if (child.forAllWindows(callback, traverseTopToBottom)) {
1667 return true;
1668 }
1669 }
1670 } else {
1671 final int count = mChildren.size();
1672 for (int i = 0; i < count; i++) {
1673 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001674 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001675 continue;
1676 }
Robert Carr9785cf32018-04-25 15:06:07 -07001677
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001678 if (child.forAllWindows(callback, traverseTopToBottom)) {
1679 return true;
1680 }
1681 }
1682 }
1683 return false;
1684 }
1685
1686 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1687 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1688 }
1689
Wale Ogunwale399c8692017-05-08 14:22:42 -07001690 @Override
1691 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001692 final WindowManagerPolicy policy = mService.mPolicy;
1693
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001694 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001695 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001696 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1697 + " is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001698 // If the display is frozen, some activities may be in the middle of restarting, and
1699 // thus have removed their old window. If the window has the flag to hide the lock
1700 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1701 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001702 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001703 } else if (policy.isKeyguardLocked()) {
1704 // Use the last orientation the while the display is frozen with the keyguard
1705 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1706 // window. We don't want to check the show when locked window directly though as
1707 // things aren't stable while the display is frozen, for example the window could be
1708 // momentarily unavailable due to activity relaunch.
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001709 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1710 + " is frozen while keyguard locked, return " + mLastOrientation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08001711 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001712 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001713 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001714 final int orientation = mAboveAppWindowsContainers.getOrientation();
1715 if (orientation != SCREEN_ORIENTATION_UNSET) {
1716 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001717 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001718 }
1719
Wale Ogunwale399c8692017-05-08 14:22:42 -07001720 // Top system windows are not requesting an orientation. Start searching from apps.
1721 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001722 }
1723
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001724 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001725 // Check if display metrics changed and update base values if needed.
1726 updateBaseDisplayMetricsIfNeeded();
1727
Craig Mautner722285e2012-09-07 13:55:58 -07001728 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001729 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001730
Wale Ogunwale61911492017-10-11 08:50:50 -07001731 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1732 mTaskStackContainers.getChildAt(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001733 }
Craig Mautner722285e2012-09-07 13:55:58 -07001734 }
1735
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001736 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001737 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1738 if (displayManagerInternal != null) {
1739 // Bootstrap the default logical display from the display manager.
1740 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1741 if (newDisplayInfo != null) {
1742 mDisplayInfo.copyFrom(newDisplayInfo);
1743 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001744 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001745
Andrii Kuliancd097992017-03-23 18:31:59 -07001746 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1747 mDisplayInfo.logicalDensityDpi);
1748 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1749 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1750 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01001751 mInitialDisplayCutout = mDisplayInfo.displayCutout;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001752 }
1753
Andrii Kuliancd097992017-03-23 18:31:59 -07001754 /**
1755 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1756 * values.
1757 */
1758 private void updateBaseDisplayMetricsIfNeeded() {
1759 // Get real display metrics without overrides from WM.
1760 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1761 final int orientation = mDisplayInfo.rotation;
1762 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1763 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1764 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1765 final int newDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01001766 final DisplayCutout newCutout = mDisplayInfo.displayCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07001767
1768 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1769 || mInitialDisplayHeight != newHeight
Adrian Roos1cf585052018-01-03 18:43:27 +01001770 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi
1771 || !Objects.equals(mInitialDisplayCutout, newCutout);
Andrii Kuliancd097992017-03-23 18:31:59 -07001772
1773 if (displayMetricsChanged) {
1774 // Check if display size or density is forced.
1775 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1776 || mBaseDisplayHeight != mInitialDisplayHeight;
1777 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1778
1779 // If there is an override set for base values - use it, otherwise use new values.
1780 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1781 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1782 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1783
1784 // Real display metrics changed, so we should also update initial values.
1785 mInitialDisplayWidth = newWidth;
1786 mInitialDisplayHeight = newHeight;
1787 mInitialDisplayDensity = newDensity;
Adrian Roos1cf585052018-01-03 18:43:27 +01001788 mInitialDisplayCutout = newCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07001789 mService.reconfigureDisplayLocked(this);
1790 }
1791 }
1792
Bryce Lee27cec322017-03-21 09:41:37 -07001793 /** Sets the maximum width the screen resolution can be */
1794 void setMaxUiWidth(int width) {
1795 if (DEBUG_DISPLAY) {
1796 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1797 }
1798
1799 mMaxUiWidth = width;
1800
1801 // Update existing metrics.
1802 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1803 }
1804
1805 /** Update base (override) display metrics. */
1806 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1807 mBaseDisplayWidth = baseWidth;
1808 mBaseDisplayHeight = baseHeight;
1809 mBaseDisplayDensity = baseDensity;
1810
1811 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1812 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1813 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1814 mBaseDisplayWidth = mMaxUiWidth;
1815
1816 if (DEBUG_DISPLAY) {
1817 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
1818 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
1819 + " on display:" + getDisplayId());
1820 }
1821 }
1822
1823 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08001824
1825 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07001826 }
1827
Wale Ogunwaledb506192017-12-08 10:57:32 -08001828 void getStableRect(Rect out) {
1829 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08001830 }
1831
Wale Ogunwale61911492017-10-11 08:50:50 -07001832 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001833 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
1834 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07001835
Wale Ogunwale61911492017-10-11 08:50:50 -07001836 final TaskStack stack = new TaskStack(mService, stackId, controller);
1837 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08001838 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07001839 }
1840
Andrii Kulian51c1b672017-04-07 18:39:32 -07001841 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08001842 final DisplayContent prevDc = stack.getDisplayContent();
1843 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07001844 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
1845 + " which is not currently attached to any display");
1846 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08001847 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07001848 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
1849 + " to its current displayId=" + mDisplayId);
1850 }
1851
Wale Ogunwale61911492017-10-11 08:50:50 -07001852 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07001853 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08001854 }
1855
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001856 @Override
1857 protected void addChild(DisplayChildWindowContainer child,
1858 Comparator<DisplayChildWindowContainer> comparator) {
1859 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1860 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07001861
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001862 @Override
1863 protected void addChild(DisplayChildWindowContainer child, int index) {
1864 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
1865 }
1866
1867 @Override
1868 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07001869 // Only allow removal of direct children from this display if the display is in the process
1870 // of been removed.
1871 if (mRemovingDisplay) {
1872 super.removeChild(child);
1873 return;
1874 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001875 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001876 }
1877
Andrii Kuliand2765632016-12-12 22:26:34 -08001878 @Override
1879 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
1880 // Children of the display are statically ordered, so the real intention here is to perform
1881 // the operation on the display and not the static direct children.
1882 getParent().positionChildAt(position, this, includingParents);
1883 }
1884
Winson Chung59a47ded2018-01-25 17:46:06 +00001885 void positionStackAt(int position, TaskStack child) {
1886 mTaskStackContainers.positionChildAt(position, child, false /* includingParents */);
1887 layoutAndAssignWindowLayersIfNeeded();
1888 }
1889
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001890 int taskIdFromPoint(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001891 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1892 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001893 final int taskId = stack.taskIdFromPoint(x, y);
1894 if (taskId != -1) {
1895 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07001896 }
1897 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001898 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07001899 }
1900
Chong Zhang8e89b312015-09-09 15:09:30 -07001901 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08001902 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07001903 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07001904 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07001905 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001906 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001907 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07001908 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1909 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07001910 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001911 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001912 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001913
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07001914 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
1915 if (mTmpTaskForResizePointSearchResult.searchDone) {
1916 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07001917 }
1918 }
Chong Zhang9184ec62015-09-24 12:32:21 -07001919 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07001920 }
1921
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07001922 void setTouchExcludeRegion(Task focusedTask) {
David Stevensee9e2772017-02-09 16:30:27 -08001923 // The provided task is the task on this display with focus, so if WindowManagerService's
1924 // focused app is not on this display, focusedTask will be null.
1925 if (focusedTask == null) {
1926 mTouchExcludeRegion.setEmpty();
1927 } else {
1928 mTouchExcludeRegion.set(mBaseDisplayRect);
1929 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
1930 mTmpRect2.setEmpty();
Wale Ogunwale61911492017-10-11 08:50:50 -07001931 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1932 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001933 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
1934 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08001935 }
1936 // If we removed the focused task above, add it back and only leave its
1937 // outside touch area in the exclusion. TapDectector is not interested in
1938 // any touch inside the focused task itself.
1939 if (!mTmpRect2.isEmpty()) {
1940 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
1941 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08001942 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001943 final WindowState inputMethod = mService.mInputMethodWindow;
1944 if (inputMethod != null && inputMethod.isVisibleLw()) {
1945 // If the input method is visible and the user is typing, we don't want these touch
1946 // events to be intercepted and used to change focus. This would likely cause a
1947 // disappearance of the input method.
1948 inputMethod.getTouchableRegion(mTmpRegion);
Andrii Kulian7a31b772017-05-02 13:22:42 -07001949 if (inputMethod.getDisplayId() == mDisplayId) {
1950 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1951 } else {
1952 // IME is on a different display, so we need to update its tap detector.
1953 // TODO(multidisplay): Remove when IME will always appear on same display.
1954 inputMethod.getDisplayContent().setTouchExcludeRegion(null /* focusedTask */);
1955 }
Filip Gruszczynski912d9192015-12-01 16:14:04 -08001956 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001957 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
Andrii Kulian4b6599e2018-01-15 17:24:08 -08001958 final WindowState win = mTapExcludedWindows.get(i);
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08001959 win.getTouchableRegion(mTmpRegion);
1960 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
1961 }
Andrii Kulian4b6599e2018-01-15 17:24:08 -08001962 for (int i = mTapExcludeProvidingWindows.size() - 1; i >= 0; i--) {
1963 final WindowState win = mTapExcludeProvidingWindows.valueAt(i);
1964 win.amendTapExcludeRegion(mTouchExcludeRegion);
1965 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08001966 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07001967 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001968 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07001969 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01001970 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
1971 }
Craig Mautner1bef3892015-02-17 15:09:47 -08001972 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07001973 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08001974 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07001975 }
1976
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001977 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07001978 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001979 super.switchUser();
1980 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07001981 }
1982
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07001983 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001984 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
1985 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07001986 }
1987 }
1988
Wale Ogunwale10124582016-09-15 20:25:50 -07001989 @Override
1990 void removeIfPossible() {
1991 if (isAnimating()) {
1992 mDeferredRemoval = true;
1993 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07001994 }
Wale Ogunwale10124582016-09-15 20:25:50 -07001995 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07001996 }
1997
Wale Ogunwale10124582016-09-15 20:25:50 -07001998 @Override
1999 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002000 mRemovingDisplay = true;
2001 try {
2002 super.removeImmediately();
2003 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -07002004 if (mService.canDispatchPointerEvents()) {
2005 if (mTapDetector != null) {
2006 mService.unregisterPointerEventListener(mTapDetector);
2007 }
2008 if (mDisplayId == DEFAULT_DISPLAY && mService.mMousePositionTracker != null) {
2009 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
2010 }
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002011 }
Jorim Jaggia3844032018-01-03 15:54:43 +01002012 mService.mAnimator.removeDisplayLocked(mDisplayId);
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002013 } finally {
2014 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08002015 }
Bryce Leef19cbe22018-02-02 15:09:21 -08002016
2017 mService.onDisplayRemoved(mDisplayId);
Craig Mautner95da1082014-02-24 17:54:35 -08002018 }
2019
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002020 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07002021 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002022 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002023 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
2024
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002025 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002026 removeImmediately();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002027 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08002028 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002029 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08002030 }
2031
Andrii Kulian0214ed92017-05-16 13:44:05 -07002032 /** @return 'true' if removal of this display content is deferred due to active animation. */
2033 boolean isRemovalDeferred() {
2034 return mDeferredRemoval;
2035 }
2036
Wale Ogunwale10124582016-09-15 20:25:50 -07002037 boolean animateForIme(float interpolatedValue, float animationTarget,
2038 float dividerAnimationTarget) {
2039 boolean updated = false;
2040
Wale Ogunwale61911492017-10-11 08:50:50 -07002041 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2042 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002043 if (stack == null || !stack.isAdjustedForIme()) {
2044 continue;
2045 }
2046
2047 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
2048 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2049 updated = true;
2050 } else {
2051 mDividerControllerLocked.mLastAnimationProgress =
2052 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
2053 mDividerControllerLocked.mLastDividerProgress =
2054 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
2055 updated |= stack.updateAdjustForIme(
2056 mDividerControllerLocked.mLastAnimationProgress,
2057 mDividerControllerLocked.mLastDividerProgress,
2058 false /* force */);
2059 }
2060 if (interpolatedValue >= 1f) {
2061 stack.endImeAdjustAnimation();
2062 }
2063 }
2064
2065 return updated;
2066 }
2067
2068 boolean clearImeAdjustAnimation() {
2069 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002070 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2071 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002072 if (stack != null && stack.isAdjustedForIme()) {
2073 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2074 changed = true;
2075 }
2076 }
2077 return changed;
2078 }
2079
2080 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002081 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2082 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002083 if (stack.isVisible() && stack.isAdjustedForIme()) {
2084 stack.beginImeAdjustAnimation();
2085 }
2086 }
2087 }
2088
2089 void adjustForImeIfNeeded() {
2090 final WindowState imeWin = mService.mInputMethodWindow;
2091 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2092 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002093 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002094 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2095 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2096 imeTargetStack.getDockSide() : DOCKED_INVALID;
2097 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2098 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2099 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002100 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002101 final boolean imeHeightChanged = imeVisible &&
2102 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2103
2104 // The divider could be adjusted for IME position, or be thinner than usual,
2105 // or both. There are three possible cases:
2106 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2107 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2108 // - If IME is not visible, divider is not moved and is normal width.
2109
2110 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002111 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2112 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002113 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002114 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2115 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002116 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2117 } else {
2118 stack.resetAdjustedForIme(false);
2119 }
2120 }
2121 mDividerControllerLocked.setAdjustedForIme(
2122 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2123 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002124 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2125 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002126 stack.resetAdjustedForIme(!dockVisible);
2127 }
2128 mDividerControllerLocked.setAdjustedForIme(
2129 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2130 }
Winson Chung655332c2016-10-31 13:14:28 -07002131 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002132 }
2133
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002134 /**
2135 * If a window that has an animation specifying a colored background and the current wallpaper
2136 * is visible, then the color goes *below* the wallpaper so we don't cause the wallpaper to
2137 * suddenly disappear.
2138 */
2139 int getLayerForAnimationBackground(WindowStateAnimator winAnimator) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002140 final WindowState visibleWallpaper = mBelowAppWindowsContainers.getWindow(
2141 w -> w.mIsWallpaper && w.isVisibleNow());
2142
2143 if (visibleWallpaper != null) {
2144 return visibleWallpaper.mWinAnimator.mAnimLayer;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002145 }
2146 return winAnimator.mAnimLayer;
2147 }
2148
Wale Ogunwale10124582016-09-15 20:25:50 -07002149 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002150 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2151 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002152 stack.prepareFreezingTaskBounds();
2153 }
2154 }
2155
Wale Ogunwale94744212015-09-21 19:01:47 -07002156 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002157 getBounds(mTmpRect, newRotation);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002158
2159 // Compute a transform matrix to undo the coordinate space transformation,
2160 // and present the window at the same physical position it previously occupied.
2161 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2162 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2163
2164 mTmpRectF.set(bounds);
2165 mTmpMatrix.mapRect(mTmpRectF);
2166 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002167 }
2168
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002169 static int deltaRotation(int oldRotation, int newRotation) {
2170 int delta = newRotation - oldRotation;
2171 if (delta < 0) delta += 4;
2172 return delta;
2173 }
2174
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002175 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002176 Matrix outMatrix) {
2177 // For rotations without Z-ordering we don't need the target rectangle's position.
2178 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2179 displayHeight, outMatrix);
2180 }
2181
2182 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2183 float displayWidth, float displayHeight, Matrix outMatrix) {
2184 switch (rotation) {
2185 case ROTATION_0:
2186 outMatrix.reset();
2187 break;
2188 case ROTATION_270:
2189 outMatrix.setRotate(270, 0, 0);
2190 outMatrix.postTranslate(0, displayHeight);
2191 outMatrix.postTranslate(rectTop, 0);
2192 break;
2193 case ROTATION_180:
2194 outMatrix.reset();
2195 break;
2196 case ROTATION_90:
2197 outMatrix.setRotate(90, 0, 0);
2198 outMatrix.postTranslate(displayWidth, 0);
2199 outMatrix.postTranslate(-rectTop, rectLeft);
2200 break;
2201 }
2202 }
2203
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002204 @CallSuper
2205 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002206 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002207 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002208 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002209 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002210 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2211 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002212 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002213 }
2214 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2215 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002216 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2217 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002218 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002219 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002220 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2221 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002222 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002223 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002224 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2225 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002226 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002227 }
2228 proto.write(DPI, mBaseDisplayDensity);
2229 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002230 proto.write(ROTATION, mRotation);
2231 final ScreenRotationAnimation screenRotationAnimation =
2232 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2233 if (screenRotationAnimation != null) {
2234 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2235 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002236 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002237 proto.end(token);
2238 }
2239
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002240 @Override
2241 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2242 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002243 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2244 final String subPrefix = " " + prefix;
2245 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2246 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2247 pw.print("dpi");
2248 if (mInitialDisplayWidth != mBaseDisplayWidth
2249 || mInitialDisplayHeight != mBaseDisplayHeight
2250 || mInitialDisplayDensity != mBaseDisplayDensity) {
2251 pw.print(" base=");
2252 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2253 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2254 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002255 if (mDisplayScalingDisabled) {
2256 pw.println(" noscale");
2257 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002258 pw.print(" cur=");
2259 pw.print(mDisplayInfo.logicalWidth);
2260 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2261 pw.print(" app=");
2262 pw.print(mDisplayInfo.appWidth);
2263 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2264 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2265 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2266 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2267 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002268 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002269 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002270 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002271
Craig Mautnerdc548482014-02-05 13:35:24 -08002272 pw.println();
Adrian Roos5251b1d2018-03-23 18:57:43 +01002273 pw.print(prefix); pw.print("mLayoutSeq="); pw.println(mLayoutSeq);
2274
2275 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002276 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002277 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2278 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002279 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002280 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002281
Craig Mautnerdc548482014-02-05 13:35:24 -08002282 pw.println();
2283 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002284 pw.println();
2285 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002286 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002287 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002288 pw.print(" Exiting #"); pw.print(i);
2289 pw.print(' '); pw.print(token);
2290 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002291 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002292 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002293 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002294
Jorim Jaggi31f71702016-05-04 16:43:04 -07002295 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002296
2297 // Dump stack references
2298 final TaskStack homeStack = getHomeStack();
2299 if (homeStack != null) {
2300 pw.println(prefix + "homeStack=" + homeStack.getName());
2301 }
2302 final TaskStack pinnedStack = getPinnedStack();
2303 if (pinnedStack != null) {
2304 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2305 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002306 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002307 if (splitScreenPrimaryStack != null) {
2308 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2309 }
2310
2311 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002312 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002313 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002314 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002315
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002316 pw.println();
2317 mDisplayFrames.dump(prefix, pw);
Craig Mautner59c00972012-07-30 12:10:24 -07002318 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002319
2320 @Override
2321 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002322 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002323 }
2324
2325 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002326 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002327 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002328
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002329 /** Returns true if the stack in the windowing mode is visible. */
2330 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002331 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002332 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002333 }
2334
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002335 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002336 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002337 final int x = (int) xf;
2338 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002339 final WindowState touchedWin = getWindow(w -> {
2340 final int flags = w.mAttrs.flags;
2341 if (!w.isVisibleLw()) {
2342 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002343 }
2344 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002345 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002346 }
2347
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002348 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002349 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002350 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002351 }
2352
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002353 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002354
2355 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002356 return mTmpRegion.contains(x, y) || touchFlags == 0;
2357 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002358
2359 return touchedWin;
2360 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002361
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002362 boolean canAddToastWindowForUid(int uid) {
2363 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002364 // Also if the app is focused adding more than one toast at
2365 // a time for better backwards compatibility.
2366 final WindowState focusedWindowForUid = getWindow(w ->
2367 w.mOwnerUid == uid && w.isFocused());
2368 if (focusedWindowForUid != null) {
2369 return true;
2370 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002371 final WindowState win = getWindow(w ->
2372 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2373 && !w.mWindowRemovalAllowed);
2374 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002375 }
2376
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002377 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002378 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2379 return;
2380 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002381
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002382 // Used to communicate the old focus to the callback method.
2383 mTmpWindow = oldFocus;
2384
2385 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002386 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002387
2388 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002389 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002390
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002391 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002392
2393 if (mTmpWindow == null) {
2394 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2395 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002396 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002397 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002398 }
2399
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002400 /** Updates the layer assignment of windows on this display. */
2401 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002402 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07002403 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002404 if (setLayoutNeeded) {
2405 setLayoutNeeded();
2406 }
Robert Carrb1579c82017-09-05 14:54:47 -07002407
Robert Carrf59b8dd2017-10-02 18:58:36 -07002408 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002409 // the application of this transaction until the animation pass triggers
2410 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2411 // the hiding and showing of surfaces.
2412 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002413 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002414 }
2415
Wale Ogunwale1666e312016-12-16 11:27:18 -08002416 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2417 // moving containers or resizing them. Need to investigate the best way to have it automatically
2418 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002419 void layoutAndAssignWindowLayersIfNeeded() {
2420 mService.mWindowsChanged = true;
2421 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002422
2423 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2424 false /*updateInputWindows*/)) {
2425 assignWindowLayers(false /* setLayoutNeeded */);
2426 }
2427
2428 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
2429 mService.mWindowPlacerLocked.performSurfacePlacement();
2430 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
2431 }
2432
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002433 /** Returns true if a leaked surface was destroyed */
2434 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002435 // Used to indicate that a surface was leaked.
2436 mTmpWindow = null;
2437 forAllWindows(w -> {
2438 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002439 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002440 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002441 }
2442 if (!mService.mSessions.contains(wsa.mSession)) {
2443 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002444 + w + " surface=" + wsa.mSurfaceController
2445 + " token=" + w.mToken
2446 + " pid=" + w.mSession.mPid
2447 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002448 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002449 mService.mForceRemoves.add(w);
2450 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002451 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002452 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002453 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002454 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002455 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002456 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002457 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002458 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002459 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002460
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002461 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002462 }
2463
2464 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002465 * Determine and return the window that should be the IME target.
2466 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2467 * @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 +00002468 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002469 WindowState computeImeTarget(boolean updateImeTarget) {
2470 if (mService.mInputMethodWindow == null) {
2471 // There isn't an IME so there shouldn't be a target...That was easy!
2472 if (updateImeTarget) {
2473 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2474 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002475 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002476 }
2477 return null;
2478 }
2479
Chavi Weingarten3a748552018-05-14 17:32:42 +00002480 final WindowState curTarget = mService.mInputMethodTarget;
2481 if (!canUpdateImeTarget()) {
2482 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Defer updating IME target");
2483 return curTarget;
2484 }
2485
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002486 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2487 // same display. Or even when the current IME/target are not on the same screen as the next
2488 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002489 mUpdateImeTarget = updateImeTarget;
2490 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002491
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002492
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002493 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2494 // to be on top of it, but it is not -really- where input will go. So look down below
2495 // for a real window to target...
2496 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2497 final AppWindowToken token = target.mAppToken;
2498 if (token != null) {
2499 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2500 if (betterTarget != null) {
2501 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002502 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002503 }
2504 }
2505
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002506 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2507 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002508
Chavi Weingarten3a748552018-05-14 17:32:42 +00002509 // Now, a special case -- if the last target's window is in the process of exiting, and the
2510 // new target is home, keep on the last target to avoid flicker. Home is a special case
2511 // since its above other stacks in the ordering list, but layed out below the others.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002512 if (curTarget != null && curTarget.isDisplayedLw() && curTarget.isClosing()
Chavi Weingarten3a748552018-05-14 17:32:42 +00002513 && (target == null || target.isActivityTypeHome())) {
chaviw4ad54912018-05-30 11:05:44 -07002514 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "New target is home while current target is "
Chavi Weingarten3a748552018-05-14 17:32:42 +00002515 + "closing, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002516 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002517 }
2518
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002519 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2520 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002521
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002522 if (target == null) {
2523 if (updateImeTarget) {
2524 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2525 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2526 + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002527 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002528 }
2529
2530 return null;
2531 }
2532
2533 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002534 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2535 if (token != null) {
2536
2537 // Now some fun for dealing with window animations that modify the Z order. We need
2538 // to look at all windows below the current target that are in this app, finding the
2539 // highest visible one in layering.
2540 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002541 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002542 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002543 }
2544
2545 if (highestTarget != null) {
2546 final AppTransition appTransition = mService.mAppTransition;
2547 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
2548 + " animating=" + highestTarget.mWinAnimator.isAnimationSet()
2549 + " layer=" + highestTarget.mWinAnimator.mAnimLayer
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002550 + " new layer=" + target.mWinAnimator.mAnimLayer);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002551
2552 if (appTransition.isTransitionSet()) {
2553 // If we are currently setting up for an animation, hold everything until we
2554 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002555 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002556 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002557 } else if (highestTarget.mWinAnimator.isAnimationSet() &&
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002558 highestTarget.mWinAnimator.mAnimLayer > target.mWinAnimator.mAnimLayer) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002559 // If the window we are currently targeting is involved with an animation,
2560 // and it is on top of the next target we will be over, then hold off on
2561 // moving until that is done.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002562 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002563 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002564 }
2565 }
2566 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002567
2568 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2569 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002570 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002571 }
2572
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002573 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002574 }
2575
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002576 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002577 if (target == mService.mInputMethodTarget
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002578 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002579 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002580 }
2581
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002582 mService.mInputMethodTarget = target;
2583 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002584 assignWindowLayers(false /* setLayoutNeeded */);
2585 }
2586
2587 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2588 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2589 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2590 }
2591
2592 // Used to indicate we have reached the first window in the range we are interested in.
2593 mTmpWindow = null;
2594
2595 // TODO: Figure-out a more efficient way to do this.
2596 final WindowState candidate = getWindow(w -> {
2597 if (w == top) {
2598 // Reached the first window in the range we are interested in.
2599 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002600 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002601 if (mTmpWindow == null) {
2602 return false;
2603 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002604
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002605 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2606 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002607 }
2608 // If we reached the bottom of the range of windows we are considering,
2609 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002610 if (w == bottom) {
2611 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002612 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002613 return false;
2614 });
2615
2616 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002617 }
2618
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002619 void setLayoutNeeded() {
2620 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2621 mLayoutNeeded = true;
2622 }
2623
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002624 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002625 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2626 mLayoutNeeded = false;
2627 }
2628
2629 boolean isLayoutNeeded() {
2630 return mLayoutNeeded;
2631 }
2632
Wale Ogunwale02319a62016-09-26 15:21:22 -07002633 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2634 if (mTokenMap.isEmpty()) {
2635 return;
2636 }
2637 pw.println(" Display #" + mDisplayId);
2638 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2639 while (it.hasNext()) {
2640 final WindowToken token = it.next();
2641 pw.print(" ");
2642 pw.print(token);
2643 if (dumpAll) {
2644 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002645 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07002646 } else {
2647 pw.println();
2648 }
2649 }
2650 }
2651
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002652 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002653 final int[] index = new int[1];
2654 forAllWindows(w -> {
2655 final WindowStateAnimator wAnim = w.mWinAnimator;
2656 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2657 index[0] = index[0] + 1;
2658 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002659 }
2660
Jorim Jaggife762342016-10-13 14:33:27 +02002661 /**
2662 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2663 */
2664 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2665 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002666 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002667 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2668 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01002669 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02002670 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002671 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02002672 }
2673
Wale Ogunwale494009b82016-10-21 09:01:38 -07002674 boolean checkWaitingForWindows() {
2675
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002676 mHaveBootMsg = false;
2677 mHaveApp = false;
2678 mHaveWallpaper = false;
2679 mHaveKeyguard = true;
2680
2681 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002682 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
2683 return true;
2684 }
2685 if (w.isDrawnLw()) {
2686 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002687 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002688 } else if (w.mAttrs.type == TYPE_APPLICATION
2689 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002690 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002691 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002692 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002693 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002694 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002695 }
2696 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002697 return false;
2698 });
2699
2700 if (visibleWindow != null) {
2701 // We have a visible window.
2702 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002703 }
2704
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002705 // if the wallpaper service is disabled on the device, we're never going to have
2706 // wallpaper, don't bother waiting for it
2707 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
2708 com.android.internal.R.bool.config_enableWallpaperService)
Daichi Hironoc1432222018-03-29 13:06:07 +09002709 && mService.mContext.getResources().getBoolean(
2710 com.android.internal.R.bool.config_checkWallpaperAtBoot)
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002711 && !mService.mOnlyCore;
2712
Wale Ogunwale494009b82016-10-21 09:01:38 -07002713 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
2714 "******** booted=" + mService.mSystemBooted
2715 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002716 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
2717 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
2718 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002719
2720 // If we are turning on the screen to show the boot message, don't do it until the boot
2721 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002722 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002723 return true;
2724 }
2725
2726 // If we are turning on the screen after the boot is completed normally, don't do so until
2727 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002728 if (mService.mSystemBooted
2729 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002730 return true;
2731 }
2732
2733 return false;
2734 }
2735
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002736 void updateWindowsForAnimator(WindowAnimator animator) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002737 mTmpWindowAnimator = animator;
2738 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002739 }
2740
2741 void updateWallpaperForAnimator(WindowAnimator animator) {
Jorim Jaggi7156b652016-10-25 08:31:58 -07002742 resetAnimationBackgroundAnimator();
2743
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002744 // Used to indicate a detached wallpaper.
2745 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002746 mTmpWindowAnimator = animator;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002747
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002748 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002749
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002750 if (animator.mWindowDetachedWallpaper != mTmpWindow) {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002751 if (DEBUG_WALLPAPER) Slog.v(TAG, "Detached wallpaper changed from "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002752 + animator.mWindowDetachedWallpaper + " to " + mTmpWindow);
2753 animator.mWindowDetachedWallpaper = mTmpWindow;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002754 animator.mBulkUpdateParams |= SET_WALLPAPER_MAY_CHANGE;
2755 }
2756 }
2757
Wale Ogunwale494009b82016-10-21 09:01:38 -07002758 boolean inputMethodClientHasFocus(IInputMethodClient client) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002759 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002760 if (imFocus == null) {
2761 return false;
2762 }
2763
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002764 if (DEBUG_INPUT_METHOD) {
2765 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
2766 Slog.i(TAG_WM, "Current focus: " + mService.mCurrentFocus);
2767 Slog.i(TAG_WM, "Last focus: " + mService.mLastFocus);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002768 }
2769
Wale Ogunwale494009b82016-10-21 09:01:38 -07002770 final IInputMethodClient imeClient = imFocus.mSession.mClient;
2771
2772 if (DEBUG_INPUT_METHOD) {
2773 Slog.i(TAG_WM, "IM target client: " + imeClient);
2774 if (imeClient != null) {
2775 Slog.i(TAG_WM, "IM target client binder: " + imeClient.asBinder());
2776 Slog.i(TAG_WM, "Requesting client binder: " + client.asBinder());
2777 }
2778 }
2779
2780 return imeClient != null && imeClient.asBinder() == client.asBinder();
2781 }
2782
2783 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002784 final WindowState win = getWindow(
2785 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
2786 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002787 }
2788
2789 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002790 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07002791 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002792 final int curValue = w.mSystemUiVisibility;
2793 final int diff = (curValue ^ visibility) & globalDiff;
2794 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002795 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002796 w.mSeq++;
2797 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002798 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002799 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
2800 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07002801 visibility, newValue, diff);
2802 }
2803 } catch (RemoteException e) {
2804 // so sorry
2805 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002806 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002807 }
2808
2809 void onWindowFreezeTimeout() {
2810 Slog.w(TAG_WM, "Window freeze timeout expired.");
2811 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002812
2813 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07002814 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002815 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002816 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07002817 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07002818 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
2819 - mService.mDisplayFreezeTime);
2820 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002821 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002822 mService.mWindowPlacerLocked.performSurfacePlacement();
2823 }
2824
2825 void waitForAllWindowsDrawn() {
2826 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002827 forAllWindows(w -> {
2828 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
2829 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
2830 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07002831 // Force add to mResizingWindows.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002832 w.mLastContentInsets.set(-1, -1, -1, -1);
2833 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002834 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002835 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07002836 }
2837
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002838 // TODO: Super crazy long method that should be broken down...
2839 boolean applySurfaceChangesTransaction(boolean recoveringMemory) {
2840
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002841 final int dw = mDisplayInfo.logicalWidth;
2842 final int dh = mDisplayInfo.logicalHeight;
2843 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
2844
2845 mTmpUpdateAllDrawn.clear();
2846
2847 int repeats = 0;
2848 do {
2849 repeats++;
2850 if (repeats > 6) {
2851 Slog.w(TAG, "Animation repeat aborted after too many iterations");
2852 clearLayoutNeeded();
2853 break;
2854 }
2855
2856 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
2857 pendingLayoutChanges);
2858
Andrii Kulian839def92016-11-02 10:58:58 -07002859 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
2860 // the wallpaper window jumping across displays.
2861 // Remove check for default display when there will be support for multiple wallpaper
2862 // targets (on different displays).
2863 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002864 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002865 }
2866
2867 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
2868 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
Robert Carrae606b42018-02-15 15:36:23 -08002869 if (mService.updateOrientationFromAppTokensLocked(mDisplayId)) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002870 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07002871 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002872 }
2873 }
2874
2875 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2876 setLayoutNeeded();
2877 }
2878
2879 // FIRST LOOP: Perform a layout, if needed.
2880 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
2881 performLayout(repeats == 1, false /* updateInputWindows */);
2882 } else {
2883 Slog.w(TAG, "Layout repeat skipped after too many iterations");
2884 }
2885
2886 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
2887 pendingLayoutChanges = 0;
2888
2889 if (isDefaultDisplay) {
2890 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002891 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002892 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
2893 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
2894 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
2895 }
2896 } while (pendingLayoutChanges != 0);
2897
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002898 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002899
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002900 mTmpRecoveringMemory = recoveringMemory;
2901 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01002902 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002903
2904 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002905 mTmpApplySurfaceChangesTransactionState.displayHasContent,
2906 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
2907 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002908 true /* inTraversal, must call performTraversalInTrans... below */);
2909
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07002910 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
2911 if (wallpaperVisible != mLastWallpaperVisible) {
2912 mLastWallpaperVisible = wallpaperVisible;
2913 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
2914 }
2915
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002916 while (!mTmpUpdateAllDrawn.isEmpty()) {
2917 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
2918 // See if any windows have been drawn, so they (and others associated with them)
2919 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07002920 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002921 }
2922
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002923 return mTmpApplySurfaceChangesTransactionState.focusDisplayed;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002924 }
2925
Bryce Leef3c6a472017-11-14 14:53:06 -08002926 private void updateBounds() {
2927 calculateBounds(mTmpBounds);
2928 setBounds(mTmpBounds);
2929 }
2930
2931 // Determines the current display bounds based on the current state
2932 private void calculateBounds(Rect out) {
2933 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
2934 final int orientation = mDisplayInfo.rotation;
2935 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
2936 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
2937 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
2938 int width = mDisplayInfo.logicalWidth;
2939 int left = (physWidth - width) / 2;
2940 int height = mDisplayInfo.logicalHeight;
2941 int top = (physHeight - height) / 2;
2942 out.set(left, top, left + width, top + height);
2943 }
2944
2945 @Override
2946 public void getBounds(Rect out) {
2947 calculateBounds(out);
2948 }
2949
2950 private void getBounds(Rect out, int orientation) {
2951 getBounds(out);
2952
2953 // Rotate the Rect if needed.
2954 final int currentRotation = mDisplayInfo.rotation;
2955 final int rotationDelta = deltaRotation(currentRotation, orientation);
2956 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
2957 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
2958 mTmpRectF.set(out);
2959 mTmpMatrix.mapRect(mTmpRectF);
2960 mTmpRectF.round(out);
2961 }
2962 }
2963
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002964 void performLayout(boolean initial, boolean updateInputWindows) {
2965 if (!isLayoutNeeded()) {
2966 return;
2967 }
2968 clearLayoutNeeded();
2969
2970 final int dw = mDisplayInfo.logicalWidth;
2971 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002972 if (DEBUG_LAYOUT) {
2973 Slog.v(TAG, "-------------------------------------");
2974 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
2975 }
2976
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01002977 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
2978 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002979 // TODO: Not sure if we really need to set the rotation here since we are updating from the
2980 // display info above...
2981 mDisplayFrames.mRotation = mRotation;
2982 mService.mPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002983 if (isDefaultDisplay) {
2984 // Not needed on non-default displays.
2985 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
2986 mService.mScreenRect.set(0, 0, dw, dh);
2987 }
2988
Adrian Roos5251b1d2018-03-23 18:57:43 +01002989 int seq = mLayoutSeq + 1;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002990 if (seq < 0) seq = 0;
Adrian Roos5251b1d2018-03-23 18:57:43 +01002991 mLayoutSeq = seq;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002992
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002993 // Used to indicate that we have processed the dream window and all additional windows are
2994 // behind it.
2995 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002996 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002997
2998 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002999 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003000
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003001 // Used to indicate that we have processed the dream window and all additional attached
3002 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003003 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003004 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003005
3006 // Now perform layout of attached windows, which usually depend on the position of the
3007 // window they are attached to. XXX does not deal with windows that are attached to windows
3008 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003009 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003010
3011 // Window frames may have changed. Tell the input dispatcher about it.
3012 mService.mInputMonitor.layoutInputConsumers(dw, dh);
3013 mService.mInputMonitor.setUpdateInputWindowsNeededLw();
3014 if (updateInputWindows) {
3015 mService.mInputMonitor.updateInputWindowsLw(false /*force*/);
3016 }
3017
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003018 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
3019 }
3020
3021 /**
3022 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
3023 * In portrait mode, it grabs the full screenshot.
3024 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003025 * @param config of the output bitmap
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003026 */
chaviw0315a1a2018-03-05 15:28:35 -08003027 Bitmap screenshotDisplayLocked(Bitmap.Config config) {
3028 if (!mService.mPolicy.isScreenOn()) {
3029 if (DEBUG_SCREENSHOT) {
3030 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003031 }
chaviw0315a1a2018-03-05 15:28:35 -08003032 return null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003033 }
chaviwfbe47df2017-11-10 16:14:49 -08003034
chaviw0315a1a2018-03-05 15:28:35 -08003035 int dw = mDisplayInfo.logicalWidth;
3036 int dh = mDisplayInfo.logicalHeight;
chaviwfbe47df2017-11-10 16:14:49 -08003037
chaviw0315a1a2018-03-05 15:28:35 -08003038 if (dw <= 0 || dh <= 0) {
3039 return null;
3040 }
chaviwfbe47df2017-11-10 16:14:49 -08003041
chaviw0315a1a2018-03-05 15:28:35 -08003042 final Rect frame = new Rect(0, 0, dw, dh);
chaviwfbe47df2017-11-10 16:14:49 -08003043
chaviw0315a1a2018-03-05 15:28:35 -08003044 // The screenshot API does not apply the current screen rotation.
3045 int rot = mDisplay.getRotation();
chaviwfbe47df2017-11-10 16:14:49 -08003046
chaviw0315a1a2018-03-05 15:28:35 -08003047 if (rot == ROTATION_90 || rot == ROTATION_270) {
3048 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3049 }
chaviwfbe47df2017-11-10 16:14:49 -08003050
chaviw0315a1a2018-03-05 15:28:35 -08003051 // SurfaceFlinger is not aware of orientation, so convert our logical
3052 // crop to SurfaceFlinger's portrait orientation.
3053 convertCropForSurfaceFlinger(frame, rot, dw, dh);
3054
3055 final ScreenRotationAnimation screenRotationAnimation =
3056 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3057 final boolean inRotation = screenRotationAnimation != null &&
3058 screenRotationAnimation.isAnimating();
3059 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
3060
3061 // TODO(b/68392460): We should screenshot Task controls directly
3062 // but it's difficult at the moment as the Task doesn't have the
3063 // correct size set.
3064 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, 0, 1, inRotation, rot);
3065 if (bitmap == null) {
3066 Slog.w(TAG_WM, "Failed to take screenshot");
3067 return null;
3068 }
3069
3070 // Create a copy of the screenshot that is immutable and backed in ashmem.
3071 // This greatly reduces the overhead of passing the bitmap between processes.
3072 final Bitmap ret = bitmap.createAshmemBitmap(config);
3073 bitmap.recycle();
3074 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003075 }
3076
3077 // TODO: Can this use createRotationMatrix()?
3078 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3079 if (rot == Surface.ROTATION_90) {
3080 final int tmp = crop.top;
3081 crop.top = dw - crop.right;
3082 crop.right = crop.bottom;
3083 crop.bottom = dw - crop.left;
3084 crop.left = tmp;
3085 } else if (rot == Surface.ROTATION_180) {
3086 int tmp = crop.top;
3087 crop.top = dh - crop.bottom;
3088 crop.bottom = dh - tmp;
3089 tmp = crop.right;
3090 crop.right = dw - crop.left;
3091 crop.left = dw - tmp;
3092 } else if (rot == Surface.ROTATION_270) {
3093 final int tmp = crop.top;
3094 crop.top = crop.left;
3095 crop.left = dh - crop.bottom;
3096 crop.bottom = crop.right;
3097 crop.right = dh - tmp;
3098 }
3099 }
3100
3101 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003102 // Used to indicate the layout is needed.
3103 mTmpWindow = null;
3104
3105 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003106 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003107 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003108 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003109 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003110 w.setDisplayLayoutNeeded();
3111 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003112 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003113
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003114 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003115 mService.mWindowPlacerLocked.performSurfacePlacement();
3116 }
3117 }
3118
Wale Ogunwale1666e312016-12-16 11:27:18 -08003119 void setExitingTokensHasVisible(boolean hasVisible) {
3120 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3121 mExitingTokens.get(i).hasVisible = hasVisible;
3122 }
3123
3124 // Initialize state of exiting applications.
3125 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3126 }
3127
3128 void removeExistingTokensIfPossible() {
3129 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3130 final WindowToken token = mExitingTokens.get(i);
3131 if (!token.hasVisible) {
3132 mExitingTokens.remove(i);
3133 }
3134 }
3135
3136 // Time to remove any exiting applications?
3137 mTaskStackContainers.removeExistingAppTokensIfPossible();
3138 }
3139
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003140 @Override
3141 void onDescendantOverrideConfigurationChanged() {
3142 setLayoutNeeded();
3143 mService.requestTraversal();
3144 }
3145
David Stevens9440dc82017-03-16 19:00:20 -07003146 boolean okToDisplay() {
3147 if (mDisplayId == DEFAULT_DISPLAY) {
3148 return !mService.mDisplayFrozen
3149 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3150 }
3151 return mDisplayInfo.state == Display.STATE_ON;
3152 }
3153
3154 boolean okToAnimate() {
3155 return okToDisplay() &&
3156 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3157 }
3158
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003159 static final class TaskForResizePointSearchResult {
3160 boolean searchDone;
3161 Task taskForResize;
3162
3163 void reset() {
3164 searchDone = false;
3165 taskForResize = null;
3166 }
3167 }
Robert Carr3b716242016-08-16 16:02:21 -07003168
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003169 private static final class ApplySurfaceChangesTransactionState {
3170 boolean displayHasContent;
3171 boolean obscured;
3172 boolean syswin;
3173 boolean focusDisplayed;
3174 float preferredRefreshRate;
3175 int preferredModeId;
3176
3177 void reset() {
3178 displayHasContent = false;
3179 obscured = false;
3180 syswin = false;
3181 focusDisplayed = false;
3182 preferredRefreshRate = 0;
3183 preferredModeId = 0;
3184 }
3185 }
3186
3187 private static final class ScreenshotApplicationState {
3188 WindowState appWin;
3189 int maxLayer;
3190 int minLayer;
3191 boolean screenshotReady;
3192
3193 void reset(boolean screenshotReady) {
3194 appWin = null;
3195 maxLayer = 0;
3196 minLayer = 0;
3197 this.screenshotReady = screenshotReady;
3198 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3199 }
3200 }
3201
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003202 /**
3203 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3204 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3205 * homogeneous children type which is currently required by sub-classes of
3206 * {@link WindowContainer} class.
3207 */
3208 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3209
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003210 DisplayChildWindowContainer(WindowManagerService service) {
3211 super(service);
3212 }
3213
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003214 @Override
3215 boolean fillsParent() {
3216 return true;
3217 }
3218
3219 @Override
3220 boolean isVisible() {
3221 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003222 }
3223 }
3224
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003225 /**
3226 * Window container class that contains all containers on this display relating to Apps.
3227 * I.e Activities.
3228 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003229 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003230 /**
3231 * A control placed at the appropriate level for transitions to occur.
3232 */
chaviw23ee71c2017-12-18 11:29:41 -08003233 SurfaceControl mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02003234 SurfaceControl mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02003235 SurfaceControl mHomeAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003236
Robert Carrf7a7ca82017-12-06 13:17:07 -08003237 /**
3238 * Given that the split-screen divider does not have an AppWindowToken, it
3239 * will have to live inside of a "NonAppWindowContainer", in particular
3240 * {@link DisplayContent#mAboveAppWindowsContainers}. However, in visual Z order
3241 * it will need to be interleaved with some of our children, appearing on top of
3242 * both docked stacks but underneath any assistant stacks.
3243 *
3244 * To solve this problem we have this anchor control, which will always exist so
3245 * we can always assign it the correct value in our {@link #assignChildLayers}.
3246 * Likewise since it always exists, {@link AboveAppWindowContainers} can always
3247 * assign the divider a layer relative to it. This way we prevent linking lifecycle
3248 * events between the two containers.
3249 */
3250 SurfaceControl mSplitScreenDividerAnchor = null;
3251
Wale Ogunwale61911492017-10-11 08:50:50 -07003252 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3253 // through the list to find them.
3254 private TaskStack mHomeStack = null;
3255 private TaskStack mPinnedStack = null;
3256 private TaskStack mSplitScreenPrimaryStack = null;
3257
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003258 TaskStackContainers(WindowManagerService service) {
3259 super(service);
3260 }
3261
Wale Ogunwale61911492017-10-11 08:50:50 -07003262 /**
3263 * Returns the topmost stack on the display that is compatible with the input windowing mode
3264 * and activity type. Null is no compatible stack on the display.
3265 */
3266 TaskStack getStack(int windowingMode, int activityType) {
3267 if (activityType == ACTIVITY_TYPE_HOME) {
3268 return mHomeStack;
3269 }
3270 if (windowingMode == WINDOWING_MODE_PINNED) {
3271 return mPinnedStack;
3272 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3273 return mSplitScreenPrimaryStack;
3274 }
3275 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3276 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003277 if (activityType == ACTIVITY_TYPE_UNDEFINED
3278 && windowingMode == stack.getWindowingMode()) {
3279 // Passing in undefined type means we want to match the topmost stack with the
3280 // windowing mode.
3281 return stack;
3282 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003283 if (stack.isCompatible(windowingMode, activityType)) {
3284 return stack;
3285 }
3286 }
3287 return null;
3288 }
3289
3290 @VisibleForTesting
3291 TaskStack getTopStack() {
3292 return mTaskStackContainers.getChildCount() > 0
3293 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3294 }
3295
3296 TaskStack getHomeStack() {
3297 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3298 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3299 }
3300 return mHomeStack;
3301 }
3302
3303 TaskStack getPinnedStack() {
3304 return mPinnedStack;
3305 }
3306
Matthew Ng64e77cf2017-10-31 14:01:31 -07003307 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003308 return mSplitScreenPrimaryStack;
3309 }
3310
Winson Chunge2d72172018-01-25 17:46:20 +00003311 ArrayList<Task> getVisibleTasks() {
3312 final ArrayList<Task> visibleTasks = new ArrayList<>();
3313 forAllTasks(task -> {
3314 if (task.isVisible()) {
3315 visibleTasks.add(task);
3316 }
3317 });
3318 return visibleTasks;
3319 }
3320
Andrii Kulian839def92016-11-02 10:58:58 -07003321 /**
3322 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003323 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003324 */
3325 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003326 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003327 addChild(stack, onTop);
3328 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003329 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003330
Wale Ogunwale61911492017-10-11 08:50:50 -07003331 void onStackWindowingModeChanged(TaskStack stack) {
3332 removeStackReferenceIfNeeded(stack);
3333 addStackReferenceIfNeeded(stack);
3334 if (stack == mPinnedStack && getTopStack() != stack) {
3335 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3336 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3337 }
3338 }
3339
3340 private void addStackReferenceIfNeeded(TaskStack stack) {
3341 if (stack.isActivityTypeHome()) {
3342 if (mHomeStack != null) {
3343 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
3344 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
3345 }
3346 mHomeStack = stack;
3347 }
3348 final int windowingMode = stack.getWindowingMode();
3349 if (windowingMode == WINDOWING_MODE_PINNED) {
3350 if (mPinnedStack != null) {
3351 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3352 + mPinnedStack + " already exist on display=" + this
3353 + " stack=" + stack);
3354 }
3355 mPinnedStack = stack;
3356 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3357 if (mSplitScreenPrimaryStack != null) {
3358 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3359 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3360 + " already exist on display=" + this + " stack=" + stack);
3361 }
3362 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003363 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003364 }
3365 }
3366
3367 private void removeStackReferenceIfNeeded(TaskStack stack) {
3368 if (stack == mHomeStack) {
3369 mHomeStack = null;
3370 } else if (stack == mPinnedStack) {
3371 mPinnedStack = null;
3372 } else if (stack == mSplitScreenPrimaryStack) {
3373 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003374 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3375 mService.setDockedStackCreateStateLocked(
3376 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3377 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003378 }
Andrii Kulian839def92016-11-02 10:58:58 -07003379 }
3380
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003381 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003382 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3383 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003384 addChild(stack, addIndex);
3385 setLayoutNeeded();
3386 }
3387
Wale Ogunwale61911492017-10-11 08:50:50 -07003388 @Override
3389 protected void removeChild(TaskStack stack) {
3390 super.removeChild(stack);
3391 removeStackReferenceIfNeeded(stack);
3392 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003393
3394 @Override
3395 boolean isOnTop() {
3396 // Considered always on top
3397 return true;
3398 }
3399
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003400 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003401 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003402 if (child.getWindowConfiguration().isAlwaysOnTop()
3403 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003404 // This stack is always-on-top, override the default behavior.
3405 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3406
3407 // Moving to its current position, as we must call super but we don't want to
3408 // perform any meaningful action.
3409 final int currentPosition = mChildren.indexOf(child);
3410 super.positionChildAt(currentPosition, child, false /* includingParents */);
3411 return;
3412 }
3413
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003414 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3415 super.positionChildAt(targetPosition, child, includingParents);
3416
3417 setLayoutNeeded();
3418 }
3419
3420 /**
3421 * When stack is added or repositioned, find a proper position for it.
3422 * This will make sure that pinned stack always stays on top.
3423 * @param requestedPosition Position requested by caller.
3424 * @param stack Stack to be added or positioned.
3425 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3426 * @return The proper position for the stack.
3427 */
3428 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
3429 final int topChildPosition = mChildren.size() - 1;
3430 boolean toTop = requestedPosition == POSITION_TOP;
3431 toTop |= adding ? requestedPosition >= topChildPosition + 1
3432 : requestedPosition >= topChildPosition;
3433 int targetPosition = requestedPosition;
3434
Wale Ogunwale61911492017-10-11 08:50:50 -07003435 if (toTop && stack.getWindowingMode() != WINDOWING_MODE_PINNED && hasPinnedStack()) {
Bryce Lee48f4b572017-04-10 10:54:15 -07003436 // The pinned stack is always the top most stack (always-on-top) when it is present.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003437 TaskStack topStack = mChildren.get(topChildPosition);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07003438 if (topStack.getWindowingMode() != WINDOWING_MODE_PINNED) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003439 throw new IllegalStateException("Pinned stack isn't top stack??? " + mChildren);
3440 }
3441
3442 // So, stack is moved just below the pinned stack.
3443 // When we're adding a new stack the target is the current pinned stack position.
3444 // When we're positioning an existing stack the target is the position below pinned
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08003445 // stack, because WindowContainer#positionAt() first removes element and then adds
3446 // it to specified place.
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003447 targetPosition = adding ? topChildPosition : topChildPosition - 1;
3448 }
3449
3450 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003451 }
3452
3453 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003454 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3455 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003456 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003457 if (super.forAllWindows(callback, traverseTopToBottom)) {
3458 return true;
3459 }
3460 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3461 return true;
3462 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003463 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003464 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3465 return true;
3466 }
3467 if (super.forAllWindows(callback, traverseTopToBottom)) {
3468 return true;
3469 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003470 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003471 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003472 }
3473
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003474 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003475 boolean traverseTopToBottom) {
3476 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3477 // app tokens.
3478 // TODO: Investigate if we need to continue to do this or if we can just process them
3479 // in-order.
3480 if (traverseTopToBottom) {
3481 for (int i = mChildren.size() - 1; i >= 0; --i) {
3482 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3483 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003484 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3485 traverseTopToBottom)) {
3486 return true;
3487 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003488 }
3489 }
3490 } else {
3491 final int count = mChildren.size();
3492 for (int i = 0; i < count; ++i) {
3493 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3494 final int appTokensCount = appTokens.size();
3495 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003496 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3497 traverseTopToBottom)) {
3498 return true;
3499 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003500 }
3501 }
3502 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003503 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003504 }
3505
Wale Ogunwale1666e312016-12-16 11:27:18 -08003506 void setExitingTokensHasVisible(boolean hasVisible) {
3507 for (int i = mChildren.size() - 1; i >= 0; --i) {
3508 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3509 for (int j = appTokens.size() - 1; j >= 0; --j) {
3510 appTokens.get(j).hasVisible = hasVisible;
3511 }
3512 }
3513 }
3514
3515 void removeExistingAppTokensIfPossible() {
3516 for (int i = mChildren.size() - 1; i >= 0; --i) {
3517 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3518 for (int j = appTokens.size() - 1; j >= 0; --j) {
3519 final AppWindowToken token = appTokens.get(j);
3520 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3521 && (!token.mIsExiting || token.isEmpty())) {
3522 // Make sure there is no animation running on this token, so any windows
3523 // associated with it will be removed as soon as their animations are
3524 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003525 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003526 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3527 "performLayout: App token exiting now removed" + token);
3528 token.removeIfPossible();
3529 }
3530 }
3531 }
3532 }
3533
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003534 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003535 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003536 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
3537 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003538 // Apps and their containers are not allowed to specify an orientation while the
3539 // docked or freeform stack is visible...except for the home stack/task if the
3540 // docked stack is minimized and it actually set something.
3541 if (mHomeStack != null && mHomeStack.isVisible()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07003542 && mDividerControllerLocked.isMinimizedDock()
3543 // TODO(b/110159357): Work around to unblock the release for failing test
3544 // ActivityManagerAppConfigurationTests#testSplitscreenPortraitAppOrientationRequests
3545 // which shouldn't be failing since home stack shouldn't be visible. We need
3546 // to dig deeper to see why it is failing. NOTE: Not failing on current
3547 // master...
3548 && !(mDividerControllerLocked.isHomeStackResizable()
3549 && mHomeStack.matchParentBounds())) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003550 final int orientation = mHomeStack.getOrientation();
3551 if (orientation != SCREEN_ORIENTATION_UNSET) {
3552 return orientation;
3553 }
3554 }
3555 return SCREEN_ORIENTATION_UNSPECIFIED;
3556 }
3557
3558 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07003559 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
3560 PackageManager.FEATURE_AUTOMOTIVE);
3561 if (isCar) {
3562 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
3563 // allow anything but the default orientation.
3564 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07003565 "Forcing UNSPECIFIED orientation in car for display id=" + mDisplayId
3566 + ". Ignoring " + orientation);
Dean Harding3e5a1522017-10-06 09:19:01 -07003567 return SCREEN_ORIENTATION_UNSPECIFIED;
3568 }
3569
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003570 if (orientation != SCREEN_ORIENTATION_UNSET
3571 && orientation != SCREEN_ORIENTATION_BEHIND) {
3572 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07003573 "App is requesting an orientation, return " + orientation
3574 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003575 return orientation;
3576 }
3577
3578 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07003579 "No app is requesting an orientation, return " + mLastOrientation
3580 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003581 // The next app has not been requested to be visible, so we keep the current orientation
3582 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003583 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003584 }
Robert Carrb1579c82017-09-05 14:54:47 -07003585
3586 @Override
3587 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carr2f8aa392018-01-31 14:46:51 -08003588 assignStackOrdering(t);
Robert Carrf7a7ca82017-12-06 13:17:07 -08003589
Robert Carr2f8aa392018-01-31 14:46:51 -08003590 for (int i = 0; i < mChildren.size(); i++) {
3591 final TaskStack s = mChildren.get(i);
3592 s.assignChildLayers(t);
3593 }
3594 }
3595
3596 void assignStackOrdering(SurfaceControl.Transaction t) {
Jorim Jaggibe418292018-03-26 16:14:12 +02003597
Robert Carrf7a7ca82017-12-06 13:17:07 -08003598 final int HOME_STACK_STATE = 0;
3599 final int NORMAL_STACK_STATE = 1;
3600 final int ALWAYS_ON_TOP_STATE = 2;
3601
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003602 int layer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08003603 int layerForAnimationLayer = 0;
Jorim Jaggibe418292018-03-26 16:14:12 +02003604 int layerForBoostedAnimationLayer = 0;
Jorim Jaggi391790622018-04-18 15:30:44 +02003605 int layerForHomeAnimationLayer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08003606
Robert Carrf7a7ca82017-12-06 13:17:07 -08003607 for (int state = 0; state <= ALWAYS_ON_TOP_STATE; state++) {
3608 for (int i = 0; i < mChildren.size(); i++) {
3609 final TaskStack s = mChildren.get(i);
Robert Carr2f8aa392018-01-31 14:46:51 -08003610 if (state == HOME_STACK_STATE && !s.isActivityTypeHome()) {
3611 continue;
3612 } else if (state == NORMAL_STACK_STATE && (s.isActivityTypeHome()
3613 || s.isAlwaysOnTop())) {
3614 continue;
3615 } else if (state == ALWAYS_ON_TOP_STATE && !s.isAlwaysOnTop()) {
3616 continue;
3617 }
3618 s.assignLayer(t, layer++);
3619 if (s.inSplitScreenWindowingMode() && mSplitScreenDividerAnchor != null) {
3620 t.setLayer(mSplitScreenDividerAnchor, layer++);
3621 }
Winson Chungd41f71d2018-03-16 15:26:07 -07003622 if ((s.isTaskAnimating() || s.isAppAnimating())
3623 && state != ALWAYS_ON_TOP_STATE) {
Robert Carr2f8aa392018-01-31 14:46:51 -08003624 // Ensure the animation layer ends up above the
3625 // highest animating stack and no higher.
3626 layerForAnimationLayer = layer++;
Robert Carrf7a7ca82017-12-06 13:17:07 -08003627 }
Jorim Jaggibe418292018-03-26 16:14:12 +02003628 if (state != ALWAYS_ON_TOP_STATE) {
3629 layerForBoostedAnimationLayer = layer++;
3630 }
Robert Carrf7a7ca82017-12-06 13:17:07 -08003631 }
Jorim Jaggi391790622018-04-18 15:30:44 +02003632 if (state == HOME_STACK_STATE) {
3633 layerForHomeAnimationLayer = layer++;
3634 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003635 }
Robert Carr2f8aa392018-01-31 14:46:51 -08003636 if (mAppAnimationLayer != null) {
3637 t.setLayer(mAppAnimationLayer, layerForAnimationLayer);
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003638 }
Jorim Jaggibe418292018-03-26 16:14:12 +02003639 if (mBoostedAppAnimationLayer != null) {
3640 t.setLayer(mBoostedAppAnimationLayer, layerForBoostedAnimationLayer);
3641 }
Jorim Jaggi391790622018-04-18 15:30:44 +02003642 if (mHomeAppAnimationLayer != null) {
3643 t.setLayer(mHomeAppAnimationLayer, layerForHomeAnimationLayer);
3644 }
Robert Carrb1579c82017-09-05 14:54:47 -07003645 }
3646
3647 @Override
Jorim Jaggi391790622018-04-18 15:30:44 +02003648 SurfaceControl getAppAnimationLayer(@AnimationLayer int animationLayer) {
3649 switch (animationLayer) {
3650 case ANIMATION_LAYER_BOOSTED:
3651 return mBoostedAppAnimationLayer;
3652 case ANIMATION_LAYER_HOME:
3653 return mHomeAppAnimationLayer;
3654 case ANIMATION_LAYER_STANDARD:
3655 default:
3656 return mAppAnimationLayer;
3657 }
chaviw23ee71c2017-12-18 11:29:41 -08003658 }
3659
Robert Carrf7a7ca82017-12-06 13:17:07 -08003660 SurfaceControl getSplitScreenDividerAnchor() {
3661 return mSplitScreenDividerAnchor;
3662 }
3663
chaviw23ee71c2017-12-18 11:29:41 -08003664 @Override
Robert Carrb1579c82017-09-05 14:54:47 -07003665 void onParentSet() {
3666 super.onParentSet();
3667 if (getParent() != null) {
chaviw23ee71c2017-12-18 11:29:41 -08003668 mAppAnimationLayer = makeChildSurface(null)
3669 .setName("animationLayer")
3670 .build();
Jorim Jaggibe418292018-03-26 16:14:12 +02003671 mBoostedAppAnimationLayer = makeChildSurface(null)
3672 .setName("boostedAnimationLayer")
3673 .build();
Jorim Jaggi391790622018-04-18 15:30:44 +02003674 mHomeAppAnimationLayer = makeChildSurface(null)
3675 .setName("homeAnimationLayer")
3676 .build();
Robert Carrf7a7ca82017-12-06 13:17:07 -08003677 mSplitScreenDividerAnchor = makeChildSurface(null)
3678 .setName("splitScreenDividerAnchor")
3679 .build();
3680 getPendingTransaction()
3681 .show(mAppAnimationLayer)
Jorim Jaggibe418292018-03-26 16:14:12 +02003682 .show(mBoostedAppAnimationLayer)
Jorim Jaggi391790622018-04-18 15:30:44 +02003683 .show(mHomeAppAnimationLayer)
Robert Carrf7a7ca82017-12-06 13:17:07 -08003684 .show(mSplitScreenDividerAnchor);
chaviw23ee71c2017-12-18 11:29:41 -08003685 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07003686 } else {
chaviw23ee71c2017-12-18 11:29:41 -08003687 mAppAnimationLayer.destroy();
3688 mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02003689 mBoostedAppAnimationLayer.destroy();
3690 mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02003691 mHomeAppAnimationLayer.destroy();
3692 mHomeAppAnimationLayer = null;
Robert Carrf7a7ca82017-12-06 13:17:07 -08003693 mSplitScreenDividerAnchor.destroy();
3694 mSplitScreenDividerAnchor = null;
Robert Carrb1579c82017-09-05 14:54:47 -07003695 }
3696 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003697 }
3698
Robert Carree4d4b92017-11-22 12:21:46 -08003699 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003700 AboveAppWindowContainers(String name, WindowManagerService service) {
3701 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08003702 }
3703
Robert Carrb9506032018-02-13 13:54:00 -08003704 @Override
Adrian Roos28c25e22018-05-31 18:07:28 +02003705 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
3706 final SurfaceControl.Builder builder = super.makeChildSurface(child);
3707 if (child instanceof WindowToken && ((WindowToken) child).mRoundedCornerOverlay) {
3708 // To draw above the ColorFade layer during the screen off transition, the
3709 // rounded corner overlays need to be at the root of the surface hierarchy.
3710 // TODO: move the ColorLayer into the display overlay layer such that this is not
3711 // necessary anymore.
3712 builder.setParent(null);
3713 }
3714 return builder;
3715 }
3716
3717 @Override
Robert Carrb9506032018-02-13 13:54:00 -08003718 void assignChildLayers(SurfaceControl.Transaction t) {
3719 assignChildLayers(t, null /* imeContainer */);
3720 }
3721
Robert Carree4d4b92017-11-22 12:21:46 -08003722 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
3723 boolean needAssignIme = imeContainer != null
3724 && imeContainer.getSurfaceControl() != null;
3725 for (int j = 0; j < mChildren.size(); ++j) {
3726 final WindowToken wt = mChildren.get(j);
Robert Carrf7a7ca82017-12-06 13:17:07 -08003727
3728 // See {@link mSplitScreenDividerAnchor}
3729 if (wt.windowType == TYPE_DOCK_DIVIDER) {
3730 wt.assignRelativeLayer(t, mTaskStackContainers.getSplitScreenDividerAnchor(), 1);
3731 continue;
3732 }
Adrian Roos28c25e22018-05-31 18:07:28 +02003733 if (wt.mRoundedCornerOverlay) {
3734 wt.assignLayer(t, WindowManagerPolicy.COLOR_FADE_LAYER + 1);
3735 continue;
3736 }
Robert Carree4d4b92017-11-22 12:21:46 -08003737 wt.assignLayer(t, j);
3738 wt.assignChildLayers(t);
3739
3740 int layer = mService.mPolicy.getWindowLayerFromTypeLw(
3741 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08003742
3743 if (needAssignIme && layer >= mService.mPolicy.getWindowLayerFromTypeLw(
3744 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003745 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08003746 needAssignIme = false;
3747 }
3748 }
3749 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003750 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08003751 }
3752 }
3753 }
3754
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003755 /**
3756 * Window container class that contains all containers on this display that are not related to
3757 * Apps. E.g. status bar.
3758 */
Robert Carree4d4b92017-11-22 12:21:46 -08003759 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07003760 /**
3761 * Compares two child window tokens returns -1 if the first is lesser than the second in
3762 * terms of z-order and 1 otherwise.
3763 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003764 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07003765 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08003766 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
3767 token1.mOwnerCanManageAppTokens)
3768 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
3769 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003770
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003771 private final Predicate<WindowState> mGetOrientingWindow = w -> {
3772 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
3773 return false;
3774 }
3775 final int req = w.mAttrs.screenOrientation;
3776 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
3777 || req == SCREEN_ORIENTATION_UNSET) {
3778 return false;
3779 }
3780 return true;
3781 };
3782
Wale Ogunwale3a931692016-11-02 16:49:48 -07003783 private final String mName;
chaviwf29223e2018-01-31 13:23:51 -08003784 private final Dimmer mDimmer = new Dimmer(this);
3785 private final Rect mTmpDimBoundsRect = new Rect();
3786
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003787 NonAppWindowContainers(String name, WindowManagerService service) {
3788 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07003789 mName = name;
3790 }
3791
3792 void addChild(WindowToken token) {
3793 addChild(token, mWindowComparator);
3794 }
3795
3796 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003797 int getOrientation() {
3798 final WindowManagerPolicy policy = mService.mPolicy;
3799 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003800 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003801
3802 if (win != null) {
3803 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003804 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003805 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07003806 if (mService.mKeyguardGoingAway) {
3807 // Keyguard can't affect the orientation if it is going away...
3808 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
3809 return SCREEN_ORIENTATION_UNSET;
3810 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003811 }
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07003812 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req
3813 + " for display id=" + mDisplayId);
Andrii Kulian8ee72852017-03-10 10:36:45 -08003814 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003815 }
3816
Andrii Kulian8ee72852017-03-10 10:36:45 -08003817 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003818
Jorim Jaggi1c530592018-04-06 15:11:47 +02003819 // Only allow force setting the orientation when all unknown visibilities have been
3820 // resolved, as otherwise we just may be starting another occluding activity.
3821 final boolean isUnoccluding =
3822 mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
3823 && mService.mUnknownAppVisibilityController.allResolved();
3824 if (policy.isKeyguardShowingAndNotOccluded() || isUnoccluding) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08003825 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003826 }
3827
3828 return SCREEN_ORIENTATION_UNSET;
3829 }
3830
3831 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07003832 String getName() {
3833 return mName;
3834 }
chaviwf29223e2018-01-31 13:23:51 -08003835
3836 @Override
3837 Dimmer getDimmer() {
3838 return mDimmer;
3839 }
3840
3841 @Override
3842 void prepareSurfaces() {
3843 mDimmer.resetDimStates();
3844 super.prepareSurfaces();
3845 getBounds(mTmpDimBoundsRect);
3846
3847 if (mDimmer.updateDims(getPendingTransaction(), mTmpDimBoundsRect)) {
3848 scheduleAnimation();
3849 }
3850 }
Robert Carr3b716242016-08-16 16:02:21 -07003851 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01003852
Robert Carr9034d962018-01-04 18:27:42 -08003853 private class NonMagnifiableWindowContainers extends NonAppWindowContainers {
3854 NonMagnifiableWindowContainers(String name, WindowManagerService service) {
3855 super(name, service);
3856 }
3857
3858 @Override
3859 void applyMagnificationSpec(Transaction t, MagnificationSpec spec) {
3860 }
3861 };
3862
Robert Carrb1579c82017-09-05 14:54:47 -07003863 SurfaceControl.Builder makeSurface(SurfaceSession s) {
3864 return mService.makeSurfaceBuilder(s)
3865 .setParent(mWindowingLayer);
3866 }
3867
3868 @Override
3869 SurfaceSession getSession() {
3870 return mSession;
3871 }
3872
3873 @Override
3874 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07003875 SurfaceSession s = child != null ? child.getSession() : getSession();
3876 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrb1579c82017-09-05 14:54:47 -07003877 b.setSize(mSurfaceSize, mSurfaceSize);
Robert Carrf59b8dd2017-10-02 18:58:36 -07003878
3879 if (child == null) {
3880 return b;
3881 }
3882
Robert Carree4d4b92017-11-22 12:21:46 -08003883 return b.setName(child.getName())
3884 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07003885 }
3886
3887 /**
3888 * The makeSurface variants are for use by the window-container
3889 * hierarchy. makeOverlay here is a function for various non windowing
3890 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
3891 * and other potpourii.
3892 */
3893 SurfaceControl.Builder makeOverlay() {
3894 return mService.makeSurfaceBuilder(mSession)
3895 .setParent(mOverlayLayer);
3896 }
3897
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09003898 /**
3899 * Reparents the given surface to mOverlayLayer.
3900 */
3901 void reparentToOverlay(Transaction transaction, SurfaceControl surface) {
3902 transaction.reparent(surface, mOverlayLayer.getHandle());
3903 }
3904
Robert Carrb1579c82017-09-05 14:54:47 -07003905 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carr24be9ab2018-04-30 17:54:53 -07003906 if (spec.scale != 1.0) {
3907 mMagnificationSpec = spec;
3908 } else {
3909 mMagnificationSpec = null;
3910 }
3911
Robert Carrf59b8dd2017-10-02 18:58:36 -07003912 applyMagnificationSpec(getPendingTransaction(), spec);
3913 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07003914 }
3915
Robert Carr24be9ab2018-04-30 17:54:53 -07003916 void reapplyMagnificationSpec() {
3917 if (mMagnificationSpec != null) {
3918 applyMagnificationSpec(getPendingTransaction(), mMagnificationSpec);
3919 }
3920 }
3921
Robert Carrb1579c82017-09-05 14:54:47 -07003922 @Override
3923 void onParentSet() {
3924 // Since we are the top of the SurfaceControl hierarchy here
3925 // we create the root surfaces explicitly rather than chaining
3926 // up as the default implementation in onParentSet does. So we
3927 // explicitly do NOT call super here.
3928 }
3929
3930 @Override
3931 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07003932
3933 // These are layers as children of "mWindowingLayer"
3934 mBelowAppWindowsContainers.assignLayer(t, 0);
3935 mTaskStackContainers.assignLayer(t, 1);
3936 mAboveAppWindowsContainers.assignLayer(t, 2);
3937
3938 WindowState imeTarget = mService.mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08003939 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08003940
Robert Carree4d4b92017-11-22 12:21:46 -08003941 // In the case where we have an IME target that is not in split-screen
3942 // mode IME assignment is easy. We just need the IME to go directly above
3943 // the target. This way children of the target will naturally go above the IME
3944 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08003945 //
Robert Carree4d4b92017-11-22 12:21:46 -08003946 // In the case of split-screen windowing mode, we need to elevate the IME above the
3947 // docked divider while keeping the app itself below the docked divider, so instead
3948 // we use relative layering of the IME targets child windows, and place the
3949 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08003950 //
Robert Carr234b6332018-03-20 13:38:16 -07003951 // In the case the IME target is animating, the animation Z order may be different
3952 // than the WindowContainer Z order, so it's difficult to be sure we have the correct
3953 // IME target. In this case we just layer the IME over all transitions by placing it in the
3954 // above applications layer.
3955 //
Robert Carree4d4b92017-11-22 12:21:46 -08003956 // In the case where we have no IME target we assign it where it's base layer would
3957 // place it in the AboveAppWindowContainers.
Robert Carr234b6332018-03-20 13:38:16 -07003958 if (imeTarget != null && !(imeTarget.inSplitScreenWindowingMode()
3959 || imeTarget.mToken.isAppAnimating())
Robert Carree4d4b92017-11-22 12:21:46 -08003960 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003961 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07003962 // TODO: We need to use an extra level on the app surface to ensure
3963 // this is always above SurfaceView but always below attached window.
3964 1);
Robert Carree4d4b92017-11-22 12:21:46 -08003965 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07003966 }
3967
3968 // Above we have assigned layers to our children, now we ask them to assign
3969 // layers to their children.
3970 mBelowAppWindowsContainers.assignChildLayers(t);
3971 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08003972 mAboveAppWindowsContainers.assignChildLayers(t,
3973 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07003974 mImeWindowsContainers.assignChildLayers(t);
3975 }
3976
3977 /**
3978 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
3979 * that the IME target is one of the docked applications. We'd like the docked divider to be
3980 * above both of the applications, and we'd like the IME to be above the docked divider.
3981 * However we need child windows of the applications to be above the IME (Text drag handles).
3982 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
3983 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
3984 * with {@link #WindowState#assignLayer}
3985 */
3986 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
Robert Carrbb5de662018-04-26 13:29:51 -07003987 child.assignRelativeLayer(t, mImeWindowsContainers.getSurfaceControl(), 1);
Robert Carrb1579c82017-09-05 14:54:47 -07003988 }
3989
3990 @Override
Jorim Jaggi9af095b2017-12-12 17:18:57 +01003991 void prepareSurfaces() {
3992 final ScreenRotationAnimation screenRotationAnimation =
3993 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
3994 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
3995 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
3996 mPendingTransaction.setMatrix(mWindowingLayer,
3997 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
3998 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
3999 mPendingTransaction.setPosition(mWindowingLayer,
4000 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
4001 mPendingTransaction.setAlpha(mWindowingLayer,
4002 screenRotationAnimation.getEnterTransformation().getAlpha());
4003 }
Chavi Weingartenb736e322018-02-23 00:27:54 +00004004
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004005 super.prepareSurfaces();
4006 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004007
Jorim Jaggibe418292018-03-26 16:14:12 +02004008 void assignStackOrdering() {
4009 mTaskStackContainers.assignStackOrdering(getPendingTransaction());
Robert Carr2f8aa392018-01-31 14:46:51 -08004010 }
Chavi Weingarten3a748552018-05-14 17:32:42 +00004011
4012 /**
4013 * Increment the deferral count to determine whether to update the IME target.
4014 */
4015 void deferUpdateImeTarget() {
4016 mDeferUpdateImeTargetCount++;
4017 }
4018
4019 /**
4020 * Decrement the deferral count to determine whether to update the IME target. If the count
4021 * reaches 0, a new ime target will get computed.
4022 */
4023 void continueUpdateImeTarget() {
4024 if (mDeferUpdateImeTargetCount == 0) {
4025 return;
4026 }
4027
4028 mDeferUpdateImeTargetCount--;
4029 if (mDeferUpdateImeTargetCount == 0) {
4030 computeImeTarget(true /* updateImeTarget */);
4031 }
4032 }
4033
4034 /**
4035 * @return Whether a new IME target should be computed.
4036 */
4037 private boolean canUpdateImeTarget() {
4038 return mDeferUpdateImeTargetCount == 0;
4039 }
Craig Mautner59c00972012-07-30 12:10:24 -07004040}