blob: 730e42208616a6264bdad4b785f1870ccebe9924 [file] [log] [blame]
Craig Mautner59c00972012-07-30 12:10:24 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wm;
18
Wale Ogunwale65ebd952018-04-25 15:41:44 -070019import static android.app.ActivityTaskManager.SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT;
Wale Ogunwale61911492017-10-11 08:50:50 -070020import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070021import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
Wale Ogunwale44f036f2017-09-29 05:09:09 -070022import static android.app.WindowConfiguration.WINDOWING_MODE_FREEFORM;
Wale Ogunwaleb62139d2017-09-20 15:37:35 -070023import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
24import static android.app.WindowConfiguration.WINDOWING_MODE_SPLIT_SCREEN_PRIMARY;
Wale Ogunwalea77e1462016-09-28 10:09:46 -070025import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_BEHIND;
Wale Ogunwale51362492016-09-08 17:49:17 -070026import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSET;
27import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale687b4272017-07-27 02:56:23 -070028import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
29import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
Wale Ogunwale19e452e2016-10-12 12:36:29 -070030import static android.view.Display.DEFAULT_DISPLAY;
31import static android.view.Display.FLAG_PRIVATE;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -070032import static android.view.Surface.ROTATION_0;
33import static android.view.Surface.ROTATION_180;
34import static android.view.Surface.ROTATION_270;
35import static android.view.Surface.ROTATION_90;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070036import static android.view.View.GONE;
Wale Ogunwale10124582016-09-15 20:25:50 -070037import static android.view.WindowManager.DOCKED_BOTTOM;
38import static android.view.WindowManager.DOCKED_INVALID;
39import static android.view.WindowManager.DOCKED_TOP;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -080040import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
41import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
42import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
Wale Ogunwale494009b82016-10-21 09:01:38 -070043import static android.view.WindowManager.LayoutParams.FLAG_SECURE;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070044import static android.view.WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070045import static android.view.WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE;
46import static android.view.WindowManager.LayoutParams.NEEDS_MENU_UNSET;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070047import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
Wale Ogunwale494009b82016-10-21 09:01:38 -070048import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070049import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
Wale Ogunwale494009b82016-10-21 09:01:38 -070050import static android.view.WindowManager.LayoutParams.TYPE_BOOT_PROGRESS;
Robert Carrf7a7ca82017-12-06 13:17:07 -080051import static android.view.WindowManager.LayoutParams.TYPE_DOCK_DIVIDER;
Wale Ogunwale494009b82016-10-21 09:01:38 -070052import static android.view.WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070053import static android.view.WindowManager.LayoutParams.TYPE_DREAM;
Wale Ogunwale3a931692016-11-02 16:49:48 -070054import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD;
55import static android.view.WindowManager.LayoutParams.TYPE_INPUT_METHOD_DIALOG;
Wale Ogunwale494009b82016-10-21 09:01:38 -070056import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070057import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG;
58import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
Svetoslav Ganovaa076532016-08-01 19:16:43 -070059import static android.view.WindowManager.LayoutParams.TYPE_TOAST;
Wale Ogunwaleec731152016-09-08 20:18:57 -070060import static android.view.WindowManager.LayoutParams.TYPE_WALLPAPER;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080061import static android.view.WindowManager.TRANSIT_KEYGUARD_UNOCCLUDE;
62
Adrian Roose99bc052017-11-20 17:55:31 +010063import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_ANIM;
64import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_CONFIG;
65import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_LAYOUT;
66import static com.android.server.policy.WindowManagerPolicy.FINISH_LAYOUT_REDO_WALLPAPER;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080067import static com.android.server.wm.DisplayContentProto.ABOVE_APP_WINDOWS;
68import static com.android.server.wm.DisplayContentProto.BELOW_APP_WINDOWS;
69import static com.android.server.wm.DisplayContentProto.DISPLAY_FRAMES;
70import static com.android.server.wm.DisplayContentProto.DISPLAY_INFO;
71import static com.android.server.wm.DisplayContentProto.DOCKED_STACK_DIVIDER_CONTROLLER;
72import static com.android.server.wm.DisplayContentProto.DPI;
Tiger Huang1e5b10a2018-07-30 20:19:51 +080073import static com.android.server.wm.DisplayContentProto.FOCUSED_APP;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080074import static com.android.server.wm.DisplayContentProto.ID;
75import static com.android.server.wm.DisplayContentProto.IME_WINDOWS;
76import static com.android.server.wm.DisplayContentProto.PINNED_STACK_CONTROLLER;
77import static com.android.server.wm.DisplayContentProto.ROTATION;
78import static com.android.server.wm.DisplayContentProto.SCREEN_ROTATION_ANIMATION;
79import static com.android.server.wm.DisplayContentProto.STACKS;
Louis Chang7501e332018-08-20 13:08:39 +080080import static com.android.server.wm.DisplayContentProto.SURFACE_SIZE;
Riddle Hsua4d6fa22018-08-11 00:50:39 +080081import static com.android.server.wm.DisplayContentProto.WINDOW_CONTAINER;
Wale Ogunwale1666e312016-12-16 11:27:18 -080082import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ADD_REMOVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -070083import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_BOOT;
Wale Ogunwale10124582016-09-15 20:25:50 -070084import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_DISPLAY;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -070085import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS;
86import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_FOCUS_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070087import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_INPUT_METHOD;
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -070088import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070089import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_LAYOUT_REPEATS;
Jorim Jaggife762342016-10-13 14:33:27 +020090import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_ORIENTATION;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070091import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREENSHOT;
Wale Ogunwale494009b82016-10-21 09:01:38 -070092import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_SCREEN_ON;
Andrii Kulian839def92016-11-02 10:58:58 -070093import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_STACK;
Wale Ogunwale1666e312016-12-16 11:27:18 -080094import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_TOKEN_MOVEMENT;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070095import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070096import static com.android.server.wm.WindowManagerDebugConfig.DEBUG_WALLPAPER_LIGHT;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -070097import static com.android.server.wm.WindowManagerDebugConfig.SHOW_STACK_CRAWLS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -070098import static com.android.server.wm.WindowManagerDebugConfig.SHOW_TRANSACTIONS;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -070099import static com.android.server.wm.WindowManagerDebugConfig.TAG_WITH_CLASS_NAME;
Filip Gruszczynski0bd180d2015-12-07 15:43:52 -0800100import static com.android.server.wm.WindowManagerDebugConfig.TAG_WM;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700101import static com.android.server.wm.WindowManagerService.CUSTOM_SCREEN_ROTATION;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800102import static com.android.server.wm.WindowManagerService.H.REPORT_FOCUS_CHANGE;
103import static com.android.server.wm.WindowManagerService.H.REPORT_LOSING_FOCUS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700104import static com.android.server.wm.WindowManagerService.H.SEND_NEW_CONFIGURATION;
105import static com.android.server.wm.WindowManagerService.H.UPDATE_DOCKED_STACK_DIVIDER;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700106import static com.android.server.wm.WindowManagerService.H.WINDOW_HIDE_TIMEOUT;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700107import static com.android.server.wm.WindowManagerService.LAYOUT_REPEAT_THRESHOLD;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700108import static com.android.server.wm.WindowManagerService.MAX_ANIMATION_DURATION;
109import static com.android.server.wm.WindowManagerService.SEAMLESS_ROTATION_TIMEOUT_DURATION;
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800110import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_PLACING_SURFACES;
111import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_REMOVING_FOCUS;
112import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_ASSIGN_LAYERS;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700113import static com.android.server.wm.WindowManagerService.UPDATE_FOCUS_WILL_PLACE_SURFACES;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700114import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_ACTIVE;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700115import static com.android.server.wm.WindowManagerService.WINDOWS_FREEZING_SCREENS_TIMEOUT;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700116import static com.android.server.wm.WindowManagerService.WINDOW_FREEZE_TIMEOUT_DURATION;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700117import static com.android.server.wm.WindowManagerService.dipToPixel;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700118import static com.android.server.wm.WindowManagerService.logSurface;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700119import static com.android.server.wm.WindowState.RESIZE_HANDLE_WIDTH_IN_DP;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700120import static com.android.server.wm.WindowStateAnimator.DRAW_PENDING;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700121import static com.android.server.wm.WindowStateAnimator.READY_TO_SHOW;
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700122import static com.android.server.wm.WindowSurfacePlacer.SET_WALLPAPER_MAY_CHANGE;
Riddle Hsua4d6fa22018-08-11 00:50:39 +0800123import static com.android.server.wm.utils.CoordinateTransforms.transformPhysicalToLogicalCoordinates;
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -0700124
Wale Ogunwale0d5609b2017-09-13 05:55:07 -0700125import android.annotation.CallSuper;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700126import android.annotation.NonNull;
Dean Harding3e5a1522017-10-06 09:19:01 -0700127import android.content.pm.PackageManager;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700128import android.content.res.CompatibilityInfo;
Andrii Kulian441e4492016-09-29 15:25:00 -0700129import android.content.res.Configuration;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700130import android.graphics.Bitmap;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700131import android.graphics.Matrix;
Craig Mautnerc00204b2013-03-05 15:02:14 -0800132import android.graphics.Rect;
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700133import android.graphics.RectF;
Craig Mautner6601b7b2013-04-29 10:29:11 -0700134import android.graphics.Region;
Jorim Jaggid47e7e12016-03-01 09:57:38 +0100135import android.graphics.Region.Op;
Wale Ogunwaleb699ce02016-07-18 12:05:30 -0700136import android.hardware.display.DisplayManagerInternal;
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700137import android.os.Debug;
Wale Ogunwaleb0f3b832016-10-17 10:13:07 -0700138import android.os.Handler;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700139import android.os.IBinder;
Wale Ogunwale494009b82016-10-21 09:01:38 -0700140import android.os.RemoteException;
141import android.os.SystemClock;
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100142import android.os.Trace;
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800143import android.util.ArraySet;
Chong Zhang8e89b312015-09-09 15:09:30 -0700144import android.util.DisplayMetrics;
Craig Mautnerde4ef022013-04-07 19:01:33 -0700145import android.util.Slog;
Steven Timotiusaf03df62017-07-18 16:56:43 -0700146import android.util.proto.ProtoOutputStream;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700147import android.view.Display;
Adrian Roos1cf585052018-01-03 18:43:27 +0100148import android.view.DisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700149import android.view.DisplayInfo;
Riddle Hsua4d6fa22018-08-11 00:50:39 +0800150import android.view.Gravity;
Andrii Kulian06d07d62017-03-14 11:11:47 -0700151import android.view.InputDevice;
Robert Carrb1579c82017-09-05 14:54:47 -0700152import android.view.MagnificationSpec;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700153import android.view.Surface;
154import android.view.SurfaceControl;
Jorim Jaggia5e10572017-11-15 14:36:26 +0100155import android.view.SurfaceControl.Transaction;
Robert Carrb1579c82017-09-05 14:54:47 -0700156import android.view.SurfaceSession;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700157
Bryce Lee48f4b572017-04-10 10:54:15 -0700158import com.android.internal.annotations.VisibleForTesting;
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -0800159import com.android.internal.util.ToBooleanFunction;
Adrian Roose99bc052017-11-20 17:55:31 +0100160import com.android.server.policy.WindowManagerPolicy;
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100161import com.android.server.wm.utils.RotationCache;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100162import com.android.server.wm.utils.WmDisplayCutout;
Craig Mautner59c00972012-07-30 12:10:24 -0700163
164import java.io.PrintWriter;
165import java.util.ArrayList;
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700166import java.util.Comparator;
Wale Ogunwale02319a62016-09-26 15:21:22 -0700167import java.util.HashMap;
168import java.util.Iterator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700169import java.util.LinkedList;
Andrii Kulian3a507b52016-09-19 18:14:12 -0700170import java.util.List;
Adrian Roos1cf585052018-01-03 18:43:27 +0100171import java.util.Objects;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800172import java.util.function.Consumer;
173import java.util.function.Predicate;
Craig Mautner59c00972012-07-30 12:10:24 -0700174
Craig Mautner59c00972012-07-30 12:10:24 -0700175/**
176 * Utility class for keeping track of the WindowStates and other pertinent contents of a
177 * particular Display.
178 *
179 * IMPORTANT: No method from this class should ever be used without holding
180 * WindowManagerService.mWindowMap.
181 */
Riddle Hsuad256a12018-07-18 16:11:30 +0800182class DisplayContent extends WindowContainer<DisplayContent.DisplayChildWindowContainer>
183 implements WindowManagerPolicy.DisplayContentInfo {
Wale Ogunwale824ab5c2016-10-20 09:31:56 -0700184 private static final String TAG = TAG_WITH_CLASS_NAME ? "DisplayContent" : TAG_WM;
Craig Mautner59c00972012-07-30 12:10:24 -0700185
186 /** Unique identifier of this stack. */
187 private final int mDisplayId;
188
Wale Ogunwale3a931692016-11-02 16:49:48 -0700189 /** The containers below are the only child containers the display can have. */
190 // Contains all window containers that are related to apps (Activities)
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100191 private final TaskStackContainers mTaskStackContainers = new TaskStackContainers(mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700192 // Contains all non-app window containers that should be displayed above the app containers
193 // (e.g. Status bar)
Robert Carree4d4b92017-11-22 12:21:46 -0800194 private final AboveAppWindowContainers mAboveAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100195 new AboveAppWindowContainers("mAboveAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700196 // Contains all non-app window containers that should be displayed below the app containers
197 // (e.g. Wallpaper).
198 private final NonAppWindowContainers mBelowAppWindowsContainers =
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100199 new NonAppWindowContainers("mBelowAppWindowsContainers", mService);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700200 // Contains all IME window containers. Note that the z-ordering of the IME windows will depend
201 // 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 -0800202 // window containers together and move them in-sync if/when needed. We use a subclass of
203 // WindowContainer which is omitted from screen magnification, as the IME is never magnified.
204 private final NonMagnifiableWindowContainers mImeWindowsContainers =
205 new NonMagnifiableWindowContainers("mImeWindowsContainers", mService);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700206
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000207 private WindowState mTmpWindow;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800208 private WindowState mTmpWindow2;
209 private WindowAnimator mTmpWindowAnimator;
210 private boolean mTmpRecoveringMemory;
211 private boolean mUpdateImeTarget;
212 private boolean mTmpInitial;
Bryce Lee27cec322017-03-21 09:41:37 -0700213 private int mMaxUiWidth;
Craig Mautner59c00972012-07-30 12:10:24 -0700214
Wale Ogunwale02319a62016-09-26 15:21:22 -0700215 // Mapping from a token IBinder to a WindowToken object on this display.
216 private final HashMap<IBinder, WindowToken> mTokenMap = new HashMap();
217
Andrii Kuliancd097992017-03-23 18:31:59 -0700218 // Initial display metrics.
Craig Mautner59c00972012-07-30 12:10:24 -0700219 int mInitialDisplayWidth = 0;
220 int mInitialDisplayHeight = 0;
Dianne Hackborndde331c2012-08-03 14:01:57 -0700221 int mInitialDisplayDensity = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700222
Adrian Roos1cf585052018-01-03 18:43:27 +0100223 DisplayCutout mInitialDisplayCutout;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100224 private final RotationCache<DisplayCutout, WmDisplayCutout> mDisplayCutoutCache
Adrian Roos2aa0fcd2018-02-19 18:07:49 +0100225 = new RotationCache<>(this::calculateDisplayCutoutForRotationUncached);
Adrian Roos1cf585052018-01-03 18:43:27 +0100226
Andrii Kuliancd097992017-03-23 18:31:59 -0700227 /**
228 * Overridden display size. Initialized with {@link #mInitialDisplayWidth}
229 * and {@link #mInitialDisplayHeight}, but can be set via shell command "adb shell wm size".
230 * @see WindowManagerService#setForcedDisplaySize(int, int, int)
231 */
Craig Mautner59c00972012-07-30 12:10:24 -0700232 int mBaseDisplayWidth = 0;
233 int mBaseDisplayHeight = 0;
Andrii Kuliancd097992017-03-23 18:31:59 -0700234 /**
235 * Overridden display density for current user. Initialized with {@link #mInitialDisplayDensity}
236 * but can be set from Settings or via shell command "adb shell wm density".
237 * @see WindowManagerService#setForcedDisplayDensityForUser(int, int, int)
238 */
Dianne Hackborndde331c2012-08-03 14:01:57 -0700239 int mBaseDisplayDensity = 0;
Jeff Brownd46747a2015-04-15 19:02:36 -0700240 boolean mDisplayScalingDisabled;
Craig Mautner2d5618c2012-10-18 13:55:47 -0700241 private final DisplayInfo mDisplayInfo = new DisplayInfo();
242 private final Display mDisplay;
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700243 private final DisplayMetrics mDisplayMetrics = new DisplayMetrics();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800244 private final DisplayPolicy mDisplayPolicy;
245 private DisplayRotation mDisplayRotation;
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000246 DisplayFrames mDisplayFrames;
247
Andrii Kulian06d07d62017-03-14 11:11:47 -0700248 /**
249 * For default display it contains real metrics, empty for others.
250 * @see WindowManagerService#createWatermarkInTransaction()
251 */
252 final DisplayMetrics mRealDisplayMetrics = new DisplayMetrics();
253 /** @see #computeCompatSmallestWidth(boolean, int, int, int, int) */
254 private final DisplayMetrics mTmpDisplayMetrics = new DisplayMetrics();
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700255
Andrii Kulian06d07d62017-03-14 11:11:47 -0700256 /**
257 * Compat metrics computed based on {@link #mDisplayMetrics}.
258 * @see #updateDisplayAndOrientation(int)
259 */
260 private final DisplayMetrics mCompatDisplayMetrics = new DisplayMetrics();
261
262 /** The desired scaling factor for compatible apps. */
263 float mCompatibleScreenScale;
Craig Mautner59c00972012-07-30 12:10:24 -0700264
Andrii Kulian8ee72852017-03-10 10:36:45 -0800265 /**
266 * Current rotation of the display.
267 * Constants as per {@link android.view.Surface.Rotation}.
268 *
Robert Carrae606b42018-02-15 15:36:23 -0800269 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800270 */
271 private int mRotation = 0;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700272
Andrii Kulian8ee72852017-03-10 10:36:45 -0800273 /**
274 * Last applied orientation of the display.
275 * Constants as per {@link android.content.pm.ActivityInfo.ScreenOrientation}.
276 *
277 * @see WindowManagerService#updateOrientationFromAppTokensLocked(boolean, int)
278 */
279 private int mLastOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700280
Andrii Kulian8ee72852017-03-10 10:36:45 -0800281 /**
282 * Flag indicating that the application is receiving an orientation that has different metrics
283 * than it expected. E.g. Portrait instead of Landscape.
284 *
Robert Carrae606b42018-02-15 15:36:23 -0800285 * @see #updateRotationUnchecked()
Andrii Kulian8ee72852017-03-10 10:36:45 -0800286 */
287 private boolean mAltOrientation = false;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700288
Andrii Kulian8ee72852017-03-10 10:36:45 -0800289 /**
290 * Orientation forced by some window. If there is no visible window that specifies orientation
291 * it is set to {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_UNSPECIFIED}.
292 *
293 * @see NonAppWindowContainers#getOrientation()
294 */
295 private int mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700296
Andrii Kulian8ee72852017-03-10 10:36:45 -0800297 /**
298 * Last orientation forced by the keyguard. It is applied when keyguard is shown and is not
299 * occluded.
300 *
301 * @see NonAppWindowContainers#getOrientation()
302 */
303 private int mLastKeyguardForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
304
Lucas Dupin1ead7fc2017-05-24 14:14:44 -0700305 /**
306 * Keep track of wallpaper visibility to notify changes.
307 */
308 private boolean mLastWallpaperVisible = false;
309
Andrii Kulian06d07d62017-03-14 11:11:47 -0700310 private Rect mBaseDisplayRect = new Rect();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700311
Craig Mautner39834192012-09-02 07:47:24 -0700312 // Accessed directly by all users.
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -0700313 private boolean mLayoutNeeded;
Craig Mautner76a71652012-09-03 23:23:58 -0700314 int pendingLayoutChanges;
Riddle Hsu654a6f92018-07-13 22:59:36 +0800315 int mDeferredRotationPauseCount;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800316
Andrii Kulian839def92016-11-02 10:58:58 -0700317 // TODO(multi-display): remove some of the usages.
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800318 @VisibleForTesting
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800319 boolean isDefaultDisplay;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800320
Andrii Kulianf0379de2018-03-14 16:24:07 -0700321 /**
322 * Flag indicating whether WindowManager should override info for this display in
323 * DisplayManager.
324 */
325 boolean mShouldOverrideDisplayConfiguration = true;
Craig Mautner39834192012-09-02 07:47:24 -0700326
Craig Mautnerdc548482014-02-05 13:35:24 -0800327 /** Window tokens that are in the process of exiting, but still on screen for animations. */
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700328 final ArrayList<WindowToken> mExitingTokens = new ArrayList<>();
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800329
Wale Ogunwalee4a0c572015-06-30 08:40:31 -0700330 /** Detect user tapping outside of current focused task bounds .*/
331 TaskTapPointerEventListener mTapDetector;
Craig Mautnercf910b02013-04-23 11:23:27 -0700332
Craig Mautner6601b7b2013-04-29 10:29:11 -0700333 /** Detect user tapping outside of current focused stack bounds .*/
Wale Ogunwale9adfe572016-09-08 20:43:58 -0700334 private Region mTouchExcludeRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700335
Craig Mautner6601b7b2013-04-29 10:29:11 -0700336 /** Save allocating when calculating rects */
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800337 private final Rect mTmpRect = new Rect();
338 private final Rect mTmpRect2 = new Rect();
Andrii Kulian4dfb9c42016-10-11 20:06:27 -0700339 private final RectF mTmpRectF = new RectF();
340 private final Matrix mTmpMatrix = new Matrix();
Filip Gruszczynski912d9192015-12-01 16:14:04 -0800341 private final Region mTmpRegion = new Region();
Craig Mautner6601b7b2013-04-29 10:29:11 -0700342
Bryce Leef3c6a472017-11-14 14:53:06 -0800343 /** Used for handing back size of display */
344 private final Rect mTmpBounds = new Rect();
345
Craig Mautner95da1082014-02-24 17:54:35 -0800346 /** Remove this display when animation on it has completed. */
Wale Ogunwaled4a00a02016-10-10 11:29:17 -0700347 private boolean mDeferredRemoval;
Craig Mautner1bf2b872014-02-05 15:37:40 -0800348
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700349 final DockedStackDividerController mDividerControllerLocked;
Winson Chung655332c2016-10-31 13:14:28 -0700350 final PinnedStackController mPinnedStackControllerLocked;
Filip Gruszczynski466f3212015-09-21 17:57:57 -0700351
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800352 final ArrayList<WindowState> mTapExcludedWindows = new ArrayList<>();
Andrii Kulian4b6599e2018-01-15 17:24:08 -0800353 /** A collection of windows that provide tap exclude regions inside of them. */
354 final ArraySet<WindowState> mTapExcludeProvidingWindows = new ArraySet<>();
Filip Gruszczynskiecf67222015-12-11 15:16:36 -0800355
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000356 private boolean mHaveBootMsg = false;
357 private boolean mHaveApp = false;
358 private boolean mHaveWallpaper = false;
359 private boolean mHaveKeyguard = true;
Wale Ogunwalef7cab102016-10-25 15:25:14 -0700360
361 private final LinkedList<AppWindowToken> mTmpUpdateAllDrawn = new LinkedList();
362
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700363 private final TaskForResizePointSearchResult mTmpTaskForResizePointSearchResult =
364 new TaskForResizePointSearchResult();
Wale Ogunwale6213caa2016-12-02 16:47:15 +0000365 private final ApplySurfaceChangesTransactionState mTmpApplySurfaceChangesTransactionState =
366 new ApplySurfaceChangesTransactionState();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -0700367
Wale Ogunwale601a3f02016-10-17 08:39:39 -0700368 // True if this display is in the process of being removed. Used to determine if the removal of
369 // the display's direct children should be allowed.
370 private boolean mRemovingDisplay = false;
371
Bryce Leed1871262017-06-12 14:12:29 -0700372 // {@code false} if this display is in the processing of being created.
373 private boolean mDisplayReady = false;
374
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800375 WallpaperController mWallpaperController;
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700376
Robert Carrb1579c82017-09-05 14:54:47 -0700377 private final SurfaceSession mSession = new SurfaceSession();
378
379 /**
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800380 * Window that is currently interacting with the user. This window is responsible for receiving
381 * key events and pointer events from the user.
382 */
383 WindowState mCurrentFocus = null;
384
385 /**
386 * The last focused window that we've notified the client that the focus is changed.
387 */
388 WindowState mLastFocus = null;
389
390 /**
391 * Windows that have lost input focus and are waiting for the new focus window to be displayed
392 * before they are told about this.
393 */
394 ArrayList<WindowState> mLosingFocus = new ArrayList<>();
395
396 /**
397 * The foreground app of this display. Windows below this app cannot be the focused window. If
398 * the user taps on the area outside of the task of the focused app, we will notify AM about the
399 * new task the user wants to interact with.
400 */
401 AppWindowToken mFocusedApp = null;
402
403 /** Windows added since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
404 final ArrayList<WindowState> mWinAddedSinceNullFocus = new ArrayList<>();
405
406 /** Windows removed since {@link #mCurrentFocus} was set to null. Used for ANR blaming. */
407 final ArrayList<WindowState> mWinRemovedSinceNullFocus = new ArrayList<>();
408
409 /**
Robert Carrb1579c82017-09-05 14:54:47 -0700410 * We organize all top-level Surfaces in to the following layers.
411 * mOverlayLayer contains a few Surfaces which are always on top of others
Robert Carree4d4b92017-11-22 12:21:46 -0800412 * and omitted from Screen-Magnification, for example the strict mode flash or
413 * the magnification overlay itself.
Robert Carrb1579c82017-09-05 14:54:47 -0700414 * {@link #mWindowingLayer} contains everything else.
415 */
416 private SurfaceControl mOverlayLayer;
417
418 /**
419 * See {@link #mOverlayLayer}
420 */
421 private SurfaceControl mWindowingLayer;
422
423 /**
424 * Specifies the size of the surfaces in {@link #mOverlayLayer} and {@link #mWindowingLayer}.
425 * <p>
426 * For these surfaces currently we use a surface based on the larger of width or height so we
427 * don't have to resize when rotating the display.
428 */
429 private int mSurfaceSize;
430
Adrian Roos5251b1d2018-03-23 18:57:43 +0100431 /**
432 * Sequence number for the current layout pass.
433 */
434 int mLayoutSeq = 0;
435
Chavi Weingarten3a748552018-05-14 17:32:42 +0000436 /**
437 * Specifies the count to determine whether to defer updating the IME target until ready.
438 */
439 private int mDeferUpdateImeTargetCount;
440
Jorim Jaggi9af095b2017-12-12 17:18:57 +0100441 /** Temporary float array to retrieve 3x3 matrix values. */
442 private final float[] mTmpFloats = new float[9];
443
Robert Carr24be9ab2018-04-30 17:54:53 -0700444 private MagnificationSpec mMagnificationSpec;
445
Arthur Hung95b38a92018-07-20 18:56:12 +0800446 private InputMonitor mInputMonitor;
447
Jorim Jaggif1292892018-09-10 11:58:13 +0200448 /** Caches the value whether told display manager that we have content. */
449 private boolean mLastHasContent;
450
lumark90120a82018-08-15 00:33:03 +0800451 /**
452 * The input method window for this display.
453 */
454 WindowState mInputMethodWindow;
455
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800456 private final Consumer<WindowState> mUpdateWindowsForAnimator = w -> {
457 WindowStateAnimator winAnimator = w.mWinAnimator;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800458 final AppWindowToken atoken = w.mAppToken;
459 if (winAnimator.mDrawState == READY_TO_SHOW) {
Jorim Jaggiaf0d6d22018-06-08 15:25:35 +0200460 if (atoken == null || atoken.canShowWindows()) {
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800461 if (w.performShowLocked()) {
462 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
463 if (DEBUG_LAYOUT_REPEATS) {
464 mService.mWindowPlacerLocked.debugLayoutRepeats(
465 "updateWindowsAndWallpaperLocked 5", pendingLayoutChanges);
466 }
467 }
468 }
469 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800470 };
471
472 private final Consumer<WindowState> mUpdateWallpaperForAnimator = w -> {
473 final WindowStateAnimator winAnimator = w.mWinAnimator;
474 if (winAnimator.mSurfaceController == null || !winAnimator.hasSurface()) {
475 return;
476 }
477
Jorim Jaggi8f520872018-08-14 17:00:20 +0200478 // If this window is animating, ensure the animation background is set.
479 final AnimationAdapter anim = w.mAppToken != null
480 ? w.mAppToken.getAnimation()
481 : w.getAnimation();
482 if (anim != null) {
483 final int color = anim.getBackgroundColor();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800484 if (color != 0) {
485 final TaskStack stack = w.getStack();
486 if (stack != null) {
487 stack.setAnimationBackground(winAnimator, color);
488 }
489 }
490 }
491 };
492
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800493 private final Consumer<WindowState> mScheduleToastTimeout = w -> {
494 final int lostFocusUid = mTmpWindow.mOwnerUid;
495 final Handler handler = mService.mH;
496 if (w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == lostFocusUid) {
497 if (!handler.hasMessages(WINDOW_HIDE_TIMEOUT, w)) {
498 handler.sendMessageDelayed(handler.obtainMessage(WINDOW_HIDE_TIMEOUT, w),
499 w.mAttrs.hideTimeoutMilliseconds);
500 }
501 }
502 };
503
504 private final ToBooleanFunction<WindowState> mFindFocusedWindow = w -> {
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800505 final AppWindowToken focusedApp = mFocusedApp;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800506 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Looking for focus: " + w
507 + ", flags=" + w.mAttrs.flags + ", canReceive=" + w.canReceiveKeys());
508
509 if (!w.canReceiveKeys()) {
510 return false;
511 }
512
513 final AppWindowToken wtoken = w.mAppToken;
514
515 // If this window's application has been removed, just skip it.
516 if (wtoken != null && (wtoken.removed || wtoken.sendingToBottom)) {
517 if (DEBUG_FOCUS) Slog.v(TAG_WM, "Skipping " + wtoken + " because "
518 + (wtoken.removed ? "removed" : "sendingToBottom"));
519 return false;
520 }
521
522 if (focusedApp == null) {
523 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp=null"
524 + " using new focus @ " + w);
525 mTmpWindow = w;
526 return true;
527 }
528
529 if (!focusedApp.windowsAreFocusable()) {
530 // Current focused app windows aren't focusable...
531 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: focusedApp windows not"
532 + " focusable using new focus @ " + w);
533 mTmpWindow = w;
534 return true;
535 }
536
537 // Descend through all of the app tokens and find the first that either matches
538 // win.mAppToken (return win) or mFocusedApp (return null).
539 if (wtoken != null && w.mAttrs.type != TYPE_APPLICATION_STARTING) {
540 if (focusedApp.compareTo(wtoken) > 0) {
541 // App stack below focused app stack. No focus for you!!!
542 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM,
543 "findFocusedWindow: Reached focused app=" + focusedApp);
544 mTmpWindow = null;
545 return true;
546 }
547 }
548
549 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: Found new focus @ " + w);
550 mTmpWindow = w;
551 return true;
552 };
553
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800554 private final Consumer<WindowState> mPerformLayout = w -> {
555 // Don't do layout of a window if it is not visible, or soon won't be visible, to avoid
556 // wasting time and funky changes while a window is animating away.
557 final boolean gone = (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w))
558 || w.isGoneForLayoutLw();
559
560 if (DEBUG_LAYOUT && !w.mLayoutAttached) {
561 Slog.v(TAG, "1ST PASS " + w + ": gone=" + gone + " mHaveFrame=" + w.mHaveFrame
562 + " mLayoutAttached=" + w.mLayoutAttached
563 + " screen changed=" + w.isConfigChanged());
564 final AppWindowToken atoken = w.mAppToken;
565 if (gone) Slog.v(TAG, " GONE: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200566 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800567 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
568 + " parentHidden=" + w.isParentWindowHidden());
569 else Slog.v(TAG, " VIS: mViewVisibility=" + w.mViewVisibility
Jorim Jaggif5f9e122017-10-24 18:21:09 +0200570 + " mRelayoutCalled=" + w.mRelayoutCalled + " hidden=" + w.mToken.isHidden()
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800571 + " hiddenRequested=" + (atoken != null && atoken.hiddenRequested)
572 + " parentHidden=" + w.isParentWindowHidden());
573 }
574
575 // If this view is GONE, then skip it -- keep the current frame, and let the caller know
576 // so they can ignore it if they want. (We do the normal layout for INVISIBLE windows,
577 // since that means "perform layout as normal, just don't display").
578 if (!gone || !w.mHaveFrame || w.mLayoutNeeded
579 || ((w.isConfigChanged() || w.setReportResizeHints())
580 && !w.isGoneForLayoutLw() &&
581 ((w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0 ||
582 (w.mHasSurface && w.mAppToken != null &&
583 w.mAppToken.layoutConfigChanges)))) {
584 if (!w.mLayoutAttached) {
585 if (mTmpInitial) {
586 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700587 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800588 }
589 if (w.mAttrs.type == TYPE_DREAM) {
590 // Don't layout windows behind a dream, so that if it does stuff like hide
591 // the status bar we won't get a bad transition when it goes away.
592 mTmpWindow = w;
593 }
594 w.mLayoutNeeded = false;
595 w.prelayout();
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200596 final boolean firstLayout = !w.isLaidOut();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000597 mService.mPolicy.layoutWindowLw(w, null, mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100598 w.mLayoutSeq = mLayoutSeq;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800599
Jorim Jaggidc9385a2017-05-13 02:00:31 +0200600 // If this is the first layout, we need to initialize the last inset values as
601 // otherwise we'd immediately cause an unnecessary resize.
602 if (firstLayout) {
603 w.updateLastInsetValues();
604 }
605
Adrian Roos23df3a32018-03-15 15:41:13 +0100606 if (w.mAppToken != null) {
607 w.mAppToken.layoutLetterbox(w);
608 }
609
chaviw492139a2018-07-16 16:07:35 -0700610 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700611 + " mContainingFrame=" + w.getContainingFrame()
612 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800613 }
614 }
615 };
616
617 private final Consumer<WindowState> mPerformLayoutAttached = w -> {
618 if (w.mLayoutAttached) {
619 if (DEBUG_LAYOUT) Slog.v(TAG, "2ND PASS " + w + " mHaveFrame=" + w.mHaveFrame
620 + " mViewVisibility=" + w.mViewVisibility
621 + " mRelayoutCalled=" + w.mRelayoutCalled);
622 // If this view is GONE, then skip it -- keep the current frame, and let the caller
623 // know so they can ignore it if they want. (We do the normal layout for INVISIBLE
624 // windows, since that means "perform layout as normal, just don't display").
625 if (mTmpWindow != null && mService.mPolicy.canBeHiddenByKeyguardLw(w)) {
626 return;
627 }
628 if ((w.mViewVisibility != GONE && w.mRelayoutCalled) || !w.mHaveFrame
629 || w.mLayoutNeeded) {
630 if (mTmpInitial) {
631 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - initial");
chaviw1454b392018-08-06 09:54:04 -0700632 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800633 }
634 w.mLayoutNeeded = false;
635 w.prelayout();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +0000636 mService.mPolicy.layoutWindowLw(w, w.getParentWindow(), mDisplayFrames);
Adrian Roos5251b1d2018-03-23 18:57:43 +0100637 w.mLayoutSeq = mLayoutSeq;
chaviw492139a2018-07-16 16:07:35 -0700638 if (DEBUG_LAYOUT) Slog.v(TAG, " LAYOUT: mFrame=" + w.getFrameLw()
chaviw553b0212018-07-12 13:37:01 -0700639 + " mContainingFrame=" + w.getContainingFrame()
640 + " mDisplayFrame=" + w.getDisplayFrameLw());
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800641 }
642 } else if (w.mAttrs.type == TYPE_DREAM) {
643 // Don't layout windows behind a dream, so that if it does stuff like hide the
644 // status bar we won't get a bad transition when it goes away.
645 mTmpWindow = mTmpWindow2;
646 }
647 };
648
649 private final Predicate<WindowState> mComputeImeTargetPredicate = w -> {
650 if (DEBUG_INPUT_METHOD && mUpdateImeTarget) Slog.i(TAG_WM, "Checking window @" + w
651 + " fl=0x" + Integer.toHexString(w.mAttrs.flags));
652 return w.canBeImeTarget();
653 };
654
655 private final Consumer<WindowState> mApplyPostLayoutPolicy =
656 w -> mService.mPolicy.applyPostLayoutPolicyLw(w, w.mAttrs, w.getParentWindow(),
657 mService.mInputMethodTarget);
658
659 private final Consumer<WindowState> mApplySurfaceChangesTransaction = w -> {
660 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
661 final boolean obscuredChanged = w.mObscured !=
662 mTmpApplySurfaceChangesTransactionState.obscured;
663 final RootWindowContainer root = mService.mRoot;
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800664
665 // Update effect.
666 w.mObscured = mTmpApplySurfaceChangesTransactionState.obscured;
667 if (!mTmpApplySurfaceChangesTransactionState.obscured) {
668 final boolean isDisplayed = w.isDisplayedLw();
669
670 if (isDisplayed && w.isObscuringDisplay()) {
671 // This window completely covers everything behind it, so we want to leave all
672 // of them as undimmed (for performance reasons).
673 root.mObscuringWindow = w;
674 mTmpApplySurfaceChangesTransactionState.obscured = true;
675 }
676
677 mTmpApplySurfaceChangesTransactionState.displayHasContent |=
678 root.handleNotObscuredLocked(w,
679 mTmpApplySurfaceChangesTransactionState.obscured,
680 mTmpApplySurfaceChangesTransactionState.syswin);
681
682 if (w.mHasSurface && isDisplayed) {
683 final int type = w.mAttrs.type;
684 if (type == TYPE_SYSTEM_DIALOG || type == TYPE_SYSTEM_ERROR
685 || (w.mAttrs.privateFlags & PRIVATE_FLAG_KEYGUARD) != 0) {
686 mTmpApplySurfaceChangesTransactionState.syswin = true;
687 }
688 if (mTmpApplySurfaceChangesTransactionState.preferredRefreshRate == 0
689 && w.mAttrs.preferredRefreshRate != 0) {
690 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate
691 = w.mAttrs.preferredRefreshRate;
692 }
693 if (mTmpApplySurfaceChangesTransactionState.preferredModeId == 0
694 && w.mAttrs.preferredDisplayModeId != 0) {
695 mTmpApplySurfaceChangesTransactionState.preferredModeId
696 = w.mAttrs.preferredDisplayModeId;
697 }
698 }
699 }
700
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800701 if (isDefaultDisplay && obscuredChanged && w.isVisibleLw()
702 && mWallpaperController.isWallpaperTarget(w)) {
703 // This is the wallpaper target and its obscured state changed... make sure the
704 // current wallpaper's visibility has been updated accordingly.
705 mWallpaperController.updateWallpaperVisibility();
706 }
707
chaviw161ea3e2018-01-31 12:01:12 -0800708 w.handleWindowMovedIfNeeded();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800709
710 final WindowStateAnimator winAnimator = w.mWinAnimator;
711
712 //Slog.i(TAG, "Window " + this + " clearing mContentChanged - done placing");
chaviw1454b392018-08-06 09:54:04 -0700713 w.resetContentChanged();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800714
715 // Moved from updateWindowsAndWallpaperLocked().
716 if (w.mHasSurface) {
717 // Take care of the window being ready to display.
718 final boolean committed = winAnimator.commitFinishDrawingLocked();
719 if (isDefaultDisplay && committed) {
720 if (w.mAttrs.type == TYPE_DREAM) {
721 // HACK: When a dream is shown, it may at that point hide the lock screen.
722 // So we need to redo the layout to let the phone window manager make this
723 // happen.
724 pendingLayoutChanges |= FINISH_LAYOUT_REDO_LAYOUT;
725 if (DEBUG_LAYOUT_REPEATS) {
726 surfacePlacer.debugLayoutRepeats(
727 "dream and commitFinishDrawingLocked true",
728 pendingLayoutChanges);
729 }
730 }
731 if ((w.mAttrs.flags & FLAG_SHOW_WALLPAPER) != 0) {
732 if (DEBUG_WALLPAPER_LIGHT) Slog.v(TAG,
733 "First draw done in potential wallpaper target " + w);
734 root.mWallpaperMayChange = true;
735 pendingLayoutChanges |= FINISH_LAYOUT_REDO_WALLPAPER;
736 if (DEBUG_LAYOUT_REPEATS) {
737 surfacePlacer.debugLayoutRepeats(
738 "wallpaper and commitFinishDrawingLocked true",
739 pendingLayoutChanges);
740 }
741 }
742 }
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800743 }
744
745 final AppWindowToken atoken = w.mAppToken;
746 if (atoken != null) {
Adrian Roos23df3a32018-03-15 15:41:13 +0100747 atoken.updateLetterboxSurface(w);
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800748 final boolean updateAllDrawn = atoken.updateDrawnWindowStates(w);
749 if (updateAllDrawn && !mTmpUpdateAllDrawn.contains(atoken)) {
750 mTmpUpdateAllDrawn.add(atoken);
751 }
752 }
753
Tiger Huang1e5b10a2018-07-30 20:19:51 +0800754 if (!mLosingFocus.isEmpty() && w.isFocused() && w.isDisplayedLw()) {
755 mService.mH.obtainMessage(REPORT_LOSING_FOCUS, this).sendToTarget();
Wale Ogunwale1e129a42016-11-21 13:03:47 -0800756 }
757
758 w.updateResizingWindowIfNeeded();
759 };
760
Craig Mautnerb1fd65c02013-02-05 13:34:57 -0800761 /**
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800762 * Create new {@link DisplayContent} instance, add itself to the root window container and
763 * initialize direct children.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700764 * @param display May not be null.
Craig Mautnerdf88d732014-01-27 09:21:32 -0800765 * @param service You know.
Wale Ogunwale0303c572016-10-20 10:16:29 -0700766 * @param wallpaperController wallpaper windows controller used to adjust the positioning of the
767 * wallpaper windows in the window list.
Craig Mautner2d5618c2012-10-18 13:55:47 -0700768 */
Wale Ogunwalec69694a2016-10-18 13:51:15 -0700769 DisplayContent(Display display, WindowManagerService service,
Bryce Leef19cbe22018-02-02 15:09:21 -0800770 WallpaperController wallpaperController, DisplayWindowController controller) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +0100771 super(service);
Bryce Leef19cbe22018-02-02 15:09:21 -0800772 setController(controller);
Wale Ogunwale1666e312016-12-16 11:27:18 -0800773 if (service.mRoot.getDisplayContent(display.getDisplayId()) != null) {
774 throw new IllegalArgumentException("Display with ID=" + display.getDisplayId()
775 + " already exists=" + service.mRoot.getDisplayContent(display.getDisplayId())
776 + " new=" + display);
777 }
778
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700779 mDisplay = display;
780 mDisplayId = display.getDisplayId();
Wale Ogunwale0303c572016-10-20 10:16:29 -0700781 mWallpaperController = wallpaperController;
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700782 display.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700783 display.getMetrics(mDisplayMetrics);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700784 isDefaultDisplay = mDisplayId == DEFAULT_DISPLAY;
Adrian Roos6a4fa0e2018-03-05 19:50:16 +0100785 mDisplayFrames = new DisplayFrames(mDisplayId, mDisplayInfo,
786 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -0700787 initializeDisplayBaseInfo();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800788 mDisplayPolicy = new DisplayPolicy(service);
789 mDisplayRotation = new DisplayRotation(service, this);
790 if (isDefaultDisplay) {
791 // The policy may be invoked right after here, so it requires the necessary default
792 // fields of this display content.
793 mService.mPolicy.setDefaultDisplay(this);
794 }
Jorim Jaggi42625d1b2016-02-11 20:11:07 -0800795 mDividerControllerLocked = new DockedStackDividerController(service, this);
Winson Chung655332c2016-10-31 13:14:28 -0700796 mPinnedStackControllerLocked = new PinnedStackController(service, this);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700797
Robert Carr74a66a22018-02-23 12:17:51 -0800798 // We use this as our arbitrary surface size for buffer-less parents
799 // that don't impose cropping on their children. It may need to be larger
800 // than the display size because fullscreen windows can be shifted offscreen
801 // due to surfaceInsets. 2 times the largest display dimension feels like an
802 // appropriately arbitrary number. Eventually we would like to give SurfaceFlinger
803 // layers the ability to match their parent sizes and be able to skip
804 // such arbitrary size settings.
805 mSurfaceSize = Math.max(mBaseDisplayHeight, mBaseDisplayWidth) * 2;
Robert Carrb1579c82017-09-05 14:54:47 -0700806
807 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(mSession)
808 .setSize(mSurfaceSize, mSurfaceSize)
809 .setOpaque(true);
810 mWindowingLayer = b.setName("Display Root").build();
811 mOverlayLayer = b.setName("Display Overlays").build();
812
Robert Carrf59b8dd2017-10-02 18:58:36 -0700813 getPendingTransaction().setLayer(mWindowingLayer, 0)
Robert Carrb1579c82017-09-05 14:54:47 -0700814 .setLayerStack(mWindowingLayer, mDisplayId)
815 .show(mWindowingLayer)
816 .setLayer(mOverlayLayer, 1)
817 .setLayerStack(mOverlayLayer, mDisplayId)
818 .show(mOverlayLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700819 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -0700820
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700821 // These are the only direct children we should ever have and they are permanent.
Wale Ogunwale3a931692016-11-02 16:49:48 -0700822 super.addChild(mBelowAppWindowsContainers, null);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700823 super.addChild(mTaskStackContainers, null);
Wale Ogunwale3a931692016-11-02 16:49:48 -0700824 super.addChild(mAboveAppWindowsContainers, null);
825 super.addChild(mImeWindowsContainers, null);
Andrii Kulian367ff7f2017-01-25 19:45:34 -0800826
827 // Add itself as a child to the root container.
828 mService.mRoot.addChild(this, null);
Bryce Leed1871262017-06-12 14:12:29 -0700829
830 // TODO(b/62541591): evaluate whether this is the best spot to declare the
831 // {@link DisplayContent} ready for use.
832 mDisplayReady = true;
Arthur Hung95b38a92018-07-20 18:56:12 +0800833
Arthur Hung39134b22018-08-14 11:58:28 +0800834 mInputMonitor = new InputMonitor(service, mDisplayId);
Bryce Leed1871262017-06-12 14:12:29 -0700835 }
836
837 boolean isReady() {
838 // The display is ready when the system and the individual display are both ready.
839 return mService.mDisplayReady && mDisplayReady;
Craig Mautner59c00972012-07-30 12:10:24 -0700840 }
841
842 int getDisplayId() {
843 return mDisplayId;
844 }
845
Wale Ogunwale02319a62016-09-26 15:21:22 -0700846 WindowToken getWindowToken(IBinder binder) {
847 return mTokenMap.get(binder);
848 }
849
850 AppWindowToken getAppWindowToken(IBinder binder) {
851 final WindowToken token = getWindowToken(binder);
852 if (token == null) {
853 return null;
854 }
855 return token.asAppWindowToken();
856 }
857
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700858 private void addWindowToken(IBinder binder, WindowToken token) {
Wale Ogunwale02319a62016-09-26 15:21:22 -0700859 final DisplayContent dc = mService.mRoot.getWindowTokenDisplay(token);
860 if (dc != null) {
861 // We currently don't support adding a window token to the display if the display
862 // already has the binder mapped to another token. If there is a use case for supporting
863 // this moving forward we will either need to merge the WindowTokens some how or have
864 // the binder map to a list of window tokens.
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700865 throw new IllegalArgumentException("Can't map token=" + token + " to display="
866 + getName() + " already mapped to display=" + dc + " tokens=" + dc.mTokenMap);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700867 }
Wale Ogunwale2f569ed2017-05-08 09:15:49 -0700868 if (binder == null) {
869 throw new IllegalArgumentException("Can't map token=" + token + " to display="
870 + getName() + " binder is null");
871 }
872 if (token == null) {
873 throw new IllegalArgumentException("Can't map null token to display="
874 + getName() + " binder=" + binder);
875 }
876
Wale Ogunwale02319a62016-09-26 15:21:22 -0700877 mTokenMap.put(binder, token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700878
879 if (token.asAppWindowToken() == null) {
880 // Add non-app token to container hierarchy on the display. App tokens are added through
881 // the parent container managing them (e.g. Tasks).
Wale Ogunwale3a931692016-11-02 16:49:48 -0700882 switch (token.windowType) {
883 case TYPE_WALLPAPER:
884 mBelowAppWindowsContainers.addChild(token);
885 break;
886 case TYPE_INPUT_METHOD:
887 case TYPE_INPUT_METHOD_DIALOG:
888 mImeWindowsContainers.addChild(token);
889 break;
890 default:
891 mAboveAppWindowsContainers.addChild(token);
892 break;
893 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700894 }
Wale Ogunwale02319a62016-09-26 15:21:22 -0700895 }
896
897 WindowToken removeWindowToken(IBinder binder) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700898 final WindowToken token = mTokenMap.remove(binder);
899 if (token != null && token.asAppWindowToken() == null) {
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800900 token.setExiting();
901 }
902 return token;
903 }
904
905 /** Changes the display the input window token is housed on to this one. */
906 void reParentWindowToken(WindowToken token) {
907 final DisplayContent prevDc = token.getDisplayContent();
908 if (prevDc == this) {
909 return;
910 }
Andrii Kulian6cc1a1d2016-12-27 23:52:59 -0800911 if (prevDc != null && prevDc.mTokenMap.remove(token.token) != null
912 && token.asAppWindowToken() == null) {
913 // Removed the token from the map, but made sure it's not an app token before removing
914 // from parent.
915 token.getParent().removeChild(token);
Wale Ogunwale19e452e2016-10-12 12:36:29 -0700916 }
Wale Ogunwale3d0bfd92016-12-05 11:38:02 -0800917
918 addWindowToken(token.token, token);
Wale Ogunwale02319a62016-09-26 15:21:22 -0700919 }
920
Wale Ogunwaleac2561e2016-11-01 15:43:46 -0700921 void removeAppToken(IBinder binder) {
922 final WindowToken token = removeWindowToken(binder);
923 if (token == null) {
924 Slog.w(TAG_WM, "removeAppToken: Attempted to remove non-existing token: " + binder);
925 return;
926 }
927
928 final AppWindowToken appToken = token.asAppWindowToken();
929
930 if (appToken == null) {
931 Slog.w(TAG_WM, "Attempted to remove non-App token: " + binder + " token=" + token);
932 return;
933 }
934
935 appToken.onRemovedFromDisplay();
936 }
937
Riddle Hsuad256a12018-07-18 16:11:30 +0800938 @Override
Riddle Hsu86cb7de2018-08-13 23:29:58 +0800939 DisplayWindowController getController() {
940 return (DisplayWindowController) super.getController();
941 }
942
943 @Override
Riddle Hsuad256a12018-07-18 16:11:30 +0800944 public Display getDisplay() {
Craig Mautnerb47bbc32012-08-22 17:41:48 -0700945 return mDisplay;
946 }
947
Craig Mautner59c00972012-07-30 12:10:24 -0700948 DisplayInfo getDisplayInfo() {
949 return mDisplayInfo;
950 }
951
Wale Ogunwale231b06e2015-09-16 12:03:09 -0700952 DisplayMetrics getDisplayMetrics() {
953 return mDisplayMetrics;
954 }
955
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800956 DisplayPolicy getDisplayPolicy() {
957 return mDisplayPolicy;
958 }
959
Riddle Hsuad256a12018-07-18 16:11:30 +0800960 @Override
961 public DisplayRotation getDisplayRotation() {
962 return mDisplayRotation;
963 }
964
Riddle Hsu5ce4bb32018-07-18 16:11:30 +0800965 @VisibleForTesting
966 void setDisplayRotation(DisplayRotation displayRotation) {
967 mDisplayRotation = displayRotation;
968 }
969
Andrii Kulian8ee72852017-03-10 10:36:45 -0800970 int getRotation() {
971 return mRotation;
972 }
973
Andrii Kulianef5ce1c2018-03-20 19:27:24 -0700974 @VisibleForTesting
Andrii Kulian8ee72852017-03-10 10:36:45 -0800975 void setRotation(int newRotation) {
976 mRotation = newRotation;
Riddle Hsuad256a12018-07-18 16:11:30 +0800977 mDisplayRotation.setRotation(newRotation);
Andrii Kulian8ee72852017-03-10 10:36:45 -0800978 }
979
980 int getLastOrientation() {
981 return mLastOrientation;
982 }
983
984 void setLastOrientation(int orientation) {
985 mLastOrientation = orientation;
986 }
987
988 boolean getAltOrientation() {
989 return mAltOrientation;
990 }
991
992 void setAltOrientation(boolean altOrientation) {
993 mAltOrientation = altOrientation;
994 }
995
996 int getLastWindowForcedOrientation() {
997 return mLastWindowForcedOrientation;
998 }
999
Andrii Kulian06d07d62017-03-14 11:11:47 -07001000 /**
Riddle Hsu654a6f92018-07-13 22:59:36 +08001001 * Temporarily pauses rotation changes until resumed.
1002 *
1003 * This can be used to prevent rotation changes from occurring while the user is
1004 * performing certain operations, such as drag and drop.
1005 *
1006 * This call nests and must be matched by an equal number of calls to
1007 * {@link #resumeRotationLocked}.
1008 */
1009 void pauseRotationLocked() {
1010 mDeferredRotationPauseCount++;
1011 }
1012
1013 /**
1014 * Resumes normal rotation changes after being paused.
1015 */
1016 void resumeRotationLocked() {
1017 if (mDeferredRotationPauseCount <= 0) {
1018 return;
1019 }
1020
1021 mDeferredRotationPauseCount--;
1022 if (mDeferredRotationPauseCount == 0) {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001023 updateRotationAndSendNewConfigIfNeeded();
Riddle Hsu654a6f92018-07-13 22:59:36 +08001024 }
1025 }
1026
1027 /**
Riddle Hsuad256a12018-07-18 16:11:30 +08001028 * If this is true we have updated our desired orientation, but not yet changed the real
1029 * orientation our applied our screen rotation animation. For example, because a previous
1030 * screen rotation was in progress.
1031 *
1032 * @return {@code true} if the there is an ongoing rotation change.
1033 */
1034 boolean rotationNeedsUpdate() {
1035 final int lastOrientation = getLastOrientation();
1036 final int oldRotation = getRotation();
1037 final boolean oldAltOrientation = getAltOrientation();
1038
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001039 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Riddle Hsuad256a12018-07-18 16:11:30 +08001040 final boolean altOrientation = !mDisplayRotation.rotationHasCompatibleMetrics(
1041 lastOrientation, rotation);
1042 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1043 return false;
1044 }
1045 return true;
1046 }
1047
1048 /**
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001049 * Update rotation of the display and send configuration if the rotation is changed.
1050 *
1051 * @return {@code true} if the rotation has been changed and the new config is sent.
1052 */
1053 boolean updateRotationAndSendNewConfigIfNeeded() {
1054 final boolean changed = updateRotationUnchecked(false /* forceUpdate */);
1055 if (changed) {
1056 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
1057 }
1058 return changed;
1059 }
1060
1061 /**
Andrii Kulian06d07d62017-03-14 11:11:47 -07001062 * Update rotation of the display.
1063 *
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001064 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
1065 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
Andrii Kulian06d07d62017-03-14 11:11:47 -07001066 */
Robert Carrae606b42018-02-15 15:36:23 -08001067 boolean updateRotationUnchecked() {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001068 return updateRotationUnchecked(false /* forceUpdate */);
1069 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001070
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001071 /**
1072 * Update rotation of the display with an option to force the update.
1073 * @param forceUpdate Force the rotation update. Sometimes in WM we might skip updating
1074 * orientation because we're waiting for some rotation to finish or display
1075 * to unfreeze, which results in configuration of the previously visible
1076 * activity being applied to a newly visible one. Forcing the rotation
1077 * update allows to workaround this issue.
1078 * @return {@code true} if the rotation has been changed. In this case YOU MUST CALL
1079 * {@link WindowManagerService#sendNewConfiguration(int)} TO UNFREEZE THE SCREEN.
1080 */
1081 boolean updateRotationUnchecked(boolean forceUpdate) {
1082 ScreenRotationAnimation screenRotationAnimation;
1083 if (!forceUpdate) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001084 if (mDeferredRotationPauseCount > 0) {
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001085 // Rotation updates have been paused temporarily. Defer the update until
1086 // updates have been resumed.
1087 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, rotation is paused.");
1088 return false;
1089 }
1090
1091 screenRotationAnimation =
1092 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
1093 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
1094 // Rotation updates cannot be performed while the previous rotation change
1095 // animation is still in progress. Skip this update. We will try updating
1096 // again after the animation is finished and the display is unfrozen.
1097 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, animation in progress.");
1098 return false;
1099 }
1100 if (mService.mDisplayFrozen) {
1101 // Even if the screen rotation animation has finished (e.g. isAnimating
1102 // returns false), there is still some time where we haven't yet unfrozen
1103 // the display. We also need to abort rotation here.
1104 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
1105 "Deferring rotation, still finishing previous rotation");
1106 return false;
1107 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001108 }
1109
1110 if (!mService.mDisplayEnabled) {
1111 // No point choosing a rotation if the display is not enabled.
1112 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Deferring rotation, display is not enabled.");
1113 return false;
1114 }
1115
1116 final int oldRotation = mRotation;
1117 final int lastOrientation = mLastOrientation;
1118 final boolean oldAltOrientation = mAltOrientation;
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001119 final int rotation = mDisplayRotation.rotationForOrientation(lastOrientation, oldRotation);
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001120 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Computed rotation=" + rotation + " for display id="
1121 + mDisplayId + " based on lastOrientation=" + lastOrientation
1122 + " and oldRotation=" + oldRotation);
Riddle Hsuad256a12018-07-18 16:11:30 +08001123 boolean mayRotateSeamlessly = mService.mPolicy.shouldRotateSeamlessly(mDisplayRotation,
1124 oldRotation, rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001125
Robert Carr0e007272017-10-02 13:00:55 -07001126 if (mayRotateSeamlessly) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001127 final WindowState seamlessRotated = getWindow((w) -> w.mSeamlesslyRotated);
Andrii Kulian6cdcfe42018-05-23 17:59:43 -07001128 if (seamlessRotated != null && !forceUpdate) {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001129 // We can't rotate (seamlessly or not) while waiting for the last seamless rotation
1130 // to complete (that is, waiting for windows to redraw). It's tempting to check
1131 // w.mSeamlessRotationCount but that could be incorrect in the case of
1132 // window-removal.
1133 return false;
1134 }
Robert Carr0e007272017-10-02 13:00:55 -07001135
1136 // In the presence of the PINNED stack or System Alert
Wale Ogunwale44f036f2017-09-29 05:09:09 -07001137 // windows we unfortunately can not seamlessly rotate.
Wale Ogunwale61911492017-10-11 08:50:50 -07001138 if (hasPinnedStack()) {
Robert Carr0e007272017-10-02 13:00:55 -07001139 mayRotateSeamlessly = false;
1140 }
1141 for (int i = 0; i < mService.mSessions.size(); i++) {
1142 if (mService.mSessions.valueAt(i).hasAlertWindowSurfaces()) {
1143 mayRotateSeamlessly = false;
1144 break;
1145 }
1146 }
Andrii Kulian06d07d62017-03-14 11:11:47 -07001147 }
Robert Carr0e007272017-10-02 13:00:55 -07001148 final boolean rotateSeamlessly = mayRotateSeamlessly;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001149
1150 // TODO: Implement forced rotation changes.
1151 // Set mAltOrientation to indicate that the application is receiving
1152 // an orientation that has different metrics than it expected.
1153 // eg. Portrait instead of Landscape.
1154
Riddle Hsuad256a12018-07-18 16:11:30 +08001155 final boolean altOrientation = !mDisplayRotation.rotationHasCompatibleMetrics(
Andrii Kulian06d07d62017-03-14 11:11:47 -07001156 lastOrientation, rotation);
1157
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001158 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1159 + " selected orientation " + lastOrientation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001160 + ", got rotation " + rotation + " which has "
1161 + (altOrientation ? "incompatible" : "compatible") + " metrics");
1162
1163 if (oldRotation == rotation && oldAltOrientation == altOrientation) {
1164 // No change.
1165 return false;
1166 }
1167
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001168 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1169 + " rotation changed to " + rotation
Andrii Kulian06d07d62017-03-14 11:11:47 -07001170 + (altOrientation ? " (alt)" : "") + " from " + oldRotation
1171 + (oldAltOrientation ? " (alt)" : "") + ", lastOrientation=" + lastOrientation);
1172
1173 if (DisplayContent.deltaRotation(rotation, oldRotation) != 2) {
1174 mService.mWaitingForConfig = true;
1175 }
1176
Riddle Hsuad256a12018-07-18 16:11:30 +08001177 setRotation(rotation);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001178 mAltOrientation = altOrientation;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001179
1180 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_ACTIVE;
Riddle Hsu654a6f92018-07-13 22:59:36 +08001181 mService.mH.sendNewMessageDelayed(WindowManagerService.H.WINDOW_FREEZE_TIMEOUT,
1182 this, WINDOW_FREEZE_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001183
1184 setLayoutNeeded();
1185 final int[] anim = new int[2];
Robert Carrf59b8dd2017-10-02 18:58:36 -07001186 mService.mPolicy.selectRotationAnimationLw(anim);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001187
1188 if (!rotateSeamlessly) {
Robert Carrae606b42018-02-15 15:36:23 -08001189 mService.startFreezingDisplayLocked(anim[0], anim[1], this);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001190 // startFreezingDisplayLocked can reset the ScreenRotationAnimation.
1191 screenRotationAnimation = mService.mAnimator.getScreenRotationAnimationLocked(
1192 mDisplayId);
1193 } else {
1194 // The screen rotation animation uses a screenshot to freeze the screen
1195 // while windows resize underneath.
1196 // When we are rotating seamlessly, we allow the elements to transition
1197 // to their rotated state independently and without a freeze required.
1198 screenRotationAnimation = null;
1199
Robert Carrefc75702018-02-16 11:46:16 -08001200 mService.startSeamlessRotation();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001201 }
1202
1203 // We need to update our screen size information to match the new rotation. If the rotation
1204 // has actually changed then this method will return true and, according to the comment at
1205 // the top of the method, the caller is obligated to call computeNewConfigurationLocked().
1206 // By updating the Display info here it will be available to
1207 // #computeScreenConfiguration() later.
1208 updateDisplayAndOrientation(getConfiguration().uiMode);
1209
Robert Carrae606b42018-02-15 15:36:23 -08001210 // NOTE: We disable the rotation in the emulator because
1211 // it doesn't support hardware OpenGL emulation yet.
1212 if (CUSTOM_SCREEN_ROTATION && screenRotationAnimation != null
1213 && screenRotationAnimation.hasScreenshot()) {
1214 if (screenRotationAnimation.setRotation(getPendingTransaction(), rotation,
1215 MAX_ANIMATION_DURATION, mService.getTransitionAnimationScaleLocked(),
1216 mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight)) {
1217 mService.scheduleAnimationLocked();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001218 }
1219 }
1220
Vishnu Nair83537a72018-07-19 21:27:48 -07001221 forAllWindows(w -> {
Vishnu Nairddd80742018-08-21 14:12:46 -07001222 w.seamlesslyRotateIfAllowed(getPendingTransaction(), oldRotation, rotation,
1223 rotateSeamlessly);
Vishnu Nair83537a72018-07-19 21:27:48 -07001224 }, true /* traverseTopToBottom */);
1225
Robert Carrae606b42018-02-15 15:36:23 -08001226 mService.mDisplayManagerInternal.performTraversal(getPendingTransaction());
1227 scheduleAnimation();
1228
Andrii Kulian06d07d62017-03-14 11:11:47 -07001229 forAllWindows(w -> {
Andrii Kulian06d07d62017-03-14 11:11:47 -07001230 if (w.mHasSurface && !rotateSeamlessly) {
1231 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Set mOrientationChanging of " + w);
Bryce Lee8c3cf382017-07-06 19:47:10 -07001232 w.setOrientationChanging(true);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001233 mService.mRoot.mOrientationChangeComplete = false;
1234 w.mLastFreezeDuration = 0;
1235 }
1236 w.mReportOrientationChanged = true;
1237 }, true /* traverseTopToBottom */);
1238
1239 if (rotateSeamlessly) {
Riddle Hsu654a6f92018-07-13 22:59:36 +08001240 mService.mH.sendNewMessageDelayed(WindowManagerService.H.SEAMLESS_ROTATION_TIMEOUT,
1241 this, SEAMLESS_ROTATION_TIMEOUT_DURATION);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001242 }
1243
1244 for (int i = mService.mRotationWatchers.size() - 1; i >= 0; i--) {
1245 final WindowManagerService.RotationWatcher rotationWatcher
1246 = mService.mRotationWatchers.get(i);
1247 if (rotationWatcher.mDisplayId == mDisplayId) {
1248 try {
1249 rotationWatcher.mWatcher.onRotationChanged(rotation);
1250 } catch (RemoteException e) {
1251 // Ignore
1252 }
1253 }
1254 }
1255
1256 // TODO (multi-display): Magnification is supported only for the default display.
1257 // Announce rotation only if we will not animate as we already have the
1258 // windows in final state. Otherwise, we make this call at the rotation end.
1259 if (screenRotationAnimation == null && mService.mAccessibilityController != null
1260 && isDefaultDisplay) {
1261 mService.mAccessibilityController.onRotationChangedLocked(this);
1262 }
1263
1264 return true;
1265 }
1266
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001267 void configureDisplayPolicy() {
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08001268 final int width = mBaseDisplayWidth;
1269 final int height = mBaseDisplayHeight;
1270 final int shortSize;
1271 final int longSize;
1272 if (width > height) {
1273 shortSize = height;
1274 longSize = width;
1275 } else {
1276 shortSize = width;
1277 longSize = height;
1278 }
1279
1280 final int shortSizeDp = shortSize * DisplayMetrics.DENSITY_DEFAULT / mBaseDisplayDensity;
1281 final int longSizeDp = longSize * DisplayMetrics.DENSITY_DEFAULT / mBaseDisplayDensity;
1282
1283 mDisplayRotation.configure(width, height, shortSizeDp, longSizeDp);
1284 mDisplayPolicy.configure(width, height, shortSizeDp);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001285
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001286 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
1287 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00001288 }
1289
Andrii Kulian06d07d62017-03-14 11:11:47 -07001290 /**
1291 * Update {@link #mDisplayInfo} and other internal variables when display is rotated or config
1292 * changed.
1293 * Do not call if {@link WindowManagerService#mDisplayReady} == false.
1294 */
1295 private DisplayInfo updateDisplayAndOrientation(int uiMode) {
1296 // Use the effective "visual" dimensions based on current rotation
1297 final boolean rotated = (mRotation == ROTATION_90 || mRotation == ROTATION_270);
1298 final int realdw = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
1299 final int realdh = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
1300 int dw = realdw;
1301 int dh = realdh;
1302
1303 if (mAltOrientation) {
1304 if (realdw > realdh) {
1305 // Turn landscape into portrait.
1306 int maxw = (int)(realdh/1.3f);
1307 if (maxw < realdw) {
1308 dw = maxw;
1309 }
1310 } else {
1311 // Turn portrait into landscape.
1312 int maxh = (int)(realdw/1.3f);
1313 if (maxh < realdh) {
1314 dh = maxh;
1315 }
1316 }
1317 }
1318
1319 // Update application display metrics.
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001320 final WmDisplayCutout wmDisplayCutout = calculateDisplayCutoutForRotation(mRotation);
1321 final DisplayCutout displayCutout = wmDisplayCutout.getDisplayCutout();
1322
Andrii Kulian06d07d62017-03-14 11:11:47 -07001323 final int appWidth = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, mRotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001324 mDisplayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001325 final int appHeight = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, mRotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001326 mDisplayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001327 mDisplayInfo.rotation = mRotation;
1328 mDisplayInfo.logicalWidth = dw;
1329 mDisplayInfo.logicalHeight = dh;
1330 mDisplayInfo.logicalDensityDpi = mBaseDisplayDensity;
1331 mDisplayInfo.appWidth = appWidth;
1332 mDisplayInfo.appHeight = appHeight;
1333 if (isDefaultDisplay) {
1334 mDisplayInfo.getLogicalMetrics(mRealDisplayMetrics,
1335 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null);
1336 }
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001337 mDisplayInfo.displayCutout = displayCutout.isEmpty() ? null : displayCutout;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001338 mDisplayInfo.getAppMetrics(mDisplayMetrics);
1339 if (mDisplayScalingDisabled) {
1340 mDisplayInfo.flags |= Display.FLAG_SCALING_DISABLED;
1341 } else {
1342 mDisplayInfo.flags &= ~Display.FLAG_SCALING_DISABLED;
1343 }
1344
Andrii Kulianf0379de2018-03-14 16:24:07 -07001345 // We usually set the override info in DisplayManager so that we get consistent display
1346 // metrics values when displays are changing and don't send out new values until WM is aware
1347 // of them. However, we don't do this for displays that serve as containers for ActivityView
1348 // because we don't want letter-/pillar-boxing during resize.
1349 final DisplayInfo overrideDisplayInfo = mShouldOverrideDisplayConfiguration
1350 ? mDisplayInfo : null;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001351 mService.mDisplayManagerInternal.setDisplayInfoOverrideFromWindowManager(mDisplayId,
Andrii Kulianf0379de2018-03-14 16:24:07 -07001352 overrideDisplayInfo);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001353
1354 mBaseDisplayRect.set(0, 0, dw, dh);
1355
1356 if (isDefaultDisplay) {
1357 mCompatibleScreenScale = CompatibilityInfo.computeCompatibleScaling(mDisplayMetrics,
1358 mCompatDisplayMetrics);
1359 }
Bryce Leef3c6a472017-11-14 14:53:06 -08001360
1361 updateBounds();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001362 return mDisplayInfo;
1363 }
1364
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001365 WmDisplayCutout calculateDisplayCutoutForRotation(int rotation) {
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001366 return mDisplayCutoutCache.getOrCompute(mInitialDisplayCutout, rotation);
1367 }
1368
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001369 private WmDisplayCutout calculateDisplayCutoutForRotationUncached(
Adrian Roos2aa0fcd2018-02-19 18:07:49 +01001370 DisplayCutout cutout, int rotation) {
Adrian Roos24264212018-02-19 16:26:15 +01001371 if (cutout == null || cutout == DisplayCutout.NO_CUTOUT) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001372 return WmDisplayCutout.NO_CUTOUT;
Adrian Roos1cf585052018-01-03 18:43:27 +01001373 }
Adrian Roos11c25582018-02-19 18:06:36 +01001374 if (rotation == ROTATION_0) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001375 return WmDisplayCutout.computeSafeInsets(
1376 cutout, mInitialDisplayWidth, mInitialDisplayHeight);
Adrian Roos24264212018-02-19 16:26:15 +01001377 }
Adrian Roosbed538e2018-02-21 17:50:07 +01001378 final boolean rotated = (rotation == ROTATION_90 || rotation == ROTATION_270);
Jorim Jaggi60640512018-06-29 01:14:31 +02001379 final List<Rect> bounds = WmDisplayCutout.computeSafeInsets(
1380 cutout, mInitialDisplayWidth, mInitialDisplayHeight)
1381 .getDisplayCutout().getBoundingRects();
Adrian Roos11c25582018-02-19 18:06:36 +01001382 transformPhysicalToLogicalCoordinates(rotation, mInitialDisplayWidth, mInitialDisplayHeight,
1383 mTmpMatrix);
Jorim Jaggi60640512018-06-29 01:14:31 +02001384 final Region region = Region.obtain();
1385 for (int i = 0; i < bounds.size(); i++) {
1386 final Rect rect = bounds.get(i);
1387 final RectF rectF = new RectF(bounds.get(i));
1388 mTmpMatrix.mapRect(rectF);
1389 rectF.round(rect);
1390 region.op(rect, Op.UNION);
1391 }
1392
1393 return WmDisplayCutout.computeSafeInsets(DisplayCutout.fromBounds(region),
Adrian Roos24264212018-02-19 16:26:15 +01001394 rotated ? mInitialDisplayHeight : mInitialDisplayWidth,
1395 rotated ? mInitialDisplayWidth : mInitialDisplayHeight);
Adrian Roos1cf585052018-01-03 18:43:27 +01001396 }
1397
Andrii Kulian06d07d62017-03-14 11:11:47 -07001398 /**
1399 * Compute display configuration based on display properties and policy settings.
1400 * Do not call if mDisplayReady == false.
1401 */
1402 void computeScreenConfiguration(Configuration config) {
1403 final DisplayInfo displayInfo = updateDisplayAndOrientation(config.uiMode);
1404
1405 final int dw = displayInfo.logicalWidth;
1406 final int dh = displayInfo.logicalHeight;
Wale Ogunwale687b4272017-07-27 02:56:23 -07001407 config.orientation = (dw <= dh) ? ORIENTATION_PORTRAIT : ORIENTATION_LANDSCAPE;
Garfield Tane0846042018-07-26 13:42:04 -07001408 config.windowConfiguration.setWindowingMode(getWindowingMode());
Bryce Leec1f2f2a2017-06-22 15:29:42 -07001409
Adrian Roos11c25582018-02-19 18:06:36 +01001410 final float density = mDisplayMetrics.density;
Andrii Kulian06d07d62017-03-14 11:11:47 -07001411 config.screenWidthDp =
1412 (int)(mService.mPolicy.getConfigDisplayWidth(dw, dh, displayInfo.rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001413 config.uiMode, mDisplayId, displayInfo.displayCutout) / density);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001414 config.screenHeightDp =
1415 (int)(mService.mPolicy.getConfigDisplayHeight(dw, dh, displayInfo.rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001416 config.uiMode, mDisplayId, displayInfo.displayCutout) / density);
Bryce Lee7566d762017-03-30 09:34:15 -07001417
Adrian Roos11c25582018-02-19 18:06:36 +01001418 mService.mPolicy.getNonDecorInsetsLw(displayInfo.rotation, dw, dh,
1419 displayInfo.displayCutout, mTmpRect);
Bryce Lee7566d762017-03-30 09:34:15 -07001420 final int leftInset = mTmpRect.left;
1421 final int topInset = mTmpRect.top;
1422 // appBounds at the root level should mirror the app screen size.
Wale Ogunwale822e5122017-07-26 06:02:24 -07001423 config.windowConfiguration.setAppBounds(leftInset /* left */, topInset /* top */,
1424 leftInset + displayInfo.appWidth /* right */,
1425 topInset + displayInfo.appHeight /* bottom */);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001426 final boolean rotated = (displayInfo.rotation == Surface.ROTATION_90
1427 || displayInfo.rotation == Surface.ROTATION_270);
1428
1429 computeSizeRangesAndScreenLayout(displayInfo, mDisplayId, rotated, config.uiMode, dw, dh,
Adrian Roos11c25582018-02-19 18:06:36 +01001430 density, config);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001431
1432 config.screenLayout = (config.screenLayout & ~Configuration.SCREENLAYOUT_ROUND_MASK)
1433 | ((displayInfo.flags & Display.FLAG_ROUND) != 0
1434 ? Configuration.SCREENLAYOUT_ROUND_YES
1435 : Configuration.SCREENLAYOUT_ROUND_NO);
1436
1437 config.compatScreenWidthDp = (int)(config.screenWidthDp / mCompatibleScreenScale);
1438 config.compatScreenHeightDp = (int)(config.screenHeightDp / mCompatibleScreenScale);
1439 config.compatSmallestScreenWidthDp = computeCompatSmallestWidth(rotated, config.uiMode, dw,
1440 dh, mDisplayId);
1441 config.densityDpi = displayInfo.logicalDensityDpi;
1442
1443 config.colorMode =
Chia-I Wuf739bad2018-05-09 12:36:57 -07001444 ((displayInfo.isHdr() && mService.hasHdrSupport())
Andrii Kulian06d07d62017-03-14 11:11:47 -07001445 ? Configuration.COLOR_MODE_HDR_YES
1446 : Configuration.COLOR_MODE_HDR_NO)
Romain Guye89d0bb2017-06-20 12:23:42 -07001447 | (displayInfo.isWideColorGamut() && mService.hasWideColorGamutSupport()
Andrii Kulian06d07d62017-03-14 11:11:47 -07001448 ? Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_YES
1449 : Configuration.COLOR_MODE_WIDE_COLOR_GAMUT_NO);
1450
1451 // Update the configuration based on available input devices, lid switch,
1452 // and platform configuration.
1453 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1454 config.keyboard = Configuration.KEYBOARD_NOKEYS;
1455 config.navigation = Configuration.NAVIGATION_NONAV;
1456
1457 int keyboardPresence = 0;
1458 int navigationPresence = 0;
1459 final InputDevice[] devices = mService.mInputManager.getInputDevices();
1460 final int len = devices != null ? devices.length : 0;
1461 for (int i = 0; i < len; i++) {
1462 InputDevice device = devices[i];
1463 if (!device.isVirtual()) {
1464 final int sources = device.getSources();
1465 final int presenceFlag = device.isExternal() ?
1466 WindowManagerPolicy.PRESENCE_EXTERNAL :
1467 WindowManagerPolicy.PRESENCE_INTERNAL;
1468
1469 // TODO(multi-display): Configure on per-display basis.
1470 if (mService.mIsTouchDevice) {
1471 if ((sources & InputDevice.SOURCE_TOUCHSCREEN) ==
1472 InputDevice.SOURCE_TOUCHSCREEN) {
1473 config.touchscreen = Configuration.TOUCHSCREEN_FINGER;
1474 }
1475 } else {
1476 config.touchscreen = Configuration.TOUCHSCREEN_NOTOUCH;
1477 }
1478
1479 if ((sources & InputDevice.SOURCE_TRACKBALL) == InputDevice.SOURCE_TRACKBALL) {
1480 config.navigation = Configuration.NAVIGATION_TRACKBALL;
1481 navigationPresence |= presenceFlag;
1482 } else if ((sources & InputDevice.SOURCE_DPAD) == InputDevice.SOURCE_DPAD
1483 && config.navigation == Configuration.NAVIGATION_NONAV) {
1484 config.navigation = Configuration.NAVIGATION_DPAD;
1485 navigationPresence |= presenceFlag;
1486 }
1487
1488 if (device.getKeyboardType() == InputDevice.KEYBOARD_TYPE_ALPHABETIC) {
1489 config.keyboard = Configuration.KEYBOARD_QWERTY;
1490 keyboardPresence |= presenceFlag;
1491 }
1492 }
1493 }
1494
1495 if (config.navigation == Configuration.NAVIGATION_NONAV && mService.mHasPermanentDpad) {
1496 config.navigation = Configuration.NAVIGATION_DPAD;
1497 navigationPresence |= WindowManagerPolicy.PRESENCE_INTERNAL;
1498 }
1499
1500 // Determine whether a hard keyboard is available and enabled.
1501 // TODO(multi-display): Should the hardware keyboard be tied to a display or to a device?
1502 boolean hardKeyboardAvailable = config.keyboard != Configuration.KEYBOARD_NOKEYS;
1503 if (hardKeyboardAvailable != mService.mHardKeyboardAvailable) {
1504 mService.mHardKeyboardAvailable = hardKeyboardAvailable;
1505 mService.mH.removeMessages(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1506 mService.mH.sendEmptyMessage(WindowManagerService.H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
1507 }
1508
1509 // Let the policy update hidden states.
1510 config.keyboardHidden = Configuration.KEYBOARDHIDDEN_NO;
1511 config.hardKeyboardHidden = Configuration.HARDKEYBOARDHIDDEN_NO;
1512 config.navigationHidden = Configuration.NAVIGATIONHIDDEN_NO;
1513 mService.mPolicy.adjustConfigurationLw(config, keyboardPresence, navigationPresence);
1514 }
1515
1516 private int computeCompatSmallestWidth(boolean rotated, int uiMode, int dw, int dh,
1517 int displayId) {
1518 mTmpDisplayMetrics.setTo(mDisplayMetrics);
1519 final DisplayMetrics tmpDm = mTmpDisplayMetrics;
1520 final int unrotDw, unrotDh;
1521 if (rotated) {
1522 unrotDw = dh;
1523 unrotDh = dw;
1524 } else {
1525 unrotDw = dw;
1526 unrotDh = dh;
1527 }
1528 int sw = reduceCompatConfigWidthSize(0, Surface.ROTATION_0, uiMode, tmpDm, unrotDw, unrotDh,
1529 displayId);
1530 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_90, uiMode, tmpDm, unrotDh, unrotDw,
1531 displayId);
1532 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_180, uiMode, tmpDm, unrotDw, unrotDh,
1533 displayId);
1534 sw = reduceCompatConfigWidthSize(sw, Surface.ROTATION_270, uiMode, tmpDm, unrotDh, unrotDw,
1535 displayId);
1536 return sw;
1537 }
1538
1539 private int reduceCompatConfigWidthSize(int curSize, int rotation, int uiMode,
1540 DisplayMetrics dm, int dw, int dh, int displayId) {
1541 dm.noncompatWidthPixels = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001542 displayId, mDisplayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001543 dm.noncompatHeightPixels = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation,
Adrian Roos11c25582018-02-19 18:06:36 +01001544 uiMode, displayId, mDisplayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001545 float scale = CompatibilityInfo.computeCompatibleScaling(dm, null);
1546 int size = (int)(((dm.noncompatWidthPixels / scale) / dm.density) + .5f);
1547 if (curSize == 0 || size < curSize) {
1548 curSize = size;
1549 }
1550 return curSize;
1551 }
1552
1553 private void computeSizeRangesAndScreenLayout(DisplayInfo displayInfo, int displayId,
1554 boolean rotated, int uiMode, int dw, int dh, float density, Configuration outConfig) {
1555
1556 // We need to determine the smallest width that will occur under normal
1557 // operation. To this, start with the base screen size and compute the
1558 // width under the different possible rotations. We need to un-rotate
1559 // the current screen dimensions before doing this.
1560 int unrotDw, unrotDh;
1561 if (rotated) {
1562 unrotDw = dh;
1563 unrotDh = dw;
1564 } else {
1565 unrotDw = dw;
1566 unrotDh = dh;
1567 }
1568 displayInfo.smallestNominalAppWidth = 1<<30;
1569 displayInfo.smallestNominalAppHeight = 1<<30;
1570 displayInfo.largestNominalAppWidth = 0;
1571 displayInfo.largestNominalAppHeight = 0;
1572 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_0, uiMode, unrotDw,
1573 unrotDh);
1574 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_90, uiMode, unrotDh,
1575 unrotDw);
1576 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_180, uiMode, unrotDw,
1577 unrotDh);
1578 adjustDisplaySizeRanges(displayInfo, displayId, Surface.ROTATION_270, uiMode, unrotDh,
1579 unrotDw);
1580 int sl = Configuration.resetScreenLayout(outConfig.screenLayout);
1581 sl = reduceConfigLayout(sl, Surface.ROTATION_0, density, unrotDw, unrotDh, uiMode,
1582 displayId);
1583 sl = reduceConfigLayout(sl, Surface.ROTATION_90, density, unrotDh, unrotDw, uiMode,
1584 displayId);
1585 sl = reduceConfigLayout(sl, Surface.ROTATION_180, density, unrotDw, unrotDh, uiMode,
1586 displayId);
1587 sl = reduceConfigLayout(sl, Surface.ROTATION_270, density, unrotDh, unrotDw, uiMode,
1588 displayId);
1589 outConfig.smallestScreenWidthDp = (int)(displayInfo.smallestNominalAppWidth / density);
1590 outConfig.screenLayout = sl;
1591 }
1592
1593 private int reduceConfigLayout(int curLayout, int rotation, float density, int dw, int dh,
1594 int uiMode, int displayId) {
1595 // Get the app screen size at this rotation.
Adrian Roos11c25582018-02-19 18:06:36 +01001596 int w = mService.mPolicy.getNonDecorDisplayWidth(dw, dh, rotation, uiMode, displayId,
1597 mDisplayInfo.displayCutout);
1598 int h = mService.mPolicy.getNonDecorDisplayHeight(dw, dh, rotation, uiMode, displayId,
1599 mDisplayInfo.displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001600
1601 // Compute the screen layout size class for this rotation.
1602 int longSize = w;
1603 int shortSize = h;
1604 if (longSize < shortSize) {
1605 int tmp = longSize;
1606 longSize = shortSize;
1607 shortSize = tmp;
1608 }
1609 longSize = (int)(longSize/density);
1610 shortSize = (int)(shortSize/density);
1611 return Configuration.reduceScreenLayout(curLayout, longSize, shortSize);
1612 }
1613
1614 private void adjustDisplaySizeRanges(DisplayInfo displayInfo, int displayId, int rotation,
1615 int uiMode, int dw, int dh) {
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01001616 final DisplayCutout displayCutout = calculateDisplayCutoutForRotation(
1617 rotation).getDisplayCutout();
Andrii Kulian06d07d62017-03-14 11:11:47 -07001618 final int width = mService.mPolicy.getConfigDisplayWidth(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001619 displayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001620 if (width < displayInfo.smallestNominalAppWidth) {
1621 displayInfo.smallestNominalAppWidth = width;
1622 }
1623 if (width > displayInfo.largestNominalAppWidth) {
1624 displayInfo.largestNominalAppWidth = width;
1625 }
1626 final int height = mService.mPolicy.getConfigDisplayHeight(dw, dh, rotation, uiMode,
Adrian Roos11c25582018-02-19 18:06:36 +01001627 displayId, displayCutout);
Andrii Kulian06d07d62017-03-14 11:11:47 -07001628 if (height < displayInfo.smallestNominalAppHeight) {
1629 displayInfo.smallestNominalAppHeight = height;
1630 }
1631 if (height > displayInfo.largestNominalAppHeight) {
1632 displayInfo.largestNominalAppHeight = height;
1633 }
1634 }
1635
Riddle Hsua4d6fa22018-08-11 00:50:39 +08001636 /**
1637 * Apps that use the compact menu panel (as controlled by the panelMenuIsCompact
1638 * theme attribute) on devices that feature a physical options menu key attempt to position
1639 * their menu panel window along the edge of the screen nearest the physical menu key.
1640 * This lowers the travel distance between invoking the menu panel and selecting
1641 * a menu option.
1642 *
1643 * This method helps control where that menu is placed. Its current implementation makes
1644 * assumptions about the menu key and its relationship to the screen based on whether
1645 * the device's natural orientation is portrait (width < height) or landscape.
1646 *
1647 * The menu key is assumed to be located along the bottom edge of natural-portrait
1648 * devices and along the right edge of natural-landscape devices. If these assumptions
1649 * do not hold for the target device, this method should be changed to reflect that.
1650 *
1651 * @return A {@link Gravity} value for placing the options menu window.
1652 */
1653 int getPreferredOptionsPanelGravity() {
1654 final int rotation = getRotation();
1655 if (mInitialDisplayWidth < mInitialDisplayHeight) {
1656 // On devices with a natural orientation of portrait.
1657 switch (rotation) {
1658 default:
1659 case Surface.ROTATION_0:
1660 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1661 case Surface.ROTATION_90:
1662 return Gravity.RIGHT | Gravity.BOTTOM;
1663 case Surface.ROTATION_180:
1664 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1665 case Surface.ROTATION_270:
1666 return Gravity.START | Gravity.BOTTOM;
1667 }
1668 }
1669
1670 // On devices with a natural orientation of landscape.
1671 switch (rotation) {
1672 default:
1673 case Surface.ROTATION_0:
1674 return Gravity.RIGHT | Gravity.BOTTOM;
1675 case Surface.ROTATION_90:
1676 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1677 case Surface.ROTATION_180:
1678 return Gravity.START | Gravity.BOTTOM;
1679 case Surface.ROTATION_270:
1680 return Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
1681 }
1682 }
1683
Jorim Jaggi61f39a72015-10-29 16:54:18 +01001684 DockedStackDividerController getDockedDividerController() {
1685 return mDividerControllerLocked;
1686 }
1687
Winson Chung655332c2016-10-31 13:14:28 -07001688 PinnedStackController getPinnedStackController() {
1689 return mPinnedStackControllerLocked;
1690 }
1691
Jeff Browna506a6e2013-06-04 00:02:38 -07001692 /**
1693 * Returns true if the specified UID has access to this display.
1694 */
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001695 boolean hasAccess(int uid) {
Jeff Browna506a6e2013-06-04 00:02:38 -07001696 return mDisplay.hasAccess(uid);
1697 }
1698
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001699 boolean isPrivate() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07001700 return (mDisplay.getFlags() & FLAG_PRIVATE) != 0;
keunyounga446bf02013-06-21 19:07:57 -07001701 }
1702
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001703 TaskStack getHomeStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07001704 return mTaskStackContainers.getHomeStack();
Craig Mautnerd5d5d0f2013-04-03 15:08:21 -07001705 }
1706
Wale Ogunwale61911492017-10-11 08:50:50 -07001707 /**
1708 * @return The primary split-screen stack, but only if it is visible, and {@code null} otherwise.
1709 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001710 TaskStack getSplitScreenPrimaryStack() {
1711 TaskStack stack = mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001712 return (stack != null && stack.isVisible()) ? stack : null;
1713 }
1714
Robert Carr9785cf32018-04-25 15:06:07 -07001715 boolean hasSplitScreenPrimaryStack() {
1716 return getSplitScreenPrimaryStack() != null;
1717 }
1718
Wale Ogunwale61911492017-10-11 08:50:50 -07001719 /**
Matthew Ng64e77cf2017-10-31 14:01:31 -07001720 * Like {@link #getSplitScreenPrimaryStack}, but also returns the stack if it's currently
Wale Ogunwale61911492017-10-11 08:50:50 -07001721 * not visible.
1722 */
Matthew Ng64e77cf2017-10-31 14:01:31 -07001723 TaskStack getSplitScreenPrimaryStackIgnoringVisibility() {
1724 return mTaskStackContainers.getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07001725 }
1726
1727 TaskStack getPinnedStack() {
1728 return mTaskStackContainers.getPinnedStack();
1729 }
1730
1731 private boolean hasPinnedStack() {
1732 return mTaskStackContainers.getPinnedStack() != null;
Chong Zhangd9d35bd2016-08-04 17:55:21 -07001733 }
1734
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001735 /**
1736 * Returns the topmost stack on the display that is compatible with the input windowing mode.
1737 * Null is no compatible stack on the display.
1738 */
Wale Ogunwale30e441d2017-11-09 08:28:45 -08001739 TaskStack getTopStackInWindowingMode(int windowingMode) {
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001740 return getStack(windowingMode, ACTIVITY_TYPE_UNDEFINED);
1741 }
1742
1743 /**
1744 * Returns the topmost stack on the display that is compatible with the input windowing mode and
1745 * activity type. Null is no compatible stack on the display.
1746 */
Wale Ogunwale68278562017-09-23 17:13:55 -07001747 TaskStack getStack(int windowingMode, int activityType) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001748 return mTaskStackContainers.getStack(windowingMode, activityType);
Wale Ogunwaleb62139d2017-09-20 15:37:35 -07001749 }
1750
Bryce Lee48f4b572017-04-10 10:54:15 -07001751 @VisibleForTesting
Kazuki Takise148d00a2018-05-31 15:32:19 +09001752 WindowList<TaskStack> getStacks() {
1753 return mTaskStackContainers.mChildren;
1754 }
1755
1756 @VisibleForTesting
Wale Ogunwale61911492017-10-11 08:50:50 -07001757 TaskStack getTopStack() {
1758 return mTaskStackContainers.getTopStack();
Bryce Lee48f4b572017-04-10 10:54:15 -07001759 }
1760
Winson Chunge2d72172018-01-25 17:46:20 +00001761 ArrayList<Task> getVisibleTasks() {
1762 return mTaskStackContainers.getVisibleTasks();
1763 }
1764
Wale Ogunwale61911492017-10-11 08:50:50 -07001765 void onStackWindowingModeChanged(TaskStack stack) {
1766 mTaskStackContainers.onStackWindowingModeChanged(stack);
Bryce Lee48f4b572017-04-10 10:54:15 -07001767 }
1768
Andrii Kulian441e4492016-09-29 15:25:00 -07001769 @Override
Wale Ogunwale98d62312017-07-12 09:24:56 -07001770 public void onConfigurationChanged(Configuration newParentConfig) {
Andrii Kulian441e4492016-09-29 15:25:00 -07001771 super.onConfigurationChanged(newParentConfig);
1772
Andrii Kulian3a507b52016-09-19 18:14:12 -07001773 // The display size information is heavily dependent on the resources in the current
1774 // configuration, so we need to reconfigure it every time the configuration changes.
1775 // See {@link PhoneWindowManager#setInitialDisplaySize}...sigh...
1776 mService.reconfigureDisplayLocked(this);
1777
Bryce Leef3c6a472017-11-14 14:53:06 -08001778 final DockedStackDividerController dividerController = getDockedDividerController();
1779
1780 if (dividerController != null) {
1781 getDockedDividerController().onConfigurationChanged();
1782 }
1783
1784 final PinnedStackController pinnedStackController = getPinnedStackController();
1785
1786 if (pinnedStackController != null) {
1787 getPinnedStackController().onConfigurationChanged();
1788 }
Andrii Kulian441e4492016-09-29 15:25:00 -07001789 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001790
Andrii Kulian441e4492016-09-29 15:25:00 -07001791 /**
1792 * Callback used to trigger bounds update after configuration change and get ids of stacks whose
1793 * bounds were updated.
1794 */
Bryce Leeaea60d22018-01-31 14:42:15 -08001795 void updateStackBoundsAfterConfigChange(@NonNull List<TaskStack> changedStackList) {
Wale Ogunwale61911492017-10-11 08:50:50 -07001796 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1797 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Andrii Kulian441e4492016-09-29 15:25:00 -07001798 if (stack.updateBoundsAfterConfigChange()) {
Bryce Leeaea60d22018-01-31 14:42:15 -08001799 changedStackList.add(stack);
Andrii Kulian3a507b52016-09-19 18:14:12 -07001800 }
1801 }
Winson Chung32c566f2017-04-11 18:31:21 -07001802
1803 // If there was no pinned stack, we still need to notify the controller of the display info
1804 // update as a result of the config change. We do this here to consolidate the flow between
1805 // changes when there is and is not a stack.
Wale Ogunwale61911492017-10-11 08:50:50 -07001806 if (!hasPinnedStack()) {
Winson Chung32c566f2017-04-11 18:31:21 -07001807 mPinnedStackControllerLocked.onDisplayInfoChanged();
1808 }
Andrii Kulian3a507b52016-09-19 18:14:12 -07001809 }
1810
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001811 @Override
1812 boolean fillsParent() {
1813 return true;
1814 }
1815
1816 @Override
1817 boolean isVisible() {
1818 return true;
1819 }
1820
1821 @Override
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001822 void onAppTransitionDone() {
Wale Ogunwale10124582016-09-15 20:25:50 -07001823 super.onAppTransitionDone();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001824 mService.mWindowsChanged = true;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07001825 }
1826
Robert Carr9785cf32018-04-25 15:06:07 -07001827 /**
1828 * In split-screen mode we process the IME containers above the docked divider
1829 * rather than directly above their target.
1830 */
1831 private boolean skipTraverseChild(WindowContainer child) {
1832 if (child == mImeWindowsContainers && mService.mInputMethodTarget != null
1833 && !hasSplitScreenPrimaryStack()) {
1834 return true;
1835 }
1836 return false;
1837 }
1838
Wale Ogunwaleba51ca22016-09-23 06:06:54 -07001839 @Override
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001840 boolean forAllWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1841 // Special handling so we can process IME windows with #forAllImeWindows above their IME
1842 // target, or here in order if there isn't an IME target.
1843 if (traverseTopToBottom) {
1844 for (int i = mChildren.size() - 1; i >= 0; --i) {
1845 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001846 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001847 continue;
1848 }
Robert Carr9785cf32018-04-25 15:06:07 -07001849
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001850 if (child.forAllWindows(callback, traverseTopToBottom)) {
1851 return true;
1852 }
1853 }
1854 } else {
1855 final int count = mChildren.size();
1856 for (int i = 0; i < count; i++) {
1857 final DisplayChildWindowContainer child = mChildren.get(i);
Robert Carr9785cf32018-04-25 15:06:07 -07001858 if (skipTraverseChild(child)) {
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001859 continue;
1860 }
Robert Carr9785cf32018-04-25 15:06:07 -07001861
Wale Ogunwale3c1170d2016-12-02 14:44:52 -08001862 if (child.forAllWindows(callback, traverseTopToBottom)) {
1863 return true;
1864 }
1865 }
1866 }
1867 return false;
1868 }
1869
1870 boolean forAllImeWindows(ToBooleanFunction<WindowState> callback, boolean traverseTopToBottom) {
1871 return mImeWindowsContainers.forAllWindows(callback, traverseTopToBottom);
1872 }
1873
Wale Ogunwale399c8692017-05-08 14:22:42 -07001874 @Override
1875 int getOrientation() {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001876 final WindowManagerPolicy policy = mService.mPolicy;
1877
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001878 if (mService.mDisplayFrozen) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08001879 if (mLastWindowForcedOrientation != SCREEN_ORIENTATION_UNSPECIFIED) {
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001880 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1881 + " is frozen, return " + mLastWindowForcedOrientation);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001882 // If the display is frozen, some activities may be in the middle of restarting, and
1883 // thus have removed their old window. If the window has the flag to hide the lock
1884 // screen, then the lock screen can re-appear and inflict its own orientation on us.
1885 // Keep the orientation stable until this all settles down.
Andrii Kulian8ee72852017-03-10 10:36:45 -08001886 return mLastWindowForcedOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001887 } else if (policy.isKeyguardLocked()) {
1888 // Use the last orientation the while the display is frozen with the keyguard
1889 // locked. This could be the keyguard forced orientation or from a SHOW_WHEN_LOCKED
1890 // window. We don't want to check the show when locked window directly though as
1891 // things aren't stable while the display is frozen, for example the window could be
1892 // momentarily unavailable due to activity relaunch.
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07001893 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, "Display id=" + mDisplayId
1894 + " is frozen while keyguard locked, return " + mLastOrientation);
Andrii Kulian8ee72852017-03-10 10:36:45 -08001895 return mLastOrientation;
Wale Ogunwale51362492016-09-08 17:49:17 -07001896 }
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001897 } else {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00001898 final int orientation = mAboveAppWindowsContainers.getOrientation();
1899 if (orientation != SCREEN_ORIENTATION_UNSET) {
1900 return orientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07001901 }
Wale Ogunwale51362492016-09-08 17:49:17 -07001902 }
1903
Wale Ogunwale399c8692017-05-08 14:22:42 -07001904 // Top system windows are not requesting an orientation. Start searching from apps.
1905 return mTaskStackContainers.getOrientation();
Wale Ogunwale51362492016-09-08 17:49:17 -07001906 }
1907
Craig Mautner46ac6fa2013-08-01 10:06:34 -07001908 void updateDisplayInfo() {
Andrii Kuliancd097992017-03-23 18:31:59 -07001909 // Check if display metrics changed and update base values if needed.
1910 updateBaseDisplayMetricsIfNeeded();
1911
Craig Mautner722285e2012-09-07 13:55:58 -07001912 mDisplay.getDisplayInfo(mDisplayInfo);
Wale Ogunwale231b06e2015-09-16 12:03:09 -07001913 mDisplay.getMetrics(mDisplayMetrics);
Andrii Kuliancd097992017-03-23 18:31:59 -07001914
Wale Ogunwale61911492017-10-11 08:50:50 -07001915 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
1916 mTaskStackContainers.getChildAt(i).updateDisplayInfo(null);
Craig Mautnerbdc748af2013-12-02 14:08:25 -08001917 }
Craig Mautner722285e2012-09-07 13:55:58 -07001918 }
1919
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001920 void initializeDisplayBaseInfo() {
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001921 final DisplayManagerInternal displayManagerInternal = mService.mDisplayManagerInternal;
1922 if (displayManagerInternal != null) {
1923 // Bootstrap the default logical display from the display manager.
1924 final DisplayInfo newDisplayInfo = displayManagerInternal.getDisplayInfo(mDisplayId);
1925 if (newDisplayInfo != null) {
1926 mDisplayInfo.copyFrom(newDisplayInfo);
1927 }
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001928 }
Wale Ogunwaleb699ce02016-07-18 12:05:30 -07001929
Andrii Kuliancd097992017-03-23 18:31:59 -07001930 updateBaseDisplayMetrics(mDisplayInfo.logicalWidth, mDisplayInfo.logicalHeight,
1931 mDisplayInfo.logicalDensityDpi);
1932 mInitialDisplayWidth = mDisplayInfo.logicalWidth;
1933 mInitialDisplayHeight = mDisplayInfo.logicalHeight;
1934 mInitialDisplayDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01001935 mInitialDisplayCutout = mDisplayInfo.displayCutout;
Wale Ogunwalefd04d8c2015-09-30 10:09:39 -07001936 }
1937
Andrii Kuliancd097992017-03-23 18:31:59 -07001938 /**
1939 * If display metrics changed, overrides are not set and it's not just a rotation - update base
1940 * values.
1941 */
1942 private void updateBaseDisplayMetricsIfNeeded() {
1943 // Get real display metrics without overrides from WM.
1944 mService.mDisplayManagerInternal.getNonOverrideDisplayInfo(mDisplayId, mDisplayInfo);
1945 final int orientation = mDisplayInfo.rotation;
1946 final boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
1947 final int newWidth = rotated ? mDisplayInfo.logicalHeight : mDisplayInfo.logicalWidth;
1948 final int newHeight = rotated ? mDisplayInfo.logicalWidth : mDisplayInfo.logicalHeight;
1949 final int newDensity = mDisplayInfo.logicalDensityDpi;
Adrian Roos1cf585052018-01-03 18:43:27 +01001950 final DisplayCutout newCutout = mDisplayInfo.displayCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07001951
1952 final boolean displayMetricsChanged = mInitialDisplayWidth != newWidth
1953 || mInitialDisplayHeight != newHeight
Adrian Roos1cf585052018-01-03 18:43:27 +01001954 || mInitialDisplayDensity != mDisplayInfo.logicalDensityDpi
1955 || !Objects.equals(mInitialDisplayCutout, newCutout);
Andrii Kuliancd097992017-03-23 18:31:59 -07001956
1957 if (displayMetricsChanged) {
1958 // Check if display size or density is forced.
1959 final boolean isDisplaySizeForced = mBaseDisplayWidth != mInitialDisplayWidth
1960 || mBaseDisplayHeight != mInitialDisplayHeight;
1961 final boolean isDisplayDensityForced = mBaseDisplayDensity != mInitialDisplayDensity;
1962
1963 // If there is an override set for base values - use it, otherwise use new values.
1964 updateBaseDisplayMetrics(isDisplaySizeForced ? mBaseDisplayWidth : newWidth,
1965 isDisplaySizeForced ? mBaseDisplayHeight : newHeight,
1966 isDisplayDensityForced ? mBaseDisplayDensity : newDensity);
1967
1968 // Real display metrics changed, so we should also update initial values.
1969 mInitialDisplayWidth = newWidth;
1970 mInitialDisplayHeight = newHeight;
1971 mInitialDisplayDensity = newDensity;
Adrian Roos1cf585052018-01-03 18:43:27 +01001972 mInitialDisplayCutout = newCutout;
Andrii Kuliancd097992017-03-23 18:31:59 -07001973 mService.reconfigureDisplayLocked(this);
1974 }
1975 }
1976
Bryce Lee27cec322017-03-21 09:41:37 -07001977 /** Sets the maximum width the screen resolution can be */
1978 void setMaxUiWidth(int width) {
1979 if (DEBUG_DISPLAY) {
1980 Slog.v(TAG_WM, "Setting max ui width:" + width + " on display:" + getDisplayId());
1981 }
1982
1983 mMaxUiWidth = width;
1984
1985 // Update existing metrics.
1986 updateBaseDisplayMetrics(mBaseDisplayWidth, mBaseDisplayHeight, mBaseDisplayDensity);
1987 }
1988
1989 /** Update base (override) display metrics. */
1990 void updateBaseDisplayMetrics(int baseWidth, int baseHeight, int baseDensity) {
1991 mBaseDisplayWidth = baseWidth;
1992 mBaseDisplayHeight = baseHeight;
1993 mBaseDisplayDensity = baseDensity;
1994
1995 if (mMaxUiWidth > 0 && mBaseDisplayWidth > mMaxUiWidth) {
1996 mBaseDisplayHeight = (mMaxUiWidth * mBaseDisplayHeight) / mBaseDisplayWidth;
1997 mBaseDisplayDensity = (mMaxUiWidth * mBaseDisplayDensity) / mBaseDisplayWidth;
1998 mBaseDisplayWidth = mMaxUiWidth;
1999
2000 if (DEBUG_DISPLAY) {
2001 Slog.v(TAG_WM, "Applying config restraints:" + mBaseDisplayWidth + "x"
2002 + mBaseDisplayHeight + " at density:" + mBaseDisplayDensity
2003 + " on display:" + getDisplayId());
2004 }
2005 }
2006
2007 mBaseDisplayRect.set(0, 0, mBaseDisplayWidth, mBaseDisplayHeight);
Bryce Leef3c6a472017-11-14 14:53:06 -08002008
2009 updateBounds();
Bryce Lee27cec322017-03-21 09:41:37 -07002010 }
2011
Wale Ogunwaledb506192017-12-08 10:57:32 -08002012 void getStableRect(Rect out) {
2013 out.set(mDisplayFrames.mStable);
Chong Zhangf66db432016-01-13 10:39:51 -08002014 }
2015
Wale Ogunwale61911492017-10-11 08:50:50 -07002016 TaskStack createStack(int stackId, boolean onTop, StackWindowController controller) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002017 if (DEBUG_STACK) Slog.d(TAG_WM, "Create new stackId=" + stackId + " on displayId="
2018 + mDisplayId);
Andrii Kulian839def92016-11-02 10:58:58 -07002019
Wale Ogunwale61911492017-10-11 08:50:50 -07002020 final TaskStack stack = new TaskStack(mService, stackId, controller);
2021 mTaskStackContainers.addStackToDisplay(stack, onTop);
Wale Ogunwale1666e312016-12-16 11:27:18 -08002022 return stack;
Andrii Kulian839def92016-11-02 10:58:58 -07002023 }
2024
Andrii Kulian51c1b672017-04-07 18:39:32 -07002025 void moveStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale1666e312016-12-16 11:27:18 -08002026 final DisplayContent prevDc = stack.getDisplayContent();
2027 if (prevDc == null) {
Andrii Kulian839def92016-11-02 10:58:58 -07002028 throw new IllegalStateException("Trying to move stackId=" + stack.mStackId
2029 + " which is not currently attached to any display");
2030 }
Wale Ogunwale1666e312016-12-16 11:27:18 -08002031 if (prevDc.getDisplayId() == mDisplayId) {
Andrii Kulian839def92016-11-02 10:58:58 -07002032 throw new IllegalArgumentException("Trying to move stackId=" + stack.mStackId
2033 + " to its current displayId=" + mDisplayId);
2034 }
2035
Wale Ogunwale61911492017-10-11 08:50:50 -07002036 prevDc.mTaskStackContainers.removeChild(stack);
Andrii Kulian51c1b672017-04-07 18:39:32 -07002037 mTaskStackContainers.addStackToDisplay(stack, onTop);
Craig Mautnerc00204b2013-03-05 15:02:14 -08002038 }
2039
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002040 @Override
2041 protected void addChild(DisplayChildWindowContainer child,
2042 Comparator<DisplayChildWindowContainer> comparator) {
2043 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2044 }
Wale Ogunwale1e60e0c2015-10-28 13:36:10 -07002045
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002046 @Override
2047 protected void addChild(DisplayChildWindowContainer child, int index) {
2048 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
2049 }
2050
2051 @Override
2052 protected void removeChild(DisplayChildWindowContainer child) {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002053 // Only allow removal of direct children from this display if the display is in the process
2054 // of been removed.
2055 if (mRemovingDisplay) {
2056 super.removeChild(child);
2057 return;
2058 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002059 throw new UnsupportedOperationException("See DisplayChildWindowContainer");
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002060 }
2061
Andrii Kuliand2765632016-12-12 22:26:34 -08002062 @Override
2063 void positionChildAt(int position, DisplayChildWindowContainer child, boolean includingParents) {
2064 // Children of the display are statically ordered, so the real intention here is to perform
2065 // the operation on the display and not the static direct children.
2066 getParent().positionChildAt(position, this, includingParents);
2067 }
2068
Riddle Hsu57831b52018-07-27 00:31:48 +08002069 void positionStackAt(int position, TaskStack child, boolean includingParents) {
2070 mTaskStackContainers.positionChildAt(position, child, includingParents);
Winson Chung59a47ded2018-01-25 17:46:06 +00002071 layoutAndAssignWindowLayersIfNeeded();
2072 }
2073
Wale Ogunwalee4a0c572015-06-30 08:40:31 -07002074 int taskIdFromPoint(int x, int y) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002075 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2076 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002077 final int taskId = stack.taskIdFromPoint(x, y);
2078 if (taskId != -1) {
2079 return taskId;
Craig Mautner967212c2013-04-13 21:10:58 -07002080 }
2081 }
Craig Mautnerbdc748af2013-12-02 14:08:25 -08002082 return -1;
Craig Mautnercf910b02013-04-23 11:23:27 -07002083 }
2084
Chong Zhang8e89b312015-09-09 15:09:30 -07002085 /**
Chong Zhangd8ceb852015-11-11 14:53:41 -08002086 * Find the task whose outside touch area (for resizing) (x, y) falls within.
Chong Zhang9184ec62015-09-24 12:32:21 -07002087 * Returns null if the touch doesn't fall into a resizing area.
Chong Zhang8e89b312015-09-09 15:09:30 -07002088 */
Wale Ogunwale15ead902016-09-02 14:30:11 -07002089 Task findTaskForResizePoint(int x, int y) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002090 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002091 mTmpTaskForResizePointSearchResult.reset();
Wale Ogunwale61911492017-10-11 08:50:50 -07002092 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2093 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale3382ab12017-07-27 08:55:03 -07002094 if (!stack.getWindowConfiguration().canResizeTask()) {
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002095 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002096 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002097
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002098 stack.findTaskForResizePoint(x, y, delta, mTmpTaskForResizePointSearchResult);
2099 if (mTmpTaskForResizePointSearchResult.searchDone) {
2100 return mTmpTaskForResizePointSearchResult.taskForResize;
Chong Zhang8e89b312015-09-09 15:09:30 -07002101 }
2102 }
Chong Zhang9184ec62015-09-24 12:32:21 -07002103 return null;
Chong Zhang8e89b312015-09-09 15:09:30 -07002104 }
2105
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002106 void updateTouchExcludeRegion() {
2107 final Task focusedTask = (mFocusedApp != null ? mFocusedApp.getTask() : null);
David Stevensee9e2772017-02-09 16:30:27 -08002108 if (focusedTask == null) {
2109 mTouchExcludeRegion.setEmpty();
2110 } else {
2111 mTouchExcludeRegion.set(mBaseDisplayRect);
2112 final int delta = dipToPixel(RESIZE_HANDLE_WIDTH_IN_DP, mDisplayMetrics);
2113 mTmpRect2.setEmpty();
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002114 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0;
2115 --stackNdx) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002116 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002117 stack.setTouchExcludeRegion(focusedTask, delta, mTouchExcludeRegion,
2118 mDisplayFrames.mContent, mTmpRect2);
David Stevensee9e2772017-02-09 16:30:27 -08002119 }
2120 // If we removed the focused task above, add it back and only leave its
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002121 // outside touch area in the exclusion. TapDetector is not interested in
David Stevensee9e2772017-02-09 16:30:27 -08002122 // any touch inside the focused task itself.
2123 if (!mTmpRect2.isEmpty()) {
2124 mTouchExcludeRegion.op(mTmpRect2, Region.Op.UNION);
2125 }
Chong Zhangd8ceb852015-11-11 14:53:41 -08002126 }
lumark90120a82018-08-15 00:33:03 +08002127 if (mInputMethodWindow != null && mInputMethodWindow.isVisibleLw()) {
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002128 // If the input method is visible and the user is typing, we don't want these touch
2129 // events to be intercepted and used to change focus. This would likely cause a
2130 // disappearance of the input method.
lumark90120a82018-08-15 00:33:03 +08002131 mInputMethodWindow.getTouchableRegion(mTmpRegion);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002132 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
Filip Gruszczynski912d9192015-12-01 16:14:04 -08002133 }
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002134 for (int i = mTapExcludedWindows.size() - 1; i >= 0; i--) {
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002135 final WindowState win = mTapExcludedWindows.get(i);
Filip Gruszczynskiecf67222015-12-11 15:16:36 -08002136 win.getTouchableRegion(mTmpRegion);
2137 mTouchExcludeRegion.op(mTmpRegion, Region.Op.UNION);
2138 }
Andrii Kulian4b6599e2018-01-15 17:24:08 -08002139 for (int i = mTapExcludeProvidingWindows.size() - 1; i >= 0; i--) {
2140 final WindowState win = mTapExcludeProvidingWindows.valueAt(i);
2141 win.amendTapExcludeRegion(mTouchExcludeRegion);
2142 }
Andrii Kulian03c403d2016-11-11 11:14:12 -08002143 // TODO(multi-display): Support docked stacks on secondary displays.
Matthew Ng64e77cf2017-10-31 14:01:31 -07002144 if (mDisplayId == DEFAULT_DISPLAY && getSplitScreenPrimaryStack() != null) {
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002145 mDividerControllerLocked.getTouchRegion(mTmpRect);
Jorim Jaggi7f19cb82016-03-25 19:37:44 -07002146 mTmpRegion.set(mTmpRect);
Jorim Jaggid47e7e12016-03-01 09:57:38 +01002147 mTouchExcludeRegion.op(mTmpRegion, Op.UNION);
2148 }
Craig Mautner1bef3892015-02-17 15:09:47 -08002149 if (mTapDetector != null) {
Chong Zhang2e2c81a2016-07-15 11:28:17 -07002150 mTapDetector.setTouchExcludeRegion(mTouchExcludeRegion);
Craig Mautner1bef3892015-02-17 15:09:47 -08002151 }
Craig Mautner6601b7b2013-04-29 10:29:11 -07002152 }
2153
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002154 @Override
Wale Ogunwalee05f5012016-09-16 16:27:29 -07002155 void switchUser() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002156 super.switchUser();
2157 mService.mWindowsChanged = true;
Craig Mautner858d8a62013-04-23 17:08:34 -07002158 }
2159
Wale Ogunwaleac2561e2016-11-01 15:43:46 -07002160 private void resetAnimationBackgroundAnimator() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002161 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2162 mTaskStackContainers.getChildAt(stackNdx).resetAnimationBackgroundAnimator();
Craig Mautner05d29032013-05-03 13:40:13 -07002163 }
2164 }
2165
Wale Ogunwale10124582016-09-15 20:25:50 -07002166 @Override
2167 void removeIfPossible() {
2168 if (isAnimating()) {
2169 mDeferredRemoval = true;
2170 return;
Craig Mautner2eb15342013-08-07 13:13:35 -07002171 }
Wale Ogunwale10124582016-09-15 20:25:50 -07002172 removeImmediately();
Craig Mautner2eb15342013-08-07 13:13:35 -07002173 }
2174
Wale Ogunwale10124582016-09-15 20:25:50 -07002175 @Override
2176 void removeImmediately() {
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002177 mRemovingDisplay = true;
2178 try {
2179 super.removeImmediately();
2180 if (DEBUG_DISPLAY) Slog.v(TAG_WM, "Removing display=" + this);
Tarandeep Singh7ac8d3a2017-09-05 11:09:39 -07002181 if (mService.canDispatchPointerEvents()) {
2182 if (mTapDetector != null) {
2183 mService.unregisterPointerEventListener(mTapDetector);
2184 }
2185 if (mDisplayId == DEFAULT_DISPLAY && mService.mMousePositionTracker != null) {
2186 mService.unregisterPointerEventListener(mService.mMousePositionTracker);
2187 }
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002188 }
Jorim Jaggia3844032018-01-03 15:54:43 +01002189 mService.mAnimator.removeDisplayLocked(mDisplayId);
Louis Chang207f9112018-07-30 08:58:54 +08002190 mWindowingLayer.release();
2191 mOverlayLayer.release();
Wale Ogunwale601a3f02016-10-17 08:39:39 -07002192 } finally {
2193 mRemovingDisplay = false;
Craig Mautner95da1082014-02-24 17:54:35 -08002194 }
Bryce Leef19cbe22018-02-02 15:09:21 -08002195
Arthur Hung39134b22018-08-14 11:58:28 +08002196 mInputMonitor.onRemoved();
Bryce Leef19cbe22018-02-02 15:09:21 -08002197 mService.onDisplayRemoved(mDisplayId);
Craig Mautner95da1082014-02-24 17:54:35 -08002198 }
2199
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002200 /** Returns true if a removal action is still being deferred. */
Wale Ogunwale10124582016-09-15 20:25:50 -07002201 @Override
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002202 boolean checkCompleteDeferredRemoval() {
Wale Ogunwale10124582016-09-15 20:25:50 -07002203 final boolean stillDeferringRemoval = super.checkCompleteDeferredRemoval();
2204
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002205 if (!stillDeferringRemoval && mDeferredRemoval) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002206 removeImmediately();
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002207 return false;
Craig Mautner95da1082014-02-24 17:54:35 -08002208 }
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07002209 return true;
Craig Mautner95da1082014-02-24 17:54:35 -08002210 }
2211
Andrii Kulian0214ed92017-05-16 13:44:05 -07002212 /** @return 'true' if removal of this display content is deferred due to active animation. */
2213 boolean isRemovalDeferred() {
2214 return mDeferredRemoval;
2215 }
2216
Wale Ogunwale10124582016-09-15 20:25:50 -07002217 boolean animateForIme(float interpolatedValue, float animationTarget,
2218 float dividerAnimationTarget) {
2219 boolean updated = false;
2220
Wale Ogunwale61911492017-10-11 08:50:50 -07002221 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2222 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002223 if (stack == null || !stack.isAdjustedForIme()) {
2224 continue;
2225 }
2226
2227 if (interpolatedValue >= 1f && animationTarget == 0f && dividerAnimationTarget == 0f) {
2228 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2229 updated = true;
2230 } else {
2231 mDividerControllerLocked.mLastAnimationProgress =
2232 mDividerControllerLocked.getInterpolatedAnimationValue(interpolatedValue);
2233 mDividerControllerLocked.mLastDividerProgress =
2234 mDividerControllerLocked.getInterpolatedDividerValue(interpolatedValue);
2235 updated |= stack.updateAdjustForIme(
2236 mDividerControllerLocked.mLastAnimationProgress,
2237 mDividerControllerLocked.mLastDividerProgress,
2238 false /* force */);
2239 }
2240 if (interpolatedValue >= 1f) {
2241 stack.endImeAdjustAnimation();
2242 }
2243 }
2244
2245 return updated;
2246 }
2247
2248 boolean clearImeAdjustAnimation() {
2249 boolean changed = false;
Wale Ogunwale61911492017-10-11 08:50:50 -07002250 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2251 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002252 if (stack != null && stack.isAdjustedForIme()) {
2253 stack.resetAdjustedForIme(true /* adjustBoundsNow */);
2254 changed = true;
2255 }
2256 }
2257 return changed;
2258 }
2259
2260 void beginImeAdjustAnimation() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002261 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2262 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002263 if (stack.isVisible() && stack.isAdjustedForIme()) {
2264 stack.beginImeAdjustAnimation();
2265 }
2266 }
2267 }
2268
2269 void adjustForImeIfNeeded() {
lumark90120a82018-08-15 00:33:03 +08002270 final WindowState imeWin = mInputMethodWindow;
Wale Ogunwale10124582016-09-15 20:25:50 -07002271 final boolean imeVisible = imeWin != null && imeWin.isVisibleLw() && imeWin.isDisplayedLw()
2272 && !mDividerControllerLocked.isImeHideRequested();
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002273 final boolean dockVisible = isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY);
Wale Ogunwale10124582016-09-15 20:25:50 -07002274 final TaskStack imeTargetStack = mService.getImeFocusStackLocked();
2275 final int imeDockSide = (dockVisible && imeTargetStack != null) ?
2276 imeTargetStack.getDockSide() : DOCKED_INVALID;
2277 final boolean imeOnTop = (imeDockSide == DOCKED_TOP);
2278 final boolean imeOnBottom = (imeDockSide == DOCKED_BOTTOM);
2279 final boolean dockMinimized = mDividerControllerLocked.isMinimizedDock();
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002280 final int imeHeight = mDisplayFrames.getInputMethodWindowVisibleHeight();
Wale Ogunwale10124582016-09-15 20:25:50 -07002281 final boolean imeHeightChanged = imeVisible &&
2282 imeHeight != mDividerControllerLocked.getImeHeightAdjustedFor();
2283
2284 // The divider could be adjusted for IME position, or be thinner than usual,
2285 // or both. There are three possible cases:
2286 // - If IME is visible, and focus is on top, divider is not moved for IME but thinner.
2287 // - If IME is visible, and focus is on bottom, divider is moved for IME and thinner.
2288 // - If IME is not visible, divider is not moved and is normal width.
2289
2290 if (imeVisible && dockVisible && (imeOnTop || imeOnBottom) && !dockMinimized) {
Wale Ogunwale61911492017-10-11 08:50:50 -07002291 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2292 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002293 final boolean isDockedOnBottom = stack.getDockSide() == DOCKED_BOTTOM;
Wale Ogunwale68278562017-09-23 17:13:55 -07002294 if (stack.isVisible() && (imeOnBottom || isDockedOnBottom)
2295 && stack.inSplitScreenWindowingMode()) {
Wale Ogunwale10124582016-09-15 20:25:50 -07002296 stack.setAdjustedForIme(imeWin, imeOnBottom && imeHeightChanged);
2297 } else {
2298 stack.resetAdjustedForIme(false);
2299 }
2300 }
2301 mDividerControllerLocked.setAdjustedForIme(
2302 imeOnBottom /*ime*/, true /*divider*/, true /*animate*/, imeWin, imeHeight);
2303 } else {
Wale Ogunwale61911492017-10-11 08:50:50 -07002304 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
2305 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale10124582016-09-15 20:25:50 -07002306 stack.resetAdjustedForIme(!dockVisible);
2307 }
2308 mDividerControllerLocked.setAdjustedForIme(
2309 false /*ime*/, false /*divider*/, dockVisible /*animate*/, imeWin, imeHeight);
2310 }
Winson Chung655332c2016-10-31 13:14:28 -07002311 mPinnedStackControllerLocked.setAdjustedForIme(imeVisible, imeHeight);
Wale Ogunwale10124582016-09-15 20:25:50 -07002312 }
2313
2314 void prepareFreezingTaskBounds() {
Wale Ogunwale61911492017-10-11 08:50:50 -07002315 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2316 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Wale Ogunwale10124582016-09-15 20:25:50 -07002317 stack.prepareFreezingTaskBounds();
2318 }
2319 }
2320
Wale Ogunwale94744212015-09-21 19:01:47 -07002321 void rotateBounds(int oldRotation, int newRotation, Rect bounds) {
Bryce Leef3c6a472017-11-14 14:53:06 -08002322 getBounds(mTmpRect, newRotation);
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002323
2324 // Compute a transform matrix to undo the coordinate space transformation,
2325 // and present the window at the same physical position it previously occupied.
2326 final int deltaRotation = deltaRotation(newRotation, oldRotation);
2327 createRotationMatrix(deltaRotation, mTmpRect.width(), mTmpRect.height(), mTmpMatrix);
2328
2329 mTmpRectF.set(bounds);
2330 mTmpMatrix.mapRect(mTmpRectF);
2331 mTmpRectF.round(bounds);
Wale Ogunwale94744212015-09-21 19:01:47 -07002332 }
2333
Wale Ogunwale4a02d812015-02-12 23:01:38 -08002334 static int deltaRotation(int oldRotation, int newRotation) {
2335 int delta = newRotation - oldRotation;
2336 if (delta < 0) delta += 4;
2337 return delta;
2338 }
2339
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002340 private static void createRotationMatrix(int rotation, float displayWidth, float displayHeight,
Andrii Kulian4dfb9c42016-10-11 20:06:27 -07002341 Matrix outMatrix) {
2342 // For rotations without Z-ordering we don't need the target rectangle's position.
2343 createRotationMatrix(rotation, 0 /* rectLeft */, 0 /* rectTop */, displayWidth,
2344 displayHeight, outMatrix);
2345 }
2346
2347 static void createRotationMatrix(int rotation, float rectLeft, float rectTop,
2348 float displayWidth, float displayHeight, Matrix outMatrix) {
2349 switch (rotation) {
2350 case ROTATION_0:
2351 outMatrix.reset();
2352 break;
2353 case ROTATION_270:
2354 outMatrix.setRotate(270, 0, 0);
2355 outMatrix.postTranslate(0, displayHeight);
2356 outMatrix.postTranslate(rectTop, 0);
2357 break;
2358 case ROTATION_180:
2359 outMatrix.reset();
2360 break;
2361 case ROTATION_90:
2362 outMatrix.setRotate(90, 0, 0);
2363 outMatrix.postTranslate(displayWidth, 0);
2364 outMatrix.postTranslate(-rectTop, rectLeft);
2365 break;
2366 }
2367 }
2368
Wale Ogunwale0d5609b2017-09-13 05:55:07 -07002369 @CallSuper
2370 @Override
Adrian Roos4921ccf2017-09-28 16:54:06 +02002371 public void writeToProto(ProtoOutputStream proto, long fieldId, boolean trim) {
Steven Timotiusaf03df62017-07-18 16:56:43 -07002372 final long token = proto.start(fieldId);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002373 super.writeToProto(proto, WINDOW_CONTAINER, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002374 proto.write(ID, mDisplayId);
Wale Ogunwale61911492017-10-11 08:50:50 -07002375 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2376 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002377 stack.writeToProto(proto, STACKS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002378 }
2379 mDividerControllerLocked.writeToProto(proto, DOCKED_STACK_DIVIDER_CONTROLLER);
2380 mPinnedStackControllerLocked.writeToProto(proto, PINNED_STACK_CONTROLLER);
Wale Ogunwale61911492017-10-11 08:50:50 -07002381 for (int i = mAboveAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2382 final WindowToken windowToken = mAboveAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002383 windowToken.writeToProto(proto, ABOVE_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002384 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002385 for (int i = mBelowAppWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2386 final WindowToken windowToken = mBelowAppWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002387 windowToken.writeToProto(proto, BELOW_APP_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002388 }
Wale Ogunwale61911492017-10-11 08:50:50 -07002389 for (int i = mImeWindowsContainers.getChildCount() - 1; i >= 0; --i) {
2390 final WindowToken windowToken = mImeWindowsContainers.getChildAt(i);
Adrian Roos4921ccf2017-09-28 16:54:06 +02002391 windowToken.writeToProto(proto, IME_WINDOWS, trim);
Steven Timotiusaf03df62017-07-18 16:56:43 -07002392 }
2393 proto.write(DPI, mBaseDisplayDensity);
2394 mDisplayInfo.writeToProto(proto, DISPLAY_INFO);
Steven Timotiusf2d68892017-08-28 17:00:01 -07002395 proto.write(ROTATION, mRotation);
2396 final ScreenRotationAnimation screenRotationAnimation =
2397 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
2398 if (screenRotationAnimation != null) {
2399 screenRotationAnimation.writeToProto(proto, SCREEN_ROTATION_ANIMATION);
2400 }
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002401 mDisplayFrames.writeToProto(proto, DISPLAY_FRAMES);
Louis Chang7501e332018-08-20 13:08:39 +08002402 proto.write(SURFACE_SIZE, mSurfaceSize);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002403 if (mFocusedApp != null) {
2404 mFocusedApp.writeNameToProto(proto, FOCUSED_APP);
2405 }
Steven Timotiusaf03df62017-07-18 16:56:43 -07002406 proto.end(token);
2407 }
2408
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002409 @Override
2410 public void dump(PrintWriter pw, String prefix, boolean dumpAll) {
2411 super.dump(pw, prefix, dumpAll);
Craig Mautnera91f9e22012-09-14 16:22:08 -07002412 pw.print(prefix); pw.print("Display: mDisplayId="); pw.println(mDisplayId);
2413 final String subPrefix = " " + prefix;
2414 pw.print(subPrefix); pw.print("init="); pw.print(mInitialDisplayWidth); pw.print("x");
2415 pw.print(mInitialDisplayHeight); pw.print(" "); pw.print(mInitialDisplayDensity);
2416 pw.print("dpi");
2417 if (mInitialDisplayWidth != mBaseDisplayWidth
2418 || mInitialDisplayHeight != mBaseDisplayHeight
2419 || mInitialDisplayDensity != mBaseDisplayDensity) {
2420 pw.print(" base=");
2421 pw.print(mBaseDisplayWidth); pw.print("x"); pw.print(mBaseDisplayHeight);
2422 pw.print(" "); pw.print(mBaseDisplayDensity); pw.print("dpi");
2423 }
Jeff Brownd46747a2015-04-15 19:02:36 -07002424 if (mDisplayScalingDisabled) {
2425 pw.println(" noscale");
2426 }
Craig Mautnera91f9e22012-09-14 16:22:08 -07002427 pw.print(" cur=");
2428 pw.print(mDisplayInfo.logicalWidth);
2429 pw.print("x"); pw.print(mDisplayInfo.logicalHeight);
2430 pw.print(" app=");
2431 pw.print(mDisplayInfo.appWidth);
2432 pw.print("x"); pw.print(mDisplayInfo.appHeight);
2433 pw.print(" rng="); pw.print(mDisplayInfo.smallestNominalAppWidth);
2434 pw.print("x"); pw.print(mDisplayInfo.smallestNominalAppHeight);
2435 pw.print("-"); pw.print(mDisplayInfo.largestNominalAppWidth);
2436 pw.print("x"); pw.println(mDisplayInfo.largestNominalAppHeight);
David Stevensf833ba92017-03-16 19:00:20 -07002437 pw.print(subPrefix + "deferred=" + mDeferredRemoval
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002438 + " mLayoutNeeded=" + mLayoutNeeded);
David Stevensf833ba92017-03-16 19:00:20 -07002439 pw.println(" mTouchExcludeRegion=" + mTouchExcludeRegion);
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002440
Craig Mautnerdc548482014-02-05 13:35:24 -08002441 pw.println();
Adrian Roos5251b1d2018-03-23 18:57:43 +01002442 pw.print(prefix); pw.print("mLayoutSeq="); pw.println(mLayoutSeq);
Riddle Hsu654a6f92018-07-13 22:59:36 +08002443 pw.print(prefix);
2444 pw.print("mDeferredRotationPauseCount="); pw.println(mDeferredRotationPauseCount);
Adrian Roos5251b1d2018-03-23 18:57:43 +01002445
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002446 pw.print(" mCurrentFocus="); pw.println(mCurrentFocus);
2447 if (mLastFocus != mCurrentFocus) {
2448 pw.print(" mLastFocus="); pw.println(mLastFocus);
2449 }
2450 if (mLosingFocus.size() > 0) {
2451 pw.println();
2452 pw.println(" Windows losing focus:");
2453 for (int i = mLosingFocus.size() - 1; i >= 0; i--) {
2454 final WindowState w = mLosingFocus.get(i);
2455 pw.print(" Losing #"); pw.print(i); pw.print(' ');
2456 pw.print(w);
2457 if (dumpAll) {
2458 pw.println(":");
2459 w.dump(pw, " ", true);
2460 } else {
2461 pw.println();
2462 }
2463 }
2464 }
2465 pw.print(" mFocusedApp="); pw.println(mFocusedApp);
2466
Adrian Roos5251b1d2018-03-23 18:57:43 +01002467 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002468 pw.println(prefix + "Application tokens in top down Z order:");
Wale Ogunwale61911492017-10-11 08:50:50 -07002469 for (int stackNdx = mTaskStackContainers.getChildCount() - 1; stackNdx >= 0; --stackNdx) {
2470 final TaskStack stack = mTaskStackContainers.getChildAt(stackNdx);
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002471 stack.dump(pw, prefix + " ", dumpAll);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002472 }
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002473
Craig Mautnerdc548482014-02-05 13:35:24 -08002474 pw.println();
2475 if (!mExitingTokens.isEmpty()) {
Craig Mautnerde4ef022013-04-07 19:01:33 -07002476 pw.println();
2477 pw.println(" Exiting tokens:");
Wale Ogunwaleb429e682016-01-06 12:36:34 -08002478 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002479 final WindowToken token = mExitingTokens.get(i);
Craig Mautnerde4ef022013-04-07 19:01:33 -07002480 pw.print(" Exiting #"); pw.print(i);
2481 pw.print(' '); pw.print(token);
2482 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002483 token.dump(pw, " ", dumpAll);
Craig Mautnerb1fd65c02013-02-05 13:34:57 -08002484 }
Craig Mautnerde4ef022013-04-07 19:01:33 -07002485 }
Robert Carrf59b8dd2017-10-02 18:58:36 -07002486
Jorim Jaggi31f71702016-05-04 16:43:04 -07002487 pw.println();
Wale Ogunwale61911492017-10-11 08:50:50 -07002488
2489 // Dump stack references
2490 final TaskStack homeStack = getHomeStack();
2491 if (homeStack != null) {
2492 pw.println(prefix + "homeStack=" + homeStack.getName());
2493 }
2494 final TaskStack pinnedStack = getPinnedStack();
2495 if (pinnedStack != null) {
2496 pw.println(prefix + "pinnedStack=" + pinnedStack.getName());
2497 }
Matthew Ng64e77cf2017-10-31 14:01:31 -07002498 final TaskStack splitScreenPrimaryStack = getSplitScreenPrimaryStack();
Wale Ogunwale61911492017-10-11 08:50:50 -07002499 if (splitScreenPrimaryStack != null) {
2500 pw.println(prefix + "splitScreenPrimaryStack=" + splitScreenPrimaryStack.getName());
2501 }
2502
2503 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002504 mDividerControllerLocked.dump(prefix, pw);
Winson Chung655332c2016-10-31 13:14:28 -07002505 pw.println();
Jorim Jaggiad5d2842016-11-01 18:22:53 -07002506 mPinnedStackControllerLocked.dump(prefix, pw);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002507
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00002508 pw.println();
2509 mDisplayFrames.dump(prefix, pw);
Arthur Hung95b38a92018-07-20 18:56:12 +08002510 pw.println();
Riddle Hsu5ce4bb32018-07-18 16:11:30 +08002511 mDisplayPolicy.dump(prefix, pw);
2512 pw.println();
Riddle Hsuad256a12018-07-18 16:11:30 +08002513 mDisplayRotation.dump(prefix, pw);
2514 pw.println();
Arthur Hung95b38a92018-07-20 18:56:12 +08002515 mInputMonitor.dump(pw, " ");
Craig Mautner59c00972012-07-30 12:10:24 -07002516 }
Craig Mautnere0a38842013-12-16 16:14:02 -08002517
2518 @Override
2519 public String toString() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002520 return "Display " + mDisplayId + " info=" + mDisplayInfo + " stacks=" + mChildren;
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002521 }
2522
2523 String getName() {
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002524 return "Display " + mDisplayId + " name=\"" + mDisplayInfo.name + "\"";
Craig Mautnere0a38842013-12-16 16:14:02 -08002525 }
Filip Gruszczynski466f3212015-09-21 17:57:57 -07002526
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002527 /** Returns true if the stack in the windowing mode is visible. */
2528 boolean isStackVisible(int windowingMode) {
Wale Ogunwale30e441d2017-11-09 08:28:45 -08002529 final TaskStack stack = getTopStackInWindowingMode(windowingMode);
Wale Ogunwale44f036f2017-09-29 05:09:09 -07002530 return stack != null && stack.isVisible();
Andrii Kulian7cd7c2d2017-01-18 12:14:37 -08002531 }
2532
Wale Ogunwale9adfe572016-09-08 20:43:58 -07002533 /** Find the visible, touch-deliverable window under the given point */
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002534 WindowState getTouchableWinAtPointLocked(float xf, float yf) {
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002535 final int x = (int) xf;
2536 final int y = (int) yf;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002537 final WindowState touchedWin = getWindow(w -> {
2538 final int flags = w.mAttrs.flags;
2539 if (!w.isVisibleLw()) {
2540 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002541 }
2542 if ((flags & FLAG_NOT_TOUCHABLE) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002543 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002544 }
2545
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002546 w.getVisibleBounds(mTmpRect);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002547 if (!mTmpRect.contains(x, y)) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002548 return false;
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002549 }
2550
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002551 w.getTouchableRegion(mTmpRegion);
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002552
2553 final int touchFlags = flags & (FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002554 return mTmpRegion.contains(x, y) || touchFlags == 0;
2555 });
Vladislav Kaznacheev5d6bdeb2016-02-12 17:07:20 -08002556
2557 return touchedWin;
2558 }
Jorim Jaggi6626f542016-08-22 13:08:44 -07002559
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002560 boolean canAddToastWindowForUid(int uid) {
2561 // We allow one toast window per UID being shown at a time.
Svet Ganovdaca8ee2017-01-21 17:40:40 -08002562 // Also if the app is focused adding more than one toast at
2563 // a time for better backwards compatibility.
2564 final WindowState focusedWindowForUid = getWindow(w ->
2565 w.mOwnerUid == uid && w.isFocused());
2566 if (focusedWindowForUid != null) {
2567 return true;
2568 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002569 final WindowState win = getWindow(w ->
2570 w.mAttrs.type == TYPE_TOAST && w.mOwnerUid == uid && !w.mPermanentlyHidden
2571 && !w.mWindowRemovalAllowed);
2572 return win == null;
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002573 }
2574
Wale Ogunwale19e452e2016-10-12 12:36:29 -07002575 void scheduleToastWindowsTimeoutIfNeededLocked(WindowState oldFocus, WindowState newFocus) {
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002576 if (oldFocus == null || (newFocus != null && newFocus.mOwnerUid == oldFocus.mOwnerUid)) {
2577 return;
2578 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002579
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002580 // Used to communicate the old focus to the callback method.
2581 mTmpWindow = oldFocus;
2582
2583 forAllWindows(mScheduleToastTimeout, false /* traverseTopToBottom */);
Svetoslav Ganovaa076532016-08-01 19:16:43 -07002584 }
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002585
2586 WindowState findFocusedWindow() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002587 mTmpWindow = null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002588
Wale Ogunwale1e129a42016-11-21 13:03:47 -08002589 forAllWindows(mFindFocusedWindow, true /* traverseTopToBottom */);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002590
2591 if (mTmpWindow == null) {
2592 if (DEBUG_FOCUS_LIGHT) Slog.v(TAG_WM, "findFocusedWindow: No focusable windows.");
2593 return null;
Wale Ogunwale63d4ecc2016-09-08 18:48:26 -07002594 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002595 return mTmpWindow;
Wale Ogunwaleec731152016-09-08 20:18:57 -07002596 }
2597
Tiger Huang1e5b10a2018-07-30 20:19:51 +08002598
2599 /**
2600 * Update the focused window and make some adjustments if the focus has changed.
2601 *
2602 * @param mode Indicates the situation we are in. Possible modes are:
2603 * {@link WindowManagerService#UPDATE_FOCUS_NORMAL},
2604 * {@link WindowManagerService#UPDATE_FOCUS_PLACING_SURFACES},
2605 * {@link WindowManagerService#UPDATE_FOCUS_WILL_PLACE_SURFACES},
2606 * {@link WindowManagerService#UPDATE_FOCUS_REMOVING_FOCUS}
2607 * @param updateInputWindows Whether to sync the window information to the input module.
2608 * @return {@code true} if the focused window has changed.
2609 */
2610 boolean updateFocusedWindowLocked(int mode, boolean updateInputWindows, boolean focusFound) {
2611 final WindowState newFocus = findFocusedWindow();
2612 if (mCurrentFocus == newFocus) {
2613 return false;
2614 }
2615 mService.mH.obtainMessage(REPORT_FOCUS_CHANGE, this).sendToTarget();
2616 boolean imWindowChanged = false;
2617 // TODO (b/111080190): Multi-Session IME
2618 if (!focusFound) {
2619 final WindowState imWindow = mInputMethodWindow;
2620 if (imWindow != null) {
2621 final WindowState prevTarget = mService.mInputMethodTarget;
2622
2623 final WindowState newTarget = computeImeTarget(true /* updateImeTarget*/);
2624 imWindowChanged = prevTarget != newTarget;
2625
2626 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS
2627 && mode != UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2628 assignWindowLayers(false /* setLayoutNeeded */);
2629 }
2630 }
2631 }
2632
2633 if (imWindowChanged) {
2634 mService.mWindowsChanged = true;
2635 setLayoutNeeded();
2636 }
2637
2638 if (DEBUG_FOCUS_LIGHT || mService.localLOGV) Slog.v(TAG_WM, "Changing focus from "
2639 + mCurrentFocus + " to " + newFocus + " displayId=" + getDisplayId()
2640 + " Callers=" + Debug.getCallers(4));
2641 final WindowState oldFocus = mCurrentFocus;
2642 mCurrentFocus = newFocus;
2643 mLosingFocus.remove(newFocus);
2644
2645 if (newFocus != null) {
2646 mWinAddedSinceNullFocus.clear();
2647 mWinRemovedSinceNullFocus.clear();
2648
2649 if (newFocus.canReceiveKeys()) {
2650 // Displaying a window implicitly causes dispatching to be unpaused.
2651 // This is to protect against bugs if someone pauses dispatching but
2652 // forgets to resume.
2653 newFocus.mToken.paused = false;
2654 }
2655 }
2656
2657 // System UI is only shown on the default display.
2658 int focusChanged = isDefaultDisplay
2659 ? mService.mPolicy.focusChangedLw(oldFocus, newFocus) : 0;
2660
2661 if (imWindowChanged && oldFocus != mInputMethodWindow) {
2662 // Focus of the input method window changed. Perform layout if needed.
2663 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2664 performLayout(true /*initial*/, updateInputWindows);
2665 focusChanged &= ~FINISH_LAYOUT_REDO_LAYOUT;
2666 } else if (mode == UPDATE_FOCUS_WILL_PLACE_SURFACES) {
2667 // Client will do the layout, but we need to assign layers
2668 // for handleNewWindowLocked() below.
2669 assignWindowLayers(false /* setLayoutNeeded */);
2670 }
2671 }
2672
2673 if ((focusChanged & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
2674 // The change in focus caused us to need to do a layout. Okay.
2675 setLayoutNeeded();
2676 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2677 performLayout(true /*initial*/, updateInputWindows);
2678 } else if (mode == UPDATE_FOCUS_REMOVING_FOCUS) {
2679 mService.mRoot.performSurfacePlacement(false);
2680 }
2681 }
2682
2683 if (mode != UPDATE_FOCUS_WILL_ASSIGN_LAYERS) {
2684 // If we defer assigning layers, then the caller is responsible for doing this part.
2685 getInputMonitor().setInputFocusLw(newFocus, updateInputWindows);
2686 }
2687
2688 adjustForImeIfNeeded();
2689
2690 // We may need to schedule some toast windows to be removed. The toasts for an app that
2691 // does not have input focus are removed within a timeout to prevent apps to redress
2692 // other apps' UI.
2693 scheduleToastWindowsTimeoutIfNeededLocked(oldFocus, newFocus);
2694
2695 if (mode == UPDATE_FOCUS_PLACING_SURFACES) {
2696 pendingLayoutChanges |= FINISH_LAYOUT_REDO_ANIM;
2697 }
2698 return true;
2699 }
2700
2701 /**
2702 * Set the new focused app to this display.
2703 *
2704 * @param newFocus the new focused AppWindowToken.
2705 * @return true if the focused app is changed.
2706 */
2707 boolean setFocusedApp(AppWindowToken newFocus) {
2708 if (newFocus != null) {
2709 final DisplayContent appDisplay = newFocus.getDisplayContent();
2710 if (appDisplay != this) {
2711 throw new IllegalStateException(newFocus + " is not on " + getName()
2712 + " but " + ((appDisplay != null) ? appDisplay.getName() : "none"));
2713 }
2714 }
2715 if (mFocusedApp == newFocus) {
2716 return false;
2717 }
2718 mFocusedApp = newFocus;
2719 getInputMonitor().setFocusedAppLw(newFocus);
2720 updateTouchExcludeRegion();
2721 return true;
2722 }
2723
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002724 /** Updates the layer assignment of windows on this display. */
2725 void assignWindowLayers(boolean setLayoutNeeded) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002726 Trace.traceBegin(Trace.TRACE_TAG_WINDOW_MANAGER, "assignWindowLayers");
Robert Carrf59b8dd2017-10-02 18:58:36 -07002727 assignChildLayers(getPendingTransaction());
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002728 if (setLayoutNeeded) {
2729 setLayoutNeeded();
2730 }
Robert Carrb1579c82017-09-05 14:54:47 -07002731
Robert Carrf59b8dd2017-10-02 18:58:36 -07002732 // We accumlate the layer changes in-to "getPendingTransaction()" but we defer
Robert Carrb1579c82017-09-05 14:54:47 -07002733 // the application of this transaction until the animation pass triggers
2734 // prepareSurfaces. This allows us to synchronize Z-ordering changes with
2735 // the hiding and showing of surfaces.
2736 scheduleAnimation();
Jorim Jaggiffe128d2017-11-30 13:54:36 +01002737 Trace.traceEnd(Trace.TRACE_TAG_WINDOW_MANAGER);
Wale Ogunwalec69694a2016-10-18 13:51:15 -07002738 }
2739
Wale Ogunwale1666e312016-12-16 11:27:18 -08002740 // TODO: This should probably be called any time a visual change is made to the hierarchy like
2741 // moving containers or resizing them. Need to investigate the best way to have it automatically
2742 // happen so we don't run into issues with programmers forgetting to do it.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002743 void layoutAndAssignWindowLayersIfNeeded() {
2744 mService.mWindowsChanged = true;
2745 setLayoutNeeded();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002746
2747 if (!mService.updateFocusedWindowLocked(UPDATE_FOCUS_WILL_PLACE_SURFACES,
2748 false /*updateInputWindows*/)) {
2749 assignWindowLayers(false /* setLayoutNeeded */);
2750 }
2751
Arthur Hung95b38a92018-07-20 18:56:12 +08002752 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002753 mService.mWindowPlacerLocked.performSurfacePlacement();
Arthur Hung95b38a92018-07-20 18:56:12 +08002754 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002755 }
2756
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002757 /** Returns true if a leaked surface was destroyed */
2758 boolean destroyLeakedSurfaces() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002759 // Used to indicate that a surface was leaked.
2760 mTmpWindow = null;
2761 forAllWindows(w -> {
2762 final WindowStateAnimator wsa = w.mWinAnimator;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002763 if (wsa.mSurfaceController == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002764 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002765 }
2766 if (!mService.mSessions.contains(wsa.mSession)) {
2767 Slog.w(TAG_WM, "LEAKED SURFACE (session doesn't exist): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002768 + w + " surface=" + wsa.mSurfaceController
2769 + " token=" + w.mToken
2770 + " pid=" + w.mSession.mPid
2771 + " uid=" + w.mSession.mUid);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002772 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002773 mService.mForceRemoves.add(w);
2774 mTmpWindow = w;
Wale Ogunwale89973222017-04-23 18:39:45 -07002775 } else if (w.mAppToken != null && w.mAppToken.isClientHidden()) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002776 Slog.w(TAG_WM, "LEAKED SURFACE (app token hidden): "
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002777 + w + " surface=" + wsa.mSurfaceController
Jorim Jaggie7d2b852017-08-28 17:55:15 +02002778 + " token=" + w.mAppToken);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002779 if (SHOW_TRANSACTIONS) logSurface(w, "LEAK DESTROY", false);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002780 wsa.destroySurface();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002781 mTmpWindow = w;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002782 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002783 }, false /* traverseTopToBottom */);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002784
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002785 return mTmpWindow != null;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002786 }
2787
2788 /**
lumark90120a82018-08-15 00:33:03 +08002789 * Set input method window for the display.
2790 * @param win Set when window added or Null when destroyed.
2791 */
2792 void setInputMethodWindowLocked(WindowState win) {
2793 mInputMethodWindow = win;
lumark70865a82018-09-17 16:56:26 +08002794 // Update display configuration for IME process.
2795 if (mInputMethodWindow != null) {
2796 final int imePid = mInputMethodWindow.mSession.mPid;
2797 mService.mAtmInternal.onImeWindowSetOnDisplay(imePid,
2798 mInputMethodWindow.getDisplayId());
2799 }
lumark90120a82018-08-15 00:33:03 +08002800 computeImeTarget(true /* updateImeTarget */);
2801 }
2802
2803 /**
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002804 * Determine and return the window that should be the IME target.
2805 * @param updateImeTarget If true the system IME target will be updated to match what we found.
2806 * @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 +00002807 */
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002808 WindowState computeImeTarget(boolean updateImeTarget) {
lumark90120a82018-08-15 00:33:03 +08002809 if (mInputMethodWindow == null) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002810 // There isn't an IME so there shouldn't be a target...That was easy!
2811 if (updateImeTarget) {
2812 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from "
2813 + mService.mInputMethodTarget + " to null since mInputMethodWindow is null");
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002814 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002815 }
2816 return null;
2817 }
2818
Chavi Weingarten3a748552018-05-14 17:32:42 +00002819 final WindowState curTarget = mService.mInputMethodTarget;
2820 if (!canUpdateImeTarget()) {
2821 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Defer updating IME target");
2822 return curTarget;
2823 }
2824
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002825 // TODO(multidisplay): Needs some serious rethought when the target and IME are not on the
2826 // same display. Or even when the current IME/target are not on the same screen as the next
2827 // IME/target. For now only look for input windows on the main screen.
Wale Ogunwale8e44f6c2017-03-09 15:25:10 -08002828 mUpdateImeTarget = updateImeTarget;
2829 WindowState target = getWindow(mComputeImeTargetPredicate);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002830
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002831
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002832 // Yet more tricksyness! If this window is a "starting" window, we do actually want
2833 // to be on top of it, but it is not -really- where input will go. So look down below
2834 // for a real window to target...
2835 if (target != null && target.mAttrs.type == TYPE_APPLICATION_STARTING) {
2836 final AppWindowToken token = target.mAppToken;
2837 if (token != null) {
2838 final WindowState betterTarget = token.getImeTargetBelowWindow(target);
2839 if (betterTarget != null) {
2840 target = betterTarget;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002841 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002842 }
2843 }
2844
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002845 if (DEBUG_INPUT_METHOD && updateImeTarget) Slog.v(TAG_WM,
2846 "Proposed new IME target: " + target);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002847
chaviw1c13ad32018-06-12 16:44:23 -07002848 // Now, a special case -- if the last target's window is in the process of exiting, but
2849 // not removed, and the new target is home, keep on the last target to avoid flicker.
2850 // Home is a special case since its above other stacks in the ordering list, but layed
2851 // out below the others.
2852 if (curTarget != null && !curTarget.mRemoved && curTarget.isDisplayedLw()
2853 && curTarget.isClosing() && (target == null || target.isActivityTypeHome())) {
chaviw4ad54912018-05-30 11:05:44 -07002854 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "New target is home while current target is "
Chavi Weingarten3a748552018-05-14 17:32:42 +00002855 + "closing, not changing");
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002856 return curTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002857 }
2858
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002859 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, "Desired input method target=" + target
2860 + " updateImeTarget=" + updateImeTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002861
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002862 if (target == null) {
2863 if (updateImeTarget) {
2864 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget
2865 + " to null." + (SHOW_STACK_CRAWLS ? " Callers="
2866 + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002867 setInputMethodTarget(null, mService.mInputMethodTargetWaitingAnim);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002868 }
2869
2870 return null;
2871 }
2872
2873 if (updateImeTarget) {
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002874 AppWindowToken token = curTarget == null ? null : curTarget.mAppToken;
2875 if (token != null) {
2876
2877 // Now some fun for dealing with window animations that modify the Z order. We need
2878 // to look at all windows below the current target that are in this app, finding the
2879 // highest visible one in layering.
2880 WindowState highestTarget = null;
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002881 if (token.isSelfAnimating()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002882 highestTarget = token.getHighestAnimLayerWindow(curTarget);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002883 }
2884
2885 if (highestTarget != null) {
2886 final AppTransition appTransition = mService.mAppTransition;
2887 if (DEBUG_INPUT_METHOD) Slog.v(TAG_WM, appTransition + " " + highestTarget
Jorim Jaggi35d328a2018-08-14 17:00:20 +02002888 + " animating=" + highestTarget.isAnimating());
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002889
2890 if (appTransition.isTransitionSet()) {
2891 // If we are currently setting up for an animation, hold everything until we
2892 // can find out what will happen.
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002893 setInputMethodTarget(highestTarget, true);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002894 return highestTarget;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002895 }
2896 }
2897 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002898
2899 if (DEBUG_INPUT_METHOD) Slog.w(TAG_WM, "Moving IM target from " + curTarget + " to "
2900 + target + (SHOW_STACK_CRAWLS ? " Callers=" + Debug.getCallers(4) : ""));
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002901 setInputMethodTarget(target, false);
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002902 }
2903
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002904 return target;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002905 }
2906
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002907 private void setInputMethodTarget(WindowState target, boolean targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002908 if (target == mService.mInputMethodTarget
Jorim Jaggib0fc8172017-11-23 17:04:08 +00002909 && mService.mInputMethodTargetWaitingAnim == targetWaitingAnim) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002910 return;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002911 }
2912
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002913 mService.mInputMethodTarget = target;
2914 mService.mInputMethodTargetWaitingAnim = targetWaitingAnim;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002915 assignWindowLayers(false /* setLayoutNeeded */);
2916 }
2917
2918 boolean getNeedsMenu(WindowState top, WindowManagerPolicy.WindowState bottom) {
2919 if (top.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2920 return top.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
2921 }
2922
2923 // Used to indicate we have reached the first window in the range we are interested in.
2924 mTmpWindow = null;
2925
2926 // TODO: Figure-out a more efficient way to do this.
2927 final WindowState candidate = getWindow(w -> {
2928 if (w == top) {
2929 // Reached the first window in the range we are interested in.
2930 mTmpWindow = w;
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002931 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002932 if (mTmpWindow == null) {
2933 return false;
2934 }
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00002935
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002936 if (w.mAttrs.needsMenuKey != NEEDS_MENU_UNSET) {
2937 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002938 }
2939 // If we reached the bottom of the range of windows we are considering,
2940 // assume no menu is needed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002941 if (w == bottom) {
2942 return true;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002943 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002944 return false;
2945 });
2946
2947 return candidate != null && candidate.mAttrs.needsMenuKey == NEEDS_MENU_SET_TRUE;
Wale Ogunwaleae9adbf2016-10-18 15:17:06 -07002948 }
2949
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002950 void setLayoutNeeded() {
2951 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "setLayoutNeeded: callers=" + Debug.getCallers(3));
2952 mLayoutNeeded = true;
2953 }
2954
Wale Ogunwalef7cab102016-10-25 15:25:14 -07002955 private void clearLayoutNeeded() {
Wale Ogunwale2b06bfc2016-09-28 14:17:05 -07002956 if (DEBUG_LAYOUT) Slog.w(TAG_WM, "clearLayoutNeeded: callers=" + Debug.getCallers(3));
2957 mLayoutNeeded = false;
2958 }
2959
2960 boolean isLayoutNeeded() {
2961 return mLayoutNeeded;
2962 }
2963
Wale Ogunwale02319a62016-09-26 15:21:22 -07002964 void dumpTokens(PrintWriter pw, boolean dumpAll) {
2965 if (mTokenMap.isEmpty()) {
2966 return;
2967 }
2968 pw.println(" Display #" + mDisplayId);
2969 final Iterator<WindowToken> it = mTokenMap.values().iterator();
2970 while (it.hasNext()) {
2971 final WindowToken token = it.next();
2972 pw.print(" ");
2973 pw.print(token);
2974 if (dumpAll) {
2975 pw.println(':');
Jorim Jaggif5f9e122017-10-24 18:21:09 +02002976 token.dump(pw, " ", dumpAll);
Wale Ogunwale02319a62016-09-26 15:21:22 -07002977 } else {
2978 pw.println();
2979 }
2980 }
2981 }
2982
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002983 void dumpWindowAnimators(PrintWriter pw, String subPrefix) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002984 final int[] index = new int[1];
2985 forAllWindows(w -> {
2986 final WindowStateAnimator wAnim = w.mWinAnimator;
2987 pw.println(subPrefix + "Window #" + index[0] + ": " + wAnim);
2988 index[0] = index[0] + 1;
2989 }, false /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07002990 }
2991
Jorim Jaggife762342016-10-13 14:33:27 +02002992 /**
2993 * Starts the Keyguard exit animation on all windows that don't belong to an app token.
2994 */
2995 void startKeyguardExitOnNonAppWindows(boolean onWallpaper, boolean goingToShade) {
2996 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00002997 forAllWindows(w -> {
Jorim Jaggic6ae42a2017-05-08 23:42:02 +02002998 if (w.mAppToken == null && policy.canBeHiddenByKeyguardLw(w)
2999 && w.wouldBeVisibleIfPolicyIgnored() && !w.isVisible()) {
Jorim Jaggia5e10572017-11-15 14:36:26 +01003000 w.startAnimation(policy.createHiddenByKeyguardExit(onWallpaper, goingToShade));
Jorim Jaggife762342016-10-13 14:33:27 +02003001 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003002 }, true /* traverseTopToBottom */);
Jorim Jaggife762342016-10-13 14:33:27 +02003003 }
3004
Wale Ogunwale494009b82016-10-21 09:01:38 -07003005 boolean checkWaitingForWindows() {
3006
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003007 mHaveBootMsg = false;
3008 mHaveApp = false;
3009 mHaveWallpaper = false;
3010 mHaveKeyguard = true;
3011
3012 final WindowState visibleWindow = getWindow(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003013 if (w.isVisibleLw() && !w.mObscured && !w.isDrawnLw()) {
3014 return true;
3015 }
3016 if (w.isDrawnLw()) {
3017 if (w.mAttrs.type == TYPE_BOOT_PROGRESS) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003018 mHaveBootMsg = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003019 } else if (w.mAttrs.type == TYPE_APPLICATION
3020 || w.mAttrs.type == TYPE_DRAWN_APPLICATION) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003021 mHaveApp = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003022 } else if (w.mAttrs.type == TYPE_WALLPAPER) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003023 mHaveWallpaper = true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003024 } else if (w.mAttrs.type == TYPE_STATUS_BAR) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003025 mHaveKeyguard = mService.mPolicy.isKeyguardDrawnLw();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003026 }
3027 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003028 return false;
3029 });
3030
3031 if (visibleWindow != null) {
3032 // We have a visible window.
3033 return true;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003034 }
3035
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003036 // if the wallpaper service is disabled on the device, we're never going to have
3037 // wallpaper, don't bother waiting for it
3038 boolean wallpaperEnabled = mService.mContext.getResources().getBoolean(
3039 com.android.internal.R.bool.config_enableWallpaperService)
Daichi Hironoc1432222018-03-29 13:06:07 +09003040 && mService.mContext.getResources().getBoolean(
3041 com.android.internal.R.bool.config_checkWallpaperAtBoot)
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003042 && !mService.mOnlyCore;
3043
Wale Ogunwale494009b82016-10-21 09:01:38 -07003044 if (DEBUG_SCREEN_ON || DEBUG_BOOT) Slog.i(TAG_WM,
3045 "******** booted=" + mService.mSystemBooted
3046 + " msg=" + mService.mShowingBootMessages
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003047 + " haveBoot=" + mHaveBootMsg + " haveApp=" + mHaveApp
3048 + " haveWall=" + mHaveWallpaper + " wallEnabled=" + wallpaperEnabled
3049 + " haveKeyguard=" + mHaveKeyguard);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003050
3051 // If we are turning on the screen to show the boot message, don't do it until the boot
3052 // message is actually displayed.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003053 if (!mService.mSystemBooted && !mHaveBootMsg) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003054 return true;
3055 }
3056
3057 // If we are turning on the screen after the boot is completed normally, don't do so until
3058 // we have the application and wallpaper.
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003059 if (mService.mSystemBooted
3060 && ((!mHaveApp && !mHaveKeyguard) || (wallpaperEnabled && !mHaveWallpaper))) {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003061 return true;
3062 }
3063
3064 return false;
3065 }
3066
Jorim Jaggi8f520872018-08-14 17:00:20 +02003067 void updateWindowsForAnimator() {
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003068 forAllWindows(mUpdateWindowsForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003069 }
3070
Jorim Jaggi8f520872018-08-14 17:00:20 +02003071 /**
3072 * Updates the {@link TaskStack#setAnimationBackground} for all windows.
3073 */
3074 void updateBackgroundForAnimator() {
Jorim Jaggi7156b652016-10-25 08:31:58 -07003075 resetAnimationBackgroundAnimator();
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003076 forAllWindows(mUpdateWallpaperForAnimator, true /* traverseTopToBottom */);
Wale Ogunwale824ab5c2016-10-20 09:31:56 -07003077 }
3078
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003079 boolean isInputMethodClientFocus(int uid, int pid) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003080 final WindowState imFocus = computeImeTarget(false /* updateImeTarget */);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003081 if (imFocus == null) {
3082 return false;
3083 }
3084
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003085 if (DEBUG_INPUT_METHOD) {
3086 Slog.i(TAG_WM, "Desired input method target: " + imFocus);
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003087 Slog.i(TAG_WM, "Current focus: " + mCurrentFocus + " displayId=" + mDisplayId);
3088 Slog.i(TAG_WM, "Last focus: " + mLastFocus + " displayId=" + mDisplayId);
Jorim Jaggiffa5a9d2016-11-23 16:59:19 +00003089 }
3090
Wale Ogunwale494009b82016-10-21 09:01:38 -07003091 if (DEBUG_INPUT_METHOD) {
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003092 Slog.i(TAG_WM, "IM target uid/pid: " + imFocus.mSession.mUid
3093 + "/" + imFocus.mSession.mPid);
3094 Slog.i(TAG_WM, "Requesting client uid/pid: " + uid + "/" + pid);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003095 }
3096
Yohei Yukawa41f89c32018-09-19 14:30:04 -07003097 return imFocus.mSession.mUid == uid && imFocus.mSession.mPid == pid;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003098 }
3099
3100 boolean hasSecureWindowOnScreen() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003101 final WindowState win = getWindow(
3102 w -> w.isOnScreen() && (w.mAttrs.flags & FLAG_SECURE) != 0);
3103 return win != null;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003104 }
3105
3106 void updateSystemUiVisibility(int visibility, int globalDiff) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003107 forAllWindows(w -> {
Wale Ogunwale494009b82016-10-21 09:01:38 -07003108 try {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003109 final int curValue = w.mSystemUiVisibility;
3110 final int diff = (curValue ^ visibility) & globalDiff;
3111 final int newValue = (curValue & ~diff) | (visibility & diff);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003112 if (newValue != curValue) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003113 w.mSeq++;
3114 w.mSystemUiVisibility = newValue;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003115 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003116 if (newValue != curValue || w.mAttrs.hasSystemUiListeners) {
3117 w.mClient.dispatchSystemUiVisibilityChanged(w.mSeq,
Wale Ogunwale494009b82016-10-21 09:01:38 -07003118 visibility, newValue, diff);
3119 }
3120 } catch (RemoteException e) {
3121 // so sorry
3122 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003123 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003124 }
3125
3126 void onWindowFreezeTimeout() {
3127 Slog.w(TAG_WM, "Window freeze timeout expired.");
3128 mService.mWindowsFreezingScreen = WINDOWS_FREEZING_SCREENS_TIMEOUT;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003129
3130 forAllWindows(w -> {
Bryce Lee8c3cf382017-07-06 19:47:10 -07003131 if (!w.getOrientationChanging()) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003132 return;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003133 }
Robert Carr9c1c3a02017-08-08 12:59:01 -07003134 w.orientationChangeTimedOut();
Wale Ogunwale494009b82016-10-21 09:01:38 -07003135 w.mLastFreezeDuration = (int)(SystemClock.elapsedRealtime()
3136 - mService.mDisplayFreezeTime);
3137 Slog.w(TAG_WM, "Force clearing orientation change: " + w);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003138 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003139 mService.mWindowPlacerLocked.performSurfacePlacement();
3140 }
3141
3142 void waitForAllWindowsDrawn() {
3143 final WindowManagerPolicy policy = mService.mPolicy;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003144 forAllWindows(w -> {
3145 final boolean keyguard = policy.isKeyguardHostWindow(w.mAttrs);
3146 if (w.isVisibleLw() && (w.mAppToken != null || keyguard)) {
3147 w.mWinAnimator.mDrawState = DRAW_PENDING;
Wale Ogunwale494009b82016-10-21 09:01:38 -07003148 // Force add to mResizingWindows.
chaviw9c81e632018-07-31 11:17:52 -07003149 w.resetLastContentInsets();
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003150 mService.mWaitingForDrawn.add(w);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003151 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003152 }, true /* traverseTopToBottom */);
Wale Ogunwale494009b82016-10-21 09:01:38 -07003153 }
3154
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003155 // TODO: Super crazy long method that should be broken down...
Tiger Huang1e5b10a2018-07-30 20:19:51 +08003156 void applySurfaceChangesTransaction(boolean recoveringMemory) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003157
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003158 final int dw = mDisplayInfo.logicalWidth;
3159 final int dh = mDisplayInfo.logicalHeight;
3160 final WindowSurfacePlacer surfacePlacer = mService.mWindowPlacerLocked;
3161
3162 mTmpUpdateAllDrawn.clear();
3163
3164 int repeats = 0;
3165 do {
3166 repeats++;
3167 if (repeats > 6) {
3168 Slog.w(TAG, "Animation repeat aborted after too many iterations");
3169 clearLayoutNeeded();
3170 break;
3171 }
3172
3173 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats("On entry to LockedInner",
3174 pendingLayoutChanges);
3175
Andrii Kulian839def92016-11-02 10:58:58 -07003176 // TODO(multi-display): For now adjusting wallpaper only on primary display to avoid
3177 // the wallpaper window jumping across displays.
3178 // Remove check for default display when there will be support for multiple wallpaper
3179 // targets (on different displays).
3180 if (isDefaultDisplay && (pendingLayoutChanges & FINISH_LAYOUT_REDO_WALLPAPER) != 0) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003181 mWallpaperController.adjustWallpaperWindows(this);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003182 }
3183
Riddle Hsu654a6f92018-07-13 22:59:36 +08003184 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_CONFIG) != 0) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003185 if (DEBUG_LAYOUT) Slog.v(TAG, "Computing new config from layout");
Robert Carrae606b42018-02-15 15:36:23 -08003186 if (mService.updateOrientationFromAppTokensLocked(mDisplayId)) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003187 setLayoutNeeded();
Andrii Kulianb17e8692016-11-01 10:37:22 -07003188 mService.mH.obtainMessage(SEND_NEW_CONFIGURATION, mDisplayId).sendToTarget();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003189 }
3190 }
3191
3192 if ((pendingLayoutChanges & FINISH_LAYOUT_REDO_LAYOUT) != 0) {
3193 setLayoutNeeded();
3194 }
3195
3196 // FIRST LOOP: Perform a layout, if needed.
3197 if (repeats < LAYOUT_REPEAT_THRESHOLD) {
3198 performLayout(repeats == 1, false /* updateInputWindows */);
3199 } else {
3200 Slog.w(TAG, "Layout repeat skipped after too many iterations");
3201 }
3202
3203 // FIRST AND ONE HALF LOOP: Make WindowManagerPolicy think it is animating.
3204 pendingLayoutChanges = 0;
3205
3206 if (isDefaultDisplay) {
3207 mService.mPolicy.beginPostLayoutPolicyLw(dw, dh);
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003208 forAllWindows(mApplyPostLayoutPolicy, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003209 pendingLayoutChanges |= mService.mPolicy.finishPostLayoutPolicyLw();
3210 if (DEBUG_LAYOUT_REPEATS) surfacePlacer.debugLayoutRepeats(
3211 "after finishPostLayoutPolicyLw", pendingLayoutChanges);
3212 }
3213 } while (pendingLayoutChanges != 0);
3214
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003215 mTmpApplySurfaceChangesTransactionState.reset();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003216
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003217 mTmpRecoveringMemory = recoveringMemory;
3218 forAllWindows(mApplySurfaceChangesTransaction, true /* traverseTopToBottom */);
Jorim Jaggi6b0f8462018-01-05 17:23:47 +01003219 prepareSurfaces();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003220
Jorim Jaggif1292892018-09-10 11:58:13 +02003221 mLastHasContent = mTmpApplySurfaceChangesTransactionState.displayHasContent;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003222 mService.mDisplayManagerInternal.setDisplayProperties(mDisplayId,
Jorim Jaggif1292892018-09-10 11:58:13 +02003223 mLastHasContent,
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003224 mTmpApplySurfaceChangesTransactionState.preferredRefreshRate,
3225 mTmpApplySurfaceChangesTransactionState.preferredModeId,
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003226 true /* inTraversal, must call performTraversalInTrans... below */);
3227
Lucas Dupin1ead7fc2017-05-24 14:14:44 -07003228 final boolean wallpaperVisible = mWallpaperController.isWallpaperVisible();
3229 if (wallpaperVisible != mLastWallpaperVisible) {
3230 mLastWallpaperVisible = wallpaperVisible;
3231 mService.mWallpaperVisibilityListeners.notifyWallpaperVisibilityChanged(this);
3232 }
3233
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003234 while (!mTmpUpdateAllDrawn.isEmpty()) {
3235 final AppWindowToken atoken = mTmpUpdateAllDrawn.removeLast();
3236 // See if any windows have been drawn, so they (and others associated with them)
3237 // can now be shown.
Matthew Ng498c71d2017-04-18 13:55:45 -07003238 atoken.updateAllDrawn();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003239 }
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003240 }
3241
Bryce Leef3c6a472017-11-14 14:53:06 -08003242 private void updateBounds() {
3243 calculateBounds(mTmpBounds);
3244 setBounds(mTmpBounds);
3245 }
3246
3247 // Determines the current display bounds based on the current state
3248 private void calculateBounds(Rect out) {
3249 // Uses same calculation as in LogicalDisplay#configureDisplayInTransactionLocked.
3250 final int orientation = mDisplayInfo.rotation;
3251 boolean rotated = (orientation == ROTATION_90 || orientation == ROTATION_270);
3252 final int physWidth = rotated ? mBaseDisplayHeight : mBaseDisplayWidth;
3253 final int physHeight = rotated ? mBaseDisplayWidth : mBaseDisplayHeight;
3254 int width = mDisplayInfo.logicalWidth;
3255 int left = (physWidth - width) / 2;
3256 int height = mDisplayInfo.logicalHeight;
3257 int top = (physHeight - height) / 2;
3258 out.set(left, top, left + width, top + height);
3259 }
3260
3261 @Override
3262 public void getBounds(Rect out) {
3263 calculateBounds(out);
3264 }
3265
3266 private void getBounds(Rect out, int orientation) {
3267 getBounds(out);
3268
3269 // Rotate the Rect if needed.
3270 final int currentRotation = mDisplayInfo.rotation;
3271 final int rotationDelta = deltaRotation(currentRotation, orientation);
3272 if (rotationDelta == ROTATION_90 || rotationDelta == ROTATION_270) {
3273 createRotationMatrix(rotationDelta, mBaseDisplayWidth, mBaseDisplayHeight, mTmpMatrix);
3274 mTmpRectF.set(out);
3275 mTmpMatrix.mapRect(mTmpRectF);
3276 mTmpRectF.round(out);
3277 }
3278 }
3279
Louis Chang7501e332018-08-20 13:08:39 +08003280 int getSurfaceSize() {
3281 return mSurfaceSize;
3282 }
3283
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003284 void performLayout(boolean initial, boolean updateInputWindows) {
3285 if (!isLayoutNeeded()) {
3286 return;
3287 }
3288 clearLayoutNeeded();
3289
3290 final int dw = mDisplayInfo.logicalWidth;
3291 final int dh = mDisplayInfo.logicalHeight;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003292 if (DEBUG_LAYOUT) {
3293 Slog.v(TAG, "-------------------------------------");
3294 Slog.v(TAG, "performLayout: needed=" + isLayoutNeeded() + " dw=" + dw + " dh=" + dh);
3295 }
3296
Adrian Roos6a4fa0e2018-03-05 19:50:16 +01003297 mDisplayFrames.onDisplayInfoUpdated(mDisplayInfo,
3298 calculateDisplayCutoutForRotation(mDisplayInfo.rotation));
Wale Ogunwale828ff7e2017-11-14 01:01:29 +00003299 // TODO: Not sure if we really need to set the rotation here since we are updating from the
3300 // display info above...
3301 mDisplayFrames.mRotation = mRotation;
3302 mService.mPolicy.beginLayoutLw(mDisplayFrames, getConfiguration().uiMode);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003303 if (isDefaultDisplay) {
3304 // Not needed on non-default displays.
3305 mService.mSystemDecorLayer = mService.mPolicy.getSystemDecorLayerLw();
3306 mService.mScreenRect.set(0, 0, dw, dh);
3307 }
3308
Adrian Roos5251b1d2018-03-23 18:57:43 +01003309 int seq = mLayoutSeq + 1;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003310 if (seq < 0) seq = 0;
Adrian Roos5251b1d2018-03-23 18:57:43 +01003311 mLayoutSeq = seq;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003312
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003313 // Used to indicate that we have processed the dream window and all additional windows are
3314 // behind it.
3315 mTmpWindow = null;
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003316 mTmpInitial = initial;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003317
3318 // First perform layout of any root windows (not attached to another window).
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003319 forAllWindows(mPerformLayout, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003320
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003321 // Used to indicate that we have processed the dream window and all additional attached
3322 // windows are behind it.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003323 mTmpWindow2 = mTmpWindow;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003324 mTmpWindow = null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003325
3326 // Now perform layout of attached windows, which usually depend on the position of the
3327 // window they are attached to. XXX does not deal with windows that are attached to windows
3328 // that are themselves attached.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08003329 forAllWindows(mPerformLayoutAttached, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003330
3331 // Window frames may have changed. Tell the input dispatcher about it.
Arthur Hung95b38a92018-07-20 18:56:12 +08003332 mInputMonitor.layoutInputConsumers(dw, dh);
3333 mInputMonitor.setUpdateInputWindowsNeededLw();
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003334 if (updateInputWindows) {
Arthur Hung95b38a92018-07-20 18:56:12 +08003335 mInputMonitor.updateInputWindowsLw(false /*force*/);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003336 }
3337
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003338 mService.mH.sendEmptyMessage(UPDATE_DOCKED_STACK_DIVIDER);
3339 }
3340
3341 /**
3342 * Takes a snapshot of the display. In landscape mode this grabs the whole screen.
3343 * In portrait mode, it grabs the full screenshot.
3344 *
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003345 * @param config of the output bitmap
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003346 */
chaviw0315a1a2018-03-05 15:28:35 -08003347 Bitmap screenshotDisplayLocked(Bitmap.Config config) {
3348 if (!mService.mPolicy.isScreenOn()) {
3349 if (DEBUG_SCREENSHOT) {
3350 Slog.i(TAG_WM, "Attempted to take screenshot while display was off.");
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003351 }
chaviw0315a1a2018-03-05 15:28:35 -08003352 return null;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003353 }
chaviwfbe47df2017-11-10 16:14:49 -08003354
chaviw0315a1a2018-03-05 15:28:35 -08003355 int dw = mDisplayInfo.logicalWidth;
3356 int dh = mDisplayInfo.logicalHeight;
chaviwfbe47df2017-11-10 16:14:49 -08003357
chaviw0315a1a2018-03-05 15:28:35 -08003358 if (dw <= 0 || dh <= 0) {
3359 return null;
3360 }
chaviwfbe47df2017-11-10 16:14:49 -08003361
chaviw0315a1a2018-03-05 15:28:35 -08003362 final Rect frame = new Rect(0, 0, dw, dh);
chaviwfbe47df2017-11-10 16:14:49 -08003363
chaviw0315a1a2018-03-05 15:28:35 -08003364 // The screenshot API does not apply the current screen rotation.
3365 int rot = mDisplay.getRotation();
chaviwfbe47df2017-11-10 16:14:49 -08003366
chaviw0315a1a2018-03-05 15:28:35 -08003367 if (rot == ROTATION_90 || rot == ROTATION_270) {
3368 rot = (rot == ROTATION_90) ? ROTATION_270 : ROTATION_90;
3369 }
chaviwfbe47df2017-11-10 16:14:49 -08003370
chaviw0315a1a2018-03-05 15:28:35 -08003371 // SurfaceFlinger is not aware of orientation, so convert our logical
3372 // crop to SurfaceFlinger's portrait orientation.
3373 convertCropForSurfaceFlinger(frame, rot, dw, dh);
3374
3375 final ScreenRotationAnimation screenRotationAnimation =
3376 mService.mAnimator.getScreenRotationAnimationLocked(DEFAULT_DISPLAY);
3377 final boolean inRotation = screenRotationAnimation != null &&
3378 screenRotationAnimation.isAnimating();
3379 if (DEBUG_SCREENSHOT && inRotation) Slog.v(TAG_WM, "Taking screenshot while rotating");
3380
3381 // TODO(b/68392460): We should screenshot Task controls directly
3382 // but it's difficult at the moment as the Task doesn't have the
3383 // correct size set.
chaviw08520a02018-09-10 16:44:56 -07003384 final Bitmap bitmap = SurfaceControl.screenshot(frame, dw, dh, inRotation, rot);
chaviw0315a1a2018-03-05 15:28:35 -08003385 if (bitmap == null) {
3386 Slog.w(TAG_WM, "Failed to take screenshot");
3387 return null;
3388 }
3389
3390 // Create a copy of the screenshot that is immutable and backed in ashmem.
3391 // This greatly reduces the overhead of passing the bitmap between processes.
3392 final Bitmap ret = bitmap.createAshmemBitmap(config);
3393 bitmap.recycle();
3394 return ret;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003395 }
3396
3397 // TODO: Can this use createRotationMatrix()?
3398 private static void convertCropForSurfaceFlinger(Rect crop, int rot, int dw, int dh) {
3399 if (rot == Surface.ROTATION_90) {
3400 final int tmp = crop.top;
3401 crop.top = dw - crop.right;
3402 crop.right = crop.bottom;
3403 crop.bottom = dw - crop.left;
3404 crop.left = tmp;
3405 } else if (rot == Surface.ROTATION_180) {
3406 int tmp = crop.top;
3407 crop.top = dh - crop.bottom;
3408 crop.bottom = dh - tmp;
3409 tmp = crop.right;
3410 crop.right = dw - crop.left;
3411 crop.left = dw - tmp;
3412 } else if (rot == Surface.ROTATION_270) {
3413 final int tmp = crop.top;
3414 crop.top = crop.left;
3415 crop.left = dh - crop.bottom;
3416 crop.bottom = crop.right;
3417 crop.right = dh - tmp;
3418 }
3419 }
3420
3421 void onSeamlessRotationTimeout() {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003422 // Used to indicate the layout is needed.
3423 mTmpWindow = null;
3424
3425 forAllWindows(w -> {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003426 if (!w.mSeamlesslyRotated) {
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003427 return;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003428 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003429 mTmpWindow = w;
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003430 w.setDisplayLayoutNeeded();
3431 mService.markForSeamlessRotation(w, false);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003432 }, true /* traverseTopToBottom */);
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003433
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003434 if (mTmpWindow != null) {
Wale Ogunwalef7cab102016-10-25 15:25:14 -07003435 mService.mWindowPlacerLocked.performSurfacePlacement();
3436 }
3437 }
3438
Wale Ogunwale1666e312016-12-16 11:27:18 -08003439 void setExitingTokensHasVisible(boolean hasVisible) {
3440 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3441 mExitingTokens.get(i).hasVisible = hasVisible;
3442 }
3443
3444 // Initialize state of exiting applications.
3445 mTaskStackContainers.setExitingTokensHasVisible(hasVisible);
3446 }
3447
3448 void removeExistingTokensIfPossible() {
3449 for (int i = mExitingTokens.size() - 1; i >= 0; i--) {
3450 final WindowToken token = mExitingTokens.get(i);
3451 if (!token.hasVisible) {
3452 mExitingTokens.remove(i);
3453 }
3454 }
3455
3456 // Time to remove any exiting applications?
3457 mTaskStackContainers.removeExistingAppTokensIfPossible();
3458 }
3459
Wale Ogunwale55ddf8f2017-03-20 08:56:38 -07003460 @Override
3461 void onDescendantOverrideConfigurationChanged() {
3462 setLayoutNeeded();
3463 mService.requestTraversal();
3464 }
3465
David Stevens9440dc82017-03-16 19:00:20 -07003466 boolean okToDisplay() {
3467 if (mDisplayId == DEFAULT_DISPLAY) {
3468 return !mService.mDisplayFrozen
3469 && mService.mDisplayEnabled && mService.mPolicy.isScreenOn();
3470 }
3471 return mDisplayInfo.state == Display.STATE_ON;
3472 }
3473
3474 boolean okToAnimate() {
3475 return okToDisplay() &&
3476 (mDisplayId != DEFAULT_DISPLAY || mService.mPolicy.okToAnimate());
3477 }
3478
Wale Ogunwale3f4433d2016-08-18 20:42:42 -07003479 static final class TaskForResizePointSearchResult {
3480 boolean searchDone;
3481 Task taskForResize;
3482
3483 void reset() {
3484 searchDone = false;
3485 taskForResize = null;
3486 }
3487 }
Robert Carr3b716242016-08-16 16:02:21 -07003488
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003489 private static final class ApplySurfaceChangesTransactionState {
3490 boolean displayHasContent;
3491 boolean obscured;
3492 boolean syswin;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003493 float preferredRefreshRate;
3494 int preferredModeId;
3495
3496 void reset() {
3497 displayHasContent = false;
3498 obscured = false;
3499 syswin = false;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00003500 preferredRefreshRate = 0;
3501 preferredModeId = 0;
3502 }
3503 }
3504
3505 private static final class ScreenshotApplicationState {
3506 WindowState appWin;
3507 int maxLayer;
3508 int minLayer;
3509 boolean screenshotReady;
3510
3511 void reset(boolean screenshotReady) {
3512 appWin = null;
3513 maxLayer = 0;
3514 minLayer = 0;
3515 this.screenshotReady = screenshotReady;
3516 minLayer = (screenshotReady) ? 0 : Integer.MAX_VALUE;
3517 }
3518 }
3519
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003520 /**
3521 * Base class for any direct child window container of {@link #DisplayContent} need to inherit
3522 * from. This is mainly a pass through class that allows {@link #DisplayContent} to have
3523 * homogeneous children type which is currently required by sub-classes of
3524 * {@link WindowContainer} class.
3525 */
3526 static class DisplayChildWindowContainer<E extends WindowContainer> extends WindowContainer<E> {
3527
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003528 DisplayChildWindowContainer(WindowManagerService service) {
3529 super(service);
3530 }
3531
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003532 @Override
3533 boolean fillsParent() {
3534 return true;
3535 }
3536
3537 @Override
3538 boolean isVisible() {
3539 return true;
Robert Carr3b716242016-08-16 16:02:21 -07003540 }
3541 }
3542
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003543 /**
3544 * Window container class that contains all containers on this display relating to Apps.
3545 * I.e Activities.
3546 */
Wale Ogunwale3a931692016-11-02 16:49:48 -07003547 private final class TaskStackContainers extends DisplayChildWindowContainer<TaskStack> {
Robert Carrb1579c82017-09-05 14:54:47 -07003548 /**
3549 * A control placed at the appropriate level for transitions to occur.
3550 */
chaviw23ee71c2017-12-18 11:29:41 -08003551 SurfaceControl mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02003552 SurfaceControl mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02003553 SurfaceControl mHomeAppAnimationLayer = null;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003554
Robert Carrf7a7ca82017-12-06 13:17:07 -08003555 /**
3556 * Given that the split-screen divider does not have an AppWindowToken, it
3557 * will have to live inside of a "NonAppWindowContainer", in particular
3558 * {@link DisplayContent#mAboveAppWindowsContainers}. However, in visual Z order
3559 * it will need to be interleaved with some of our children, appearing on top of
3560 * both docked stacks but underneath any assistant stacks.
3561 *
3562 * To solve this problem we have this anchor control, which will always exist so
3563 * we can always assign it the correct value in our {@link #assignChildLayers}.
3564 * Likewise since it always exists, {@link AboveAppWindowContainers} can always
3565 * assign the divider a layer relative to it. This way we prevent linking lifecycle
3566 * events between the two containers.
3567 */
3568 SurfaceControl mSplitScreenDividerAnchor = null;
3569
Wale Ogunwale61911492017-10-11 08:50:50 -07003570 // Cached reference to some special stacks we tend to get a lot so we don't need to loop
3571 // through the list to find them.
3572 private TaskStack mHomeStack = null;
3573 private TaskStack mPinnedStack = null;
3574 private TaskStack mSplitScreenPrimaryStack = null;
3575
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003576 TaskStackContainers(WindowManagerService service) {
3577 super(service);
3578 }
3579
Wale Ogunwale61911492017-10-11 08:50:50 -07003580 /**
3581 * Returns the topmost stack on the display that is compatible with the input windowing mode
3582 * and activity type. Null is no compatible stack on the display.
3583 */
3584 TaskStack getStack(int windowingMode, int activityType) {
3585 if (activityType == ACTIVITY_TYPE_HOME) {
3586 return mHomeStack;
3587 }
3588 if (windowingMode == WINDOWING_MODE_PINNED) {
3589 return mPinnedStack;
3590 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3591 return mSplitScreenPrimaryStack;
3592 }
3593 for (int i = mTaskStackContainers.getChildCount() - 1; i >= 0; --i) {
3594 final TaskStack stack = mTaskStackContainers.getChildAt(i);
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003595 if (activityType == ACTIVITY_TYPE_UNDEFINED
3596 && windowingMode == stack.getWindowingMode()) {
3597 // Passing in undefined type means we want to match the topmost stack with the
3598 // windowing mode.
3599 return stack;
3600 }
Wale Ogunwale61911492017-10-11 08:50:50 -07003601 if (stack.isCompatible(windowingMode, activityType)) {
3602 return stack;
3603 }
3604 }
3605 return null;
3606 }
3607
3608 @VisibleForTesting
3609 TaskStack getTopStack() {
3610 return mTaskStackContainers.getChildCount() > 0
3611 ? mTaskStackContainers.getChildAt(mTaskStackContainers.getChildCount() - 1) : null;
3612 }
3613
3614 TaskStack getHomeStack() {
3615 if (mHomeStack == null && mDisplayId == DEFAULT_DISPLAY) {
3616 Slog.e(TAG_WM, "getHomeStack: Returning null from this=" + this);
3617 }
3618 return mHomeStack;
3619 }
3620
3621 TaskStack getPinnedStack() {
3622 return mPinnedStack;
3623 }
3624
Matthew Ng64e77cf2017-10-31 14:01:31 -07003625 TaskStack getSplitScreenPrimaryStack() {
Wale Ogunwale61911492017-10-11 08:50:50 -07003626 return mSplitScreenPrimaryStack;
3627 }
3628
Winson Chunge2d72172018-01-25 17:46:20 +00003629 ArrayList<Task> getVisibleTasks() {
3630 final ArrayList<Task> visibleTasks = new ArrayList<>();
3631 forAllTasks(task -> {
3632 if (task.isVisible()) {
3633 visibleTasks.add(task);
3634 }
3635 });
3636 return visibleTasks;
3637 }
3638
Andrii Kulian839def92016-11-02 10:58:58 -07003639 /**
3640 * Adds the stack to this container.
Wale Ogunwale61911492017-10-11 08:50:50 -07003641 * @see DisplayContent#createStack(int, boolean, StackWindowController)
Andrii Kulian839def92016-11-02 10:58:58 -07003642 */
3643 void addStackToDisplay(TaskStack stack, boolean onTop) {
Wale Ogunwale61911492017-10-11 08:50:50 -07003644 addStackReferenceIfNeeded(stack);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003645 addChild(stack, onTop);
3646 stack.onDisplayChanged(DisplayContent.this);
Robert Carr3b716242016-08-16 16:02:21 -07003647 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003648
Wale Ogunwale61911492017-10-11 08:50:50 -07003649 void onStackWindowingModeChanged(TaskStack stack) {
3650 removeStackReferenceIfNeeded(stack);
3651 addStackReferenceIfNeeded(stack);
3652 if (stack == mPinnedStack && getTopStack() != stack) {
3653 // Looks like this stack changed windowing mode to pinned. Move it to the top.
3654 positionChildAt(POSITION_TOP, stack, false /* includingParents */);
3655 }
3656 }
3657
3658 private void addStackReferenceIfNeeded(TaskStack stack) {
3659 if (stack.isActivityTypeHome()) {
3660 if (mHomeStack != null) {
Louis Chang7d0037c2018-08-13 12:42:06 +08003661 throw new IllegalArgumentException("addStackReferenceIfNeeded: home stack="
Wale Ogunwale61911492017-10-11 08:50:50 -07003662 + mHomeStack + " already exist on display=" + this + " stack=" + stack);
Louis Chang7d0037c2018-08-13 12:42:06 +08003663
Wale Ogunwale61911492017-10-11 08:50:50 -07003664 }
Louis Chang7d0037c2018-08-13 12:42:06 +08003665 mHomeStack = stack;
Wale Ogunwale61911492017-10-11 08:50:50 -07003666 }
3667 final int windowingMode = stack.getWindowingMode();
3668 if (windowingMode == WINDOWING_MODE_PINNED) {
3669 if (mPinnedStack != null) {
3670 throw new IllegalArgumentException("addStackReferenceIfNeeded: pinned stack="
3671 + mPinnedStack + " already exist on display=" + this
3672 + " stack=" + stack);
3673 }
3674 mPinnedStack = stack;
3675 } else if (windowingMode == WINDOWING_MODE_SPLIT_SCREEN_PRIMARY) {
3676 if (mSplitScreenPrimaryStack != null) {
3677 throw new IllegalArgumentException("addStackReferenceIfNeeded:"
3678 + " split-screen-primary" + " stack=" + mSplitScreenPrimaryStack
3679 + " already exist on display=" + this + " stack=" + stack);
3680 }
3681 mSplitScreenPrimaryStack = stack;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003682 mDividerControllerLocked.notifyDockedStackExistsChanged(true);
Wale Ogunwale61911492017-10-11 08:50:50 -07003683 }
3684 }
3685
3686 private void removeStackReferenceIfNeeded(TaskStack stack) {
3687 if (stack == mHomeStack) {
3688 mHomeStack = null;
3689 } else if (stack == mPinnedStack) {
3690 mPinnedStack = null;
3691 } else if (stack == mSplitScreenPrimaryStack) {
3692 mSplitScreenPrimaryStack = null;
Wale Ogunwale30e441d2017-11-09 08:28:45 -08003693 // Re-set the split-screen create mode whenever the split-screen stack is removed.
3694 mService.setDockedStackCreateStateLocked(
3695 SPLIT_SCREEN_CREATE_MODE_TOP_OR_LEFT, null /* initialBounds */);
3696 mDividerControllerLocked.notifyDockedStackExistsChanged(false);
Wale Ogunwale61911492017-10-11 08:50:50 -07003697 }
Andrii Kulian839def92016-11-02 10:58:58 -07003698 }
3699
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003700 private void addChild(TaskStack stack, boolean toTop) {
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003701 final int addIndex = findPositionForStack(toTop ? mChildren.size() : 0, stack,
3702 true /* adding */);
Wale Ogunwale19e452e2016-10-12 12:36:29 -07003703 addChild(stack, addIndex);
3704 setLayoutNeeded();
3705 }
3706
Wale Ogunwale61911492017-10-11 08:50:50 -07003707 @Override
3708 protected void removeChild(TaskStack stack) {
3709 super.removeChild(stack);
3710 removeStackReferenceIfNeeded(stack);
3711 }
Bryce Lee00d586d2017-07-28 20:48:43 -07003712
3713 @Override
3714 boolean isOnTop() {
3715 // Considered always on top
3716 return true;
3717 }
3718
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003719 @Override
Andrii Kuliand2765632016-12-12 22:26:34 -08003720 void positionChildAt(int position, TaskStack child, boolean includingParents) {
Wale Ogunwale3382ab12017-07-27 08:55:03 -07003721 if (child.getWindowConfiguration().isAlwaysOnTop()
3722 && position != POSITION_TOP) {
Andrii Kuliand2765632016-12-12 22:26:34 -08003723 // This stack is always-on-top, override the default behavior.
3724 Slog.w(TAG_WM, "Ignoring move of always-on-top stack=" + this + " to bottom");
3725
3726 // Moving to its current position, as we must call super but we don't want to
3727 // perform any meaningful action.
3728 final int currentPosition = mChildren.indexOf(child);
3729 super.positionChildAt(currentPosition, child, false /* includingParents */);
3730 return;
3731 }
3732
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003733 final int targetPosition = findPositionForStack(position, child, false /* adding */);
3734 super.positionChildAt(targetPosition, child, includingParents);
3735
Riddle Hsu2f6e1742018-08-23 22:44:36 +08003736 if (includingParents) {
3737 // We still want to move the display of this stack container to top because even the
3738 // target position is adjusted to non-top, the intention of the condition is to have
3739 // higher z-order to gain focus (e.g. moving a task of a fullscreen stack to front
3740 // in a non-top display which is using picture-in-picture mode).
3741 final int topChildPosition = getChildCount() - 1;
3742 if (targetPosition < topChildPosition && position >= topChildPosition) {
3743 getParent().positionChildAt(POSITION_TOP, this /* child */,
3744 true /* includingParents */);
3745 }
3746 }
3747
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003748 setLayoutNeeded();
3749 }
3750
3751 /**
3752 * When stack is added or repositioned, find a proper position for it.
3753 * This will make sure that pinned stack always stays on top.
3754 * @param requestedPosition Position requested by caller.
3755 * @param stack Stack to be added or positioned.
3756 * @param adding Flag indicates whether we're adding a new stack or positioning an existing.
3757 * @return The proper position for the stack.
3758 */
3759 private int findPositionForStack(int requestedPosition, TaskStack stack, boolean adding) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09003760 if (stack.inPinnedWindowingMode()) {
Kazuki Takisef85197b2018-06-18 18:18:36 +09003761 return POSITION_TOP;
Kazuki Takise148d00a2018-05-31 15:32:19 +09003762 }
3763
Kazuki Takisef85197b2018-06-18 18:18:36 +09003764 final int topChildPosition = mChildren.size() - 1;
3765 int belowAlwaysOnTopPosition = POSITION_BOTTOM;
3766 for (int i = topChildPosition; i >= 0; --i) {
3767 if (getStacks().get(i) != stack && !getStacks().get(i).isAlwaysOnTop()) {
3768 belowAlwaysOnTopPosition = i;
3769 break;
3770 }
3771 }
3772
3773 // The max possible position we can insert the stack at.
3774 int maxPosition = POSITION_TOP;
3775 // The min possible position we can insert the stack at.
3776 int minPosition = POSITION_BOTTOM;
3777
3778 if (stack.isAlwaysOnTop()) {
3779 if (hasPinnedStack()) {
3780 // Always-on-top stacks go below the pinned stack.
3781 maxPosition = getStacks().indexOf(mPinnedStack) - 1;
3782 }
3783 // Always-on-top stacks need to be above all other stacks.
3784 minPosition = belowAlwaysOnTopPosition !=
3785 POSITION_BOTTOM ? belowAlwaysOnTopPosition : topChildPosition;
3786 } else {
3787 // Other stacks need to be below the always-on-top stacks.
3788 maxPosition = belowAlwaysOnTopPosition !=
Arthur Hung928d8dc2018-07-18 15:38:05 +08003789 POSITION_BOTTOM ? belowAlwaysOnTopPosition : 0;
Kazuki Takisef85197b2018-06-18 18:18:36 +09003790 }
3791
3792 int targetPosition = requestedPosition;
3793 targetPosition = Math.min(targetPosition, maxPosition);
3794 targetPosition = Math.max(targetPosition, minPosition);
3795
3796 int prevPosition = getStacks().indexOf(stack);
3797 // The positions we calculated above (maxPosition, minPosition) do not take into
3798 // consideration the following edge cases.
3799 // 1) We need to adjust the position depending on the value "adding".
3800 // 2) When we are moving a stack to another position, we also need to adjust the
3801 // position depending on whether the stack is moving to a higher or lower position.
3802 if ((targetPosition != requestedPosition) &&
3803 (adding || targetPosition < prevPosition)) {
Kazuki Takise148d00a2018-05-31 15:32:19 +09003804 targetPosition++;
3805 }
3806
Andrii Kuliancd5dcb8b2017-01-03 17:09:45 -08003807 return targetPosition;
Andrii Kuliand2765632016-12-12 22:26:34 -08003808 }
3809
3810 @Override
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003811 boolean forAllWindows(ToBooleanFunction<WindowState> callback,
3812 boolean traverseTopToBottom) {
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003813 if (traverseTopToBottom) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003814 if (super.forAllWindows(callback, traverseTopToBottom)) {
3815 return true;
3816 }
3817 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3818 return true;
3819 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003820 } else {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003821 if (forAllExitingAppTokenWindows(callback, traverseTopToBottom)) {
3822 return true;
3823 }
3824 if (super.forAllWindows(callback, traverseTopToBottom)) {
3825 return true;
3826 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003827 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003828 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003829 }
3830
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003831 private boolean forAllExitingAppTokenWindows(ToBooleanFunction<WindowState> callback,
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003832 boolean traverseTopToBottom) {
3833 // For legacy reasons we process the TaskStack.mExitingAppTokens first here before the
3834 // app tokens.
3835 // TODO: Investigate if we need to continue to do this or if we can just process them
3836 // in-order.
3837 if (traverseTopToBottom) {
3838 for (int i = mChildren.size() - 1; i >= 0; --i) {
3839 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3840 for (int j = appTokens.size() - 1; j >= 0; --j) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003841 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3842 traverseTopToBottom)) {
3843 return true;
3844 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003845 }
3846 }
3847 } else {
3848 final int count = mChildren.size();
3849 for (int i = 0; i < count; ++i) {
3850 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3851 final int appTokensCount = appTokens.size();
3852 for (int j = 0; j < appTokensCount; j++) {
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003853 if (appTokens.get(j).forAllWindowsUnchecked(callback,
3854 traverseTopToBottom)) {
3855 return true;
3856 }
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003857 }
3858 }
3859 }
Wale Ogunwalef4ebe2e2016-11-09 13:24:43 -08003860 return false;
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003861 }
3862
Wale Ogunwale1666e312016-12-16 11:27:18 -08003863 void setExitingTokensHasVisible(boolean hasVisible) {
3864 for (int i = mChildren.size() - 1; i >= 0; --i) {
3865 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3866 for (int j = appTokens.size() - 1; j >= 0; --j) {
3867 appTokens.get(j).hasVisible = hasVisible;
3868 }
3869 }
3870 }
3871
3872 void removeExistingAppTokensIfPossible() {
3873 for (int i = mChildren.size() - 1; i >= 0; --i) {
3874 final AppTokenList appTokens = mChildren.get(i).mExitingAppTokens;
3875 for (int j = appTokens.size() - 1; j >= 0; --j) {
3876 final AppWindowToken token = appTokens.get(j);
3877 if (!token.hasVisible && !mService.mClosingApps.contains(token)
3878 && (!token.mIsExiting || token.isEmpty())) {
3879 // Make sure there is no animation running on this token, so any windows
3880 // associated with it will be removed as soon as their animations are
3881 // complete.
Jorim Jaggif5f9e122017-10-24 18:21:09 +02003882 cancelAnimation();
Wale Ogunwale1666e312016-12-16 11:27:18 -08003883 if (DEBUG_ADD_REMOVE || DEBUG_TOKEN_MOVEMENT) Slog.v(TAG,
3884 "performLayout: App token exiting now removed" + token);
3885 token.removeIfPossible();
3886 }
3887 }
3888 }
3889 }
3890
Wale Ogunwaleb783fd82016-11-04 09:51:54 -07003891 @Override
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003892 int getOrientation() {
Wale Ogunwale44f036f2017-09-29 05:09:09 -07003893 if (isStackVisible(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY)
3894 || isStackVisible(WINDOWING_MODE_FREEFORM)) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003895 // Apps and their containers are not allowed to specify an orientation while the
Wale Ogunwaled0c225c2018-06-15 14:47:46 -07003896 // docked or freeform stack is visible...except for the home stack if the docked
3897 // stack is minimized and it actually set something and the bounds is different from
3898 // the display.
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003899 if (mHomeStack != null && mHomeStack.isVisible()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07003900 && mDividerControllerLocked.isMinimizedDock()
Wale Ogunwalecf2d6472018-06-14 08:55:42 -07003901 && !(mDividerControllerLocked.isHomeStackResizable()
3902 && mHomeStack.matchParentBounds())) {
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003903 final int orientation = mHomeStack.getOrientation();
3904 if (orientation != SCREEN_ORIENTATION_UNSET) {
3905 return orientation;
3906 }
3907 }
3908 return SCREEN_ORIENTATION_UNSPECIFIED;
3909 }
3910
3911 final int orientation = super.getOrientation();
Dean Harding3e5a1522017-10-06 09:19:01 -07003912 boolean isCar = mService.mContext.getPackageManager().hasSystemFeature(
3913 PackageManager.FEATURE_AUTOMOTIVE);
3914 if (isCar) {
3915 // In a car, you cannot physically rotate the screen, so it doesn't make sense to
3916 // allow anything but the default orientation.
3917 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07003918 "Forcing UNSPECIFIED orientation in car for display id=" + mDisplayId
3919 + ". Ignoring " + orientation);
Dean Harding3e5a1522017-10-06 09:19:01 -07003920 return SCREEN_ORIENTATION_UNSPECIFIED;
3921 }
3922
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003923 if (orientation != SCREEN_ORIENTATION_UNSET
3924 && orientation != SCREEN_ORIENTATION_BEHIND) {
3925 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07003926 "App is requesting an orientation, return " + orientation
3927 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003928 return orientation;
3929 }
3930
3931 if (DEBUG_ORIENTATION) Slog.v(TAG_WM,
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07003932 "No app is requesting an orientation, return " + mLastOrientation
3933 + " for display id=" + mDisplayId);
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003934 // The next app has not been requested to be visible, so we keep the current orientation
3935 // to prevent freezing/unfreezing the display too early.
Andrii Kulian8ee72852017-03-10 10:36:45 -08003936 return mLastOrientation;
Wale Ogunwalee6f806e2016-10-20 15:29:42 -07003937 }
Robert Carrb1579c82017-09-05 14:54:47 -07003938
3939 @Override
3940 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carr2f8aa392018-01-31 14:46:51 -08003941 assignStackOrdering(t);
Robert Carrf7a7ca82017-12-06 13:17:07 -08003942
Robert Carr2f8aa392018-01-31 14:46:51 -08003943 for (int i = 0; i < mChildren.size(); i++) {
3944 final TaskStack s = mChildren.get(i);
3945 s.assignChildLayers(t);
3946 }
3947 }
3948
3949 void assignStackOrdering(SurfaceControl.Transaction t) {
Jorim Jaggibe418292018-03-26 16:14:12 +02003950
Robert Carrf7a7ca82017-12-06 13:17:07 -08003951 final int HOME_STACK_STATE = 0;
3952 final int NORMAL_STACK_STATE = 1;
3953 final int ALWAYS_ON_TOP_STATE = 2;
3954
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003955 int layer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08003956 int layerForAnimationLayer = 0;
Jorim Jaggibe418292018-03-26 16:14:12 +02003957 int layerForBoostedAnimationLayer = 0;
Jorim Jaggi391790622018-04-18 15:30:44 +02003958 int layerForHomeAnimationLayer = 0;
Robert Carr2f8aa392018-01-31 14:46:51 -08003959
Robert Carrf7a7ca82017-12-06 13:17:07 -08003960 for (int state = 0; state <= ALWAYS_ON_TOP_STATE; state++) {
3961 for (int i = 0; i < mChildren.size(); i++) {
3962 final TaskStack s = mChildren.get(i);
Robert Carr2f8aa392018-01-31 14:46:51 -08003963 if (state == HOME_STACK_STATE && !s.isActivityTypeHome()) {
3964 continue;
3965 } else if (state == NORMAL_STACK_STATE && (s.isActivityTypeHome()
3966 || s.isAlwaysOnTop())) {
3967 continue;
3968 } else if (state == ALWAYS_ON_TOP_STATE && !s.isAlwaysOnTop()) {
3969 continue;
3970 }
3971 s.assignLayer(t, layer++);
3972 if (s.inSplitScreenWindowingMode() && mSplitScreenDividerAnchor != null) {
3973 t.setLayer(mSplitScreenDividerAnchor, layer++);
3974 }
Winson Chungd41f71d2018-03-16 15:26:07 -07003975 if ((s.isTaskAnimating() || s.isAppAnimating())
3976 && state != ALWAYS_ON_TOP_STATE) {
Robert Carr2f8aa392018-01-31 14:46:51 -08003977 // Ensure the animation layer ends up above the
3978 // highest animating stack and no higher.
3979 layerForAnimationLayer = layer++;
Robert Carrf7a7ca82017-12-06 13:17:07 -08003980 }
Jorim Jaggibe418292018-03-26 16:14:12 +02003981 if (state != ALWAYS_ON_TOP_STATE) {
3982 layerForBoostedAnimationLayer = layer++;
3983 }
Robert Carrf7a7ca82017-12-06 13:17:07 -08003984 }
Jorim Jaggi391790622018-04-18 15:30:44 +02003985 if (state == HOME_STACK_STATE) {
3986 layerForHomeAnimationLayer = layer++;
3987 }
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003988 }
Robert Carr2f8aa392018-01-31 14:46:51 -08003989 if (mAppAnimationLayer != null) {
3990 t.setLayer(mAppAnimationLayer, layerForAnimationLayer);
Jorim Jaggiffe128d2017-11-30 13:54:36 +01003991 }
Jorim Jaggibe418292018-03-26 16:14:12 +02003992 if (mBoostedAppAnimationLayer != null) {
3993 t.setLayer(mBoostedAppAnimationLayer, layerForBoostedAnimationLayer);
3994 }
Jorim Jaggi391790622018-04-18 15:30:44 +02003995 if (mHomeAppAnimationLayer != null) {
3996 t.setLayer(mHomeAppAnimationLayer, layerForHomeAnimationLayer);
3997 }
Robert Carrb1579c82017-09-05 14:54:47 -07003998 }
3999
4000 @Override
Jorim Jaggi391790622018-04-18 15:30:44 +02004001 SurfaceControl getAppAnimationLayer(@AnimationLayer int animationLayer) {
4002 switch (animationLayer) {
4003 case ANIMATION_LAYER_BOOSTED:
4004 return mBoostedAppAnimationLayer;
4005 case ANIMATION_LAYER_HOME:
4006 return mHomeAppAnimationLayer;
4007 case ANIMATION_LAYER_STANDARD:
4008 default:
4009 return mAppAnimationLayer;
4010 }
chaviw23ee71c2017-12-18 11:29:41 -08004011 }
4012
Robert Carrf7a7ca82017-12-06 13:17:07 -08004013 SurfaceControl getSplitScreenDividerAnchor() {
4014 return mSplitScreenDividerAnchor;
4015 }
4016
chaviw23ee71c2017-12-18 11:29:41 -08004017 @Override
Robert Carrb1579c82017-09-05 14:54:47 -07004018 void onParentSet() {
4019 super.onParentSet();
4020 if (getParent() != null) {
chaviw23ee71c2017-12-18 11:29:41 -08004021 mAppAnimationLayer = makeChildSurface(null)
4022 .setName("animationLayer")
4023 .build();
Jorim Jaggibe418292018-03-26 16:14:12 +02004024 mBoostedAppAnimationLayer = makeChildSurface(null)
4025 .setName("boostedAnimationLayer")
4026 .build();
Jorim Jaggi391790622018-04-18 15:30:44 +02004027 mHomeAppAnimationLayer = makeChildSurface(null)
4028 .setName("homeAnimationLayer")
4029 .build();
Robert Carrf7a7ca82017-12-06 13:17:07 -08004030 mSplitScreenDividerAnchor = makeChildSurface(null)
4031 .setName("splitScreenDividerAnchor")
4032 .build();
4033 getPendingTransaction()
4034 .show(mAppAnimationLayer)
Jorim Jaggibe418292018-03-26 16:14:12 +02004035 .show(mBoostedAppAnimationLayer)
Jorim Jaggi391790622018-04-18 15:30:44 +02004036 .show(mHomeAppAnimationLayer)
Robert Carrf7a7ca82017-12-06 13:17:07 -08004037 .show(mSplitScreenDividerAnchor);
chaviw23ee71c2017-12-18 11:29:41 -08004038 scheduleAnimation();
Robert Carrb1579c82017-09-05 14:54:47 -07004039 } else {
chaviw23ee71c2017-12-18 11:29:41 -08004040 mAppAnimationLayer.destroy();
4041 mAppAnimationLayer = null;
Jorim Jaggibe418292018-03-26 16:14:12 +02004042 mBoostedAppAnimationLayer.destroy();
4043 mBoostedAppAnimationLayer = null;
Jorim Jaggi391790622018-04-18 15:30:44 +02004044 mHomeAppAnimationLayer.destroy();
4045 mHomeAppAnimationLayer = null;
Robert Carrf7a7ca82017-12-06 13:17:07 -08004046 mSplitScreenDividerAnchor.destroy();
4047 mSplitScreenDividerAnchor = null;
Robert Carrb1579c82017-09-05 14:54:47 -07004048 }
4049 }
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004050 }
4051
Robert Carree4d4b92017-11-22 12:21:46 -08004052 private final class AboveAppWindowContainers extends NonAppWindowContainers {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004053 AboveAppWindowContainers(String name, WindowManagerService service) {
4054 super(name, service);
Robert Carree4d4b92017-11-22 12:21:46 -08004055 }
4056
Robert Carrb9506032018-02-13 13:54:00 -08004057 @Override
Vishnu Nair83537a72018-07-19 21:27:48 -07004058 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
4059 final SurfaceControl.Builder builder = super.makeChildSurface(child);
4060 if (child instanceof WindowToken && ((WindowToken) child).mRoundedCornerOverlay) {
4061 // To draw above the ColorFade layer during the screen off transition, the
4062 // rounded corner overlays need to be at the root of the surface hierarchy.
4063 // TODO: move the ColorLayer into the display overlay layer such that this is not
4064 // necessary anymore.
4065 builder.setParent(null);
4066 }
4067 return builder;
4068 }
4069
4070 @Override
Robert Carrb9506032018-02-13 13:54:00 -08004071 void assignChildLayers(SurfaceControl.Transaction t) {
4072 assignChildLayers(t, null /* imeContainer */);
4073 }
4074
Robert Carree4d4b92017-11-22 12:21:46 -08004075 void assignChildLayers(SurfaceControl.Transaction t, WindowContainer imeContainer) {
4076 boolean needAssignIme = imeContainer != null
4077 && imeContainer.getSurfaceControl() != null;
4078 for (int j = 0; j < mChildren.size(); ++j) {
4079 final WindowToken wt = mChildren.get(j);
Robert Carrf7a7ca82017-12-06 13:17:07 -08004080
4081 // See {@link mSplitScreenDividerAnchor}
4082 if (wt.windowType == TYPE_DOCK_DIVIDER) {
4083 wt.assignRelativeLayer(t, mTaskStackContainers.getSplitScreenDividerAnchor(), 1);
4084 continue;
4085 }
Vishnu Nair83537a72018-07-19 21:27:48 -07004086 if (wt.mRoundedCornerOverlay) {
4087 wt.assignLayer(t, WindowManagerPolicy.COLOR_FADE_LAYER + 1);
4088 continue;
4089 }
Robert Carree4d4b92017-11-22 12:21:46 -08004090 wt.assignLayer(t, j);
4091 wt.assignChildLayers(t);
4092
4093 int layer = mService.mPolicy.getWindowLayerFromTypeLw(
4094 wt.windowType, wt.mOwnerCanManageAppTokens);
Robert Carrb8e78fa2017-11-28 13:07:58 -08004095
4096 if (needAssignIme && layer >= mService.mPolicy.getWindowLayerFromTypeLw(
4097 TYPE_INPUT_METHOD_DIALOG, true)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004098 imeContainer.assignRelativeLayer(t, wt.getSurfaceControl(), -1);
Robert Carree4d4b92017-11-22 12:21:46 -08004099 needAssignIme = false;
4100 }
4101 }
4102 if (needAssignIme) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004103 imeContainer.assignRelativeLayer(t, getSurfaceControl(), Integer.MAX_VALUE);
Robert Carree4d4b92017-11-22 12:21:46 -08004104 }
4105 }
4106 }
4107
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004108 /**
4109 * Window container class that contains all containers on this display that are not related to
4110 * Apps. E.g. status bar.
4111 */
Robert Carree4d4b92017-11-22 12:21:46 -08004112 private class NonAppWindowContainers extends DisplayChildWindowContainer<WindowToken> {
Wale Ogunwale3a931692016-11-02 16:49:48 -07004113 /**
4114 * Compares two child window tokens returns -1 if the first is lesser than the second in
4115 * terms of z-order and 1 otherwise.
4116 */
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004117 private final Comparator<WindowToken> mWindowComparator = (token1, token2) ->
Wale Ogunwale3a931692016-11-02 16:49:48 -07004118 // Tokens with higher base layer are z-ordered on-top.
Wale Ogunwale5cd907d2017-01-26 14:14:08 -08004119 mService.mPolicy.getWindowLayerFromTypeLw(token1.windowType,
4120 token1.mOwnerCanManageAppTokens)
4121 < mService.mPolicy.getWindowLayerFromTypeLw(token2.windowType,
4122 token2.mOwnerCanManageAppTokens) ? -1 : 1;
Wale Ogunwale19e452e2016-10-12 12:36:29 -07004123
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004124 private final Predicate<WindowState> mGetOrientingWindow = w -> {
4125 if (!w.isVisibleLw() || !w.mPolicyVisibilityAfterAnim) {
4126 return false;
4127 }
4128 final int req = w.mAttrs.screenOrientation;
4129 if(req == SCREEN_ORIENTATION_UNSPECIFIED || req == SCREEN_ORIENTATION_BEHIND
4130 || req == SCREEN_ORIENTATION_UNSET) {
4131 return false;
4132 }
4133 return true;
4134 };
4135
Wale Ogunwale3a931692016-11-02 16:49:48 -07004136 private final String mName;
chaviwf29223e2018-01-31 13:23:51 -08004137 private final Dimmer mDimmer = new Dimmer(this);
4138 private final Rect mTmpDimBoundsRect = new Rect();
4139
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004140 NonAppWindowContainers(String name, WindowManagerService service) {
4141 super(service);
Wale Ogunwale3a931692016-11-02 16:49:48 -07004142 mName = name;
4143 }
4144
4145 void addChild(WindowToken token) {
4146 addChild(token, mWindowComparator);
4147 }
4148
4149 @Override
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004150 int getOrientation() {
4151 final WindowManagerPolicy policy = mService.mPolicy;
4152 // Find a window requesting orientation.
Wale Ogunwale1e129a42016-11-21 13:03:47 -08004153 final WindowState win = getWindow(mGetOrientingWindow);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004154
4155 if (win != null) {
4156 final int req = win.mAttrs.screenOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004157 if (policy.isKeyguardHostWindow(win.mAttrs)) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004158 mLastKeyguardForcedOrientation = req;
Wale Ogunwalebfa81ad2017-05-24 15:14:42 -07004159 if (mService.mKeyguardGoingAway) {
4160 // Keyguard can't affect the orientation if it is going away...
4161 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
4162 return SCREEN_ORIENTATION_UNSET;
4163 }
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004164 }
Andrii Kulianef5ce1c2018-03-20 19:27:24 -07004165 if (DEBUG_ORIENTATION) Slog.v(TAG_WM, win + " forcing orientation to " + req
4166 + " for display id=" + mDisplayId);
Andrii Kulian8ee72852017-03-10 10:36:45 -08004167 return (mLastWindowForcedOrientation = req);
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004168 }
4169
Andrii Kulian8ee72852017-03-10 10:36:45 -08004170 mLastWindowForcedOrientation = SCREEN_ORIENTATION_UNSPECIFIED;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004171
Jorim Jaggi1c530592018-04-06 15:11:47 +02004172 // Only allow force setting the orientation when all unknown visibilities have been
4173 // resolved, as otherwise we just may be starting another occluding activity.
4174 final boolean isUnoccluding =
4175 mService.mAppTransition.getAppTransition() == TRANSIT_KEYGUARD_UNOCCLUDE
4176 && mService.mUnknownAppVisibilityController.allResolved();
4177 if (policy.isKeyguardShowingAndNotOccluded() || isUnoccluding) {
Andrii Kulian8ee72852017-03-10 10:36:45 -08004178 return mLastKeyguardForcedOrientation;
Wale Ogunwale6213caa2016-12-02 16:47:15 +00004179 }
4180
4181 return SCREEN_ORIENTATION_UNSET;
4182 }
4183
4184 @Override
Wale Ogunwale3a931692016-11-02 16:49:48 -07004185 String getName() {
4186 return mName;
4187 }
chaviwf29223e2018-01-31 13:23:51 -08004188
4189 @Override
4190 Dimmer getDimmer() {
4191 return mDimmer;
4192 }
4193
4194 @Override
4195 void prepareSurfaces() {
4196 mDimmer.resetDimStates();
4197 super.prepareSurfaces();
4198 getBounds(mTmpDimBoundsRect);
4199
4200 if (mDimmer.updateDims(getPendingTransaction(), mTmpDimBoundsRect)) {
4201 scheduleAnimation();
4202 }
4203 }
Robert Carr3b716242016-08-16 16:02:21 -07004204 }
Jorim Jaggi6a7a8592017-01-12 00:44:33 +01004205
Robert Carr9034d962018-01-04 18:27:42 -08004206 private class NonMagnifiableWindowContainers extends NonAppWindowContainers {
4207 NonMagnifiableWindowContainers(String name, WindowManagerService service) {
4208 super(name, service);
4209 }
4210
4211 @Override
4212 void applyMagnificationSpec(Transaction t, MagnificationSpec spec) {
4213 }
4214 };
4215
Robert Carrb1579c82017-09-05 14:54:47 -07004216 SurfaceControl.Builder makeSurface(SurfaceSession s) {
4217 return mService.makeSurfaceBuilder(s)
4218 .setParent(mWindowingLayer);
4219 }
4220
4221 @Override
4222 SurfaceSession getSession() {
4223 return mSession;
4224 }
4225
4226 @Override
4227 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
Robert Carrf59b8dd2017-10-02 18:58:36 -07004228 SurfaceSession s = child != null ? child.getSession() : getSession();
4229 final SurfaceControl.Builder b = mService.makeSurfaceBuilder(s);
Robert Carrb1579c82017-09-05 14:54:47 -07004230 b.setSize(mSurfaceSize, mSurfaceSize);
Robert Carrf59b8dd2017-10-02 18:58:36 -07004231
4232 if (child == null) {
4233 return b;
4234 }
4235
Robert Carree4d4b92017-11-22 12:21:46 -08004236 return b.setName(child.getName())
4237 .setParent(mWindowingLayer);
Robert Carrb1579c82017-09-05 14:54:47 -07004238 }
4239
4240 /**
4241 * The makeSurface variants are for use by the window-container
4242 * hierarchy. makeOverlay here is a function for various non windowing
4243 * overlays like the ScreenRotation screenshot, the Strict Mode Flash
4244 * and other potpourii.
4245 */
4246 SurfaceControl.Builder makeOverlay() {
4247 return mService.makeSurfaceBuilder(mSession)
4248 .setParent(mOverlayLayer);
4249 }
4250
Daichi Hironoa1fb9be2017-12-18 17:02:54 +09004251 /**
4252 * Reparents the given surface to mOverlayLayer.
4253 */
4254 void reparentToOverlay(Transaction transaction, SurfaceControl surface) {
4255 transaction.reparent(surface, mOverlayLayer.getHandle());
4256 }
4257
Robert Carrb1579c82017-09-05 14:54:47 -07004258 void applyMagnificationSpec(MagnificationSpec spec) {
Robert Carr24be9ab2018-04-30 17:54:53 -07004259 if (spec.scale != 1.0) {
4260 mMagnificationSpec = spec;
4261 } else {
4262 mMagnificationSpec = null;
4263 }
4264
Robert Carrf59b8dd2017-10-02 18:58:36 -07004265 applyMagnificationSpec(getPendingTransaction(), spec);
4266 getPendingTransaction().apply();
Robert Carrb1579c82017-09-05 14:54:47 -07004267 }
4268
Robert Carr24be9ab2018-04-30 17:54:53 -07004269 void reapplyMagnificationSpec() {
4270 if (mMagnificationSpec != null) {
4271 applyMagnificationSpec(getPendingTransaction(), mMagnificationSpec);
4272 }
4273 }
4274
Robert Carrb1579c82017-09-05 14:54:47 -07004275 @Override
4276 void onParentSet() {
4277 // Since we are the top of the SurfaceControl hierarchy here
4278 // we create the root surfaces explicitly rather than chaining
4279 // up as the default implementation in onParentSet does. So we
4280 // explicitly do NOT call super here.
4281 }
4282
4283 @Override
4284 void assignChildLayers(SurfaceControl.Transaction t) {
Robert Carrb1579c82017-09-05 14:54:47 -07004285
4286 // These are layers as children of "mWindowingLayer"
4287 mBelowAppWindowsContainers.assignLayer(t, 0);
4288 mTaskStackContainers.assignLayer(t, 1);
4289 mAboveAppWindowsContainers.assignLayer(t, 2);
4290
4291 WindowState imeTarget = mService.mInputMethodTarget;
Robert Carree4d4b92017-11-22 12:21:46 -08004292 boolean needAssignIme = true;
Robert Carr1cafdf82017-11-22 13:56:43 -08004293
Robert Carree4d4b92017-11-22 12:21:46 -08004294 // In the case where we have an IME target that is not in split-screen
4295 // mode IME assignment is easy. We just need the IME to go directly above
4296 // the target. This way children of the target will naturally go above the IME
4297 // and everyone is happy.
Robert Carr1cafdf82017-11-22 13:56:43 -08004298 //
Robert Carree4d4b92017-11-22 12:21:46 -08004299 // In the case of split-screen windowing mode, we need to elevate the IME above the
4300 // docked divider while keeping the app itself below the docked divider, so instead
4301 // we use relative layering of the IME targets child windows, and place the
4302 // IME in the non-app layer (see {@link AboveAppWindowContainers#assignChildLayers}).
Robert Carr1cafdf82017-11-22 13:56:43 -08004303 //
Robert Carr234b6332018-03-20 13:38:16 -07004304 // In the case the IME target is animating, the animation Z order may be different
4305 // than the WindowContainer Z order, so it's difficult to be sure we have the correct
4306 // IME target. In this case we just layer the IME over all transitions by placing it in the
4307 // above applications layer.
4308 //
Robert Carree4d4b92017-11-22 12:21:46 -08004309 // In the case where we have no IME target we assign it where it's base layer would
4310 // place it in the AboveAppWindowContainers.
Robert Carr234b6332018-03-20 13:38:16 -07004311 if (imeTarget != null && !(imeTarget.inSplitScreenWindowingMode()
4312 || imeTarget.mToken.isAppAnimating())
Robert Carree4d4b92017-11-22 12:21:46 -08004313 && (imeTarget.getSurfaceControl() != null)) {
Jorim Jaggiffe128d2017-11-30 13:54:36 +01004314 mImeWindowsContainers.assignRelativeLayer(t, imeTarget.getSurfaceControl(),
Robert Carrb1579c82017-09-05 14:54:47 -07004315 // TODO: We need to use an extra level on the app surface to ensure
4316 // this is always above SurfaceView but always below attached window.
4317 1);
Robert Carree4d4b92017-11-22 12:21:46 -08004318 needAssignIme = false;
Robert Carrb1579c82017-09-05 14:54:47 -07004319 }
4320
4321 // Above we have assigned layers to our children, now we ask them to assign
4322 // layers to their children.
4323 mBelowAppWindowsContainers.assignChildLayers(t);
4324 mTaskStackContainers.assignChildLayers(t);
Robert Carree4d4b92017-11-22 12:21:46 -08004325 mAboveAppWindowsContainers.assignChildLayers(t,
4326 needAssignIme == true ? mImeWindowsContainers : null);
Robert Carrb1579c82017-09-05 14:54:47 -07004327 mImeWindowsContainers.assignChildLayers(t);
4328 }
4329
4330 /**
4331 * Here we satisfy an unfortunate special case of the IME in split-screen mode. Imagine
4332 * that the IME target is one of the docked applications. We'd like the docked divider to be
4333 * above both of the applications, and we'd like the IME to be above the docked divider.
4334 * However we need child windows of the applications to be above the IME (Text drag handles).
4335 * This is a non-strictly hierarcical layering and we need to break out of the Z ordering
4336 * somehow. We do this by relatively ordering children of the target to the IME in cooperation
4337 * with {@link #WindowState#assignLayer}
4338 */
4339 void assignRelativeLayerForImeTargetChild(SurfaceControl.Transaction t, WindowContainer child) {
Robert Carrbb5de662018-04-26 13:29:51 -07004340 child.assignRelativeLayer(t, mImeWindowsContainers.getSurfaceControl(), 1);
Robert Carrb1579c82017-09-05 14:54:47 -07004341 }
4342
4343 @Override
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004344 void prepareSurfaces() {
4345 final ScreenRotationAnimation screenRotationAnimation =
4346 mService.mAnimator.getScreenRotationAnimationLocked(mDisplayId);
4347 if (screenRotationAnimation != null && screenRotationAnimation.isAnimating()) {
4348 screenRotationAnimation.getEnterTransformation().getMatrix().getValues(mTmpFloats);
4349 mPendingTransaction.setMatrix(mWindowingLayer,
4350 mTmpFloats[Matrix.MSCALE_X], mTmpFloats[Matrix.MSKEW_Y],
4351 mTmpFloats[Matrix.MSKEW_X], mTmpFloats[Matrix.MSCALE_Y]);
4352 mPendingTransaction.setPosition(mWindowingLayer,
4353 mTmpFloats[Matrix.MTRANS_X], mTmpFloats[Matrix.MTRANS_Y]);
4354 mPendingTransaction.setAlpha(mWindowingLayer,
4355 screenRotationAnimation.getEnterTransformation().getAlpha());
4356 }
Chavi Weingartenb736e322018-02-23 00:27:54 +00004357
Jorim Jaggi9af095b2017-12-12 17:18:57 +01004358 super.prepareSurfaces();
4359 }
Robert Carr2f8aa392018-01-31 14:46:51 -08004360
Jorim Jaggibe418292018-03-26 16:14:12 +02004361 void assignStackOrdering() {
4362 mTaskStackContainers.assignStackOrdering(getPendingTransaction());
Robert Carr2f8aa392018-01-31 14:46:51 -08004363 }
Chavi Weingarten3a748552018-05-14 17:32:42 +00004364
4365 /**
4366 * Increment the deferral count to determine whether to update the IME target.
4367 */
4368 void deferUpdateImeTarget() {
4369 mDeferUpdateImeTargetCount++;
4370 }
4371
4372 /**
4373 * Decrement the deferral count to determine whether to update the IME target. If the count
4374 * reaches 0, a new ime target will get computed.
4375 */
4376 void continueUpdateImeTarget() {
4377 if (mDeferUpdateImeTargetCount == 0) {
4378 return;
4379 }
4380
4381 mDeferUpdateImeTargetCount--;
4382 if (mDeferUpdateImeTargetCount == 0) {
4383 computeImeTarget(true /* updateImeTarget */);
4384 }
4385 }
4386
4387 /**
4388 * @return Whether a new IME target should be computed.
4389 */
4390 private boolean canUpdateImeTarget() {
4391 return mDeferUpdateImeTargetCount == 0;
4392 }
Arthur Hung95b38a92018-07-20 18:56:12 +08004393
4394 InputMonitor getInputMonitor() {
4395 return mInputMonitor;
4396 }
Jorim Jaggif1292892018-09-10 11:58:13 +02004397
4398 /**
4399 * @return Cached value whether we told display manager that we have content.
4400 */
4401 boolean getLastHasContent() {
4402 return mLastHasContent;
4403 }
Craig Mautner59c00972012-07-30 12:10:24 -07004404}